<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: PHP - resize image to a specific (k) size?</title>
	<link>http://ask.metafilter.com/30668/PHP-resize-image-to-a-specific-k-size/</link>
	<description>Comments on Ask MetaFilter post PHP - resize image to a specific (k) size?</description>
	<pubDate>Thu, 12 Jan 2006 08:55:52 -0800</pubDate>
	<lastBuildDate>Thu, 12 Jan 2006 08:55:52 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: PHP - resize image to a specific (k) size?</title>
		<link>http://ask.metafilter.com/30668/PHP-resize-image-to-a-specific-k-size</link>	
		<description>Formula to resize an image (in PHP) so that the resulting file is roughly a certain size in bytes? &lt;br /&gt;&lt;br /&gt; The task: If a file uploaded through a web form is &amp;gt;50k, scale it down so it&apos;s about 50k.&lt;br&gt;
&lt;br&gt;
My PHP code for the actual scaling is working fine; what I&apos;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:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;	$pct = ($max_k * 1024) / filesize($file);&lt;br&gt;
	list($width_orig, $height_orig) = getimagesize($file);&lt;br&gt;
	$height = $height_orig * $pct;&lt;br&gt;
	$width = $width_orig * $pct;&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
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?&lt;br&gt;
&lt;br&gt;
Or is it going to come out inaccurately whatever I do because there&apos;s so much variation among individual images?&lt;br&gt;
&lt;br&gt;
Any suggestions appreciated.</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2006:site.30668</guid>
		<pubDate>Thu, 12 Jan 2006 08:53:18 -0800</pubDate>
		<dc:creator>staggernation</dc:creator>
		
			<category>php</category>
		
			<category>images</category>
		
			<category>jpeg</category>
		
			<category>gif</category>
		
			<category>scale</category>
		
			<category>resize</category>
		
	</item> <item>
		<title>By: twiggy</title>
		<link>http://ask.metafilter.com/30668/PHP-resize-image-to-a-specific-k-size#482367</link>	
		<description>&lt;i&gt;Or is it going to come out inaccurately whatever I do because there&apos;s so much variation among individual images?&lt;/i&gt;&lt;br&gt;
&lt;br&gt;
There&apos;s your problem right there.  JPG images can compress really, really well, or really really poorly...  Your best bet, though it&apos;s somewhat system intensive, is a bit of &quot;trial and error&quot;... perhaps have 2 or 3 preset sizes to size down to, and automatically choose the one that is of optimal filesize using some formula.&lt;br&gt;
&lt;br&gt;
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...</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.30668-482367</guid>
		<pubDate>Thu, 12 Jan 2006 08:55:52 -0800</pubDate>
		<dc:creator>twiggy</dc:creator>
	</item><item>
		<title>By: glenwood</title>
		<link>http://ask.metafilter.com/30668/PHP-resize-image-to-a-specific-k-size#482386</link>	
		<description>There is so much more info in a picture contributing to it&apos;s size other than it&apos;s literal dimensions. I can&apos;t imagine this would be possible.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.30668-482386</guid>
		<pubDate>Thu, 12 Jan 2006 09:15:52 -0800</pubDate>
		<dc:creator>glenwood</dc:creator>
	</item><item>
		<title>By: smackfu</title>
		<link>http://ask.metafilter.com/30668/PHP-resize-image-to-a-specific-k-size#482398</link>	
		<description>I would guess that there should be some correlation between percentage reduction and file size &amp;mdash; &lt;b&gt;for a specific given image&lt;/b&gt;.  Not in general, like you are hoping for. &lt;br&gt;
&lt;br&gt;
 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.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.30668-482398</guid>
		<pubDate>Thu, 12 Jan 2006 09:50:29 -0800</pubDate>
		<dc:creator>smackfu</dc:creator>
	</item><item>
		<title>By: Rhomboid</title>
		<link>http://ask.metafilter.com/30668/PHP-resize-image-to-a-specific-k-size#482432</link>	
		<description>You can do this with a binary search.  In pseudocode:&lt;br&gt;
&lt;br&gt;
&lt;tt&gt;factor = 0.5&lt;br&gt;
target_size = 50k&lt;br&gt;
tolerance = 3k&lt;br&gt;
do&lt;blockquote&gt;new_image = resize_by(orig_image, factor)&lt;br&gt;
diff = size(new_image) - target_size&lt;br&gt;
if(diff &amp;gt; 0)&lt;blockquote&gt;factor = factor / 1.5&lt;/blockquote&gt;else&lt;blockquote&gt;factor = factor * 1.5&lt;/blockquote&gt;&lt;/blockquote&gt;while(abs(diff) &amp;gt; tolerance)&lt;/tt&gt;&lt;br&gt;
&lt;br&gt;
Of course, you might want to limit the number of iterations to make sure that it converges without sucking down too much CPU usage.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.30668-482432</guid>
		<pubDate>Thu, 12 Jan 2006 10:15:47 -0800</pubDate>
		<dc:creator>Rhomboid</dc:creator>
	</item><item>
		<title>By: Rhomboid</title>
		<link>http://ask.metafilter.com/30668/PHP-resize-image-to-a-specific-k-size#482438</link>	
		<description>er, scratch that... that will loop forever.  You need something a little more sophisticated than just dividing/multiplying &apos;factor&apos; by some consant.  Store the previous version of &apos;factor&apos; and make the new one halfway in between the current value and the last value.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.30668-482438</guid>
		<pubDate>Thu, 12 Jan 2006 10:19:22 -0800</pubDate>
		<dc:creator>Rhomboid</dc:creator>
	</item><item>
		<title>By: staggernation</title>
		<link>http://ask.metafilter.com/30668/PHP-resize-image-to-a-specific-k-size#483014</link>	
		<description>&lt;b&gt;smackfu&lt;/b&gt;: 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!</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.30668-483014</guid>
		<pubDate>Thu, 12 Jan 2006 19:48:27 -0800</pubDate>
		<dc:creator>staggernation</dc:creator>
	</item><item>
		<title>By: bricoleur</title>
		<link>http://ask.metafilter.com/30668/PHP-resize-image-to-a-specific-k-size#483225</link>	
		<description>Using PHP to &quot;resize&quot; an image like that actually changes the &lt;i&gt;file&lt;/i&gt; size? I would have thought it just changed the display size. The original file would still have to load, right?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.30668-483225</guid>
		<pubDate>Fri, 13 Jan 2006 05:29:18 -0800</pubDate>
		<dc:creator>bricoleur</dc:creator>
	</item><item>
		<title>By: staggernation</title>
		<link>http://ask.metafilter.com/30668/PHP-resize-image-to-a-specific-k-size#483379</link>	
		<description>No, I&apos;m using PHP&apos;s &lt;a href=&quot;http://us2.php.net/gd&quot;&gt;image functions&lt;/a&gt; to resample the image and save a new image file that&apos;s a smaller size, replacing the original.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.30668-483379</guid>
		<pubDate>Fri, 13 Jan 2006 09:17:43 -0800</pubDate>
		<dc:creator>staggernation</dc:creator>
	</item>
	</channel>
</rss>
