php 批量给图片加水印代码
php 批量给图片加水印代码 我们经常会碰到要对大量的图片进行一次性增加水印,这就会碰到批量增加图片水印功能了,我们为你提供一款php批量增加水印的功能。
php教程 批量给图片加水印代码
我们经常会碰到要对大量的图片进行一次性增加水印,这就会碰到批量增加图片水印功能了,我们为你提供一款php批量增加水印的功能。
//config.php
//=================================用户配置区=================================
$per=1; //每次处理的图片数目
$dir1='img'; //输入目录
$dir2='img2'; //输出目录 输出图片会保存于$dir2/$dir1目录下面
$mark_img='www.bKjia.c0m.gif'; //水印图片
$mark_text=''; //水印文字内容 【不支持中文】
$mark_text_size=25; //水印文字大小
$mark_pos=5; //水印位置,0为随机,1为顶端居左,2为顶端居中,3为顶端居右,4为中部居左,5为中部居中,6为中部居右,7为底端居左,8为底端居中,9为底端居右;
//=================================用户配置区【结束】=================================
//mark.php加水印文件
function go( $url, $t = 0 )
{
exit( ( "" );
}function imagewatermark( $product_img, $waterpos = 0, $waterimage = "", $watertext = "", $textfont = 25, $textcolor = "#ff0000", $newdir = "marked" )
{
echo "正在处理图片:".$product_img."
";
$iswaterimage = false;
$formatmsg = "暂不支持该文件格式,请用图片处理软件将图片转换为gif、jpg、png格式。";
if ( !empty( $waterimage ) && file_exists( $waterimage ) )
{
$iswaterimage = true;
$water_info = getimagesize( $waterimage );
$water_w = $water_info[0];
$water_h = $water_info[1];
switch ( $water_info[2] )
{
case 1 :
$water_im = imagecreatefromgif( $waterimage );
break;
case 2 :
$water_im = imagecreatefromjpeg( $waterimage );
break;
case 3 :
$water_im = imagecreatefrompng( $waterimage );
break;
default :
echo $formatmsg;
return;
}
}
if ( !empty( $product_img ) && file_exists( $product_img ) )
{
$ground_info = getimagesize( $product_img );
$ground_w = $ground_info[0];
$ground_h = $ground_info[1];
switch ( $ground_info[2] )
{
case 1 :
$ground_im = imagecreatefromgif( $product_img );
break;
case 2 :
$ground_im = imagecreatefromjpeg( $product_img );
break;
case 3 :
$ground_im = imagecreatefrompng( $product_img );
break;
default :
echo $formatmsg;
return;
}
}
else
{
exit( "需要加水印的图片不存在!" );
}
if ( $iswaterimage )
{
$w = $water_w;
$h = $water_h;
$label = "图片的";
}
else
{
$temp = @imagettfbbox( @ceil( $textfont * 2.5 ), 0, "arial.ttf", $watertext );
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
unset( $temp );
$label = "文字区域";
}
if ( $ground_w {
echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
}
else
{
switch ( $waterpos )
{
case 0 :
$posx = rand( 0, $ground_w - $w );
$posy = rand( 0, $ground_h - $h );
break;
case 1 :
$posx = 0;
$posy = 0;
break;
case 2 :
$posx = ( $ground_w - $w ) / 2;
$posy = 0;
break;
case 3 :
$posx = $ground_w - $w;
$posy = 0;
break;
case 4 :
$posx = 0;
$posy = ( $ground_h - $h ) / 2;
break;
case 5 :
$posx = ( $ground_w - $w ) / 2;
$posy = ( $ground_h - $h ) / 2;
break;
case 6 :
$posx = $ground_w - $w;
$posy = ( $ground_h - $h ) / 2;
break;
case 7 :
$posx = 0;
$posy = $ground_h - $h;
break;
case 8 :
$posx = ( $ground_w - $w ) / 2;
$posy = $ground_h - $h;
break;
case 9 :
$posx = $ground_w - $w;
$posy = $ground_h - $h;
break;
default :
$posx = rand( 0, $ground_w - $w );
$posy = rand( 0, $ground_h - $h );
break;
}
imagealphablending( $ground_im, true );
imagecopyresampled($ground_im, $water_im,0,0, $posx, $posy, 0, 0, $water_w, $water_h);
if ( $iswaterimage )
{
imagecopymerge( $ground_im, $water_im, $posx, $posy, 0, 0, $water_w, $water_h,35 );
}
else
{
if ( !empty( $textcolor ) && strlen( $textcolor ) == 7 )
{
$r = hexdec( substr( $textcolor, 1, 2 ) );
$g = hexdec( substr( $textcolor, 3, 2 ) );
$b = hexdec( substr( $textcolor, 5 ) );
}
else
{
exit( "水印文字颜色格式不正确!" );
}
imagestring( $ground_im, $textfont, $posx, $posy, $watertext, imagecolorallocate( $ground_im, $r, $g, $b ) ,35);
}
switch ( $ground_info[2] )
{
case 1 :
imagegif( $ground_im, $newdir."/".$product_img );
break;
case 2 :
imagejpeg( $ground_im, $newdir."/".$product_img );
break;
case 3 :
imagepng( $ground_im, $newdir."/".$product_img );
break;
default :
echo $errormsg;
}
if ( isset( $water_info ) )
{
unset( $water_info );
}
if ( isset( $water_im ) )
{
imagedestroy( $water_im );
}
unset( $ground_info );
imagedestroy( $ground_im );
unset( $water_im );
unset( $ground_im );
}
}set_time_limit( 0 );
include( "config.php" );
if ( $_get[id] )
{
$id = $_get[id];
}
else
{
$id = 1;
}
if ( !is_dir( $dir2."/".$dir1 ) )
{
mkdir( $dir2."/".$dir1, 511 );
}
$ti = 0;
$dir = opendir( $dir1 );
while ( $file = readdir( $dir ) )
{
if ( $file == "." || $file == ".." )
{
}
else
{
++$ti;
if ( $ti {
}
else
{
if ( ( $id - 1 ) * $per {
imagewatermark( $dir1."/".$file, $mark_pos, $mark_img, $mark_text, $mark_text_size, "#ff0000", $dir2 );
}
if ( $id * $per {
closedir( $dir );
go( "mark.php?id=".( $id + 1 ) );
}
}
}
}
echo "

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
