Advertisement
6_2008-2009 Graphics/ Sound #204483

Image Resize (Aspect Ratio, Actual Resize)

I've search the PHP codes, found two folks who have published resize code. Problem with one , is that it only gives you the new size but you set it in the HTML's IMG tag, which is dumb in my opinion because you still are downloading the entire image. Another submision did the same, except you could use a width, but they did a loop decreasing the size times .5 until it was close as it can get to the width you wanted, problem with this was it was slow. In either case neither actually resized the image just defined a new size. I took an hour did some reasearch at php.net, and found basic information to make this code.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
<?
/* Informs the browser the data being sent back is a Jpeg Image */
Header ("Content-type: image/jpeg");
/* loads image passed thru script
ie: gd.php?img_name=zoom.jpg */
$src_img = imagecreatefromjpeg($img_name);
/* desired width of the thumbnail */
$picsize = 123; 
/* grabs the height and width */
$new_w = imagesx($src_img);
$new_h = imagesy($src_img);
/* calculates aspect ratio */
$aspect_ratio = $new_h / $new_w;
/* sets new size */
$new_w = $picsize;
$new_h = abs($new_w * $aspect_ratio);
/* creates new image of that size */
$dst_img = imagecreate($new_w,$new_h);  
/* copies resized portion of original image into new image */
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
/* return jpeg data back to browser */
imagejpeg($dst_img);
?>
Original Comments (3)
Recovered from Wayback Machine