PHP 中的动态水印

发布于 2024-12-14 06:52:07 字数 1969 浏览 2 评论 0原文

我正在使用此脚本在一个客户的网站上创建水印,问题是一个水印适合一张图像,但不适合另一张图像。

肖像

这适用于肖像照片

风景 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 像素,则使用纵向版本,如果超过该宽度,则使用横向版本。

任何想法将不胜感激。

谢谢,

大卫

I'm using this script to create a watermark on one of client's websites the problem is that one watermark is suitable for one image and unsuitable for another.

Portrait

This is for Portrait photos

Landscape
This is for landscape photos

Here is the script:

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

?>

However the dynamic watermark doesn't work. What i want is if the image is under 1100px wide then it uses the portrait version and if it is over that to use the landscape version instead.

Any idea would be greatly apprieciated.

Thanks,

David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2024-12-21 06:52:07

你的“动态部分”正在做的事情基本上可以总结为:

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

中间的部分完全是多余的。

你需要的只是:

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

What your "dynamic part" is doing is basically summed up as:

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

The middle one is completely redundant.

What you need is just:

$watermark =
  imagecreatefromgif("watermark_".(imagesx($image) <= 1100 ? "port" : "lans").".gif");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文