【问题标题】:Adding watermark to image in PHP在PHP中为图像添加水印
【发布时间】:2012-01-12 03:16:10
【问题描述】:

基本上,我有 2 个 php 脚本。 1个php脚本是显示,另外1个是水印功能。

我使用这个 PHP 来显示带有水印的图像:

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

这是我的 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。

感谢您抽出宝贵时间。

【问题讨论】:

  • 我使用了不同的东西,这对我来说就像一个魅力是让图像被 javascript 处理。如果您坚持在服务器(PHP)上进行图像处理,那么只需将 javascript 嵌入 php 文件中。有两种途径,但当然,我选择了 jQuery。直接 Javascript:patrick-wied.at/static/watermarkjs Jquery:patrick-wied.at/static/watermarkjs/jq 这种方法的诀窍是通过调用 .js 文件在脚本末尾(就在

标签: php watermark


【解决方案1】:

您可以使用像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(); ?>

【讨论】:

    【解决方案2】:

    您可以使用此解决方案。这里 [ $SourceFile , $DestinationFile ] 是本地目录的绝对路径。 $imgUrl 是基于 HTTP 的 URL。 水印将放置在图像的顶部。

    here 在此示例中,您必须提供存储实际图像的路径位置。然后目标文件将在该位置存储该水印图像

    另外,请注意,您必须为 Font arial.ttf 提供 Public/Absolute Path

    解决方案在 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多