PHP - resize image to a specific (k) size?
-
Formula to resize an image (in PHP) so that the resulting file is roughly a certain size in bytes? The task: If a file uploaded through a web form is >50k, scale it down so it's about 50k. My PHP code for the actual scaling is working fine; what I'm trying to figure out is the best way to calculate the target height and width. (Leaving JPEG quality out of the equation for the time being.) This tends to produce files that are too large: $pct = ($max_k * 1024) / filesize($file); list($width_orig, $height_orig) = getimagesize($file); $height = $height_orig * $pct; $width = $width_orig * $pct; I think I need to somehow either adjust the percentage based on the proportion between the two dimensions, or multiply the area by the percentage to get a desired area and then do something with that, but what? Square roots? Or is it going to come out inaccurately whatever I do because there's so much variation among individual images? Any suggestions appreciated.
-
Answer:
I would guess that there should be some correlation between percentage reduction and file size — for a specific given image. Not in general, like you are hoping for. But you can still use this fact. You can calculate that ratio by doing a test resize, and then plug that in to get a percentage reduction that gives the appropriate filesize.
staggernation at Ask.Metafilter.Com Visit the source
Other answers
Or is it going to come out inaccurately whatever I do because there's so much variation among individual images? There's your problem right there. JPG images can compress really, really well, or really really poorly... Your best bet, though it's somewhat system intensive, is a bit of "trial and error"... perhaps have 2 or 3 preset sizes to size down to, and automatically choose the one that is of optimal filesize using some formula. Also, I know that many times programmers hate using external tools, but might I suggest ImageMagick? It will allow you to save at one fixed size, but varying levels of quality (or maybe php lets you do this natively? I dunno) so that you can choose the best image...
twiggy
There is so much more info in a picture contributing to it's size other than it's literal dimensions. I can't imagine this would be possible.
glenwood
You can do this with a binary search. In pseudocode: factor = 0.5 target_size = 50k tolerance = 3k donew_image = resize_by(orig_image, factor) diff = size(new_image) - target_size if(diff > 0)factor = factor / 1.5elsefactor = factor * 1.5while(abs(diff) > tolerance) Of course, you might want to limit the number of iterations to make sure that it converges without sucking down too much CPU usage.
Rhomboid
er, scratch that... that will loop forever. You need something a little more sophisticated than just dividing/multiplying 'factor' by some consant. Store the previous version of 'factor' and make the new one halfway in between the current value and the last value.
Rhomboid
smackfu: so simple, I never would have thought of it. But it got me a lot closer to 50k in most cases than any of the other weird geometry-based crap I tried. Thanks!
staggernation
Using PHP to "resize" an image like that actually changes the file size? I would have thought it just changed the display size. The original file would still have to load, right?
bricoleur
No, I'm using PHP's http://us2.php.net/gd to resample the image and save a new image file that's a smaller size, replacing the original.
staggernation
Related Q & A:
- How to add a contact to a specific list?Best solution by Stack Overflow
- How to resize image javascript?Best solution by Stack Overflow
- How to resize image without losing EXIF data?Best solution by Stack Overflow
- How to extract a specific text from an image?Best solution by Stack Overflow
- What is the role of a Pharmacy Technician in a U.K hospital?Best solution by work.chron.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.