Jump to content

Out of Memory


Mutley

Recommended Posts

Fatal error: Out of memory (allocated 48496640) (tried to allocate 8192 bytes)

 

I tried setting:

ini_set(’memory_limit’, ‘10000M’);

 

At the top of the script but this hasn't resolved it. It's when I upload and resize images about 3mb in filesize. Any ideas?

 

Regards,

Nick.

Link to comment
Share on other sites

Not sure where these qutes come from but there not correct.

 

ini_set(’memory_limit’, ‘10000M’);

 

should be....

 

ini_set('memory_limit', '10000M');

 

You may also be best to set it to 0.

 

edit: Hmm.... the quotes even broke the boards syntax highlighting.

Link to comment
Share on other sites

My host (Hostgator) has said only a 64mb limit can be set, which sadly still doesn't work:

 

Fatal error: Out of memory (allocated 48496640) (tried to allocate 8192 bytes).

 

I don't understand why a small image, that's only about 1mb, is fine with out any limit changes, yet a 3mb image can't upload at all even with a really high limit set.

Link to comment
Share on other sites

It resizes an image by ratio and moves/copies it. Here is where the memory errors kick in, around the colour functions at bottom:

 

	if($copy){
      //print 'Image uploaded successfully.<br />';		
      $simg = imagecreatefromjpeg("$idir" . $url);		
      $currwidth = imagesx($simg);						
      $currheight = imagesy($simg);						
      if($currheight > $currwidth){						
         $zoom = $twidth / $currheight;					
         $newheight = $theight;							
         $newwidth = $currwidth * $zoom;				
      }else{											
        $zoom = $twidth / $currwidth;					
        $newwidth = $twidth;							
        $newheight = $currheight * $zoom;				
      } 
      $dimg = imagecreate($newwidth, $newheight);		
      imagetruecolortopalette($simg, false, 256);		
      for ($i = 0; $i < $palsize; $i++) {				
       $colors = ImageColorsForIndex($simg, $i);		
       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); 
      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);  
      imagejpeg($dimg, "$tdir" . $rand.'-'.$url);	
  }

Link to comment
Share on other sites

  • 4 weeks later...

Still having this problem, here is the full resize script.

 

It does the following:

1) It resizes the uploaded to a Thumbnail size

2) It resizes the uploaded image to a more reasonable size, if it is greater than 800x600

3) It deleted the temp file

4) It inserts the image location into a database

 

<?php

if(isset($_POST['SubmitImage'])){
		$title = $_POST['title'];
		$category = $_POST['category'];
		$imagefile = $_FILES['imagefile']['name'];
		$content = $_POST['pagesContent'];
	if($title == NULL || $imagefile == NULL){
		?>
			<script language="javascript">
			alert("Please fill in all the fields.");
			window.location = "admin.php?a=gallery&inner=create"
			</script>
		<?
	}else{
		// Image Upload Form Below
		$idir = "images/gallery/";								// Path To Images Directory
		$tdir = "images/gallery/thumbs/";						// Path To Thumbnails Directory
		$twidth = "150";										// Maximum Width For Thumbnail Images
		$theight = "150";										// Maximum Height For Thumbnail Images
		$lwidth = "800";										// Maximum Width For Thumbnail Images
		$lheight = "600";										// Maximum Height For Thumbnail Images

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

			if($copy){   // If The Script Was Able To Copy The Image To It's Permanent Location
			  //print 'Image uploaded successfully.<br />';		// Was Able To Successfully Upload Image
			  $simg = imagecreatefromjpeg("$idir" . $url);		// 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" . $rand.'-'.$url);		// Saving The Image
			  }
		   // If The Script Was Able To Copy The Image To It's Permanent Location
			  //print 'Image uploaded successfully.<br />';		// Was Able To Successfully Upload Image
			  $simg = imagecreatefromjpeg("$idir" . $url);		// 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, "$idir" . $rand.'-'.$url);		// Saving The Image

				$newurl = $rand.'-'.$url;
				$sql="INSERT INTO `gallery` (`cat`, `url`, `title`, `content`) VALUES ('$category', '$newurl', '$title', '$content')";
				mysql_query($sql);
								//$sql = "UPDATE gallery SET url = '".$rand.'-'.$url."' WHERE id = '".$id."' LIMIT 1";
								//mysql_query($sql);
								$oldimg = "images/gallery/$url";
								unlink($oldimg);
			  //imagedestroy($simg);								// Destroying The Temporary Image
			  //imagedestroy($dimg);								// Destroying The Other Temporary Image
			  print 'Image uploaded successfully.';				// Resize successful
				?>
					<script language="javascript">
					alert("Image Succesfully Uploaded. <?=$imgsql?>");
						window.location.href='';
					</script>
				<?
			}else{
			  print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed
			}
		  }else{
			print "<font color='#FF0000'>ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ";   // Error Message If Filetype Is Wrong
			print $file_ext;   // Show The Invalid File's Extention
			print '.</font>';
		  }
		}
}
?>

 

 

Thanks in advance,

Nick.

Link to comment
Share on other sites

I'm slightly lost as to what you're trying to achieve with all the color and palette functions. As far as i can see, this part:

 

              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

 

Is redundant. I'm not sure why you wanted to convert the original image to a palette? And the next lines really do nothing. ImageColorAllocate() does nothing except to provide color identifiers for use with other functions, except that the first call will fill the background of an image created with imagecreate(). But given that the image identifier you're using is the one you're going to copy to, there doesn't seem much point. Perhaps i've missed something along the way.

Link to comment
Share on other sites

It's the memory limit one. I upload 2.5mb+ images.

 

I can't believe it uses nearly 50mb of memory! Is there no way around this???

 

I don't think so. The only alternative would be to have some application run on the server which resizes the images, which could be called by your PHP script. Of course, that would only be better if you could find something that resized whilst using less resources.

Link to comment
Share on other sites

Its purely a memory limit issue, but it depends entirely on the dimensions of the image not the size of the file.

 

lets assume the image is 3000 x 3000 => 9000000 pixels.

If the image is an 8bit RGB file then there are 24bits of data per pixel, 8bits per channel.

 

that is 216000000 bits. or approximately 26MB of memory needed to load that image.

 

 

8bit rgb = 24bits/pixel

8bit cmyk = 32bits/pixel

16bit rgb = 48bits/pixel

16bit cmyk = 64bits/pixel

 

these calculations are rough too, i'm sure compression etc comes into play and the image formats probably dictate this slightly. I have been using the imagick extenstion for php and it doesn't seem to suffer from the same limitations, not sure if it passes the actual work directly on to imagemagick or not.

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.