imagettftext()不输出原始图像+水印

时间:2012-10-29 18:21:18

标签: php image gd imagettftext

使用as imagettftext()变体为自定义文本添加水印图像,但我必须在这里做错...因为输出结果没有预期的水印。已经尝试了一些不同的方法,但我觉得问题可能在于创建水印的方式...因为稍后在脚本上我尝试使用imagecreatefrompng(),当我应该使用其他功能...

正在使用的辅助功能:

//ini_set('gd.jpeg_ignore_warning', 1);

function imagettftextSp($image, $size, $angle, $x, $y, $color, $font, $text, $spacing = 0){
if ($spacing == 0){
    imagettftext($image, $size, $angle, $x, $y, $color, $font, $text);
}else{
    $temp_x = $x;
    for ($i = 0; $i < strlen($text); $i++){
        $bbox = imagettftext($image, $size, $angle, $temp_x, $y, $color, $font, $text[$i]);
        $temp_x += $spacing + ($bbox[2] - $bbox[0]);
    }
}
}

function calculateTextDimensions($text, $font, $size, $angle, $spacing){
$rect = imagettfbbox($size, $angle, $font, $text);
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
$spacing = ($spacing*(strlen($text)+2));
return array(
 "left"   => abs($minX) - 1,
 "top"    => abs($minY) - 1,
 "width"  => ($maxX - $minX)+$spacing,
 "height" => $maxY - $minY,
 "box"    => $rect
);
} 

PHP脚本:

$text = "C o p y r i  g  h  t   A f o n s o  G o m e s";
$font = "font/YanoneKaffeesatz-Bold-webfont.ttf";
$color = "0xFFFFFF";
$fundo = "0x779c4b";
$size = 13;
$angle = 0;
$padding = 4*5;
$spacing = 0;

$text_dimensions = calculateTextDimensions($text, $font, $size, $angle, $spacing);
$image_width = $text_dimensions["width"] + $padding;
$image_height = $text_dimensions["height"] + $padding;

$watermark = imagecreatetruecolor($image_width, $image_height);

ImageFill($watermark, 0, 0, $fundo);
imagesavealpha($watermark, true);
imagealphablending($watermark, true);
imagettftextSp($watermark,$size,$angle,$text_dimensions["left"]+($image_width/2)-($text_dimensions["width"]/2),$text_dimensions["top"]+($image_height/2)-($text_dimensions["height"]/2), $color, $font, $text, $spacing);
/* At this point the watermark is created */

$src = "TDJodmJXVXZhbUp0TDNCMVlteHBZMTlvZEcxc0wybHRZV2RsY3k5amIyNTBaWFZrYjNNdlNHVnlZbUZ5YVc4d055NXFjR2M9";
$src = base64_decode(base64_decode($src));
$marca = imagecreatefrompng($watermark); /* PROBLEM HERE? */
$imagem = imagecreatefromjpeg($src);

/* DIMENSIONS */
$mx = imagesx($marca);
$my = imagesy($marca);
$ix = imagesx($imagem);
$iy = imagesy($imagem);

/* POSITION */
$x = 0;
$y = $iy - $my - 0; 

imagecopy($imagem, $marca, $x, $y, 0, 0, $mx, $my); /* NEW IMAGE COMING OUT! */

header('Content-type: image/png');
imagepng($imagem);
imagedestroy($imagem);

很抱歉粘贴了长代码......但这比我的技能要高一点。

提前致谢。

1 个答案:

答案 0 :(得分:0)

问题解决了:))

删除此行

$marca = imagecreatefrompng($watermark); /* PROBLEM HERE? */

$ mx和$ my成为

    $mx = $image_width;
    $my = $image_height;

imagecopy()接收$ watermak变量

imagecopy($imagem, $watermark, $x, $y, 0, 0, $mx, $my); /* NEW IMAGE COMING OUT! */