2012-01-28 78 views
0

我最后的问题是繁忙,所以我必须打开一个新的。 (阅读我最后一个问题here)。 即时尝试将请求重定向到watermark.php文件以将徽标嵌入到从我的网站外部调用的图像中。但是当我使用htaccess的文件验证码:水印通过php

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 

而这些对于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); 
?> 

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.

最新问题?

+0

你的意思是“php文件只显示img文件的alt标签”? – giorgio 2012-01-28 11:59:18

+0

哼哼mm,看看这个:'Please visit my site to see this picture。访问w3获取更多信息@'http:// www.w3schools.com/tags/att_img_alt.asp' – bizzr3 2012-01-28 12:04:35

回答

1

附注: 您需要在输出图像前检查图像是否存在,如果未找到,则显示不同的图像。 e.g:

<?php 
$pathToImage='./images/'.basename($_GET['pic']); 

if(file_exists($pathToImage)==true){ 
    // Load the stamp and the photo to apply the watermark to 
    $stamp = imagecreatefrompng('tbwm.png'); 
    $im = imagecreatefromjpeg($pathToImage); 

    // 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); 
}else{ 
    header('Content-type: image/png'); 
    readfile('notfound.png'); 
} 
?> 

回复评论:

您可以缓存图像通过将文件名添加到imagepng($ IM, “new_image.png”);然后检查该文件是否存在于随后的页面加载中,这不会显着加速脚本,但会使用的磁盘空间量增加一倍。

+0

你知道这是我的网站的性能提升吗?我可以查看水印图像并在以后显示吗? – bizzr3 2012-01-28 12:53:45

7

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

RewriteRule (.*) /hotlinking/watermark.php?pic=$1 

,我认为你有一个错误,当您直接访问

http://192.168.1.190/hotlinking/watermark.php 

因为你似乎不传递图片参数。

顺便说一句:

一)我敢肯定,你将不得不修改$ _GET [“知情同意”]路径或功能imagecreatefromjpeg将无法​​打开你的形象。

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

--- UPDATE ---

好重写规则是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); 
    } 
} 

这不是完整的脚本,只是如何开始。你必须自己找出其余的。

+0

当我使用这条路径时'RewriteRule(。*)/ hotlinking/watermark .php?pic = $ 1' 404错误是这样的:'/hotlinking/hotlinking/watermark.php not found' – bizzr3 2012-01-28 12:06:59

+1

您的.htaccess位于localhost或“hotlinking”目录中? – dievardump 2012-01-28 12:12:19

+0

in/hotlinking root! – bizzr3 2012-01-28 12:15:22