imagecopyresampled Produces Poor Results on Image Enlargemen
Moderator: General Moderators
imagecopyresampled Produces Poor Results on Image Enlargemen
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
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
that looks great, do you know if there's any translations to php?jshpro2 wrote:http://www.greyc.ensicaen.fr/~dtschump/ ... ation.html
scroll down to "image resizing"
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Could you please elaborate on that some more, I'm not sure what you mean by writing PHP extensionsAmbush 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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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?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.
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.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?
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.
If anything you could use it without the ini_set and see how much memory you need.
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' );