我完全不喜欢这个...我需要一个照片上传脚本来保持png文件的透明度。目前透明区域是黑色的。任何帮助是极大的赞赏。这是我的代码:
if(isset($_POST)) {
require_once('siteInfo.php');
session_start();
$ThumbSquareSize = 90;
$BigImageMaxSize = $_POST['size'];
$ThumbPrefix = "thumb_";
$DestinationDirectory = '../Uploads/';
$Quality = 100;
if (!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name'])) {
die('Please select a file.');
}
$RandomNumber = rand(0, 9999999999);
$ImageName = str_replace(' ','-',strtolower($_FILES['ImageFile']['name']));
$ImageSize = $_FILES['ImageFile']['size'];
$TempSrc = $_FILES['ImageFile']['tmp_name'];
$ImageType = $_FILES['ImageFile']['type'];
switch (strtolower($ImageType)) {
case 'image/png':
$CreatedImage = imagecreatefrompng($_FILES['ImageFile']['tmp_name']);
break;
case 'image/gif':
$CreatedImage = imagecreatefromgif($_FILES['ImageFile']['tmp_name']);
break;
case 'image/jpeg':
case 'image/pjpeg':
$CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']);
break;
default:
// die('Unsupported file format. Please upload a PNG, GIF, or JPG file.'); //output error and exit
}
list($CurWidth,$CurHeight)=getimagesize($TempSrc);
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNumber.'.'.$ImageExt;
$thumb_DestRandImageName = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumb name
$DestRandImageName = $DestinationDirectory.$NewImageName; //Name for Big Image
if (resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType)) {
if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType)) {
echo 'Error Creating thumbnail';
}
$query = "Update Content set ".$_POST['field']."='".$NewImageName."' WHERE ID=".$_POST['ID'];
mysqli_query($dbc, $query);
mysqli_close($dbc);
echo 'Uploaded file: <span>'.$NewImageName.'</span>.';
} else {
die('Resize Error');
}
}
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType) {
if ($CurWidth <= 0 || $CurHeight <= 0) {
return false;
}
$ImageScale = min($MaxSize/$CurWidth, $MaxSize/$CurHeight);
$NewWidth = ceil($ImageScale*$CurWidth);
$NewHeight = ceil($ImageScale*$CurHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
if (imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight)) {
switch(strtolower($ImageType)) {
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
if (is_resource($NewCanves)) { imagedestroy($NewCanves); }
return true;
}
}
function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType){
if ($CurWidth <= 0 || $CurHeight <= 0) {
return false;
}
if ($CurWidth>$CurHeight) {
$y_offset = 0;
$x_offset = ($CurWidth - $CurHeight) / 2;
$square_size = $CurWidth - ($x_offset * 2);
} else {
$x_offset = 0;
$y_offset = ($CurHeight - $CurWidth) / 2;
$square_size = $CurHeight - ($y_offset * 2);
}
$NewCanves = imagecreatetruecolor($iSize, $iSize);
if (imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size)) {
switch(strtolower($ImageType)) {
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
if (is_resource($NewCanves)) { imagedestroy($NewCanves); }
return true;
}
}
答案 0 :(得分:3)
我将以下内容添加到“resizeImage”函数中:
imagealphablending($NewCanves, false);
imagesavealpha($NewCanves, true);
imagealphablending($SrcImage, true);
这解决了这个问题。