0

我正在寻找用于动态生成带有水印的图像缩略图的 PHP 类(解决方案)。任何的想法 ?

4

2 回答 2

1

我已成功使用此代码将文本添加到(缩略图)图像:

(请注意,您需要提供字体)

function createImage($in_filename, $out_filename, $width, $height)
{
    $src_img = ImageCreateFromJpeg($in_filename);

    $old_x = ImageSX($src_img);
    $old_y = ImageSY($src_img);
    $dst_img = ImageCreateTrueColor($width, $height);
    ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $old_x, $old_y);

    addWatermark($dst_img);

    ImageJpeg($dst_img, $out_filename, 80);

    ImageDestroy($dst_img);
    ImageDestroy($src_img);
}

function addWatermark($image)
{
    $text = "watermark text";

    $font = realpath($_SERVER["DOCUMENT_ROOT"] . "/code/COURBD.TTF"); // case sensitive
    if ($font == false) return;

    $fontSize = 11;
    $borderOffset = 4;

    $dimensions = ImageTtfBBox($fontSize, 0, $font, $text . "@");
    $lineWidth = ($dimensions[2] - $dimensions[0]);

    $textX = (ImageSx($image) - $lineWidth) / 2;
    $textY = $borderOffset - $dimensions[7];

    $white = ImageColorAllocate($image, 240, 240, 240);
    ImageTtfText($image, $fontSize, 0, $textX, $textY, $white, $font, $text);
}

欢迎反馈。

于 2010-07-27T11:43:11.150 回答
0

您可以使用该imagecopyresampled()函数来执行此操作。这是一个简单明了的为缩略图添加水印的教程。您也可以使用imagettftext()功能将字体用作水印

教程链接: http ://www.phpjabbers.com/phpexample.php?eid=20

于 2010-07-27T11:27:17.917 回答