views:

323

answers:

1

Hi all, I'm using a code which will upload an image, put the image in the "resize" folder, resize the image, move the image into another folder, then delete the image from the "resize" folder, however I'm getting the following error:

"Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 14172 bytes) in /home/photogra/public_html/administrator/components/com_gallery/admin.gallery.php on line 649"

The images aren't even big! (eg. 265kb)

Here's the code I'm using (with the line numbers):

635   move_uploaded_file($_FILES['image']['tmp_name'],$mainframe->getCfg( 'absolute_path' ) ."/virtualgallery/images/resize/$newname");
636   
637   /* resize images - width 600px */ 
638   $docRoot = $GLOBALS['mosConfig_absolute_path'];
639   $pathToImages = $docRoot.'/virtualgallery/images/resize/';
640   $pathToThumbs = $docRoot.'/virtualgallery/images/';
641   $thumbHeight = 600;
642   
643   $dir = opendir( $pathToImages );
644   while (false !== ($fname = readdir( $dir ))) {
645    $info = pathinfo($pathToImages . $fname);
646    if ( strtolower($info['extension']) == 'jpg' ) {
647     $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
648     $width = imagesx( $img );
649     $height = imagesy( $img );
650     $new_width = floor( $width * ( $thumbHeight / $height ) );
651     $new_height = $thumbHeight;
652     $tmp_img = imagecreatetruecolor( $new_width, $new_height );
653     imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
654     imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
655    };
656   };
657   closedir( $dir );
658   
659   /* delete file(s) from resize folder */
660   $dir = $docRoot.'/virtualgallery/images/resize/';
661   foreach(glob($dir.'*.*') as $v) {
662    unlink($v);
663   };

Also when I get that error, images are getting stuck in the "resize" folder.. If anyone can help, that'd be fantastic! :)

+2  A: 

You're trying to resize all the images in a directory without freeing the memory after each one. Try adding

imagedestroy($img);
imagedestroy($tmp_img);

For starters. Also, unlink the image as soon as you're done with it rather than iterating over the directory a second time.

Mark
Thanks for your help, seems to be working ok so far... :)
SoulieBaby