Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Poor Color With Imagetruecoloropalette()

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I have a form which resizes images but I reworked it so that I can change the image type from the form, and add a transparency if the image has a white background. It works perfectly except that the images have poor color. Removing Imagetruecoloropalette() fixes the color but then it looses the transparency! The images have poor color no matter the image type chosen. Any ideas?

Simplified code is below:

Code:
if ($source = $_FILES["Image"]["tmp_name"]) {

$fp = fopen($source, "rb");
	while(!feof($fp)) {
	$data .= fread($fp, 1024);
	} fclose($fp);

$picBG = "255, 255, 255"; # RGB-value for background
$picFG = "104, 104, 104"; # RGB-value for color levels

$colsBG = explode(",", $picBG);
$colsFG = explode(",", $picFG);

// Begin Create new thumbnail image
$NewThumbWidth = trim($_POST["ThumbWidth"]);  # input new image width
$NewThumbHeight = number_format(($size[1]/$size[0])*$NewThumbWidth, 0, '.' ,'');
$pic = ImageCreateFromString($data);

	ob_start();
		$image = ImageCreateTrueColor($NewImageWidth,$NewImageHeight);
		ImageTruecolorToPalette($image, true, 256); 
		$imagebgcolor = ImageColorAllocate($image, trim($colsBG[0]), trim($colsBG[1]), trim($colsBG[2]));
		ImageFill($image, 0, 0, $imagebgcolor);
		ImageColorAllocate($image, trim($colsFG[0]), trim($colsFG[1]), trim($colsFG[2]));

		if ($NewImageWidth < $size[0]) {
			ImageCopyResampled($image, $pic, 0, 0, 0, 0, $NewImageWidth, $NewImageHeight, $size[0], $size[1]);
		} else {
			ImageCopyResampled($image, $pic, 0, 0, 0, 0, $size[0], $size[1], $size[0], $size[1]);
		}

		switch ($_POST["ImageType"]) {
			case 1:
				ImageColorTransparent($image, $imagebgcolor);
				ImageAlphaBlending($image, true);
				ImageGIF($image, "", 100);
			break;
			case 2:
				ImageJPEG($image, "", 100);
			break;
			case 3:
				ImagePNG($image, "", 100);
			break;
			default:
				ImageJPEG($image, "", 100);						 
		}
	$Image = ob_get_contents();
	ImageDestroy($image);
	ob_end_clean();
}

It uses ob_start() since that appeared the only way to get the data into the proper format for inserting into a database (yes, I know the pros and cons) but if anyone knows another way, I am open to suggestions.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top