Hello and welcome to our community! Is this your first visit?
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
    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;
    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
    Denver, Colorado USA
    Thanked 339 Times in 331 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 -
    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