PDA

View Full Version : Image resizing functions.


Kayn
Thu 7th Aug '03, 8:23pm
I'm working on image resizing stuff, because having to create thumbnails before you upload images is a pain for my editors. Plus, some of them aren't great with graphics software. So I wanted to be awesome and make it to where they upload the image, and as the review loads on the reviews page, the image is automatically scaled down.

Not just that, but the filesize is reduced, and the aspect ratio is still there (meaning if I have an image that's 800 x 450 and need to make the width 125, it'll automatically adjust the height, thus making the thumbnail dimensions 125 x 70).

So... any of you have some clever code that'll do this? I'm currently playing with some of the built in PHP image functions (like these - http://us4.php.net/manual/en/function.imagecopyresized.php ), and any suggestions would be great.

Thanks! :)

Dimava
Thu 7th Aug '03, 8:43pm
try this:

$src_img = imagecreatefromjpeg("temp/image1.jpg");
$picsize = 200;
$new_w = imagesx($src_img);
$new_h = imagesy($src_img);
$aspect_ratio = $new_h / $new_w;
$new_w = $picsize;
$new_h = abs($new_w * $aspect_ratio);
/* creates new image of that size */

$dst_img = imagecreatetruecolor($new_w,$new_h);
for($i=0; $i<256; $i++) imagecolorallocate($dst_img, $i, $i, $i);
/* copies resized portion of original image into new image */
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_ w,$new_h,imagesx($src_img),imagesy($src_img));
$exportfile = "thumb" . "image1.jpg";
imagejpeg($dst_img, $exportfile, 80);


this is what I use for one of my sites, you might have to modify it a little to fit your needs

-Dimava