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 {
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 {
}
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 "

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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