imagecopyresampled Produces Poor Results on Image Enlargemen

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
kevin7654
Forum Newbie
Posts: 19
Joined: Sat Feb 18, 2006 2:38 pm

imagecopyresampled Produces Poor Results on Image Enlargemen

Post by kevin7654 »

Hello everyone,

I'm attempting to enlarge an image that users have uploaded, from about 600x400 pixels to 2400x1600 pixels. When I do this with imagecopyresampled, it produces very pixelated results. Does anyone know a superior way to resample an image?

Thanks,
Kevin
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I think no matter what you to .. enlaring an image 400% will severly decrease the quality
kevin7654
Forum Newbie
Posts: 19
Joined: Sat Feb 18, 2006 2:38 pm

Post by kevin7654 »

yeah I see what you're saying, I'm just hoping for a method that makes it look blurry and smooth rather than blurry and pixelated
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I wouldn't say always.. 400% is not too hard, but requires subpixel sampling. Since that can require quite a bit of processing power to perform it in a timely manor among other things.. you may need to write this functionality yourself, unless onion2k wrote one.. I know he's written somewhat similar offshoots.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

kevin7654
Forum Newbie
Posts: 19
Joined: Sat Feb 18, 2006 2:38 pm

Post by kevin7654 »

jshpro2 wrote:http://www.greyc.ensicaen.fr/~dtschump/ ... ation.html

scroll down to "image resizing"
that looks great, do you know if there's any translations to php?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

If it were me, I'd use it as a chance to get acquainted with writing PHP extensions so I wouldn't have to translate it to PHP... but that's just me. The algorithm likely is very complicated.
kevin7654
Forum Newbie
Posts: 19
Joined: Sat Feb 18, 2006 2:38 pm

Post by kevin7654 »

Ambush Commander wrote:If it were me, I'd use it as a chance to get acquainted with writing PHP extensions so I wouldn't have to translate it to PHP... but that's just me. The algorithm likely is very complicated.
Could you please elaborate on that some more, I'm not sure what you mean by writing PHP extensions
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, you've got some C++ code. You can turn it into a C++ extension, which creates functions that can be called internally to process the C++ code. No PHP translation needed!

But I've never tried doing that before, and it's not for the faint-hearted (you still need to know C++)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You could try upsampling it as you are now, and then doing a Gaussian Blur .. http://www.phpgd.com/scripts.php?script=12 .. that's my Fast Convolution code .. it should do the job, but on a 2400*1600 image it'll take a LONG time. PHP 5 has a convolution function builtin, but it'll only do a 3*3 kernel .. not enough in my opinion.
kevin7654
Forum Newbie
Posts: 19
Joined: Sat Feb 18, 2006 2:38 pm

Post by kevin7654 »

onion2k wrote:You could try upsampling it as you are now, and then doing a Gaussian Blur .. http://www.phpgd.com/scripts.php?script=12 .. that's my Fast Convolution code .. it should do the job, but on a 2400*1600 image it'll take a LONG time. PHP 5 has a convolution function builtin, but it'll only do a 3*3 kernel .. not enough in my opinion.
thanks for the suggestion onion2k. Unfortunately even when I allocate 256MB of memory to the task it still exceeds that value. So I'm still searching for other options, any other suggetions?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

kevin7654 wrote:thanks for the suggestion onion2k. Unfortunately even when I allocate 256MB of memory to the task it still exceeds that value. So I'm still searching for other options, any other suggetions?
Yeah.. memory is a bit of an issue with large images. There's no way around it .. if you want to work with big pictures you'll need to allocate as much RAM as possible. I think I've currently got PHP using 768Mb.. and I run out occasionally.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

I was running into issues with ram limits just using imagecopyresampled going down. My script kept failing on larger images and found it was a memory limit issue.
I found the following off php.net and adjusted the ini_set to fit my needs (because I needed to resize twice for thumbnail and large image added 2mb for resizing from the sampled large to thumb). There are probably issues in using this, but for my occasional uploads it seems to work fine.

Code: Select all

$imageInfo = getimagesize($uploaded);
$memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65);
echo $memoryNeeded."<br>";
if ($memoryNeeded > 6000000) { ini_set( 'memory_limit' , ($memoryNeeded + 2000000)); }

/// after resizing is complete
ini_restore ( 'memory_limit' );
If anything you could use it without the ini_set and see how much memory you need.
Post Reply