Jump to content

Get Image File Size from an Image in Memory


jyak

Recommended Posts

I'm not going to post my entier script; rest assured that there is nothing wrong with the generation of the image.

 

...
header('Content-type: image/jpeg');
imagejpeg($img, NULL, $JPGQual);
...

 

The image $img is never actually saved in a specific location, but it is in memory (how else would it be drawn). It's promptly removed from memory at the end of the script and the world goes on.

 

Now here is my question: Is it possible to grab the file size of $img in it's outputted jpeg form? For example, if the image that is generated is viewed by a user, they can right click on it, click properties and see the file size of the image - that is what I'm trying to obtain before it is sent.

 

Note:

getimagesize() will not work here because it doesn't actually obtain file size and it doesn't work with resources.

filesize() will not work here becuase the image isn't actually saved, and it doesn't work with image resources.

 

Also note that this is going to be accessed around 200,000-300,000 time per day.

 

I have two ideas of how to do this:

1) Save a temp image using the imagejpeg() function and do filesize() - I'm worried this may use much more memory and processing power than an alternative.

 

2) Use an output buffer to capture the actual "text" data from the imagejpeg() function and put it in a string. I have a function to calculate bytes in a string which will be about the same as the image's file size. - I'm worried that this may also be more resource demanding than an alternative method, or possibly too inaccurate to depend upon.

 

So, my question(s) are this:

Which method would you recommend more (#1 or 2)?

Or, do you know of a better method I can use?

 

All of this is just for me to log outgoing bandwidth; accuracy doesn't need to be dead-on, but I would at least like it to remotely resemble the true statistics.

 

Thanks very much in advance.

Link to comment
Share on other sites

200 to 300k temp images, even if brief, will kill your memory.  The creation of the image normally takes tens of MBs per image, even with ones that aren't particularly large.  Using a buffer to track the length of it makes much more sense, though I've never had need to write the code.

Link to comment
Share on other sites

Use the ob_ functions

 

<?php

// other image function handling here...

ob_start ();

imagejpeg ( $image, NULL, $JPGQual );

imagedestroy ( $image );

$image = ob_get_contents ();

ob_end_clean ();

$name = 'some_image.jpg';

header ( 'Cache-Control: max-age=31536000' );

header ( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );

header ( 'Content-Length: ' . strlen ( $image ) );

header ( 'Content-Disposition: filename="' . $name . '"' );

header ( 'Content-Type: image/jpeg; name="' . $name . '"' );

echo $image;

exit ();

?>

 

I would use method #2, because you destroy resource and are only left with the actual image as a string buffer. Remember a resource holds image width * image height * 5 (bytes) for true color images and image width * image height * 2 (bytes) for palette based images.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.