水印通过PHP

biz*_*zr3 0 php .htaccess watermark image

我的最后一个问题是忙,所以我必须开一个新的.(阅读我的最后一个问题在这里).我试图将请求重定向到watermark.php文件,以将徽标嵌入从我的网站外部调用的图像.但是当我将此代码用于htaccess文件时:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$ [NC]
RewriteCond %{HTTP_REFERER} !^$ 
RewriteCond %{HTTP_REFERER} !localhost [NC] 
RewriteCond %{HTTP_REFERER} !friendlysite\.com [NC]  
RewriteCond %{HTTP_REFERER} !google\. [NC] 
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]

RewriteRule (.*) /watermark.php?pic=$1
Run Code Online (Sandbox Code Playgroud)

这些用于watermark.php:

<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('tbwm.png');
$im = imagecreatefromjpeg($_GET['pic']);

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Run Code Online (Sandbox Code Playgroud)

php文件只显示img文件的alt标签并返回此错误: The requested URL /watermark.php was not found on this server.当我直接打开watermark.php时,此错误返回: The image http://192.168.1.190/hotlinking/watermark.php cont not be displayed because it contains errors.

有什么问题 ?

die*_*ump 7

你说:/hotlinking/watermark.php是你的文件路径,所以我认为你的重写必须是

RewriteRule (.*) /hotlinking/watermark.php?pic=$1
Run Code Online (Sandbox Code Playgroud)

我认为你直接访问时会出错

http://192.168.1.190/hotlinking/watermark.php
Run Code Online (Sandbox Code Playgroud)

因为你似乎没有传递pic参数.

顺便说说 :

a)我很确定你必须修改$ _GET ['pic']路径,否则该功能imagecreatefromjpeg 将无法打开你的图像.

b)如果它不是jpeg,你将不得不用另一个函数修改它.您必须在打开图像之前检查图像类型.

---更新---

好的RewriteRule是 RewriteRule (.*) watermark.php?pic=$1

现在您已经请求了watermark.php文件.您必须修改您的代码.$ _GET ['pic']告诉你请求了什么图像路径.您必须修改此路径才能打开图像.

Watermark.php在根目录中,所以也许只是一个dirname(__FILE__) . $_GET['pic']意志.

    <?php
// Load the stamp and the photo to apply the watermark to
$filepath = dirname(__FILE__) . $_GET['pic'];
if ( file_exists($filepath) ) 
{

    $infos = pathinfo($filepath);
    $im = null;
    switch($infos['extension']) 
    {
        case 'jpg' :
        case 'jpeg' :
            $im = imagecreatefromjpeg($filepath);
            break,
        case 'png' : 
            $im = imagecreatefrompng($filepath);

        // ....
    }

    if ( $im !== null )
    {
        $stamp = imagecreatefrompng('tbwm.png');
        // Set the margins for the stamp and get the height/width of the stamp image
        $marge_right = 10;
        $marge_bottom = 10;
        $sx = imagesx($stamp);
        $sy = imagesy($stamp);

        // Copy the stamp image onto our photo using the margin offsets and the photo 
        // width to calculate positioning of the stamp. 
        imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

        // Output and free memory
        header('Content-type: image/png');
        imagepng($im);
        imagedestroy($im);
    }
}
Run Code Online (Sandbox Code Playgroud)

这不是完整的脚本,只是如何开始.你必须自己弄清楚其余部分.