Adding watermark to image in PHP

后端 未结 2 1830
情深已故
情深已故 2020-12-21 22:31

Basically, I have 2 php script. 1 of the php scripts is to display and the other 1 is the watermark function.

I use this PHP to display the image with watermark:

相关标签:
2条回答
  • 2020-12-21 22:54

    you can use this solution. here [ $SourceFile , $DestinationFile ] are Absolute Path of local directory. $imgUrl is HTTP based URL. Watermark will be placed at the top of the image.

    here In this example, you have to give a path location where actual Image is stored. then Destination File will store that Watermark Image on that location.

    Also, note that you have to give Public/Absolute Path for Font arial.ttf.

    Solution is tested in Laravel 5.8.

    function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile,$imgUrl) {
        list($width, $height) = getimagesize($SourceFile);
        $image_p = imagecreatetruecolor($width, $height);
        $image = imagecreatefromjpeg($SourceFile);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
        $black = imagecolorallocate($image_p, 255, 255, 255);
        $font = public_path('fonts/arial.ttf');
        $font_size = 8;
        imagettftext($image_p, $font_size, 0, 10, 20, $black,$font , $WaterMarkText);
        if ($DestinationFile <> '') {
            imagejpeg ($image_p, $DestinationFile, 100);
        } else {
            header('Content-Type: image/jpeg');
            imagejpeg($image_p, null, 100);
        };
        imagedestroy($image);
        imagedestroy($image_p);
        return  $imgUrl;
    }
    
    0 讨论(0)
  • 2020-12-21 22:58

    You can add and customize your output pictures with simple php codes like this that working with TopiLib: (You can add both image and text watermark, too)

    <?php require '../topi.lib.min';
    $panel = new \TopiLib\TopiPanel('png transparent', 9, 0, 0, 0);
    $panel->createFromPNG($_GET['image'], true);
    $img = new \TopiLib\TopiImage('watermark.png', 'transparent png');
    $img->startX = 100;  //your custom start X position
    $img->startY = 100;  //your custom start Y position
    $panel->addImage($img);
    $panel->render(); ?>
    
    0 讨论(0)
提交回复
热议问题