Jump to content

[SOLVED] Fatal error: Out of memory (allocated 33292288) (tried to allocate 10320 bytes)


AdRock

Recommended Posts

I am trying to upload an image which is 702Kb which is 2580 x 1932 and I am gettting this error message

 

Fatal error: Out of memory (allocated 33292288) (tried to allocate 10320 bytes)

 

I worked out that it needs 31.75 Mb to do it and I have 48Mb available.

 

What would be cuasing this error.  It uploads fine but it's the resizing that is obviously causing the problems

 

function gallery_upload($alternate, $gallery) {

global $alternate, $gallery;
global $host,$dbUser,$dbPass,$dbName;

require_once("../php/database/connection.php");
require_once("../php/database/MySQL.php");

// Connect to the database and grab the email
$db = & new MySQL($host,$dbUser,$dbPass,$dbName);

$sql = "SELECT COUNT(*) FROM image WHERE cat='$gallery'";
$result = $db->query($sql);

$size = $result->size();

$size = leading_zeros($size, 3);

$newname = $gallery.$size;

$idir = "../images/gallery/full/";   // Path To Images Directory 
$tdir = "../images/gallery/thumbs/";   // Path To Thumbnails Directory 
$twidth = "100";   // Maximum Width For Thumbnail Images 
$theight = "75";   // Maximum Height For Thumbnail Images 
$ldir = "../images/gallery/large/";   // Path To Thumbnails Directory 
$lwidth = "400";   // Maximum Width For Thumbnail Images 
$lheight = "300";   // Maximum Height For Thumbnail Images

$picture = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use 
    if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { 
        $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
        $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location 

        if ($copy) {  
		// If The Script Was Able To Copy The Image To It's Permanent Location 
     
		//insert the image names into the database
		$query = "INSERT INTO image VALUES ('','$picture','$alternate','$picture','$gallery')";

		$result = $db->query($query);

		if($result) {

			$simg = imagecreatefromjpeg("$idir" . $picture);   // Make A New Temporary Image To Create The Thumbanil From 
			$currwidth = imagesx($simg);   // Current Image Width 
			$currheight = imagesy($simg);   // Current Image Height 
			if ($currheight > $currwidth) {   // If Height Is Greater Than Width 
				$zoom = $twidth / $currheight;   // Length Ratio For Width 
				$newheight = $theight;   // Height Is Equal To Max Height 
				$newwidth = $currwidth * $zoom;   // Creates The New Width 
			} else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) 
				$zoom = $twidth / $currwidth;   // Length Ratio For Height 
				$newwidth = $twidth;   // Width Is Equal To Max Width 
				$newheight = $currheight * $zoom;   // Creates The New Height 
			} 
			$dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail 
			imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete 
			$palsize = ImageColorsTotal($simg); 
			for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image 
				$colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 
				ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use 
			} 
			imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
			imagejpeg($dimg, "$tdir" . $picture);   // Saving The Image 
			imagedestroy($simg);   // Destroying The Temporary Image 
			imagedestroy($dimg);   // Destroying The Other Temporary Image 

			$simg = imagecreatefromjpeg("$idir" . $picture);   // Make A New Temporary Image To Create The Thumbanil From 
			$currwidth = imagesx($simg);   // Current Image Width 
			$currheight = imagesy($simg);   // Current Image Height 
			if ($currheight > $currwidth) {   // If Height Is Greater Than Width 
				$zoom = $lwidth / $currheight;   // Length Ratio For Width 
				$newheight = $lheight;   // Height Is Equal To Max Height 
				$newwidth = $currwidth * $zoom;   // Creates The New Width 
			} else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) 
				$zoom = $lwidth / $currwidth;   // Length Ratio For Height 
				$newwidth = $lwidth;   // Width Is Equal To Max Width 
				$newheight = $currheight * $zoom;   // Creates The New Height 
			} 
			$dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail 
			imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete 
			$palsize = ImageColorsTotal($simg); 
			for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image 
				$colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 
				ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use 
			} 
			imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
			imagejpeg($dimg, "$ldir" . $picture);   // Saving The Image 
			imagedestroy($simg);   // Destroying The Temporary Image 
			imagedestroy($dimg);   // Destroying The Other Temporary Image 
			return 'Correct';

			} else { 
				return 'Unable to upload image.';   // Error Message If Upload Failed
			}
		}
		else {
			return "Could not insert into database";
		}
	}
	else {
		return "Something fucked up";
	}
}

