Php adds watermarks to images in batches. We often encounter the need to add watermarks to a large number of images at a time. In this case, the image watermark function is added in batches, we provide you with a php batch add watermark function.
Php Tutorial: Add watermarks to images in batches
We often encounter the need to add watermarks to a large number of images at a time, which will lead to the batch adding of image watermarks. We provide you with a php batch adding of watermarks.
// Config. php
// ================================================= User configuration area ==== ====================================
$ Per = 1; // number of images processed each time
$ Dir1 = 'img '; // enter the Directory
$ Dir2 = 'img2'; // The output image is saved in the $ dir2/$ dir1 directory.
Export mark_img316'www.bkjia.c0m.gif '; // watermark image
$ Mark_text = ''; // watermark text content [Chinese is not supported]
$ 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 center left, 5 is center, 6: center to right, 7: bottom to left, 8: Bottom center, 9: bottom to right;
// ====================================== User configuration area [end] ======================================
// Add a watermark file to mark. php
Function go ($ url, $ t = 0)
{
Exit ("<script language = webpage Effect> settimeout (" location. replace ('". $ url. "')",". ($ t * 1000 ). ") </script> ");
}
Function imagewatermark ($ product_img, $ waterpos = 0, $ waterimage = "", $ watertext = "", $ textfont = 25, $ textcolor = "# ff0000 ", $ newdir = "marked ")
{
Echo "processing image:". $ product_img. "<br> ";
$ Iswaterimage = false;
$ Formatmsg = "this file format is not supported currently. Use image processing software to convert images to gif, jpg, and png formats. ";
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 image to be watermark does not exist! ");
}
If ($ iswaterimage)
{
$ W = $ water_w;
$ H = $ water_h;
$ Label = "image ";
}
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 to be watermark is smaller than that of the watermark". $ label. ". 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 ("the watermark text color format is incorrect! ");
}
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 "<center>