Page 1 of 1

Messed up images created with imagecreatefromgif

Posted: Mon Oct 23, 2006 1:22 pm
by woolyninja
When using imagecreatefromgif on a group of images sometimes it will output a messed up image (seems like part of image was read but then stopped halfway through). Is this a normal problem?

Here's an example of what happens... the top image is the complete image, the bottom image is how it sometimes can appear using imagecreatefromgif:
Image

Here's my code in case it helps:

Code: Select all

<?php
ob_start();

if (!defined('IMG_MAXSIZE')) define('IMG_MAXSIZE', 99999999);

$imageCreators = array(
	1  => "imagecreatefromgif",
	2  => "imagecreatefromjpeg",
	3  => "imagecreatefrompng",
	16 => "imagecreatefromxbm"
);
$imageShows = array(
	1  => "imagegif",
	2  => "imagejpeg",
	3  => "imagepng",
	16 => "imagexbm"
);
$contentTypes = array(
	1  => "image/gif",
	2  => "image/jpeg",
	3  => "image/png",
	16 => "image/xbm"
);

function imageshow($im, $imageSize) {
	global $imageShows;
	
	$filetype = $imageSize[2];
	if (isset($imageShows[$filetype])) {
		$imageShow = $imageShows[$filetype];
		if (function_exists($imageShow)) {
				$imageShow($im);
		}
	}
}

function imagecreatefromfile($filename, $imageSize) {
	global $imageCreators;

	$imCreated = "";	
	$filetype = $imageSize[2];

	if (isset($imageCreators[$filetype])) {
		$imageCreator = $imageCreators[$filetype];
		if (function_exists($imageCreator)) {
			$imCreated = $imageCreator($filename);
			if (!$imCreated) {
				$imCreated = imagecreatetruecolor(100, 100); /* Create a blank image */
				$bgc = imagecolorallocate($imCreated, 255, 255, 255);
				imagefilledrectangle($imCreated, 0, 0, 100, 100, $bgc);
				$tc = imagecolorallocate($imCreated, 0, 0, 0);
	
				/* Output an errmsg */
				imageStringWrap($imCreated, 5, "Error loading file", $tc);
			}
		}
	}

//	if ($fp = fopen($filename, 'rb')) {
//		$data = fread($fp, (filesize($filename)?filesize($filename):IMG_MAXSIZE));
//		$imCreated = imagecreatefromstring($data);
//	}
	
	return $imCreated;
}

function imageStringWrap($image, $font, $text, $color) {
	$fontwidth = ImageFontWidth($font);
	$fontheight = ImageFontHeight($font);
	$words= str_word_count($text);
	if ($words > 1){
		$string=array(strtok($text,' '));
		for ($i = 1 ; $i <= $words ; $i++){
			$string=array_merge($string,array($i=>strtok(' ')));
		}
	} else {
		$string=$text;
	}
	$vspace=4;
	$y=((imagesy($image)-($fontheight*$words)-($words*$vspace))/2);
	foreach($string as $st){
		$x=((imagesx($image)-($fontwidth * strlen($st)))/2);
		ImageString($image,$font,$x,$y,$st,$color);
		$y+=($fontheight+$vspace);
	}
}

$headerContentType = "text/html";

$imgPath = $_REQUEST['img'];
$imgMissing = $_REQUEST['noimg'];
$imgWidth = $_REQUEST['w'];
$imgHeight = $_REQUEST['h'];
$imgBGColor = $_REQUEST['bgcolor'];

$imgPathReal = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . $imgPath;
$imgMissingReal = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . $imgMissing;
//$imgPathReal = 'http://' . $_SERVER['SERVER_NAME'] . '/' . $imgPath;
//$imgMissingReal = 'http://' . $_SERVER['SERVER_NAME'] . '/' . $imgMissing;

