2011-11-08 37 views
0

我正在使用此腳本在客戶端的某個網站上創建水印,問題是一個水印適合一個圖像,並且不適合另一個。PHP中的動態水印

肖像

This is for Portrait photos

景觀 This is for landscape photos

下面是腳本:

<?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); 

?> 

但是動態水位不工作。我想要的是,如果圖像寬度低於1100像素,那麼它會使用肖像版本,如果超過,則使用橫向版本。

任何想法都會被大大忽視。

感謝,

大衛

回答

2

你的 「動態部分」 正在做的是基本概括爲:

if something 
    do A 
else if something 
    do B 
else 
    do B 

,中間的是完全多餘的。

你所需要的僅僅是:

$watermark = 
    imagecreatefromgif("watermark_".(imagesx($image) <= 1100 ? "port" : "lans").".gif");