rtimmons wrote in php 😡frustrated

it seems like I always ask the same questions

...but... I've been using this code for a while now. A while ago, I brought up the question on why it was giving me washed-out images. That was solved by changing instances of ImageCreate() to ImageCreateTrueColor(). The problem is, that the "transparent" color is not setting correctly. I have it set to 0,0,127 and have that color be its transparency, but I always get wide black margins above and below the resized image (because the script generates a square output even though it's resizing a rectangular image).

Any help would be great. The black margins didn't start appearing until I swapped in the whole "TrueColor" part, if that helps..



$image is query-string: the input image.
$tnsize is query-string: the desired output size.


<?php
// check if the 'image' parameter has been given
if (!isset($image)){ // if not generate an error image
	$img = ImageCreateTrueColor(100, 100);
	$red = ImageColorAllocate($img, 255, 0, 0);
	$yellow = ImageColorAllocate($img, 255, 255, 0);
	ImageString($img, 5, 20, 20, "No image", $yellow);
	ImageString($img, 5, 20, 40, "name", $yellow);
	ImageString($img, 5, 20, 60, "given!", $yellow);
	ImagePNG($img);
	ImageDestroy($img);
	exit();
}

// check if the 'tnsize' parameter has been given
if (!isset($tnsize)) { // if not generate an error image
	$img = ImageCreateTrueColor(100, 100);
	$red = ImageColorAllocate($img, 255, 0, 0);
	$yellow = ImageColorAllocate($img, 255, 255, 0);
	ImageString($img, 5, 20, 20, "No thumb", $yellow);
	ImageString($img, 5, 20, 40, "size", $yellow);
	ImageString($img, 5, 20, 60, "given!", $yellow);
	ImagePNG($img);
	ImageDestroy($img);
	exit();
}

// ensure that $tnsize is an integer 
$tnsize = (integer) $tnsize;
// check that the size asked for is sensible
if (($tnsize<20) or ($tnsize>300)){ // if not generate an error image
	$img = ImageCreateTrueColor(100, 100);
	$red = ImageColorAllocate($img, 255, 0, 0);
	$yellow = ImageColorAllocate($img, 255, 255, 0);
	ImageString($img, 5, 20, 20, "Bad", $yellow);
	ImageString($img, 5, 20, 40, "size", $yellow);
	ImageString($img, 5, 20, 60, "given!", $yellow);
	ImagePNG($img);
	ImageDestroy($img);
	exit();
}

// attempt to load the big image
if (!$bigimage = @ImageCreateFromJPEG($image)) {	// if loading fails
	$img = ImageCreateTrueColor(100, 100);					// generate an error
	$red = ImageColorAllocate($img, 255, 0, 0);		// image
	$yellow = ImageColorAllocate($img, 255, 255, 0);
	ImageString($img, 5, 20, 20, "Image", $yellow);
	ImageString($img, 5, 20, 40, "not", $yellow);
	ImageString($img, 5, 20, 60, "found!", $yellow);
	ImagePNG($img);
	ImageDestroy($img);
	exit();
}

// all seems to be OK so start the processing



// create the new image using the tnsize parameter
$tnimage = ImageCreateTrueColor($tnsize, $tnsize);
// allocate the background colour for the thumbnail
$darkblue = ImageColorAllocate($tnimage, 0, 0, 127);
// set the background as transparent
ImageColorTransparent($tnimage,$darkblue);
// get the parameters of the big image
$sz = GetImageSize($image);
// load our internal variables
$x = $sz[0];	// big image width
$y = $sz[1];	// big image height
// find the larger dimension
if ($x>$y) {	// if it is the width then
	$dx = 0;					// the left side of the new image
	$w = $tnsize;				// the width of the new image
	$h = ($y / $x) * $tnsize;	// the height of the new image
	$dy = ($tnsize - $h) / 2;	// the top of the new image
}else{	// if the height is larger then
	$dy = 0;					// the top of the new image
	$h = $tnsize;				// the height of the new image
	$w = ($x / $y) * $tnsize;	// the width of the new image
	$dx = ($tnsize - $w) / 2;	// the left edgeof the new image
}

// copy the resized version into the thumbnal image
ImageCopyResized($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h, $x, $y);
// generate the PNG image
ImagePNG($tnimage);
// release the memory used by the thumbnail image
ImageDestroy($tnimage);
// release the memory used by the original big image
ImageDestroy($bigimage);
// all done!
?>