Image thumbnail

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
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Image thumbnail

Post by jwalsh »

I've used the following code in simple variations in a number of other scripts, but I'm getting very strange results this time. It's always distorting the colors significantly, and it some cases making them very pixellated and mostly greyscale. Any ideas?

Code: Select all

// Check for the image's exisitance 
if (!file_exists($source)) { 
	echo 'File does not exist!'; 
} else { 
	$size = getimagesize($source); // Get the image dimensions and mime type 

	// DETERMINE WIDTH BY MODE
	switch ($_REQUEST['mode']) {
		case "gallery":
			$h = 200;
			$scale = $size[1] / $h;
			$w = $size[0] / $scale;
			break;
	} 
	
	$resize = imagecreatetruecolor($w, $h); // Create a blank image 
	
	/* Check quality option. If quality is greater than 100, return error */ 
	if ($quality > 100) { 
		echo 'The maximum quality is 100. <br>Quality changes only affect JPEG images.'; 
	} else {              
		header('Content-Type: '.$size['mime']); // Set the mime type for the image 
		switch ($size['mime']) { 
			case 'image/jpeg': 
			$im = imagecreatefromjpeg($source); 
			imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original JPEG 
			imagejpeg($resize, '', $quality); // Output the new JPEG 
			break; 
			case 'image/png': 
				$im = imagecreatefrompng($source); 
				imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original PNG 
				imagepng($resize, '', $quality); // Output the new PNG 
			break; 
		} 
	imagedestroy($im); 
	} 
}
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Seen that happen PNGs. If that's the case then you might try perserving the alpha blending.

Code: Select all

imagealphablending($dest, false);
imagesavealpha($dest, true);
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

It appears to be a server GD problem. The same code runs fine on another server... I guess I need to reinstall GD.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

Ok,

Server is working fine now, but I still have a problem. I'm totally stumped here, so here's a watered down version with the same problem.

Code: Select all

<?
// GET SOURCE FILENAME
$source = "images/NYC.jpg";

$size = getimagesize($source); // Get the image dimensions and mime type 

$h = 200;
$scale = $size[1] / $h;
$w = $size[0] / $scale;
	
$resize = imagecreatetruecolor($w, $h); // Create a blank image 
	           
header('Content-Type: '.$size['mime']); // Set the mime type for the image 

$im = imagecreatefromjpeg($source); 
imagecopyresampled($resize, $im, 0, 0, 0, 0, $w, $h, $size[0], $size[1]); // Resample the original JPEG 
imagejpeg($resize, '', $quality); // Output the new JPEG 

imagedestroy($im); 
imagedestroy($resize); 
?>
and here's the output.

http://www.designinginteractive.com/v2/thumbnail.php
Post Reply