此脚本上传图像,为其添加水印,并创建图像的缩略图。我编辑它以忽略GIF文件的水印,因为GD库在加水印时会破坏动画。 它可以很好地为相应的图像类型添加水印,但是当上传GIF时,即使它没有加水印也不会保持动画。以下是完整的代码:
<?php
if(isset($_POST))
{
$Big = 980
$Small = 400;
$ThumbPrefix = "thumb_";
$DestinationDirectory = '../folder/';
$Quality = 100;
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
die();
}
if(!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name']))
{
die('Error!');
}
$Number = substr(str_shuffle(MD5(microtime())), 0, 10);
$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!');
}
list($CurWidth,$CurHeight)=getimagesize($TempSrc);
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName);
$NewName = $Number.'-'.$ImageName.'.'.$ImageExt;
$url = 'http://'.@$_SERVER['HTTP_HOST'].strtr(dirname(dirname($_SERVER['SCRIPT_NAME'])), '\\', '/').'';
$thumb_ImageName = $DestinationDirectory.$ThumbPrefix.$NewName;
$ImageName = $DestinationDirectory.$NewName;
if(resizeImage2($CurWidth,$CurHeight,$Small,$thumb_ImageName,$CreatedImage,$Quality,$ImageType))
{
if(!resizeImage($CurWidth,$CurHeight,$MainImageSize,$ImageName,$CreatedImage,$Quality,$ImageType))
{
echo 'Error';
}
$variable = <<<IMG
<p>Image Preview</p></br>
<div class="up">
<img src="$url/folder/$NewName">
<label onclick="document.getElementById('content').value = '$url/folder/$ThumbPrefix$NewName';document.getElementById('extra').value = '$url/folder/$NewName';document.getElementById('imgprev').src = '$url/folder/$ThumbPrefix$NewName';" class="img-submit" for="upload"></label>
</div>
IMG;
echo $variable;
}else{
die('Error');
}
}
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
//Check Image size is not 0
if($CurWidth <= 0 || $CurHeight <= 0)
{
return false;
}
$NewCanves = imagecreatetruecolor($CurWidth, $CurHeight);
$watermark = 'watermark/watermark.png';
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $CurWidth, $CurHeight, $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 (($ImageType =="image/pjpeg" OR $ImageType =="image/jpeg" OR $ImageType =="image/jpg"))
{
$watermark = imagecreatefrompng($watermark);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$watermark_x = ($CurWidth-$watermark_width);
$watermark_y = ($CurHeight-$watermark_height)/2;
imagecopy($NewCanves, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_width, $watermark_height);
}
imagejpeg($NewCanves,$DestFolder,$Quality);
if(is_resource($NewCanves)) {imagedestroy($NewCanves);}
return true;
}
}
function resizeImage2($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
if($CurWidth <= 0 || $CurHeight <= 0)
{
return false;
}
//Construct a proportional size of new image
$ImageScale = min($MaxSize/$CurWidth, $MaxSize/$CurWidth);
$Width = ceil($ImageScale*$CurWidth);
$Height = ceil($ImageScale*$CurHeight);
$NewCanves = imagecreatetruecolor($Width, $Height);
$watermark = 'watermark/watermark.png';
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $Width, $Height, $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;
}
imagejpeg($NewCanves,$DestFolder,$Quality);
if(is_resource($NewCanves)) {imagedestroy($NewCanves);}
return true;
}
}
我注意到,如果我使用:
if(!move_uploaded_file($_FILES['ImageFile']['tmp_name'], $DestinationDirectory.$NewImageName ))
而不是:
if(!resizeImage($CurWidth,$CurHeight,$MainImageSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType))
然后 GIF将保持动画,但之后它不再对任何东西进行水印。我非常困惑。我对PHP的了解非常有限。有没有人有任何想法?感谢。
答案 0 :(得分:0)
如果你想让gif图像动画,你不应该将GIF图像转换为jpg或png 类型