Adding watermark to image in PHP
Asked Answered
S

3

1

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:

<img src="watermark1.php?image=photo.jpg>

This is my watermark1.php:

<?php
// this tells the browser to render jpg image
header('content-type: image/jpeg'); 

// getting the image name from GET variable
$image = $_GET['image']; 

// creating png image of watermark
$watermark = imagecreatefrompng('watermark.png');   

// getting dimensions of watermark image
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);  

// creating jpg from original image
$image_path =  $image;
$image = imagecreatefromjpeg($image_path);
//something went wrong
if ($image === false) {
    return false;
}
// getting the dimensions of original image
$size = getimagesize($image_path);
// placing the watermark 5px from bottom and right
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
// blending the images together
imagealphablending($image, true);
imagealphablending($watermark, true);
// creating the new image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
// destroying and freeing memory
imagedestroy($image);
imagedestroy($watermark);
?>

However, the watermarked image could not be displayed. I heard about GDLibrary and ImageMagicK but i have no idea what are these 2 about. Is there a way to add watermark just by adding php codes or is it a must to import the GDLibrary/ImageMagicK.

Thanks for taking your time.

Scylla answered 12/1, 2012 at 3:16 Comment(1)
I used different that works like a charm for me is to have image being manipulated by javascript. If you insist on having image manipulate on server (PHP), then just embed javascript in php file. There are two avenues but of course, I picked jQuery. Straight Javascript: patrick-wied.at/static/watermarkjs Jquery: patrick-wied.at/static/watermarkjs/jq The trick for this approach is to have codes run at the end of the script (right before </body>) by calling the .js file, then use $.ready.document() afterward for watermark config. Then voila!Darladarlan
S
1

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(); ?>
Subsocial answered 23/9, 2014 at 4:47 Comment(0)
H
1

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;
}
Haire answered 18/3, 2020 at 13:30 Comment(0)
W
0
<?php
  
$sourceImage = "image.png";
$imagetobewatermark = imagecreatefrompng($sourceImage);
$watermarktext = "Watermark Sample Text";
$font= "fonts/Roboto/Roboto-Bold.ttf";
$fontsize = "22";
$white = imagecolorallocate($imagetobewatermark, 51, 102, 0);
  
$image = imagecreatefrompng($sourceImage);
$imageSize = getimagesize($sourceImage);
  
$wmX = $imageSize[0] - 380;
$wmY = $imageSize[1] - 20;
  
imagettftext($imagetobewatermark, $fontsize, 0, $wmX, $wmY, $white, $font, $watermarktext);
  
header("Content-type:image/png");
/*
  To save image
  imagepng($imagetobewatermark, $sourceImage);
*/
  
imagepng($imagetobewatermark);
imagedestroy($imagetobewatermark);
Whippletree answered 30/12, 2022 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.