![]() |
PHP GD2 Geek Question
Hey, does anyone with PHP knowledge know how to resize an image using GD2, while maintaining quality?
I'm currently creating a buffer image using imagecreatetruecolor(), then resizing with imagecopyresampled(), and it comes out all grainy, like this: http://www.car-wallpapers.net/wallpa...gthumbnail.gif However the original image was a very large resolution and perfect, sharp quality. Anyone have any suggestions for techniques of resizing and retaining quality? Thanks. Here's my function in case it'd help: [code]function createThumbnail($imagePath, $imageName, $destImageName, $max_x, $max_y){ preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageName, $ext); switch (strtolower($ext[2])) { case 'jpg' : case 'jpeg': $im = imagecreatefromjpeg($imagePath . $imageName); break; case 'gif' : $im = imagecreatefromgif ($imagePath . $imageName); break; case 'png' : $im = imagecreatefrompng($imagePath . $imageName); break; default : $stop = true; break; } if (!isset($stop)) { $x = imagesx($im); $y = imagesy($im); if (($max_x/$max_y) < ($x/$y)) { $save = imagecreatetruecolor($x/($x/$max_x), $y/($x/$max_x)); } else { $save = imagecreatetruecolor($x/($y/$max_y), $y/($y/$max_y)); } imagecopyresampled($save, $im, 0, 0, 0, 0, imagesx($save), imagesy($save), $x, $y); // assume thumbnail output is gif imagegif($save, "{$imagePath}{$destImageName}"); imagedestroy($im); imagedestroy($save); } }[/code] |
yeah don't use GD, use Imagemagik. GD always resamples to 72 pixels per inch and makes things grainy. Where imagemagik will keep resolution and the comments included with the image. you will have to make system or shellexec calls to use it "on-the-fly" but you will be much happier with the results.
|
So there's no way with GD2? I don't believe I have IM installed...
|
update ports first then use ports to install should go smooth(i hope) you'll be much happier with im
|
did you bite the bullet and install it yet?
|
Yep, installed it, but haven't had the chance to sit down and learn it yet.
|
cool, it works quite nicely and is very full-featured. It is also well documented. one thing I love is that it will preserve aspect ratio so if you wanted to convert all thumbnails to 50 pixels high you can specify [CODE]/usr/local/bin/convert <in filename> -resize 50x <out filename>[/CODE]
|
All right, imagemagick is working great for resizing large image (1200x1600 to 800x600 for example), however when I take a large image and try to make a thumbnail out of it, I again have a problem with it being grainy.
For example, look at this thumbnail it generated: http://www.car-wallpapers.net/wallpa...-thumbnail.gif But it generated other larger resized-images from the same source image with perfect quality. :dunno |
^hahahaahah, looks familiar
|
I really want to figure this out. I've been messing with it for a while.
I threw together a test page called sample.php: http://www.car-wallpapers.net/sample.php It outputs the thumbnail image for testing, made from this full-size: http://www.car-wallpapers.net/wallpa...024x768-19.jpg The source code of sample.php: [code]<? require("include/thumbnail.php"); resizeImage("wallpapers/", "65-1024x768-19.jpg", "sample.gif", "150", "113"); header("Content-type: image/gif"); readfile("wallpapers/sample.gif"); ?>[/code] Easy enough, right? Here's my resizeImage function: [code]function resizeImage($imagePath, $imageName, $destImageName, $max_x, $max_y){ exec("convert " . $imagePath . $imageName . " -thumbnail " . $max_x . "x" . $max_y . " -quality 100 " . $imagePath . $destImageName); }[/code] What am I missing? The thumbnails are still very grainy. |
Don't use thumbnail, it does some other things to make them smaller, it may be messing with you, try just using resize. If that works then add -strip. you should also be able to omit the quality statement and default to the picture quality.
[PHP]function resizeImage($imagePath, $imageName, $destImageName, $max_x, $max_y){ exec("convert " . $imagePath . $imageName . " -resize " . $max_x . "x" . $max_y . " " . $imagePath . $destImageName); }[/PHP] |
sample.php is now using your code there, and it's still grainy.
http://www.car-wallpapers.net/sample.php :dunno |
Also that thumbnail is over 14k.... shouldn't it be more like 3-4k?
Maybe it's retaining color profiles or header info it doesn't need. I've tried adding -strip, but -thumbnail should have done that automatically. Eitherway, the filesize doesn't change. Edit: (so it's not a triple post, lol) Figured it out by talking to JMS on aim. Thanks. :thumbup |
for those following this thread, the issue was a problem changing from jpg to gif and the conversion process messing up the colors, it is now using jpg and looks great, still larger than it needs to be but it's on the right track.
|
Okay, another question.
$pwd /home/carpaper/public_html/ From there, I run this command: [code]convert wallpapers/47-1280x1024-f430spider05.jpg -thumbnail 1024x768 wallpapers/test.jpg[/code] However the generated image: http://www.car-wallpapers.net/wallpapers/test.jpg Is 960x768, when the original image was 1280x768, so it would resize fine without stretching, as it appears to be trying to prevent (thus binding to my Y value, and resizing X to whatever it wants). :dunno I've looked over http://www.imagemagick.org/script/convert.php and don't see an obvious way to force dimensions and not constrain proportions. |
| All times are GMT -5. The time now is 04:42 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Copyright © 2005-2013 UnitedBimmer.com
Ad Management by RedTyger