Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.