Go Back   United Bimmer Community - BMW Forum > UnitedBimmer- Off Topic > United Off Topic  **FOR MEMBERS ONLY** > Geek Chat
FAQ Members List Calendar Advertise With Us Mark Forums Read

Geek Chat Ask computer questions or just engage in general geek talk on here

Reply
 
Thread Tools
Old 03-14-2006, 05:47 PM   #1
komodo
 
komodo's Avatar

Name: komodo
Title: Administrator
Status: Offline
Join Date: Apr 2005
Location: Athens, GA
Rate My Car: 68 / 340
Your Ride: 1995 M3
Question 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:



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]
__________________

  Reply With Quote
Old 03-15-2006, 07:44 AM   #2
jms
 
jms's Avatar

Name: jms
Title: ______
Status: Offline
Join Date: Jul 2005
Location: Pittsburgh,PA
Rate My Car: 105 / 340
Your Ride: 99 328I Convertible
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.
  Reply With Quote
Old 03-15-2006, 10:06 AM   #3
komodo
 
komodo's Avatar

Name: komodo
Title: Administrator
Status: Offline
Join Date: Apr 2005
Location: Athens, GA
Rate My Car: 68 / 340
Your Ride: 1995 M3
So there's no way with GD2? I don't believe I have IM installed...
__________________

  Reply With Quote
Old 03-15-2006, 11:59 AM   #4
jms
 
jms's Avatar

Name: jms
Title: ______
Status: Offline
Join Date: Jul 2005
Location: Pittsburgh,PA
Rate My Car: 105 / 340
Your Ride: 99 328I Convertible
update ports first then use ports to install should go smooth(i hope) you'll be much happier with im
  Reply With Quote
Old 03-16-2006, 01:49 PM   #5
jms
 
jms's Avatar

Name: jms
Title: ______
Status: Offline
Join Date: Jul 2005
Location: Pittsburgh,PA
Rate My Car: 105 / 340
Your Ride: 99 328I Convertible
did you bite the bullet and install it yet?
  Reply With Quote
Old 03-16-2006, 07:28 PM   #6
komodo
 
komodo's Avatar

Name: komodo
Title: Administrator
Status: Offline
Join Date: Apr 2005
Location: Athens, GA
Rate My Car: 68 / 340
Your Ride: 1995 M3
Yep, installed it, but haven't had the chance to sit down and learn it yet.
__________________

  Reply With Quote
Old 03-17-2006, 07:57 AM   #7
jms
 
jms's Avatar

Name: jms
Title: ______
Status: Offline
Join Date: Jul 2005
Location: Pittsburgh,PA
Rate My Car: 105 / 340
Your Ride: 99 328I Convertible
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]
  Reply With Quote
Old 03-19-2006, 07:23 PM   #8
komodo
 
komodo's Avatar

Name: komodo
Title: Administrator
Status: Offline
Join Date: Apr 2005
Location: Athens, GA
Rate My Car: 68 / 340
Your Ride: 1995 M3
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:

But it generated other larger resized-images from the same source image with perfect quality.

__________________

  Reply With Quote
Old 03-19-2006, 09:15 PM   #9
nick_318is
 
nick_318is's Avatar

Name: nick_318is
Title: Suspended License
Status: Offline
Join Date: Jul 2005
Location: Detroit, MI, I Heart E30s
Rate My Car: 10 / 340
Your Ride: 1991 318is (e30 m42), 2007 Mazdaspeed 3
^hahahaahah, looks familiar
  Reply With Quote
Old 03-19-2006, 11:39 PM   #10
komodo
 
komodo's Avatar

Name: komodo
Title: Administrator
Status: Offline
Join Date: Apr 2005
Location: Athens, GA
Rate My Car: 68 / 340
Your Ride: 1995 M3
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.
__________________

  Reply With Quote
Old 03-19-2006, 11:54 PM   #11
jms
 
jms's Avatar

Name: jms
Title: ______
Status: Offline
Join Date: Jul 2005
Location: Pittsburgh,PA
Rate My Car: 105 / 340
Your Ride: 99 328I Convertible
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]
  Reply With Quote
Old 03-19-2006, 11:55 PM   #12
komodo
 
komodo's Avatar

Name: komodo
Title: Administrator
Status: Offline
Join Date: Apr 2005
Location: Athens, GA
Rate My Car: 68 / 340
Your Ride: 1995 M3
sample.php is now using your code there, and it's still grainy.

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

__________________

  Reply With Quote
Old 03-20-2006, 12:01 AM   #13
komodo
 
komodo's Avatar

Name: komodo
Title: Administrator
Status: Offline
Join Date: Apr 2005
Location: Athens, GA
Rate My Car: 68 / 340
Your Ride: 1995 M3
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.
__________________


Last edited by komodo; 03-20-2006 at 12:20 AM..
  Reply With Quote
Old 03-20-2006, 08:04 AM   #14
jms
 
jms's Avatar

Name: jms
Title: ______
Status: Offline
Join Date: Jul 2005
Location: Pittsburgh,PA
Rate My Car: 105 / 340
Your Ride: 99 328I Convertible
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.
  Reply With Quote
Old 03-23-2006, 04:38 PM   #15
komodo
 
komodo's Avatar

Name: komodo
Title: Administrator
Status: Offline
Join Date: Apr 2005
Location: Athens, GA
Rate My Car: 68 / 340
Your Ride: 1995 M3
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).

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.
__________________


Last edited by komodo; 03-23-2006 at 04:42 PM..
  Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airplane Flight Question komodo United Off Topic  **FOR MEMBERS ONLY** 105 07-27-2007 09:35 AM
PHP Programmers komodo Geek Chat 6 02-16-2006 05:14 PM


All times are GMT -5. The time now is 04:01 PM.

A vBSkinworks Design

 
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Copyright © 2005-2013 UnitedBimmer.com