To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > General Help

General Help Forum for General Help questions pertaining to PHP

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-05-2000, 09:28 PM   #1
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
ImageCopyResized() Quality Problems.

I am using ImageCopyResized() to resize images uploaded by users so all images are the same size. I have everything working just fine, however, the quality of the resized image is really poor. The resized images are really jagged.

You can see my script at:

http://joel.savvynet.com/~joel/resize.php?size=640&string=hello

go ahead and change the size value to see the results, (1024 is the original size)

I know how to change the jpeg compression settings on the image, but that doesn't seem to help this problem.

Does anyone have any suggestions on how to improve quality? Thanks.

Here is my source code for that script.


<?
Header("Content-type: image/jpeg");

// $size and $string are passed from the URL

$font = "/home/joel/public_html/ARIALBD.TTF"; // load the font
$im = imagecreatefromjpeg("MVC-069X.JPG"); // create the source image from jpeg file

if(ImageSX($im) > ImageSY($im)) // image is wider than it is tall
{
$width = $size;
$height = $width * (ImageSY($im) / ImageSX($im));
}
else // image is taller than it is wide, or both dimensions are the same
{
$height = $size;
$width = $height * (ImageSX($im) / ImageSY($im));
}

$im2=ImageCreate($width, $height); // create the destination image, with the correct dimensions

$white = ImageColorAllocate ($im2, 255, 255, 255); // allocate a white color
$black = ImageColorAllocate ($im2, 0, 0, 0); // allocate a black color

imagecopyresized($im2, $im, 0,0,0,0, $width, $height, ImageSX($im), ImageSY($im)); // do the resizing

$text_size = ImageTTFBBox(12, 0, $font, $string);

ImageTTFText ($im2, 12, 0, $width - 6 - $text_size[4], $height - 6 - $text_size[1], $white, $font,
$string);

Imagejpeg($im2,'', 75); // display the new, resized image

ImageDestroy($im);
ImageDestroy($im2);
?>
Anon is offline   Reply With Quote
Old 11-06-2000, 05:47 AM   #2
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: ImageCopyResized() Quality Problems.

Resizing always lowers the quality.

If you make the image bigger, the resizing routine has to "create" new pixels to fil up the empty space.
If you make an image smaller, pixels are dropped.

Make sure that when you resize, you maintain the size ratio (x to y)
(resize from 10x10 to 20x20 or from 10x20 to 20x40, not from 10x10 to 20x10)

Also, experiment with the amount of resizing. Making an image exactly half the size of the original gives good results, making it 67% will probably not give a good result.
Anon is offline   Reply With Quote
Old 11-06-2000, 11:14 AM   #3
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: ImageCopyResized() Quality Problems.

Most good graphics programs will resample an image when resizing. Photoshop and also ImageMagick will do this. So, when making an image smaller, of course pixels are dropped, but there is not a noticable loss of quality in the resulting image. ie.. smooth lines and edges still look smooth, not pixelated.

Apparently, it looks like PHP-GD does not do any resampling, but only drops pixels like you say.

Are there any tips or tricks to make GD work any better? As it stands, GD does not produce acceptable quality for a profesional site. IMHO

Joel


Vincent wrote:
-------------------------------
Resizing always lowers the quality.

If you make the image bigger, the resizing routine has to "create" new pixels to fil up the empty space.
If you make an image smaller, pixels are dropped.

Make sure that when you resize, you maintain the size ratio (x to y)
(resize from 10x10 to 20x20 or from 10x20 to 20x40, not from 10x10 to 20x10)

Also, experiment with the amount of resizing. Making an image exactly half the size of the original gives good results, making it 67% will probably not give a good result.
Anon is offline   Reply With Quote
Old 11-06-2000, 11:23 AM   #4
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: ImageCopyResized() Quality Problems.

If you want a "professional" quality, you'll have to use "professional" tools, not a _free_ gd library.

Anon is offline   Reply With Quote
Old 11-06-2000, 04:01 PM   #5
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: ImageCopyResized() Quality Problems.

Here is a script that will attempt to resize and resample an image. However, this script runs really slow, so you may need to modify your php.ini file so the script doesn't time out.

The quality still isn't the best, since GD is limited to only 256 colors. So, for other people out there with similar questions, I think you'd be better of to use ImageMagick for resizing images, especially jpeg images where you want to save some of the original quality.


<?
$size = 800;
$source_image = "MVC-674X.JPG";
$im = imagecreatefromjpeg($source_image);
if(ImageSX($im) > ImageSY($im))
{
$fraction = ImageSX($im) / $size;
}
else
{
$fraction = ImageSY($im) / $size;
}
$int = (int)$fraction + 1;
if($int < 3)
{
$int = 3;
}
$width = ImageSX($im) / $fraction;
$height = ImageSY($im) / $fraction;
$im2=ImageCreate($width, $height);
ImageCopyResized($im2, $im, 0, 0, 0, 0, $width, $height, ImageSX($im), ImageSY($im));
Imagejpeg($im2,'out1.jpg', 65);
for($x = 0; $x < $width; $x++)
{
for($y = 0; $y < $height; $y++)
{
$red = 0;
$green = 0;
$blue = 0;
for($f = 0; $f < $int; $f++)
{
for($f1 = 0; $f1 < $int; $f1++)
{
$color1_index = ImageColorAt($im, ($x * $fraction) + $f1, ($y * $fraction) + $f);
$color1 = ImageColorsForIndex($im, $color1_index);
$red += $color1["red"];
$green += $color1["green"];
$blue += $color1["blue"];
}
}
$red = $red / ($int * $int);
$green = $green / ($int * $int);
$blue = $blue / ($int * $int);
$new_color = ImageColorResolve($im2, $red, $green, $blue);
ImageSetPixel($im2, $x, $y, $new_color);
}
}
Imagejpeg($im2,'out2.jpg', 65);
ImageDestroy($im);
ImageDestroy($im2);
?>
Anon is offline   Reply With Quote
Old 11-09-2000, 07:47 PM   #6
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: ImageCopyResized() Quality Problems.

So, its ok that GD sucks because it's free, right? That says a lot about open source...

Anyway, ImageMagick is free and it rocks. PHP needs built-in support for ImageMagick, not crappy GD.
Anon is offline   Reply With Quote
Old 11-10-2000, 03:56 AM   #7
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: ImageCopyResized() Quality Problems.

First, GD doesn't suck, it rocks! I'm not complaining about the qualty at all!

My point is that you should not demand professional quality output if you don't support the development of a tool financially.

Anon is offline   Reply With Quote
Old 03-15-2001, 07:00 PM   #8
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: ImageCopyResized() Quality Problems.

Is there any way to use ImageMagick with php?

Rich
Anon is offline   Reply With Quote
Old 03-22-2001, 01:45 AM   #9
Anon
Senior Member
 
Join Date: Jun 2000
Posts: 65,386
RE: ImageCopyResized() Quality Problems.

You can call it as an external program using the exec() command. Read through the documentation for ImageMagick to learn what the command line options are. For simple stuff like uploading an image, resizing it and moveing it somewhere, it's not too bad.
Anon is offline   Reply With Quote
Reply

Bookmarks


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

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 Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 08:13 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.