简体   繁体   中英

Generate image thumbnails with watermarks on the fly in PHP

I'm looking for PHP class (solution) for generating image thumbnails with watermarks on the fly. Any idea ?

I've successfully used this code to add text to an (thumbnail) image:

(note that you'll need to provide a font)

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);
}

Feedback welcome.

You can do this using using the imagecopyresampled() function. Heres a easy and clear tutorial of adding watermark to thumbnails . Also you can use imagettftext() function to use fonts as your watermark

Tutorial Link: http://www.phpjabbers.com/phpexample.php?eid=20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM