Jump to content

imagecreatefrompng


wrathican

Recommended Posts

hey peeps. i have a function that creates thumbnail from a file stored on a server then deletes it.

 

the problem i have is that if a user uploads a PNG image my script does not generate the thumbnail, but deletes the temp file.

works with gif/jpeg but not PNG's :(

 

any ideas why this isnt working?

 

heres the script:

<?php
//$result being where the image is currently stored
//$imgDir being where the image is to be saved to
function createThumb($result, $picWidth, $imgDir) {
$poo = array();
if ($result == false) {
	//file could not be uploaded
	$poo[0] = false;
	$poo[1] = 1;
	return $poo;
}else{
	//file was uploaded. create thumbnail
	//pathinfo
	$file = pathinfo($result);
	//find the .ext
	$ext = $file['extension'];
	//strip .ext to lower
	$ext = strtolower($ext);
	//get image sizes
	$sizes = getimagesize($result);
	//ratio
	$ratio = $sizes[0]/$sizes[1];
	//create new height
	$newHeight = round($picWidth/$ratio);
	//create true colour image		
	$srcImg = imagecreatetruecolor($picWidth, $newHeight);
	if($ext == 'jpg' || $ext == 'jpeg') {
		$tmpImg = imagecreatefromjpeg($result);
	}elseif($ext == 'png') {
		$tmpImg = imagecreatefrompng($result);
	}elseif($ext == 'gif') {
		$tmpImg = imagecreatefromgif($result);
	}else{
		$poo[0] = false;
		$poo[1] = 2;
		return $poo;
	}
	//create destination image filename
	//get filename
	$name = $file['basename'];
	//dest filename
	$dstFile = $imgDir . $name;
	imagecopyresampled($srcImg, $tmpImg, 0, 0, 0, 0, $picWidth, $newHeight, $sizes[0], $sizes[1]);
	//save image
	if($ext == 'jpg' || $ext == 'jpeg') {
		imagejpeg($srcImg, $dstFile, 100);
	}elseif($ext == 'png') {
		imagepng($scrImg, $dstFile, 100);
	}elseif($ext == 'gif') {
		imagegif($srcImg, $dstFile, 100);
	}else{
		$poo[0] = false;
		$poo[1] = 3;
		return $poo;;
	}

	$killSrc = imagedestroy($srcImg);
	$killTmp = imagedestroy($tmpImg);
	if($killSrc != false && $killTmp != false){
		$killOld = unlink($result);
		if($killOld != false){
			return $name;
		}else{
			$poo[0] = false;
			$poo[1] = 4;
			return $poo;
		}
	}else{
		$poo[0] = false;
		$poo[1] = 5;
		return $poo;
	}
}
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/106230-imagecreatefrompng/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.