Php watermarks images in batches

Source: Internet
Author: User
Tags rand


// 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.111cn.net.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] ======================================

The code is as follows: Copy code

// 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.