Solved

imagecreatefromjpeg Script Stops Running - Out of Memory?

Posted on 2008-06-09
4
724 Views
Last Modified: 2010-04-21
I'm trying to write a script that will create thumbnails of all images that are in a specific directory.  However, when I run the script, it stops running before completion.  I believe that I've narrowed it down to the imagecreatefromjpeg function.  If I comment this out, everything prints as expected.  If I run it with this line, I make it through about 30 images before the script stops.  I am using imagedestroy() after each image creation, but this doesn't seem to help.  I realize that I'm not really doing anything with these thumbnails, but once I can get the script to execute all the way (hopefully), then I'll add some logic to save them.


I'm using a hosting company (1and1), so I am not able to make changes to my php.ini file.  Also, if I comment out all the echos, I eventually get a 500 error, but it's a generic one from my host which doesn't give me any specifics as to what's going on.  I've included my code and am open to comments and suggestions.

Thank you!!
<?php
	$strSourcePath = "[path to original images]";
	$strNewThumbPath = "[output path to thumbnails]";
 
	if (is_dir($strSourcePath)) {
	    if ($objDirHandle = opendir($strSourcePath)) {
	        while (($file = readdir($objDirHandle)) !== false) {
				if(filetype($strSourcePath . $file) == "file"){
					$strSourceFilePath = $strSourcePath . $file;
					echo "Processing $strSourceFilePath...<br>";
 
					$size = @getimagesize($strSourceFilePath);
 
					if($size){
						echo "&nbsp;&nbsp;&nbsp;";
						print_r($size);
						echo "<br>";
					} else {
						echo "&nbsp;&nbsp;&nbsp;<b>Error determining getimagesize info for $strSourceFilePath.</b><br>";
					}
 
					if($size[2] == 1){
						echo "&nbsp;&nbsp;&nbsp;$strSourceFilePath is a GIF<br>";
						$bolConvert = false;
					} elseif($size[2] == 2){
						echo "&nbsp;&nbsp;&nbsp;$strSourceFilePath is a JPG<br>";
						$bolConvert = true;
					} elseif($size[2] == 6){
						echo "&nbsp;&nbsp;&nbsp;$strSourceFilePath is a BMP<br>";
						$bolConvert = false;
					} else {
						echo "&nbsp;&nbsp;&nbsp;<b>$strSourceFilePath is an unsupported type</b><br>";
						$bolConvert = false;
					}
 
					if($bolConvert){
						header('Content-type: ' . $size['mime']);
						$objThumbnail = imagecreatetruecolor(100,100) ;
						$objImage = @imagecreatefromjpeg($strSourceFilePath) ;
 
						imagecopyresampled($objThumbnail, $objImage, 0, 0, 0, 0, 100, 100);
 
						imagedestroy($objThumbnail);
						imagedestroy($objImage);
						unset($size);
					} else {
						echo "&nbsp;&nbsp;&nbsp;<b>Did not convert $strSourceFilePath</b><br>";
					}
				echo "<hr>";
				}
	        }
	        closedir($objDirHandle);
	    }
	} else {
		die("<b>$strSourcePath is not a valid directory.</b>");
	}
?>

Open in new window

0
Comment
Question by:rtedi222
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
AlexanderR earned 250 total points
ID: 21748053
If your host don't want you to go above the memory limit they set then there is little you can do.

Can try 2 things before considering negotiating with them or finding a better one:
1. If you are given access to .htaccess (just about all hosts do) then follow a guide line over at http://www.whenpenguinsattack.com/2006/01/20/how-to-override-phpini/

2. you can try to use ini_set at the top of the script:
ini_set('memory_limit', 100) // play with the numbers
0
 

Author Comment

by:rtedi222
ID: 21756144
Please correct me if I'm wrong, but isn't imagedestroy() supposed to take care of cleaning up the memory that's used by creating images?  So if I call imagedestroy() for each image that I create, shouldn't my net usage be 0 (or close enough to it)?

Thanks!!
0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 21756348
hard to tell unless you see the exact error.  What you should do is recreate the environment that resembles the one of your host.  Run phpinfo() on the server, look at environmental configurations and set up your own php.ini to resemble those.  Then, once you run the script on your own computer it should give a more meaningful error.

Other than memory limit it can also be execution time. Try changing it through either set_time_limit(60) or ini_set('max_execution_time', 60)
Try different numbers there.

And even though imagedestroy resets your memory, if the host has too low memory setting then even a singe image can max it out.
0
 

Author Closing Comment

by:rtedi222
ID: 31465609
I believe that this is just a limitation of my host and there's nothing I can do about it.  Thanks for trying though!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question