Page 1 of 1

imagecopyresampled Produces Poor Results on Image Enlargemen

Posted: Sat Feb 18, 2006 2:39 pm
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

Posted: Sat Feb 18, 2006 2:45 pm
by John Cartwright
I think no matter what you to .. enlaring an image 400% will severly decrease the quality

Posted: Sat Feb 18, 2006 2:52 pm
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

Posted: Sat Feb 18, 2006 2:53 pm
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.

Posted: Sat Feb 18, 2006 3:14 pm
by josh

Posted: Sat Feb 18, 2006 4:37 pm
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?

Posted: Sat Feb 18, 2006 5:33 pm
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.

Posted: Sat Feb 18, 2006 8:35 pm
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

Posted: Sat Feb 18, 2006 10:40 pm
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++)

Posted: Sun Feb 19, 2006 8:05 am
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.

Posted: Mon Feb 20, 2006 5:50 pm
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?

Posted: Tue Feb 21, 2006 3:19 am
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.

Posted: Tue Feb 21, 2006 9:56 am
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.