PHP Batch to Image watermark Code _php tutorial

Source: Internet
Author: User
PHP batch to the image watermark code we often encounter to a large number of images to increase the watermark, this will touch the volume increase image watermark function, we provide you with a PHP batch to increase the watermark function.

PHP Tutorial Batch to add watermark code to pictures
We often encounter to a large number of images to increase the watermark for a single time, this will touch the volume increase in image watermark function, we provide you with a PHP batch to increase the watermark function.

config.php
================================= User Configuration Area =================================

$per = 1; Number of pictures processed per
$dir 1= ' img '; Enter directory
$dir 2= ' Img2 '; Output directory output image will be saved in $dir2/$dir 1 directory
$mark _img= ' www.bKjia.c0m.gif '; Watermark Picture
$mark _text= '; Watermark text content "Not supported in Chinese"
$mark _text_size=25; Watermark Text Size
$mark _pos=5; Watermark position, 0 is random, 1 for the top left, 2 for the top, 3 for the top, 4 for the middle, 5 for the middle, 6 for the middle, 7 for the bottom, 8 for the bottom, and 9 for the bottom right;

================================= User Configuration Area "End" =================================

mark.php Add watermark File
function Go ($url, $t = 0)
{
Exit ("");
}

function Imagewatermark ($product _img, $waterpos = 0, $waterimage = "", $watertext = "", $textfont =, $textcolor = "#ff 0000 ", $newdir =" marked ")
{
echo "Working with Picture:". $product _img. "
";
$iswaterimage = false;
$formatmsg = "Does not support this file format, please use the picture processing software to convert the picture to GIF, JPG, 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 a watermark 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 picture that needs to be watermarked is more than a watermark". $label. " Still small, unable to generate watermark! ";
}
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 ("Watermark text color is not properly formatted! " );
}
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 ($dir 2. ") /". $dir 1))
{
mkdir ($dir 2. " /". $dir 1, 511);
}
$ti = 0;
$dir = Opendir ($dir 1);
while ($file = Readdir ($dir))
{
if ($file = = "." | | $file = = "..")
{
}
Else
{
+ + $ti;
if ($ti < ($id-1) * $per)
{
}
Else
{
if (($id-1) * $per <= $ti && $ti < $id * $per)
{
Imagewatermark ($dir 1. " /". $file, $mark _pos, $mark _img, $mark _text, $mark _text_size," #ff0000 ", $dir 2);
}
if ($id * $per <= $ti)
{
Closedir ($dir);
Go ("mark.php?id=". ($id + 1));
}
}
}
}

echo "

http://www.bkjia.com/phpjc/633044.html www.bkjia.com True http://www.bkjia.com/phpjc/633044.html techarticle php Batch to image watermark code we often encounter to a large number of images to increase the watermark, this will touch the volume increase image watermark function, we provide you with a ...

  • 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.