Jump to content

Recommended Posts

I have this script to resize an uploaded image. It all works fine as long as I upload a jpeg. If the image is png or gif, a 100% black image is created. The black image is resized to the correct dimensions, saved to the correct folder with the correct filename and extension - it's just that it's black. setting error_reporting to E_ALL reveals nothing.

 

the relevant code:

 

<?php

function imgCreate($ext,$tmp) {
switch ($ext) {
	case 'jpg' || 'jpeg': $img=imagecreatefromjpeg($tmp);
	break;
	case 'png': $img=imagecreatefrompng($tmp);
	break;
	case 'gif': $img=imagecreatefromgif($tmp);
	break;
}
return $img;
}

function saveImg($ext,$tmpimg,$tn_path) {
switch($ext) {
	case 'jpg' || 'jpeg': imagejpeg($tmpimg,$tn_path,JPEG_QUALITY);
	break;
	case 'png': imagepng($tmpimg,$tn_path);
	break;
	case 'gif': imagegif($tmpimg,$tn_path);
	break;
}
}

function resizeImage($tmp,$filename,$ext,$width,$height) {
$img=imgCreate($ext,$tmp);
$path='../'.IMAGE_DIR.$filename;
if ($width > $height) {
	$newwidth=MAX_IMAGE_SIZE;
	$newheight=$newwidth*($height/$width);
} else if ($height > $width) {
	$newheight=MAX_IMAGE_SIZE;
	$newwidth=$newheight*($width/$height);
} else if ($height==$width) {
	$newheight=$newwidth=MAX_IMAGE_SIZE;
}
$tmpimg=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmpimg,$img,0,0,0,0,$newwidth,$newheight,$width,$height);
saveImg($ext,$tmpimg,$path);
}

?>

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.