[英]Dynamic Watermark in PHP
我正在使用此腳本在客戶端的某個網站上創建水印,問題是一個水印適合一個圖像而不適合另一個圖像。
肖像
景觀
這是腳本:
<?php
// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory asthis script
// where this script is named watermark.php
// call this script with an image tag
// <img src="watermark.php?path=imagepath"> where path is a relative path such as subdirectory/image.jpg
$imagesource = $_GET['path'];
$filetype = substr($imagesource,strlen($imagesource)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
if (!$image) die();
//This bit is the dynamic bit
if(imagesx($image) <=1100){
$watermark = @imagecreatefromgif('watermark_port.gif');
}elseif(imagesx($image) <=1600 && $imagewidth >1100){
$watermark = @imagecreatefromgif('watermark_lans.gif');
}elseif(imagesx($image) >1600){
$watermark = @imagecreatefromgif('watermark_lans.gif');
};
//End of dynamic code
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth,
$watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
但是動態水印不起作用。 我想要的是如果圖像寬度低於1100px,那么它使用肖像版本,如果它已經結束,則使用橫向版本。
任何想法都會受到極大的關注。
謝謝,
大衛
你的“動態部分”所做的基本上歸結為:
if something
do A
else if something
do B
else
do B
中間的是完全多余的。
你需要的只是:
$watermark =
imagecreatefromgif("watermark_".(imagesx($image) <= 1100 ? "port" : "lans").".gif");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.