📄 phpthumb.filters.php
字号:
$TTFbox = ImageTTFbBox($size, $angle, $ttffont, $text);
$min_x = min($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
$max_x = max($TTFbox[0], $TTFbox[2], $TTFbox[4], $TTFbox[6]);
//$text_width = round($max_x - $min_x + ($size * 0.5));
$text_width = round($max_x - $min_x);
$min_y = min($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
$max_y = max($TTFbox[1], $TTFbox[3], $TTFbox[5], $TTFbox[7]);
//$text_height = round($max_y - $min_y + ($size * 0.5));
$text_height = round($max_y - $min_y);
$TTFboxChar = ImageTTFbBox($size, $angle, $ttffont, 'pH');
$char_min_y = min($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
$char_max_y = max($TTFboxChar[1], $TTFboxChar[3], $TTFboxChar[5], $TTFboxChar[7]);
$char_height = round($char_max_y - $char_min_y);
switch ($alignment) {
case 'T':
$text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
$text_origin_y = $char_height + $margin;
break;
case 'B':
$text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
$text_origin_y = ImageSY($gdimg) - $TTFbox[1] - $margin;
break;
case 'L':
$text_origin_x = $margin;
$text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
break;
case 'R':
$text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
$text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
break;
case 'C':
$text_origin_x = round((ImageSX($gdimg) - $text_width) / 2);
$text_origin_y = round((ImageSY($gdimg) - $text_height) / 2) + $char_height;
break;
case 'TL':
$text_origin_x = $margin;
$text_origin_y = $char_height + $margin;
break;
case 'TR':
$text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
$text_origin_y = $char_height + $margin;
break;
case 'BL':
$text_origin_x = $margin;
$text_origin_y = ImageSY($gdimg) - $TTFbox[1] - $margin;
break;
case 'BR':
default:
$text_origin_x = ImageSX($gdimg) - $text_width + $TTFbox[0] - $min_x + round($size * 0.25) - $margin;
$text_origin_y = ImageSY($gdimg) - $TTFbox[1] - $margin;
break;
}
$letter_color_text = phpthumb_functions::ImageHexColorAllocate($gdimg, $hex_color, false, $opacity * 1.27);
if ($alignment == '*') {
$text_origin_y = $char_height + $margin;
while (($text_origin_y - $text_height) < ImageSY($gdimg)) {
$text_origin_x = $margin;
while ($text_origin_x < ImageSX($gdimg)) {
ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
$text_origin_x += ($text_width + $margin);
}
$text_origin_y += ($text_height + $margin);
}
} else {
//ImageRectangle($gdimg, $text_origin_x + $min_x, $text_origin_y + $TTFbox[1], $text_origin_x + $min_x + $text_width, $text_origin_y + $TTFbox[1] - $text_height, $letter_color_text);
ImageTTFtext($gdimg, $size, $angle, $text_origin_x, $text_origin_y, $letter_color_text, $ttffont, $text);
}
return true;
//if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
// if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
// $letter_color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($img_watermark, 127, 127, 127, 127);
// $letter_color_background_mask = phpthumb_functions::ImageColorAllocateAlphaSafe($img_watermark_mask, 127, 127, 127, 127);
// ImageFilledRectangle($img_watermark, 0, 0, ImageSX($img_watermark), ImageSY($img_watermark), $letter_color_background);
// ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $letter_color_background_mask);
//
// $letter_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
// $letter_color_watermark_mask = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
//
// ImageTTFtext($img_watermark, $size, $angle, $TTFbox[0] - $min_x + round($size * 0.25), $TTFbox[1] - $min_y, $letter_color_watermark, $ttffont, $text);
// ImageTTFtext($img_watermark_mask, $size, $angle, $TTFbox[0] - $min_x + round($size * 0.25), $TTFbox[1] - $min_y, -$letter_color_watermark_mask, $ttffont, $text);
//
// $this->ApplyMask($img_watermark_mask, $img_watermark);
// $this->WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
//
// ImageDestroy($img_watermark);
// ImageDestroy($img_watermark_mask);
// return true;
// }
//}
} else {
$size = min(5, max(1, $size));
$this->DebugMessage('Using built-in font (size='.$size.') for text watermark'.($ttffont ? ' because $ttffont !is_readable('.$ttffont.')' : ''), __FILE__, __LINE__);
$text_width = 0;
$text_height = 0;
foreach ($textlines as $line) {
$text_width = max($text_width, ImageFontWidth($size) * strlen($line));
$text_height += ImageFontHeight($size);
}
if ($img_watermark = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
ImageAlphaBlending($img_watermark, false);
$text_color_background = phpthumb_functions::ImageColorAllocateAlphaSafe($img_watermark, 255, 255, 255, 127);
ImageFilledRectangle($img_watermark, 0, 0, ImageSX($img_watermark), ImageSY($img_watermark), $text_color_background);
if ($angle) {
if ($img_watermark_mask = phpthumb_functions::ImageCreateFunction($text_width, $text_height)) {
$mask_color_background = ImageColorAllocate($img_watermark_mask, 0, 0, 0);
ImageAlphaBlending($img_watermark_mask, false);
ImageFilledRectangle($img_watermark_mask, 0, 0, ImageSX($img_watermark_mask), ImageSY($img_watermark_mask), $mask_color_background);
$mask_color_watermark = ImageColorAllocate($img_watermark_mask, 255, 255, 255);
}
}
$text_color_watermark = phpthumb_functions::ImageHexColorAllocate($img_watermark, $hex_color);
foreach ($textlines as $key => $line) {
switch ($alignment) {
case 'C':
case 'T':
case 'B':
$x_offset = round(($text_width - (ImageFontWidth($size) * strlen($line))) / 2);
break;
case 'L':
case 'TL':
case 'BL':
$x_offset = 0;
break;
case 'R':
case 'TR':
case 'BR':
default:
$x_offset = $text_width - (ImageFontWidth($size) * strlen($line));
break;
}
ImageString($img_watermark, $size, $x_offset, $key * ImageFontHeight($size), $line, $text_color_watermark);
if ($angle && $img_watermark_mask) {
ImageString($img_watermark_mask, $size, $x_offset, $key * ImageFontHeight($size), $text, $mask_color_watermark);
}
}
if ($angle && $img_watermark_mask) {
$img_watermark = ImageRotate($img_watermark, $angle, $text_color_background);
$img_watermark_mask = ImageRotate($img_watermark_mask, $angle, $mask_color_background);
phpthumb_filters::ApplyMask($img_watermark_mask, $img_watermark);
}
phpthumb_filters::WatermarkOverlay($gdimg, $img_watermark, $alignment, $opacity, $margin);
ImageDestroy($img_watermark);
return true;
}
}
return false;
}
function WatermarkOverlay(&$gdimg_dest, &$img_watermark, $alignment='*', $opacity=50, $margin=5) {
if (is_resource($gdimg_dest) && is_resource($img_watermark)) {
$watermark_source_x = 0;
$watermark_source_y = 0;
$img_source_width = ImageSX($gdimg_dest);
$img_source_height = ImageSY($gdimg_dest);
$watermark_source_width = ImageSX($img_watermark);
$watermark_source_height = ImageSY($img_watermark);
$watermark_opacity_percent = max(0, min(100, $opacity));
if ($margin < 1) {
$watermark_margin_percent = 1 - $margin;
} else {
$watermark_margin_percent = (100 - max(0, min(100, $margin))) / 100;
}
$watermark_margin_x = round((1 - $watermark_margin_percent) * $img_source_width);
$watermark_margin_y = round((1 - $watermark_margin_percent) * $img_source_height);
switch ($alignment) {
case '*':
if ($gdimg_tiledwatermark = phpthumb_functions::ImageCreateFunction($img_source_width, $img_source_height)) {
ImageAlphaBlending($gdimg_tiledwatermark, false);
if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
ImageSaveAlpha($gdimg_tiledwatermark, true);
}
$text_color_transparent = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_tiledwatermark, 255, 0, 255, 127);
ImageFill($gdimg_tiledwatermark, 0, 0, $text_color_transparent);
// set the tiled image transparent color to whatever the untiled image transparency index is
// ImageColorTransparent($gdimg_tiledwatermark, ImageColorTransparent($img_watermark));
// a "cleaner" way of doing it, but can't handle the margin feature :(
// ImageSetTile($gdimg_tiledwatermark, $img_watermark);
// ImageFill($gdimg_tiledwatermark, 0, 0, IMG_COLOR_TILED);
// break;
// ImageFill($gdimg_tiledwatermark, 0, 0, ImageColorTransparent($gdimg_tiledwatermark));
// tile the image as many times as can fit
for ($x = $watermark_margin_x; $x < ($img_source_width + $watermark_source_width); $x += round($watermark_source_width + ((1 - $watermark_margin_percent) * $img_source_width))) {
for ($y = $watermark_margin_y; $y < ($img_source_height + $watermark_source_height); $y += round($watermark_source_height + ((1 - $watermark_margin_percent) * $img_source_height))) {
ImageCopy(
$gdimg_tiledwatermark,
$img_watermark,
$x,
$y,
0,
0,
min($watermark_source_width, $img_source_width - $x - ((1 - $watermark_margin_percent) * $img_source_width)),
min($watermark_source_height, $img_source_height - $y - ((1 - $watermark_margin_percent) * $img_source_height))
);
}
}
$watermark_source_width = ImageSX($gdimg_tiledwatermark);
$watermark_source_height = ImageSY($gdimg_tiledwatermark);
$watermark_destination_x = 0;
$watermark_destination_y = 0;
ImageDestroy($img_watermark);
$img_watermark = $gdimg_tiledwatermark;
}
break;
case 'T':
$watermark_destination_x = round((($img_source_width / 2) - ($watermark_source_width / 2)) + $watermark_margin_x);
$watermark_destination_y = $watermark_margin_y;
break;
case 'B':
$watermark_destination_x = round((($img_source_width / 2) - ($watermark_source_width / 2)) + $watermark_margin_x);
$watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent);
break;
case 'L':
$watermark_destination_x = $watermark_margin_x;
$watermark_destination_y = round((($img_source_height / 2) - ($watermark_source_height / 2)) + $watermark_margin_y);
break;
case 'R':
$watermark_destination_x = round(($img_source_width - $watermark_source_width) * $watermark_margin_percent);
$watermark_destination_y = round((($img_source_height / 2) - ($watermark_source_height / 2)) + $watermark_margin_y);
break;
case 'C':
$watermark_destination_x = round(($img_source_width / 2) - ($watermark_source_width / 2));
$watermark_destination_y = round(($img_source_height / 2) - ($watermark_source_height / 2));
break;
case 'TL':
$watermark_destination_x = $watermark_margin_x;
$watermark_destination_y = $watermark_margin_y;
break;
case 'TR':
$watermark_destination_x = round(($img_source_width - $watermark_source_width) * $watermark_margin_percent);
$watermark_destination_y = $watermark_margin_y;
break;
case 'BL':
$watermark_destination_x = $watermark_margin_x;
$watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent);
break;
case 'BR':
default:
$watermark_destination_x = round(($img_source_width - $watermark_source_width) * $watermark_margin_percent);
$watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent);
break;
}
ImageAlphaBlending($gdimg_dest, false);
if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) {
ImageSaveAlpha($gdimg_dest, true);
ImageSaveAlpha($img_watermark, true);
}
phpthumb_functions::ImageCopyRespectAlpha($gdimg_dest, $img_watermark, $watermark_destination_x, $watermark_destination_y, 0, 0, $watermark_source_width, $watermark_source_height, $watermark_opacity_percent);
return true;
}
return false;
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -