简体   繁体   English

PHP 给图片加水印

[英]Adding watermark to image in PHP

Basically, I have 2 php script.基本上,我有 2 php 脚本。 1 of the php scripts is to display and the other 1 is the watermark function. php脚本中有1个是显示,另外1个是水印function。

I use this PHP to display the image with watermark:我使用这个 PHP 来显示带水印的图像:

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

This is my watermark1.php:这是我的水印1.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.我听说过 GDLibrary 和 ImageMagicK,但我不知道这两个是关于什么的。 Is there a way to add watermark just by adding php codes or is it a must to import the GDLibrary/ImageMagicK.有没有办法通过添加 php 代码来添加水印,或者是否必须导入 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)您可以使用像这样与TopiLib一起使用的简单 php 代码添加和自定义您的输出图片:(您也可以添加图像和文本水印)

<?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. 我使用了一种对我来说很有吸引力的方法,那就是让图像由javascript处理。 If you insist on having image manipulate on server (PHP), then just embed javascript in php file. 如果您坚持要在服务器(PHP)上进行图像处理,则只需将javascript嵌入php文件中即可。

There are two avenues but of course, I picked jQuery. 有两种方法,但是我当然选择了jQuery。

Straight Javascript: http://www.patrick-wied.at/static/watermarkjs/ 直接的Javascript: http//www.patrick-wied.at/static/watermarkjs/

Jquery: http://www.patrick-wied.at/static/watermarkjs/jq/ 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. 这种方法的窍门是通过调用.js文件在脚本末尾(在之前)运行代码,然后在水印配置之后使用$ .ready.document()。 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 有关PHP中图像水印的详细信息

you can use this solution.您可以使用此解决方案。 here [ $SourceFile , $DestinationFile ] are Absolute Path of local directory.这里 [ $SourceFile , $DestinationFile ] 是本地目录的绝对路径。 $imgUrl is HTTP based URL. $imgUrl 是基于HTTP的 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.另外,请注意您必须为字体arial.ttf 提供公共/绝对路径

Solution is tested in Laravel 5.8 .解决方案在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. GDLibrary是一个PHP扩展,它向PHP添加了图像生成功能,例如imagejpeg或imagecopy等。为了生成图像,必须确保在服务器上已安装并启用了GD。

Update 更新资料

Here you can find information about installing GD. 在这里,您可以找到有关安装GD的信息。

ImageMagick is a software for image manipulation. ImageMagick是用于图像处理的软件。 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). 它比GD强大,后者在某些情况下效果要差很多(例如ImageMagick可以很好地完成图像缩放)。 For PHP wrapper around ImageMagick take a look at this link, and for GD, take a look at this link. 对于围绕ImageMagick的PHP包装器,请查看链接,对于GD,请查看链接。 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. 另外,如果决定使用ImageMagick,请确保满足要求(在发布的链接中有要求)...基本上,您需要在服务器上安装ImageMagick。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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