Page 1 of 1
GD RUINS image quality...
Posted: Fri Dec 02, 2005 11:09 pm
by Todd_Z
Code: Select all
function resizeImage ( $img, $nw, $nh ) {
$x = imagesx( $img );
$y = imagesy( $img );
if ( $x > $nw ) {
$x = $nw;
$y = round( $nw/$x * $y );
} if ( $y > $nh ) {
$y = $nh;
$x = round( $nh/$y * $x );
}
$nimg = imagecreate( $x, $y );
imagecopyresized( $nimg, $img, 0, 0, 0, 0, $x, $y, imagesx($img), imagesy($img) );
return $nimg;
}
function createImage ( $file ) {
preg_match( "#/([a-zA-Z]{3,4})$#", $file['type'], $ext );
if ( !isset( $ext[1] ) ) return false;
$function = "imagecreatefrom".strtolower( $ext[1] );
if ( !function_exists( $function ) ) return false;
return $function( $file['tmp_name'] );
}
For example, when I resized an image 475*475 to 300*300, it ruined the quality. Turned it to black and white basically and the brightness down a lot.... Any ideas on how to fix this script?
Posted: Sat Dec 03, 2005 12:03 am
by AKA Panama Jack
If you can get your host to install
ImageMagick, if it's not already installed, then you will have access to some fantastic image processing tools through the SYSTEM command.
Code: Select all
$scale=$imagewidth."x".$imageheight."!";
system( "/usr/local/bin/mogrify -geometry $scale $newimagepath");
The $newimagepath would be the directory path to the image including the image name. IE: /home/www/website/uploadedimage/myimage.jpg
The quality is great for resized images. That's one way to avoid losing image quality as the built in image manipulation functions for PHP are not very good when resizing images.
Posted: Sat Dec 03, 2005 1:04 am
by josh
uhhh use imagecreatefromtruecolor (instead of imagecreatefromjpeg) and imagecopyresampled (instead of imagecopyresized)