Reputation: 1181
Is there a php function which will do image resizing math itself but not create a new file?
I created a simple proportionally-resize-an-image math script and I realized there is more to it than just geometry, such as aspect ratio and such. Basic geometry math squashes the image a little.
If anyone is curious, it's simply to give the user a preview of image dimensions should they choose to download, but I'd rather not clutter up the server with tons of size variations of similar images :D.
Thanks.
edit: Per request, here is the key part of the resizing code:
$ratio = min( $resize_to / $width, $resize_to/ $height );
$width = $ratio * $width;
$height = $ratio * $height;
When I see the values output, I simply do a resize in photoshop using the values and the photoshop version squashes (I do this as a visual test), so I assume my code is faulty.
Upvotes: 0
Views: 569
Reputation: 1181
This is funny. I was resizing one file, then checking another in photoshop. Human error. Went around the block to cross the street and had already crossed the street without knowing it..
Upvotes: 0
Reputation: 2352
The math required to scale an image while respecting aspect ratio is pretty straightforward. Some pseudo code:
original_width = <width of image before resizing>
original_height = <height of image before resizing>
new_width = <the max width desired for the resized image>
new_height = <the max height desired for the resized image>
if original_width >= original_height
ratio = original_width / original_height
new_height = new_width / ratio
else
ratio = original_height / original_width
new_width = original_height / ratio
end
print "Width: " + new_width
print "Height: " + new_height
The above can probably be significantly improved but I just meant to illustrate the point.
Upvotes: 3