Wow - it's been a while...
I'm having issues with the quality of an image resized using the GD library.
I'm running a windows 2003 server, with PHP installed as an IIS component.
PHP 5+ is running GD 2+. For some reason, the quality of the images that I resize is extremely poor.
It was recommended to me that I use image magick instead, but after many hours I have not been able to figure out how to get it properly installed as part of PHP and how to get it to work in general.
Essentially a few questions:
- Anybody else have quality issues w/ resizing under GD?
- Do you know how I can set up an up-to-date version of imagemagick on my windows server, and use it to resize images through PHP?
Thanks so much!
function resize ( $location, $ext, $to_name, $maxHeight = false, $maxWidth = false ) {
// get source's size
$size = GetImageSize ( $location );
$sourceWidth = $size[0];
$sourceHeight = $size[1];
if ( $maxWidth == $sourceWidth || $maxHeight == $sourceHeight ) {
copy ( $location, $to_name );
} else {
if ( $sourceWidth > $sourceHeight ) {
$percentage = ( $maxWidth / $sourceWidth );
} else {
$percentage = ( $maxHeight / $sourceHeight );
}
//gets the new value and applies the percentage, then rounds the value
$imgWidth = round ( $sourceWidth * $percentage);
$imgHeight = round ( $sourceHeight * $percentage);
// width greater then allowed
if ( $imgWidth > $maxWidth ) {
// set new sizes
$ratio = $maxWidth / $imgWidth;
$imgWidth = $maxWidth;
$imgHeight = $imgHeight * $ratio;
}
// height greater then allowed
if ( $imgHeight > $maxWidth ) {
// set new sizes
$ratio = $maxHeight / $imgHeight;
$imgHeight = $maxHeight;
$imgWidth = $imgWidth * $ratio;
}
// create image
switch ( $ext ) {
case 'jpeg':
case 'jpg':
$img = @imagecreatefromjpeg ( $location );
break;
case 'png':
$img = @imagecreatefrompng ( $location );
break;
case 'gif':
$img = @imagecreatefromgif ( $location );
break;
}
if ( $img ) {
/*resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h*/
// resize image
$full_id = ImageCreatetruecolor ( $imgWidth , $imgHeight );
ImageCopyResized ( $full_id,
$img,
0,
0,
0,
0,
$imgWidth,
$imgHeight,
$sourceWidth,
$sourceHeight
);
//
ImageJPEG ( $full_id, $to_name, 100 );
return true;
} else {
die ( 'Resize failed' );
}
}
} Manage your book, movie and music collection online