在PHP中为图像添加水印
基本上,我有 2 个 php 脚本.php脚本中的1个是显示,另外1个是水印功能.
Basically, I have 2 php script. 1 of the php scripts is to display and the other 1 is the watermark function.
我使用这个 PHP 来显示带水印的图像:
I use this PHP to display the image with watermark:
<img src="watermark1.php?image=photo.jpg>
这是我的 watermark1.php:
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);
?>
但是,无法显示带水印的图像.我听说过 GDLibrary 和 ImageMagicK,但我不知道这两个是什么.有没有办法只通过添加php代码来添加水印,或者必须导入GDLibrary/ImageMagicK.
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.
感谢您抽出宝贵时间.
您可以使用这样的简单 php 代码添加和自定义您的输出图片,这些代码可以与 TopiLib:(你也可以添加图片和文字水印)
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(); ?>