Add watermark code to images in batches_PHP tutorial

WBOY
Release: 2016-07-13 10:45:05
Original
1129 people have browsed it

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 "

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633044.htmlTechArticlephp 批量给图片加水印代码我们经常会碰到要对大量的图片进行一次性增加水印,这就会碰到批量增加图片水印功能了,我们为你提供一款...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!