Web Analytics Made Easy -
StatCounter What is the max image size that GD can handle? - CodingForum

Announcement

Collapse
No announcement yet.

What is the max image size that GD can handle?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the max image size that GD can handle?

    Hello!

    I've run into this situation where as I'm trying to create a thumbnail image from a full resolution image and the script just stops. No error, nothing! Which is odd!

    Below is the function I'm using. Which does work! I have no problems getting a thumbnail out of a 4K (3840×2160) resolution image.

    PHP Code:
    function resizeImage($sourceImage$targetImage$maxWidth$maxHeight$quality 90)
    {
        
    $isValid = @getimagesize($sourceImage);

        if (!
    $isValid)
        {
            return 
    false;
        }

        
    // Get dimensions and type of source image.
        
    list($origWidth$origHeight$type) = getimagesize($sourceImage);

        if (
    $maxWidth == 0)
        {
            
    $maxWidth  $origWidth;
        }

        if (
    $maxHeight == 0)
        {
            
    $maxHeight $origHeight;
        }

        
    // Calculate ratio of desired maximum sizes and original sizes.
        
    $widthRatio $maxWidth $origWidth;
        
    $heightRatio $maxHeight $origHeight;

        
    // Ratio used for calculating new image dimensions.
        
    $ratio min($widthRatio$heightRatio);

        
    // Calculate new image dimensions.
        
    $newWidth  = (int)$origWidth  $ratio;
        
    $newHeight = (int)$origHeight $ratio;

        
    // Create final image with new dimensions.
        
    $newImage imagecreatetruecolor($newWidth$newHeight);

        
    // Obtain image from given source file.
        
    switch(strtolower(image_type_to_mime_type($type)))
        {
            case 
    'image/jpeg':                      
                
    $image = @imagecreatefromjpeg($sourceImage);            
                if (!
    $image)
                {
                    return 
    false;
                }

                
    imagecopyresampled($newImage$image0000$newWidth$newHeight$origWidth$origHeight); 

                if(
    imagejpeg($newImage,$targetImage,$quality))
                {
                    
    // Free up the memory.
                    
    imagedestroy($image);
                    
    imagedestroy($newImage);
                    return 
    true;
                }            
            break;
            
            case 
    'image/png':
                
    $image = @imagecreatefrompng($sourceImage);

                if (!
    $image)
                {
                    return 
    false;
                }

                
    imagecopyresampled($newImage$image0000$newWidth$newHeight$origWidth$origHeight);

                if(
    imagepng($newImage,$targetImagefloor($quality 10)))
                {
                    
    // Free up the memory.
                    
    imagedestroy($image);
                    
    imagedestroy($newImage);
                    return 
    true;
                }
            break;
                    
            default:
                return 
    false;
           }

    This is how I'm calling that function. Obviously $infile is the source and $outfile is the location where the thumbnail is saved. I'm using a max of 300px both on the height and width for the finished image. Like I stated above the script just stops, I don't get the echo for failed nor the die statement.

    PHP Code:
        if (!resizeImage($infile$outfile300300)) {
            echo 
    "Image resize FAILED!<br/>";
            die(
    "Oh the humanity!<br/>");
        } 
    This is an image for a sample.

    Click image for larger version

Name:	wallpaper-20190721093731-3197.jpg
Views:	1
Size:	40.2 KB
ID:	2284772

    So I'm under the assumption that there is some resolution constraint on what the GD library can handle.

    Thanks for any input and help!
    Dave
    Last edited by MrBiggZ; Aug 10th, 2019, 02:24 PM.
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #2
    Start by removing all the @ error suppressors. If you are getting a fatal runtime error, you will never know it with those in the code. Next, create a .php script with a phpinfo() statement in it to check if php's error_reporting is set to E_ALL and either display_errors is set to ON, or log_errors is set to ON.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment


    • #3
      Originally posted by CFMaBiSmAd View Post
      Start by removing all the @ error suppressors. If you are getting a fatal runtime error, you will never know it with those in the code. Next, create a .php script with a phpinfo() statement in it to check if php's error_reporting is set to E_ALL and either display_errors is set to ON, or log_errors is set to ON.
      Oh! Seems I was told something wrong a long time ago! =\ I found that section in the doc, now it makes 100% sense!

      The second half I did already, that's what originally had me scratching my head.

      So, I'm gonna take that bad boy out of there and see what happens! Oi!

      I'll report back!
      “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

      Comment


      • #4
        The results!

        Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 65536 bytes) in C:\www\wallpapers\test.php on line 105
        That's crazy, the jpg is just a little over 2MB. And in my php.ini file memory_limit=512M. Mind you this isn't any kind of production thing, I'm just doing this on my laptop and it'll pretty much be for my own use.

        But why does it need so much memory?
        “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

        Comment


        • #5
          Originally posted by MrBiggZ View Post
          But why does it need so much memory?
          Digital image formats are compressed (similar to zipping a file) either using a raster or vector method where the data making up the image is reduced by sampling the values contained within small sections of the image, rather than to carry all the actual color data for each pixel. To edit/operate on an image at the pixel level, you must reverse this, by producing and temporarily storing the color data for each pixel.
          Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

          Comment


          • #6
            Originally posted by CFMaBiSmAd View Post
            Digital image formats are compressed (similar to zipping a file) either using a raster or vector method where the data making up the image is reduced by sampling the values contained within small sections of the image, rather than to carry all the actual color data for each pixel. To edit/operate on an image at the pixel level, you must reverse this, by producing and temporarily storing the color data for each pixel.
            Gotcha!
            “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

            Comment


            • #7
              Originally posted by MrBiggZ View Post
              But why does it need so much memory?
              Pixels are usually stored in 32 bit format (despite being 24 bit depth) so they align for faster processing. That's 4 bytes per pixel.

              3840 * 2160 * 4 == 33,177,600 == 31.64 megabytes.

              GD needs to allocate memory for loading the .jpg into, a buffer for the greymap, a buffer for the colour plane, and a final buffer. That's 98 megabytes BEFORE you even do anything to the image. (though it's possible to do it in less, GD prefers to use more memory to make it faster)

              Also remember that 512m limit is for EVERYTHING PHP is doing, every single process calling PHP shares that slice.

              That it's bombing whilst trying to allocate a 64k chunk? The image is probably choking out the pointer allocation.
              "It is amazing what can be accomplished when nobody cares who gets the credit." -- Kelly Johnson
              http://www.cutcodedown.com

              Comment

              Working...
              X