How to get image resource size in bytes with PHP and GD?

I am resizing images using php gd. The result is image resources that I want to upload to Amazon S3. It works fine if I first store the images on disk, but I would like to load them directly from memory. This is possible if I just know the byte size of the image.

Is there a way to get the size (in bytes) of the gd image resource?

+7
php image gd
source share
5 answers

You can use PHP's memory I / O stream to save the image and subsequently get the size in bytes.

What are you doing:

$img = imagecreatetruecolor(100,100); // do your processing here // now save file to memory imagejpeg($img, 'php://memory/temp.jpeg'); $size = filesize('php://memory/temp.jpeg'); 

You should now know the size

I do not know any (gd) method to get the size of the image resource.

+12
source share

I cannot write in php: // memory using imagepng, so I use ob_start (), ob_get_content () end ob_end_clean ()

 $image = imagecreatefrompng('./image.png'); //load image // do your processing here //... //... //... ob_start(); //Turn on output buffering imagejpeg($image); //Generate your image $output = ob_get_contents(); // get the image as a string in a variable ob_end_clean(); //Turn off output buffering and clean it echo strlen($output); //size in bytes 
+9
source share

This also works:

 $img = imagecreatetruecolor(100,100); // ... processing ob_start(); // start the buffer imagejpeg($img); // output image to buffer $size = ob_get_length(); // get size of buffer (in bytes) ob_end_clean(); // trash the buffer 

And now $size will have your size in bytes.

+5
source share

You can ask for help in the following answer. It works for general memory changes in php. Although, since overhead may be involved, this may be a better estimate.

Getting the size of PHP objects

0
source share

Save the image file in the desired format in dmp tmp, and then use the file size () http://php.net/manual/de/function.filesize.php before downloading it to S3 from disk.

-one
source share

All Articles