dada050909
dada050909

Reputation: 63

Adding watermark to image in PHP

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.

Upvotes: 1

Views: 7594

Answers (3)

Ankit Jindal
Ankit Jindal

Reputation: 4030

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

Upvotes: 0

pankaj
pankaj

Reputation: 1906

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

Upvotes: 1

jlrling
jlrling

Reputation: 11

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(); ?>

Upvotes: 1

Related Questions