Link to comment
Share on other sites

Would combing 2 resizes in this help with memory usage?

 

The only thing that is different is the widths and heights of the image to be created

 

$simg = imagecreatefromjpeg("$idir" . $picture);   // Make A New Temporary Image To Create The Thumbanil From 
$currwidth = imagesx($simg);   // Current Image Width 
$currheight = imagesy($simg);   // Current Image Height 
if ($currheight > $currwidth) {   // If Height Is Greater Than Width 
$zoom = $twidth / $currheight;   // Length Ratio For Width 
$newheight = $theight;   // Height Is Equal To Max Height 
$newwidth = $currwidth * $zoom;   // Creates The New Width 
} else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) 
$zoom = $twidth / $currwidth;   // Length Ratio For Height 
$newwidth = $twidth;   // Width Is Equal To Max Width 
$newheight = $currheight * $zoom;   // Creates The New Height 
} 
$dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail 
imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete 
$palsize = ImageColorsTotal($simg); 
for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image 
$colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use 
} 
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
imagejpeg($dimg, "$tdir" . $picture);   // Saving The Image 
imagedestroy($simg);   // Destroying The Temporary Image 
imagedestroy($dimg);   // Destroying The Other Temporary Image 

$simg = imagecreatefromjpeg("$idir" . $picture);   // Make A New Temporary Image To Create The Thumbanil From 
$currwidth = imagesx($simg);   // Current Image Width 
$currheight = imagesy($simg);   // Current Image Height 
if ($currheight > $currwidth) {   // If Height Is Greater Than Width 
$zoom = $lwidth / $currheight;   // Length Ratio For Width 
$newheight = $lheight;   // Height Is Equal To Max Height 
$newwidth = $currwidth * $zoom;   // Creates The New Width 
} else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) 
$zoom = $lwidth / $currwidth;   // Length Ratio For Height 
$newwidth = $lwidth;   // Width Is Equal To Max Width 
$newheight = $currheight * $zoom;   // Creates The New Height 
} 
$dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail 
imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete 
$palsize = ImageColorsTotal($simg); 
for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image 
$colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used 
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use 
} 
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
imagejpeg($dimg, "$ldir" . $picture);   // Saving The Image 
imagedestroy($simg);   // Destroying The Temporary Image 
imagedestroy($dimg);   // Destroying The Other Temporary Image

Link to comment
Share on other sites

What is your memory_limit on that box? To be honest it sounds as though you are running out of memory during your re-size operation. If you set it to 64MB in php.ini does the error go away?

 

I'm curious to know also how you worked out that you need 31.75 MB for the re-size too?

Link to comment
Share on other sites

If it was me, the issue is going to mainly be in the imagecolorallocate. I do not fully understand how it works. But it is in a loop and if that loop is dynamic depending on a picture/how many colors that picture has....right?

 

Is there a reason to use that in a loop like that. Because a picture that is 702Kb should not be eating up 32MB of memory for a simple resize...there is something wrong with that alone. At most you should only need 3 times the image size to do it, the memory for the uploaded file, the new image and the old image.

 

But, like I said, I do not understand image functions too well, and it may be necessary, although I doubt so. Imagine if someone uploaded a 2MB image, you would need like 300MB of memory just for this script to run, which is totally unacceptable for 1 script/1 person in my book.

Link to comment
Share on other sites

it looks like I need to rethink the image upload script.  This is a hecked version of one i found onliine and it seems a lot just to resize an image.

 

Do you know anywhere where i can find some sort of simple image resize script?

Link to comment
Share on other sites

<?php

// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploadfile']['tmp_name'];

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = "images/". $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);

imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
// has completed.
?> 

 

http://www.4wordsystems.com/php_image_resize.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.