perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas den Braber <tho...@delos.nl>
Subject RE: No image creation in mod_perl (was RE: Apache2/MP2 Segfaults ...)
Date Mon, 27 Mar 2006 17:29:00 GMT
I use Image::Imlib2 for on the fly image creation and it works fine for me. After the thumbnails
are created, memory is restored to normal size. Image::Imlib2 is also very vast and easy to
code.
For filetypes that are not supported by imlib2, I use imagemagick which is using some more
memory but even there, memory is restored after the thumbnail creation.
I also used it with a Treaded MPM and after 100000 thumbnails and previews the system only
uses a few megabytes more memory ( with perlInterpMaxRequests set to 200000 and PerlInterpMax
set to 8)

I think it is very useful to create thumbnails on the fly. It is maintenance free, you can
just copy images to your image directory and have them show up immediately.
 
I run on a 2 gig Ram Pentium 2.66Ghz server with SUSE 9.3 with maxserver set to 20 with a
front-end and a backend server. Never had any problems.

* This is how I use the thumbnail creation in MOD_PERL:

I check for the existence of an thumbnail and if not already created or outdated, I create
it and update the utime to the utime of the original file so you can check if un update is
needed.

Before I create a thumbnail, I generate a lock file which is removed afterwards. If the images
is corrupted or the process is killed during the thumbnail creation, the lockfile exists so
the process is not repeated.
This also prevent that two sessions are trying to generate a thumbnail from the same image
at the same time.

Here is some sample code from the resize routine:

############### sub create_iml_resized ######################

sub create_iml_resized {

  my ( $self, $sourcefile, $cachedfile ) = @_;

  # use lockfile to prevent more sessions trying to thumb the same file
  # and prevent death-loops
  $self->uselockfile($cachedfile, 'iml') or return;

  my $maxsize = $self->{maxsize};

  Image::Imlib2->set_cache_size(0);
  my $image;

  eval { $image = Image::Imlib2->load($sourcefile) };
  if ($@ || !$image){
    $status = "Error in opening image: $@";
    return;
  }

  my $doctype = $self->{ext};
  my $width   = $image->get_width;
  my $height  = $image->get_height;

  unless ( $height ){
    $status = 'unknown dimensions, corrupted image' ;
    return;
  }

  my $maxheight;
  my $maxwidth;
  my $ratio;

  if ( $self->{fixedwidth} ){
    $maxwidth  = $maxsize;
    $maxheight = $maxsize*1.5;
    $ratio = $height/$width > 1.5 ? $height/$maxheight : $width/$maxwidth ;
  } elsif ($maxsize > $fixedheight){
    $maxheight = $maxsize;
    $maxwidth  = $maxsize*1.5;
    $ratio = $width/$height > 1.5 ? $width/$maxwidth : $height/$maxheight;
  } else {
    $maxheight = $maxsize;
    $maxwidth  = $maxsize;
    $ratio = $width > $height ? $width/$maxwidth : $height/$maxheight;
  }

  # image is needs resizing
  if ( $height > $maxheight || $width > $maxwidth ){

    my $tw = int( $width/$ratio );
    my $th = int( $height/$ratio );

    my $image2=$image->create_scaled_image($tw,$th);  # Scale image

    $image2->image_set_format("jpeg"); # Convert image to JPG
    $image2->set_quality($self->{quality});
    eval { $image2->save($cachedfile) };
    if ($@){
      $status = "Error in saving image: $@";
      return;
    }

  # image size is ok but filetype is not JPEG
  } elsif ( lc $doctype ne 'jpeg' || lc $doctype ne 'jpg' ){

    $image->image_set_format("jpeg"); # Convert image to JPG
    $image->set_quality($self->{quality});
    $image->save($cachedfile);

  # Filetype and size are OK so copy to thumb cache
  } else {

    copy($sourcefile,$cachedfile) or $status = "Copy failed: $!";

  }

  $self->cleanuplockfile();

  return
}  


####################################################

Thomas den Braber



Mime
View raw message