簡體   English   中英

在php中調整動態水印png的大小

[英]Resizing a dynamic watermark png in php

我正在嘗試自動調整水印大小以覆蓋圖像的1/4。 我有水印代碼工作,但我無法正確調整大小。

<?php

$image = $_GET['src'];
$path_parts = pathinfo($image);
$extension=strtolower($path_parts['extension']);
$size = (imagesx($image)/2);

$stamp = ImageCreateFromPNG("watermark.png"); 
ImageAlphaBlending($stamp,true); 
ImageSaveAlpha($stamp,true); 



$w = imagesx($stamp); 
$h = imagesy($stamp); 

if( $w==0 or $h==0 ) die("ERROR - zero image size"); 

$percent = $size / (($w>$h)?$w:$h); 
$nw = intval($w*$percent); 
$nh = intval($h*$percent); 

$stamp_resized = ImageCreateTrueColor($nw,$nh); 

ImageAlphaBlending($stamp_resized,false); 
ImageSaveAlpha($stamp_resized,true); 

if(!empty($transparent_color)) 
{ 
    $transparent_new = ImageColorAllocate($stamp_resized,$transparent_color['red'],$transparent_color['green'],$transparent_color['blue']); 
    $transparent_new_index = ImageColorTransparent($stamp_resized,$transparent_new); 
    ImageFill($stamp_resized, 0,0, $transparent_new_index); 
} 

if(ImageCopyResized($stamp_resized,$stamp, 0,0,0,0, $nw,$nh, $w,$h )) 
{ 
    ImageDestroy($stamp); 
    $stamp = $stamp_resized; 
} 




//Everything from here on works perfect
if(file_exists($image)){
    if ($extension == 'gif')$im = imagecreatefromgif($_GET['src']);
    if ($extension == 'jpg')  {

        $im = imagecreatefromjpeg($_GET['src']);

        $marge_right = 10;
        $marge_bottom = 10;
        $sx = imagesx($stamp);
        $sy = imagesy($stamp);


        imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, $sx, $sy);

    }
}
else{
$im = imagecreatefromgif('images/no_picture.gif');
}



// Output and free memory
header('Content-type: image/jpeg');
imagejpeg($im);  
imagedestroy($im);
?>

我檢查了錯誤日志,並出現了以下錯誤:

[Thu Aug 09 16:47:05 2012] [error] [client 24.224.164.149] PHP Warning:  imagesx() expects parameter 1 to be resource, string given in {path removed}/watermark.php on line 15

[Thu Aug 09 16:47:05 2012] [error] [client 24.224.164.149] PHP Warning:  imagecreatetruecolor(): Invalid image dimensions in {path removed}/watermark.php on line 32

[Thu Aug 09 16:47:05 2012] [error] [client 24.224.164.149] PHP Warning:  imagealphablending() expects parameter 1 to be resource, boolean given in {path removed}/watermark.php on line 34

[Thu Aug 09 16:47:05 2012] [error] [client 24.224.164.149] PHP Warning:  imagesavealpha() expects parameter 1 to be resource, boolean given in {path removed}/watermark.phpp on line 35

[Thu Aug 09 16:47:05 2012] [error] [client 24.224.164.149] PHP Warning:  imagecopyresized() expects parameter 1 to be resource, boolean given in {path removed}/watermark.php on line 44

結果我傳遞的圖像是圖像的路徑($ image),而不是圖像資源($ im)本身。 我稍微重新編寫代碼,以便在調整圖像大小之前加載jpg。

這解決了所有級聯錯誤,現在工作正常。 故事的道德,檢查錯誤日志。

看起來像

 $size = (imagesx($image)/2);

可能是你的問題,因為imagesx需要一個圖像創建功能生成的圖像資源,你給它一個src。

暫無
暫無

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

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