Which is best... Image resizing functions.

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

Which is best... Image resizing functions.

Post by JellyFish »

Which is the better image resizing function: imagecopyresized() or imagecopyresampled()? Better as in results in a more clear, more accurate resized image?

I'm looking to resize an uploaded image, and I'm sure I'm not the first to be asking question about this. So any tips would be great! :D

Thanks.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

A look at the PHP manuals shows that the only difference is this sentence:

"smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity"

So, my guess is that imagecopyresampled() would give better image quality.
I can't imagine how an image resizing can be done without any interpolation, but maybe imagecopyresampled() uses higher interpolation order than imagecopyresized().
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

VladSun wrote:A look at the PHP manuals shows that the only difference is this sentence:

"smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity"

So, my guess is that imagecopyresampled() would give better image quality.
I can't imagine how an image resizing can be done without any interpolation, but maybe imagecopyresampled() uses higher interpolation order than imagecopyresized().
Yeah, that's kinda what I thought.

Also, if I'm going to resize an uploaded image then I guess this wouldn't work anymore:

Code: Select all

move_uploaded_file($_FILES['image']['tmp_name'],"/LOCATION/".$_FILES['image']['name']);
right? I'm assuming that move_uploaded_file() only works with uploaded image resources ($_FILES['image']['tmp_name']), rather then a resource that would be returned by imagegif() or something.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

1) imagecopyresampled() makes a much better image than imagecopyresized().
http://www.phpgd.com/articles.php?article=8&page=1 wrote:There is also a quicker option to resize the image. PHP's imagecopyresized() function is a lot faster, and less CPU intensive than imagecopyresampled(). However, it uses a basic 'Nearest Neighbour' algorithm so the results look blocky and poor quality.
http://www.phpgd.com/articles.php?article=8&page=1

2) move_uploaded_file() only works on newly uploaded files. You want to use the rename() function on files that haven't been newly uploaded.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

pickle wrote: 2) move_uploaded_file() only works on newly uploaded files. You want to use the rename() function on files that haven't been newly uploaded.
Why do I have to use move_uploaded_file() in particular? Isn't there another function that would take a newly created image and save it to a directory?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

if memory serves, imagecopyresampled() creates a new colour index when you're dealing with indexed formats. imagecopyresized() uses the same colour map (which is often U-G-L-Y)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Well, there are the various image creation functions.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply