Image Upload, thumbnail generation error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
brentwientjes
Forum Newbie
Posts: 8
Joined: Sat Feb 13, 2010 3:29 am

Image Upload, thumbnail generation error

Post by brentwientjes »

I am trying to upload images to the server and then scale them for thumbnails. Image files less than 2MB work. Images files more than 2MB fail. The failure occurs with the imagecreatefromjpeg($file_src) statement. Following are php and .htaccess files. htaccess file is in the web site root directory. I am stuck at the moment on why the routine fails. The calling script on the client is a Flex program that I know works so I am pretty sure it is the php script that bombs.

.htaccess file

php_value post_max_size 16M
php_value upload_max_filesize 6M

php script

$img_base = "some file dir";
$w_dst = 150; // set a maximum height and width
$h_dst = 150;

$uid = (Integer) $_POST[uid];
$ver = (Integer) $_POST[ver];
$delver = (Integer) $_POST[delver];
$new_Img = $img_base."u".$uid."v".$ver.".jpg";
unlink($new_Img); // remove old images
unlink($img_base."u".$uid."v".$delver.".jpg");

$file_src = $img_base."u0.jpg";
unlink($file_src);
move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_src);

list($w_src, $h_src, $type) = getimagesize($file_src); // create new dimensions
$ratio = $w_src/$h_src;
if ($w_dst/$h_dst > $ratio)
{$w_dst = floor($h_dst*$ratio);}
else
{$h_dst = floor($w_dst/$ratio);}

switch ($type)
{case 1: // gif -> jpg
$img_src = imagecreatefromgif($file_src);
break;
case 2: // jpeg -> jpg
$img_src = imagecreatefromjpeg($file_src);
break;
case 3: // png -> jpg
$img_src = imagecreatefrompng($file_src);
break;
}
$img_dst = imagecreatetruecolor($w_dst, $h_dst); // resample

imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $w_dst, $h_dst, $w_src, $h_src);
imagejpeg($img_dst, $new_Img); // save new image

unlink($file_src); // clean up image storage
imagedestroy($img_src);
imagedestroy($img_dst);
brentwientjes
Forum Newbie
Posts: 8
Joined: Sat Feb 13, 2010 3:29 am

Re: Image Upload, thumbnail generation error

Post by brentwientjes »

:D SOLVED. It turns out my hosting service has a standard limit of system memory size of 32MB. The solution is to either:

1. put into the code the following statement
ini_set('memory_limit', '100M');

2. add the following to the .htaccess file
php_value memory_limit 100M

I have arbitrarily picked 100MB but at least it works with images upto 5.5MB of original image size.

I hope this helps someone in the future. This problem has haunted me for more than 2 years!
Post Reply