在PHP中为图像添加水印

koaltpgm  于 2023-01-01  发布在  PHP
关注(0)|答案(3)|浏览(129)

基本上,我有2个php脚本。1个php脚本是显示和其他1是水印功能。
我使用这个PHP来显示带有水印的图像:

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

这是我的博客

<?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。
谢谢你的时间。

k4ymrczo

k4ymrczo1#

你可以添加和自定义你的输出图片,使用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(); ?>
9vw9lbht

9vw9lbht2#

你可以使用这个解决方案.这里[ $SourceFile,$DestinationFile ]是本地目录的绝对路径. $imgUrl是基于HTTP的URL.水印将被放置在图像的顶部.
这里在这个例子中,你必须给予一个实际图像存储的路径位置。然后目标文件将在那个位置存储水印图像
另外,请注意,必须为Fontarial. ttf提供公共/绝对路径
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;
}
fnvucqvd

fnvucqvd3#

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

相关问题