简体   繁体   English

即时生成PHP水印图像

[英]PHP watermark images on the fly

Background 背景

I have read through many sites including this one for a solution. 我已经通读了许多站点,包括此站点,以寻求解决方案。 I have a watermark script which up until a few months ago worked flawlessly. 我有一个水印脚本,直到几个月前它都可以正常工作。 My hosting provider performed routine updates and hosed my script. 我的托管服务提供商执行了例行更新并清除了我的脚本。 I made one adjustment to it, it worked. 我对其进行了一次调整,此方法奏效了。 With time, the watermark script seems be very heavy on system resources, causing all sorts of issues with my site including images not loading at random, etc. Currently, it appears I could run the site on PHP version 5.4 or a version or two above and below it. 随着时间的流逝,水印脚本似乎占用了大量系统资源,导致我的网站出现各种问题,包括图像无法随机加载等。目前,看来我可以在PHP 5.4或以上版本中运行该网站。和它下面。 Currently running at 5.4 if that should help. 如果有帮助,当前以5.4运行。

Explanation 说明

The script is intended to find image files from a specific location, pending on size, combine with one of several png images on the fly without overwriting the original image. 该脚本旨在从特定位置查找大小不等的图像文件,并与多个png图像之一实时组合,而不会覆盖原始图像。

The script 剧本

I have modified my script significantly in the past week, with minor performance improvement. 在过去一周中,我对脚本进行了重大修改,但性能略有改善。 I'm at my wits end, are there further improvements to this script to be made or cleaner code. 我不知所措,是否需要对该脚本进行进一步的改进或使代码更简洁。 Any assistance would greatly be appreciated. 任何帮助将不胜感激。

Files .htaccess, three jpg(s), and the watermark.php script. 文件.htaccess,三个jpg和watermark.php脚本。

.htaccess 的.htaccess

    RewriteRule ^(.*)wp-content/uploads/(.*) $1watermark.php?src=wp-content/uploads/$2

watermark.php watermark.php

<?php
$src = $_GET['src'];

    if(preg_match('/.gif/i',$src)) { $image = imagecreatefromgif($src); }
    if(preg_match('/.png/i',$src)) { $image = imagecreatefrompng($src); }
    if(preg_match('/.jpeg/i',$src)||preg_match('/.jpg/i',$src)) { $image = imagecreatefromjpeg($src); }
    if (!$image) exit();
//  if (empty($images)) die();

    if (imagesx($image) > 301 ) { $watermark = imagecreatefrompng('watermark.png'); }      // height greater than 301 then apply watermark 600x600
elseif (imagesx($image) > 175 ) { $watermark = imagecreatefrompng('watermarksm.png'); }    // height greater than 175 then apply small watermark 200x200
                           else { $watermark = imagecreatefrompng('empty.png'); }          // apply a dummy watermark resulting in none. 1x1

$dest_x = imagesx($image) - imagesx($watermark) - 0;
$dest_y = imagesy($image) - imagesy($watermark) - 0;

imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, imagesx($watermark), imagesy($watermark));
header('content-type: image/jpeg');  
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark); 
//die();
?>

A couple of things I have tried not reflected in this script is the following "minor" change. 我尝试过的未在此脚本中反映的几件事是以下“较小”更改。

if(preg_match('/.gif/i',$src)) to if(preg_match('/\\.gif$/i',$src)) if(preg_match('/.gif/i',$src))if(preg_match('/\\.gif$/i',$src))

Another variation attempted in the preg_match was with jpe$g and jp(|e)g$ . 在preg_match中尝试的另一个变体是jpe$gjp(|e)g$ Those change didn't seem to help in anyway and seemed to hurt the performance further. 这些变化似乎无济于事,而且似乎进一步损害了性能。

Again, any guidance would be appreciated. 再次,任何指导将不胜感激。 Thank you in advance. 先感谢您。

Why don't you once for all create watermarked version of all your images ? 为什么不一次为所有图像创建带水印的版本? It will avoid the server to work each time an image will be displayed on your website, and you will gain in performance. 它将避免服务器每次在您的网站上显示图像时都无法工作,从而提高性能。

If for any reason you need to display the original image, do a script that check the credential of the viewer then return the image. 如果出于任何原因需要显示原始图像,请执行一个脚本来检查查看器的凭据,然后返回图像。

First of all, those regular expressions are not the performance hogs. 首先,那些正则表达式不是性能消耗。 The real performance problems comes from image manipulation. 真正的性能问题来自图像处理。

Save the result from imagejpeg($image); 保存结果来自imagejpeg($image); into a file on disk "hidden" from the world. 放入世界上“隐藏”的磁盘文件中。 You can restrict access to that folder with .htaccess . 您可以使用.htaccess限制对该文件夹的访问。

The the logic should be something like this: 逻辑应该是这样的:

<?php
// Implement getTempFile to get a path name to the hidden folder.
$tempsrc = getTempFile($src)

$tempsrcexists = file_exists($tempsrc);

if (!$tempsrcexists)
{
    // Create image on disk.
    ...
}

// At this point, the temporary file must exist.    
$fp = fopen($tempsrcexists, 'rb');

// Output data to the client from the temporary file
header("Content-type: image/jpeg");
fpassthrough($fp);

fclose($fp);

?>

This should reduce the load on the server significantly. 这将大大减少服务器上的负载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM