Image Upload, thumbnail generation error
Posted: Sat Feb 13, 2010 4:07 am
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);
.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);