Page 1 of 1

Resize images

Posted: Sat Mar 31, 2007 8:18 pm
by jaandrws
I need the ability to resize images already on my server, not during an upload. I already know how to copy an image to another folder, now I need to reduce it to 90wx63h and save it at that size. I'm sorry if this is an easy question, but I'm not very good at functions and when I read a lot of the articles on the topic I get lost real easy.

-- James

Posted: Sun Apr 01, 2007 2:24 am
by JellyFish
What you do is use the imagecopyresized() php function. Description on this function is available at php.net.

Something like:

Code: Select all

$file_source = 'test.jpg';

// Content type
header('Content-type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($file_source);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$resized_img = imagecreatetruecolor($newwidth, $newheight);
$source_img = imagecreatefromjpeg($file_source);

// Resize
imagecopyresized($resized_img, $source_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Outputs the resized image to the browser
imagejpeg($resized_img);

Posted: Sun Apr 01, 2007 5:17 am
by Kieran Huggins
instead use imagecopyresampled() so your pictures don't look like crap - I have no idea why this isn;t the default behaviour of imagecopyresized().
imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.

Posted: Sun Apr 01, 2007 5:59 am
by stereofrog
Kieran Huggins wrote:I have no idea why this isn;t the default behaviour of imagecopyresized().
Because not every image format supports truecolor and because sometimes you need to resize without resampling.

Resizing an Image

Posted: Sun Apr 01, 2007 10:01 am
by lanasa

Re: Resizing an Image

Posted: Sun Apr 01, 2007 1:03 pm
by JellyFish
I don't understand. What's different? Is it because the quality parameter was set in the imagejpeg() function?