Resize images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaandrws
Forum Newbie
Posts: 9
Joined: Sat Mar 31, 2007 8:13 pm

Resize images

Post 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
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post 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);
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
lanasa
Forum Newbie
Posts: 14
Joined: Mon Mar 26, 2007 7:49 am
Location: Buffalo, NY

Resizing an Image

Post by lanasa »

User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Resizing an Image

Post by JellyFish »

I don't understand. What's different? Is it because the quality parameter was set in the imagejpeg() function?
Post Reply