我想用png图像文件为png图像添加水印。以下代码适用于jpeg文件,但png文件。我试图在没有运气的情况下稍微改变一下代码。
i.e. $image=imagecreatefromjpeg($original_image); into $image=imagecreatefrompng($original_image);
你能告诉我问题所在吗?
这是我的php代码:
<?php
function watermark($original_image,$original_watermark,$destination="")
{
/*
create the image from out original image
*/
$image=imagecreatefromjpeg($original_image);
/*
get the image size for watermark resize if neccessary
*/
list($imagewidth,$imageheight)=getimagesize($original_image);
/*
create the watermark
*/
$watermark = imagecreatefrompng($original_watermark);
/*
determine the watermark width and height
*/
list($watermarkwidth,$watermarkheight)=getimagesize($original_watermark);
/*
if the watermark is bigger than the original image, we simply resize it
*/
if($watermarkwidth>$imagewidth || $watermarkheight>$imageheight)
{
/*
some simple resize math
*/
$water_resize_factor = $imagewidth / $watermarkwidth;
$new_watermarkwidth = $watermarkwidth * $water_resize_factor;
$new_watermarkheight = $watermarkheight * $water_resize_factor;
/*
the new watermark creation takes place starting from here
*/
$new_watermark = imagecreatetruecolor($new_watermarkwidth , $new_watermarkheight);
/*
imagealphablending is important in order to keep
our png image (the watewrmark) transparent
*/
imagealphablending($new_watermark , false);
imagecopyresampled($new_watermark , $watermark, 0, 0, 0, 0,
$new_watermarkwidth,$new_watermarkheight,
$watermarkwidth, $watermarkheight);
/*
assign the new values to the old variables
*/
$watermarkwidth = $new_watermarkwidth;
$watermarkheight = $new_watermarkheight;
$watermark = $new_watermark;
}
/*
we establish the position of the watermark over the image
*/
$startwidth = ($imagewidth - $watermarkwidth) / 2;
$startheight = ($imageheight - $watermarkheight) / 2;
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0,
$watermarkwidth, $watermarkheight);
/*
if we have a destination image, we save it on the server...
*/
if(!empty($destination))
imagejpeg($image,$destination);
/*
... else we output the image
*/
else
imagejpeg($image);
}
?>
答案 0 :(得分:0)
这是我用来为我的图像添加水印的内容。确保$ Destination在结尾处包含“.png”,因为图像创建者会创建一个png图像。
示例:
$Source = "http://www.personal.psu.edu/jyy5075/plant4.jpg"
$Destination = "/images/watermarked1.png"
$WatermarkText = "Hurray!"
<?php
function CreateWatermark($Source, $Destination, $WatermarkText)
{
// Load the stamp and the photo to apply the watermark to
$im = CreateImage($Source);
if ($im != NULL)
{
// First we create our stamp image manually from GD
$stamp = imagecreatetruecolor(100, 30);
imagefilledrectangle($stamp, 0, 0, 100, 50, 0x000000);
imagestring($stamp, 4, 5, 5, $WatermarkText, 0xFFFFFF);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 100;
$marge_bottom = 100;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Merge the stamp onto our photo with an opacity of 50%
imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);
// Save the image to file and free memory
imagepng($im, $Destination);
imagedestroy($im);
}
}
function CreateImage($Source)
{
$types= array('jpg', 'jpeg', 'png', 'gif'); //store all the image extension types in array
$im = NULL;
$ext = pathinfo($URL, PATHINFO_EXTENSION);
if(in_array($ext,$types)) //check image extension not in the array $type
{
switch ($ext)
{
case ('jpg'):
{
$im = imagecreatefromjpeg($URL);
break;
}
case ('jpeg'):
{
$im = imagecreatefromjpeg($URL);
break;
}
case ('png'):
{
$im = imagecreatefrompng($URL);
break;
}
case ('gif'):
{
$im = imagecreatefromgif($URL);
break;
}
default:
{
$im = NULL;
break;
}
}
}
return $im;
}
?>