[英]adding watermark to png images with the help of php
我想用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);
}
?>
這是我用來給圖像加水印的內容。 在圖像創建者創建png圖像時,請確保$ Destination在結尾處包含“ .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;
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.