views:

422

answers:

3

I am working on a building a gallery and the goal is a low barrier of entry. My user is someone who takes pictures using their digital camera so file size would be between 200 - 400 KB per image.

The problem I am running into using the GD library is that each image when resized and uploaded use about 90MB of memory+ when the server has a 64 MB limit.

When I use ImageMagick it times out and throws an internal server error.

I am wondering if anyone has any experience with uploading/resizing such large image sizes and could give me some pointers.

Thanks,
Levi

edit: Here is my code to upload

  /** Begin Multiple Image Upload**/     
     $numberImages = count($_FILES['galFile']['name'])-1;

for($i=1;$i<=$numberImages;$i++)
{
$imageName = $_FILES['galFile']['name'][$i];
   $imageType = $_FILES['galFile']['type'][$i];
   $imageSize = $_FILES['galFile']['size'][$i];
   $imageTemp = $_FILES['galFile']['tmp_name'][$i];
   $imageError = $_FILES['galFile']['error'][$i];

   //Make sure it is an image
   if(in_array(end(explode(".", $imageName)), $allowed))
   {
     //Where to upload image to
     $uploadFile = $uploadDir . $imageName;
     if (file_exists($uploadFile))
  {
   //What to do if file already exists
   //Append random number to the end
   $front = explode(".", $imageName);
   $randomNum = rand(1,100);
   $front[0] = $front[0].$randomNum;
   $imageName = $front[0].".".$front[1];
   $uploadFile = $uploadDir . $imageName;
  }
         if(move_uploaded_file($imageTemp,$uploadFile))
         {
         //Add $imageName to DB
          $query = "INSERT INTO galleryImages VALUES(\"0\",\"$lastInsert\",\"$imageName\",\"$i\")";
            mysql_query($query);
            reSizePic($uploadFile);
         }
   }
}

Here is the GD code I had been using to resize:

function reSizePic($image)
{
$source_pic = $image;
$destination_pic = $image;
$max_width = 660;
$max_height = 500;

$src = imagecreatefromjpeg($source_pic);
list($width,$height)=getimagesize($source_pic);

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if(($width <= $max_width) && ($height <= $max_height))
{
    $tn_width = $width;
    $tn_height = $height;
}
elseif (($x_ratio * $height) < $max_height)
{
    $tn_height = ceil($x_ratio * $height);
    $tn_width = $max_width;
}
else
{
    $tn_width = ceil($y_ratio * $width);
    $tn_height = $max_height;
}

$tmp = imagecreatetruecolor($tn_width,$tn_height);

imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

imagejpeg($tmp,$destination_pic,100);
imagedestroy($src);
imagedestroy($tmp);
}

And this is the ImageMagick code I am using to resize:

$resource = NewMagickWand(); 
MagickReadImage($resource,$image); 
MagickSetImageCompressionQuality( $resource, 100);
$resource = MagickTransformImage($resource,'0x0','660x500');
MagickWriteImage($resource, $image);
DestroyMagickWand($resource);
+1  A: 

http://pl.php.net/imagecreatefromjpeg

The memory required to load an image using imagecreatefromjpeg() is a function of the image's dimensions and the images's bit depth, multipled by an overhead. It can calculated from this formula: Num bytes = Width * Height * Bytes per pixel * Overhead fudge factor Where Bytes per pixel = Bit depth/8, or Bits per channel * Num channels / 8.

That's about the GD part. Now the ImageMagick: have you tried a simpler approach:

$thumb = new Imagick('image.jpg');
$thumb->resizeImage($w,$h,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('thumb.jpg');
$thumb->destroy();
zalew
The above for ImageMagick did not work. It says, "Fatal error: Class 'Imagick' not found"I am using Hostgator and they have Magickwand installed, do you know if this means I can only use Magickwand commands?
Levi
Took this example from the docs comments, if you have the magick package installed, It shoud work, I think.
zalew
The code I have resizes the images fine for the first 15 images uploaded. If I upload more than 15 it doesn't end so I believe it is something to do with the server and some sort of a timeout.
Levi
A: 

I dont know anything about this question, but is the following answer useful

http://stackoverflow.com/questions/74315/what-is-the-best-way-to-handle-photo-uploads

Just in case it is not, ignore it.

Alphaneo
A: 

Please put up the following code on the top of your php file.

ini_set("memory_limit","500M");