...

View Full Version : thumbnails memory error



alex57
07-29-2007, 01:33 PM
This is a script to create thumbnails from jpgs ina directory. The jpgs are all 2.18 MB in size but whenever is gets to line 137 reading:


$image = imagecreatefromjpeg("$filein");

an error message occurss saying:

'Fatal error: Allowed memory size of 50331648 bytes exhausted (tried to allocate 11264 bytes) '

are the 2mb files too large, this script did used to work fine. thanks






<?php
$plc = $_GET['var'];
$loc = $_GET['var2'];


$str1 = $loc . "/" . $plc . "/photos/";
$str2 = $loc . "/" . $plc . "/thumbs/";

echo $str1;
echo $str2;
createThumbs($str1, $str2, '100');



function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
echo $pathToImages;
// open the directory
$dir = opendir( $pathToImages );

// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";

// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );

// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );


$imagethumbsize_w = 100; // thumbnail size (area cropped in middle of image)
$imagethumbsize_h = 100; // thumbnail size (area cropped in middle of image)
//resize_then_crop( $filein,$fileout,$imagethumbsize_w,
//$imagethumbsize_h,/*rgb*/"255","255","255");

resize_then_crop( "{$pathToImages}{$fname}", "{$pathToThumbs}{$fname}",$imagethumbsize_w,$imagethumbsize_h,$red,$green,$blue);

}
}
// close the directory
closedir( $dir );
}



function resize_then_crop( $filein,$fileout,$imagethumbsize_w,$imagethumbsize_h,$red,$green,$blue)
{

// Get new dimensions
list($width, $height) = getimagesize("$filein");
$new_width = $width * $percent;
$new_height = $height * $percent;


mysql_connect("localhost", "root", "dinosaur") or die(mysql_error());
mysql_select_db("info") or die(mysql_error());
$plc = $_GET['var'];
echo "plc = " . $plc;
$result = mysql_query("SELECT AlbumID, CollectionID FROM collection WHERE (Name = '$plc')");


$row = mysql_fetch_array($result);

$col = $row['CollectionID'];
echo "col = " . $col;

mysql_query("INSERT INTO photo (CollectionID, Filename) VALUES ('$col', '$filein')");



if(preg_match("/.jpg/i", "$filein"))
{
$format = 'image/jpeg';
}
if (preg_match("/.gif/i", "$filein"))
{
$format = 'image/gif';
}
if(preg_match("/.png/i", "$filein"))
{
$format = 'image/png';
}

switch($format)
{
case 'image/jpeg':
$image = imagecreatefromjpeg("$filein");
break;
case 'image/gif';
$image = imagecreatefromgif("$filein");
break;
case 'image/png':
$image = imagecreatefrompng("$filein");
break;
}

$width = $imagethumbsize_w ;
$height = $imagethumbsize_h ;
list($width_orig, $height_orig) = getimagesize("$filein");

if ($width_orig < $height_orig) {
$height = ($imagethumbsize_w / $width_orig) * $height_orig;
} else {
$width = ($imagethumbsize_h / $height_orig) * $width_orig;
}

if ($width < $imagethumbsize_w)
//if the width is smaller than supplied thumbnail size
{
$width = $imagethumbsize_w;
$height = ($imagethumbsize_w/ $width_orig) * $height_orig;;
}

if ($height < $imagethumbsize_h)
//if the height is smaller than supplied thumbnail size
{
$height = $imagethumbsize_h;
$width = ($imagethumbsize_h / $height_orig) * $width_orig;
}

$thumb = imagecreatetruecolor($width , $height);
$bgcolor = imagecolorallocate($thumb, $red, $green, $blue);
ImageFilledRectangle($thumb, 0, 0, $width, $height, $bgcolor);
imagealphablending($thumb, true);

imagecopyresampled($thumb, $image, 0, 0, 0, 0,
$width, $height, $width_orig, $height_orig);
$thumb2 = imagecreatetruecolor($imagethumbsize_w , $imagethumbsize_h);
// true color for best quality
$bgcolor = imagecolorallocate($thumb2, $red, $green, $blue);
ImageFilledRectangle($thumb2, 0, 0,
$imagethumbsize_w , $imagethumbsize_h , $white);
imagealphablending($thumb2, true);

$w1 =($width/2) - ($imagethumbsize_w/2);
$h1 = ($height/2) - ($imagethumbsize_h/2);

imagecopyresampled($thumb2, $thumb, 0,0, $w1, $h1,
$imagethumbsize_w , $imagethumbsize_h ,$imagethumbsize_w, $imagethumbsize_h);

// Output
//header('Content-type: image/gif');
//imagegif($thumb); //output to browser first image when testing

if ("$fileout" !="")imagegif($thumb2, "$fileout"); //write to file
header('Content-type: image/gif');
imagegif($thumb2); //output to browser
}
?>

ess
07-29-2007, 03:10 PM
It just means that the allocated memory to run your application is not large enough to perform the image generation process.

Therefore, you will have to increase memory either by contacting your web host administrators, or you can increase the memory for that particular operation yourself.

To increase the memory yourself, add the following line at the top of the file that generates the thumb nail


ini_set("memory_limit","30M");

Hope that helps.

Cheers,
Ess

alex57
07-29-2007, 03:58 PM
Hello, I tried adding that line and increased the number to 50MB too but a similiar error occurred. The script works on my local WAMP installation so this suggests it is a host problem. I may need to talk to them. however it is weird this script ran before.

Is there a way to clear the memory just before:


$image = imagecreatefromjpeg("$filein");

thanks

JordanW
07-29-2007, 04:04 PM
This is a common misconception. Maybe people still believe that just reusing the image variable will clear all previous data on the variable, but this isn't the case.

Right at the end of your script, when you are finished with the image use the code:



imagedestroy($image);


The PHP reference page can be found here:

http://uk2.php.net/imagedestroy

alex57
07-31-2007, 10:37 PM
the script creates the error on the first image loaded into the createimagefromjpeg() and before the end of the script and so destroying the image there would not make a difference

rafiki
07-31-2007, 10:58 PM
did you try it?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum