United Bimmer Community - BMW Forum

United Bimmer Community - BMW Forum (http://www.unitedbimmer.com/forums/)
-   Geek Chat (http://www.unitedbimmer.com/forums/geek-chat/)
-   -   PHP GD2 Geek Question (http://www.unitedbimmer.com/forums/geek-chat/6803-php-gd2-geek-question.html)

komodo 03-14-2006 05:47 PM

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]

jms 03-15-2006 07:44 AM

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.

komodo 03-15-2006 10:06 AM

So there's no way with GD2? I don't believe I have IM installed...

jms 03-15-2006 11:59 AM

update ports first then use ports to install should go smooth(i hope) you'll be much happier with im

jms 03-16-2006 01:49 PM

did you bite the bullet and install it yet?

komodo 03-16-2006 07:28 PM

Yep, installed it, but haven't had the chance to sit down and learn it yet.

jms 03-17-2006 07:57 AM

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]

komodo 03-19-2006 07:23 PM

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

nick_318is 03-19-2006 09:15 PM

^hahahaahah, looks familiar

komodo 03-19-2006 11:39 PM

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.

jms 03-19-2006 11:54 PM

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]

komodo 03-19-2006 11:55 PM

sample.php is now using your code there, and it's still grainy.

http://www.car-wallpapers.net/sample.php

:dunno

komodo 03-20-2006 12:01 AM

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

jms 03-20-2006 08:04 AM

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.

komodo 03-23-2006 04:38 PM

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


Search Engine Optimization by vBSEO 2.4.0 © 2005, Crawlability, Inc.