Add watermark code to images in batches_PHP tutorial
php batch watermark code for images We often encounter the need to add watermarks to a large number of pictures at one time, which leads to the function of batch adding watermarks to pictures. We provide you with a function of adding watermarks in batches in PHP.
php tutorial to add watermark code to images in batches
We often encounter the need to add watermarks to a large number of pictures at one time. This will lead to the function of batch adding watermarks to pictures. We provide you with a function of adding watermarks in batches in PHP.
//config.php
//==================================User configuration area============ =====================
$per=1; //The number of images processed each time
$dir1='img'; //Input directory
$dir2='img2'; //Output directory The output image will be saved under the $dir2/$dir1 directory
$mark_img='www.bKjia.c0m.gif'; //Watermark image
$mark_text=''; //Watermark text content [Does not support Chinese]
$mark_text_size=25; //Watermark text size
$mark_pos=5; //Watermark position, 0 is random, 1 is top left, 2 is top center, 3 is top right, 4 is middle left, 5 is middle center, 6 is middle right, 7 is bottom On the left, 8 means the bottom is in the center, 9 means the bottom is on the right;
//================================== User configuration area [End] ====== ===========================
//mark.php watermark file
function go( $url, $t = 0 )
{
exit( ( "" );
}function imagewatermark( $product_img, $waterpos = 0, $waterimage = "", $watertext = "", $textfont = 25, $textcolor = "#ff0000", $newdir = "marked" )
{
echo "Processing pictures:".$product_img."
";
$iswaterimage = false;
$formatmsg = "This file format is not supported yet. Please use image processing software to convert the image to gif, jpg, or png format.";
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( "The picture that needs to be watermarked does not exist!" );
}
If ( $iswaterimage )
{
$w = $water_w;
$h = $water_h;
$label = "Picture";
}
else
{
$temp = @imagettfbbox( @ceil( $textfont * 2.5 ), 0, "arial.ttf", $watertext );
$w = $temp[2] - $temp[6];
$h = $temp[3] - $temp[7];
unset( $temp );
$label = "Text area";
}
If ( $ground_w < $w || $ground_h < $h )
{
echo "The length or width of the image that needs to be watermarked is smaller than the watermark ".$label.", so the watermark cannot be generated!";
}
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 < ( $id - 1 ) * $per )
{
}
else
{
if ( ( $id - 1 ) * $per <= $ti && $ti < $id * $per )
{
imagewatermark( $dir1."/".$file, $mark_pos, $mark_img, $mark_text, $mark_text_size, "#ff0000", $dir2 );
}
if ( $id * $per <= $ti )
{
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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