$im = false;
if (is_numeric($imgWidth) && is_numeric($imgHeight)) {
	$im = imagecreatetruecolor(intval($imgWidth), intval($imgHeight));
	if (trim($imgBGColor) != "") {
		sscanf($imgBGColor, "%2x%2x%2x", $red, $green, $blue);
		$color = imagecolorallocate($im, $red, $green, $blue);
		imagefill($im, 0, 0, $color);
	}
}
if (trim($imgPath) != '' && file_exists($imgPathReal)) {
	$imageSize = getimagesize($imgPathReal);
	if (is_array($imageSize)) {
		$headerContentType = $contentTypes[$imageSize[2]];

		if ($im === false) {
			$im = imagecreatefromfile($imgPathReal, $imageSize);
		} else {
 			$imsrc = imagecreatefromfile($imgPathReal, $imageSize);

			$newWidth = intval($imgWidth);
			$newHeight = intval($imgHeight);

			$widthRatio = $newWidth/$imageSize[0];
			$heightRatio = $newHeight/$imageSize[1];			
 			if ($imageSize[0] > $newWidth && $imageSize[1] > $newHeight) {
				if ($widthRatio < $heightRatio) {
					$newHeight = $imageSize[1] * $widthRatio;
				} else {
					$newWidth = $imageSize[0] * $heightRatio;
				}
			} else if ($imageSize[0] > $newWidth && $imageSize[1] <= $newHeight) {
				$newHeight = $imageSize[1] * $widthRatio;
			} else if ($imageSize[0] <= $newWidth && $imageSize[1] > $newHeight) {
				$newWidth = $imageSize[0] * $heightRatio;
			} else {
				$newWidth = $imageSize[0];
				$newHeight = $imageSize[1];
			}
			imagecopyresampled($im, $imsrc, ($imgWidth/2) - ($newWidth/2), ($imgHeight/2) - ($newHeight/2), 0, 0, $newWidth, $newHeight, $imageSize[0], $imageSize[1]);
			imagedestroy($imsrc);
		}

//imagestring($im, 1, 5, 5, $contentTypes[$imageSize[2]], imagecolorallocate($im, 0, 0, 0));

		imageshow($im, $imageSize);
//		imagegif($im, 'f:/thumbs/' . substr($imgPathReal, strrpos($imgPathReal, '/')));
	}
} else if (trim($imgMissing) != '' && file_exists($imgMissingReal)) {
	$imageSize = getimagesize($imgMissingReal);
	if (is_array($imageSize)) {
		$headerContentType = $contentTypes[$imageSize[2]];

		if ($im === false) {
			$im = imagecreatefromfile($imgMissingReal, $imageSize);
		} else {
 			$imsrc = imagecreatefromfile($imgMissingReal, $imageSize);

			$newWidth = intval($imgWidth);
			$newHeight = intval($imgHeight);

			$widthRatio = $newWidth/$imageSize[0];
			$heightRatio = $newHeight/$imageSize[1];			
 			if ($imageSize[0] > $newWidth && $imageSize[1] > $newHeight) {
				if ($widthRatio < $heightRatio) {
					$newHeight = $imageSize[1] * $widthRatio;
				} else {
					$newWidth = $imageSize[0] * $heightRatio;
				}
			} else if ($imageSize[0] > $newWidth && $imageSize[1] <= $newHeight) {
				$newHeight = $imageSize[1] * $widthRatio;
			} else if ($imageSize[0] <= $newWidth && $imageSize[1] > $newHeight) {
				$newWidth = $imageSize[0] * $heightRatio;
			} else {
				$newWidth = $imageSize[0];
				$newHeight = $imageSize[1];
			}
			
			imagecopyresampled($im, $imsrc, ($imgWidth/2) - ($newWidth/2), ($imgHeight/2) - ($newHeight/2), 0, 0, $newWidth, $newHeight, $imageSize[0], $imageSize[1]);
			imagedestroy($imsrc);
		}
		imageshow($im, $imageSize);
	}
} else {
	if ($im === false) {
		$im = imagecreatetruecolor(100, 100); /* Create a blank image */
		$bgc = imagecolorallocate ($im, 255, 255, 255);
		imagefilledrectangle ($im, 0, 0, 100, 100, $bgc);
	}
	$tc = imagecolorallocate ($im, 0, 0, 0);
	
	/* Output an errmsg */
	imageStringWrap($im, 5, "No Photo Available", $tc);

	$headerContentType = "image/jpeg";
	
	imagejpeg($im);
}

header("Content-Length: " . ob_get_length());
header("Content-Type: " . $headerContentType);
ob_end_flush();

if ($im !== false) imagedestroy($im);

exit();
?>