简体   繁体   English

使用 GD 批量水印图像

[英]Batch Watermark images using GD

I have been looking high and low around the internet for a code to Batch watermark images in a folder.我一直在互联网上四处寻找代码以在文件夹中批处理水印图像。 I want to add a transparent .png watermark to them all but the only scripts i can find are for one(1) single file and not all in one directory.我想为它们添加一个透明的 .png 水印,但我能找到的唯一脚本是针对一(1)个单个文件,而不是全部在一个目录中。

I tried to search google but there was no luck at all as i mentioned in my text above,我试图搜索谷歌,但正如我在上面的文字中提到的那样,根本没有运气,

i made this code, it paste the watermark just in midle of the other image.我制作了这段代码,它将水印粘贴在另一张图像的中间。 it work fine in browser, but it doesn't in comand line它在浏览器中工作正常,但它不在命令行中
i hope it work for you.我希望它对你有用。

$dir_to_img='Wallpapers_Azul';
$watermark = "angel.png";
$dir_to_new_image='watered';

    if(is_dir($dir_to_img)){
        if($dir = opendir($dir_to_img)){
            while(($archivo = readdir($dir)) !== false){
                if($archivo != '.' && $archivo != '..' && $archivo != '.htaccess'){
                    if(substr($archivo, -4) == '.jpg' || substr($archivo, -4) == '.JPG') //only jpg files

            $im = imagecreatefrompng($watermark);
            $im2 = imagecreatefromjpeg($dir_to_img."/".$archivo);

                imagecopy($im2, $im, (imagesx($im2)/2)-(imagesx($im)/2), (imagesy($im2)/2)-(imagesy($im)/2), 0, 0, imagesx($im), imagesy($im));

        if(!is_dir($dir_to_new_image)){   //save to another dir
            mkdir($dir_to_new_image, 0777, true);
        }

        imagejpeg($im2,"$dir_to_new_image/$archivo",90);
        imagedestroy($im);
        imagedestroy($im2);
                }
            }
        closedir($dir);
        }
    }

result:结果:

original原来的

在此处输入图片说明

after

在此处输入图片说明

Use this codes working with TopiLib :使用此代码与TopiLib一起使用:

<?php 
function renderToImage($inputImagePath, $watermarkImagePath)
{
require './topi.lib.min';
$panel = new \TopiLib\TopiPanel('png transparent', 9, 0, 0, 0);
$panel->createFromPNG($inputImagePath);
$img = new \TopiLib\TopiImage($watermarkImagePath, 'transparent png');
$img->position = 'left-bottom';      //You can use 'fill', 'fit', 'stretch', 'tile', 'center', 'right', 'right-top', 'right-bottom', 'left', 'left-top', 'left-bottom'.
//Or you can change startX and startY properties
//For text watermark use TopiText object
$panel->addImage($img);    
$panel->renderToImage($inputImagePath . "Edited." . pathinfo($inputImagePath, PATHINFO_EXTENSION));
}
?>

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

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