使用作为 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);
很抱歉粘贴了长代码......但这感觉有点超出我的技能。
提前致谢。