Hi I have a site that allows admins to upload pictures. The issue I’m running into is that some of the images uploaded will fail with the following message. “Fatal error: Out of memory (allocated 46399488) (tried to allocate 3648 bytes) in /home/randon13/public_html/admin/fnc/createThumb.fnc.php on line 26” Line 26 relates to this piece of code
$im = imagecreatefromjpeg($tempFileLocation);
I have noticed that if the image is over a certain dimension it will fail 3648 X 2746. Smaller resolutions like 1600x1200 will upload just fine. This causes a problem because the admins need to have the script resize images straight from a digital camera.
I use Hostgator for the site and found out they have a 64M cap of the memory used. I know many people have ran into this but how do I get it work.
Here is my image create function.
<?php //Thumbnail creation function
function createThumbnail($max_width, $max_height, $tempFileLocation, $original_file_name,
$path_to_thumbs_directory,$fileType){
//getimagesize used for dimensions of image and image type
list($orig_width, $orig_height, $type, $attr) = getimagesize($tempFileLocation);
//calculate scaling ratio
if ($orig_width <= $max_width && $orig_height <= $max_height) {
$ratio = 1;
} elseif ($orig_width > $orig_height) {
$ratio = $max_width / $orig_width;
} else {
$ratio = $max_height / $orig_height;
}
//check to see which type of image was uploaded
//Then create image identifier with imagecreatefrom
//Type 1 = gif
if ($fileType == "gif") {
$im = imagecreatefromgif($tempFileLocation);
$extension = '.gif';
//Type 2 = jpeg
} else if($fileType == "jpeg" OR $fileType == "pjpeg" ) {
[B]$im = imagecreatefromjpeg($tempFileLocation);[/B]
$extension = '.jpeg';
//Type 3 = png
} elseif ($fileType == "png") {
$im = imagecreatefrompng($tempFileLocation);
$extension = '.png';
}
//calculate dimensions of image
if (!isset($ratio)) {
echo '<p>problem with ratio no image created!</p>';
} else {
$new_width = round($orig_width * $ratio);
$new_height = round($orig_height * $ratio);
//creates image place holder identifier of black image used in imagecopyresampled
$nm = imagecreatetruecolor($new_width, $new_height);
//$nm = imagecreate($new_width, $new_height);
//creates resized image thumbnail
imagecopyresampled($nm, $im, 0, 0, 0, 0, $new_width, $new_height, $orig_width, $orig_height);
//create image file base on type
//to be displayed or moved to thumbnail file , set image quality
switch ($type) {
case 1:
imagegif($nm, $path_to_thumbs_directory . $original_file_name . $extension, 90);
break;
case 2:
imagejpeg($nm, $path_to_thumbs_directory . $original_file_name . $extension, 90);
break;
case 3:
imagepng($nm, $path_to_thumbs_directory . $original_file_name . $extension, 100);
break;
}
// frees image from memory
imagedestroy($nm);
/*
$tn = '<p><img src="' . $path_to_thumbs_directory . $original_file_name . $extension .
'" alt=' . $orginal_file_name . ' /></p>';
$tn .= '<br />image resized<br />';
echo $tn;
*/
}
}
?>
It fails here but only if image dimension is very large.