php - 在php中调整动态水印png的大小

标签 php html gd

我正在尝试自动调整水印的大小以覆盖图像的 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。

这修复了所有级联错误,现在工作正常。故事的寓意,检查错误日志。

关于php - 在php中调整动态水印png的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11890385/

相关文章:

php 5 strpos() 返回 0 和 false 之间的区别?

html - 如何更改单选按钮 'selected' 状态外观?

HTML5 相对单位

javascript - 在 jQuery 中折叠和展开多个嵌套行

php - 使用 PHP GD 计算文本宽度

c++ - 设置库包含 C++ 中的路径

php - CakePHP v3.x 如何在没有数据库连接的情况下烘焙

php DOMDocument 如何将节点值转换为字符串

php - MySQL 查询 : Getting COUNT of one table where the ID of table 1 has a value of X in a different table

php gd 图像质量