Karl Blessing
Programmer
I was searching planetsourcecode for something to resize images into thumbnails on the fly, unfortunatly the only two PHP solutions offered there, only told the HTML the height and width to put in the <IMG> tag, which was kinda useless as you would still be downloading the whole full size of the image, so a little reasearch at php.net, and a bit of help by turning on the php_gd.dll extension in my php.ini file (I have PHP 4.1.2 Win32 binaries as my instalation, older versions may need to be recompiled with GD exentsion) and I came up with this code.
i've seen some examples, loop each time multiplying by .5 until it got close enough to the widh, but I found that to be slow and a waste of time when you could just determine the aspect ratio by deviding them. But in any case, the script above, runs in it's own php file, you call the Php passing it ?img_name=... giving it the image name you want to resize into a thumbnail, so you can use this in an image tag
<img src="gd.php?img_name=zoom.jpg"> and it'll resize your image and return the resized iamge to the browser, all without having you download the full size of the original image.
Hope this is helpfull
Link to my post on planetsourcecode :
Karl Blessing aka kb244{fastHACK}
Code:
<?
/* 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
include a second parameter of a file name if
you just want to save the new file to disk*/
imagejpeg($dst_img);
?>
i've seen some examples, loop each time multiplying by .5 until it got close enough to the widh, but I found that to be slow and a waste of time when you could just determine the aspect ratio by deviding them. But in any case, the script above, runs in it's own php file, you call the Php passing it ?img_name=... giving it the image name you want to resize into a thumbnail, so you can use this in an image tag
<img src="gd.php?img_name=zoom.jpg"> and it'll resize your image and return the resized iamge to the browser, all without having you download the full size of the original image.
Hope this is helpfull
Link to my post on planetsourcecode :
Karl Blessing aka kb244{fastHACK}