簡體   English   中英

用PHP添加水印

[英]Adding watermark with PHP

這是我的PHP水印功能:

function img_watermark($image) {
    $stamp = imagecreatefrompng('images/wm.png');
    $im = imagecreatefromjpeg($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);
}

這是我的HTML代碼:

<img src="<?php img_watermark('images/ornek.jpeg');?>" />

但這給了我這樣的東西:

....... <, u /,R M 7 a4 3eY&k Tƃ#Ki 'S。^ A 2_ ^ L* \\ LMSOݺ7 ''@@ \\ k *) 4I 5 <C LP /W 2w qopwnnnw3e ҂g QB\\ _ȋc # F $ `4;; [T b -XגsΩ( J “G ; = it。* ˗> Nw o #¨8 J wz V W ??? > { # Z 1 /?7VWo?CRVS3ӷ޶?ڝ}Ϳ O=q 〜 ? IY ?MvN Y k 7[ hwg < / O s7o。 u 3F8 | 〜ᗟ} v' # g 6 / | ~~ᫍ( ?p( B。 ?sY G> | ŗ޸V)% \\Z J 7/ ..............

我希望它向我顯示帶水印的圖像。 我怎樣才能解決這個問題?

您設置錯誤。 您的HTML應該如下所示:

<img src="image.php?src=images/ornek.jpeg" />

image.php應該是這樣的:

$stamp = imagecreatefromjpeg('images/wm.png');
$im = imagecreatefromjpeg($_GET['src']); 
$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);

您不能將二進制數據放在圖像標簽的src屬性中。

src屬性通常需要圖像的URL。

您可以使用base64編碼來實現:

$file = base64_encode(img_watermark('images/ornek.jpeg'));
echo "<img src='data:image/jpeg;base64,".$file."' alt='watermarked image'>";

並刪除函數中的header行,除非您的PHP文件應該發送圖像數據而不是HTML。 最好不要把那些東西混在一起:(

將其設置為標題:

header("Content-type:image/jpeg");

代替:

header("Content-type:image/png");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM