0
作爲imagettftext()變體使用自定義文本水印圖像,但我必須在這裏做一些錯誤...因爲輸出結果沒有按照預期的水印。已經嘗試了一些不同的方法,但是我覺得問題可能是水印創建的方式......導致後面的腳本我嘗試使用imagecreatefrompng(),當我應該使用其他函數時...imagettftext()不輸出原始圖像+水印
正在使用的輔助功能:
//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);
對不起,粘貼長碼...但這種感覺我上面的技巧一點。
在此先感謝。