...

View Full Version : script error - memory size



alex57
07-27-2007, 08:48 PM
I am usingthe same script on my local machine and on my web host. On the local machine the script works fine with a memory limit of 8M whereas the web host shows the error:

Fatal error: Allowed memory size of 50331648 bytes exhausted (tried to allocate 11264 bytes) in /hermes/web05/b1406/pow.alexwilliams57/htdocs/trip2/attempt.php on line 137

The script ran on the web host fine yesteday but didnt work today which is strange. I tried increasing the memory limit to as much as 32M but it didnt work. Line 137 of the script is:


$image = imagecreatefromjpeg("$filein");

where 'filein' is a path name to a jpg

thanks in advance

rafiki
07-27-2007, 09:02 PM
try unset()ing things your not using and mysql_free_result(); to allocate memory not sure if this would give you enough though.
also list the whole script so we can see why the memory usage is so much.

alex57
07-27-2007, 09:33 PM
I pieced this scripted together from several open sources and finally got it to do the desired effect which is to create a thumbnail of a specific size from a larger photo. The file goes through a directory and works on each jpg at a time. Its odd that the script was fine yesterday.


<?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 );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
//createThumbs("upload/","/thumbs/",100);


// The file
//$filein = ''; // File in
//$fileout = 'ttt.gif'; // Fileout - optional

//$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");

//Author Alan Reddan Silverarm Solutions
//Date 27/01/2005
//Function that works well with images.
//It takes the image and reduces its size to best fit. i.e If you have an image
//that is 200 X 100 and you want a thumbnail of 75 X 50,
//it first resizes the image to 100 X 50
//and then takes out a portion 75 X 50 from then center of the input image.
//So loads of image information is retained.
//The corollary also holds if your input image is 100 X 200
//it first resizes image to 75 X 150 and then takes out a
//portion 75 X 75 from the centre
// The advantage here is that function decides on whether
//resize is by width or height itself.
//it also decides whether to use the height or the width as the base start point
//in the case that athumbnail is rectangular


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
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum