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 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question imagecreatefromjpeg() using far too much memory for what is needed

    I am creating a website for a friend of mine who is a model.
    One of the things that she would like for the website to do, is to allow her to choose a file that she wants on the website and then for using a simple form on the website to upload the file and then make a thumbnail of that picture.

    The code I am using for the creation of the thumbnail is...
    PHP Code:
    function createThumbnail($imageDirectory$imageName$thumbDirectory$thumbHeight)
    {
        
    $srcImg imagecreatefromjpeg("$imageDirectory/$imageName");
        
    $origWidth imagesx($srcImg);
        
    $origHeight imagesy($srcImg);
        
        
    $ratio $origHeight $thumbHeight;
        
    $thumbWidth $origWidth $ratio;
        
    $dst_img=ImageCreateTrueColor($thumbWidth,$thumbHeight);
        
    imagecopyresampled($dst_img,$srcImg,0,0,0,0,$thumbWidth,$thumbHeight,$origWidth,$origHeight);
        
    imagejpeg($dst_img,$thumbDirectory "/" $imageName);

    When I am running the script I am recieving the error message
    Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 9792 bytes) in /home/fhlinux159/s/sarah.satalketo.co.uk/user/htdocs/adminPhotos.php on line 5
    (Line 5 being "$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");")
    Now considering the original file was 426KB I think it using up 32MB is a little silly.
    So I was wondering whether I have made an error somewhere and this is causing the massive usage of memory.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,146
    Thanks
    2
    Thanked 333 Times in 325 Posts
    Jpg/jpeg/gif/png images are compressed. To be able to operate on the images and do things like add text, draw on them, crop, rotate, save or output them in other formats the GD imagecreate.... functions uncompress the image data into a bitmap image.

    From the GD section of the manual -
    Warning
    Image functions are very memory intensive. Be sure to set memory_limit high enough.
    The total memory used is everything your program consumes for php code and data. If your code is looping over a number of images or has a lot of resources allocated for arrays or database result sets, you can probably free up some memory. Otherwise, you will need to increase the amount of memory available to php - see the memory_limit setting.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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