简体   繁体   中英

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.

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

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: http://www.patrick-wied.at/static/watermarkjs/

Jquery: http://www.patrick-wied.at/static/watermarkjs/jq/

The trick for this approach is to have codes run at the end of the script (right before ) by calling the .js file, then use $.ready.document() afterward for watermark config. Then voila!

Here is the code,

<?php

header('content-type: image/jpeg');
$src = $_GET['src'];
$path = pathinfo($src);
$watermark = imagecreatefrompng('watermark.png'); 
$watermark_width = imagesx($watermark); 
$watermark_height = imagesy($watermark); 
$image = imagecreatetruecolor($watermark_width, $watermark_height); 
if ($path['extension']=='png') 
$image = imagecreatefrompng($src); 
else if ($path['extension']=='jpg'||$path['extension']=='jpeg') 
$image = imagecreatefromjpeg($src); 
$size = getimagesize($_GET['src']); 
$dest_x = $size[0] - $watermark_width-10; 
$dest_y = $size[1] - $watermark_height-10; 
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50); 
imagejpeg($image); 
imagedestroy($image); 
imagedestroy($watermark); 

?> 

Details about, Image watermark in PHP

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

GDLibrary is a PHP extension which adds image generating functions to PHP, such as imagejpeg or imagecopy etc. In order to generate images, you have to make sure GD is installed and enabled on your server.

Update

Here you can find information about installing GD.

ImageMagick is a software for image manipulation. It is more powerful than GD, which works a lot worse for some things (for example image scaling is pretty much better done by ImageMagick). For PHP wrapper around ImageMagick take a look at this link, and for GD, take a look at this link. Also, if you decide to go with ImageMagick, make sure that you meet the requirements (you have them in the posted link)...bacisally you need ImageMagick installed on the server.

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