2012-01-28 45 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