GD RUINS image quality...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

GD RUINS image quality...

Post 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?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

uhhh use imagecreatefromtruecolor (instead of imagecreatefromjpeg) and imagecopyresampled (instead of imagecopyresized)
Post Reply