Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts

    thumbnails memory error

    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:

    PHP Code:
    $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 Code:
    <?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$img0000$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($thumb00$width$height$bgcolor);
    imagealphablending($thumbtrue);

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

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

    imagecopyresampled($thumb2$thumb0,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
    }
    ?>

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    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

    PHP Code:
    ini_set("memory_limit","30M"); 
    Hope that helps.

    Cheers,
    Ess

  • #3
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    $image imagecreatefromjpeg("$filein"); 
    thanks

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    159
    Thanks
    2
    Thanked 15 Times in 15 Posts
    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:

    PHP Code:
    imagedestroy($image); 
    The PHP reference page can be found here:

    http://uk2.php.net/imagedestroy

  • #5
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    did you try it?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •