Heim >Backend-Entwicklung >PHP-Tutorial >php 批量给图片加水印代码_PHP教程

php 批量给图片加水印代码_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:45:051161Durchsuche

php 批量给图片加水印代码 我们经常会碰到要对大量的图片进行一次性增加水印,这就会碰到批量增加图片水印功能了,我们为你提供一款php批量增加水印的功能。

php教程 批量给图片加水印代码
我们经常会碰到要对大量的图片进行一次性增加水印,这就会碰到批量增加图片水印功能了,我们为你提供一款php批量增加水印的功能。

//config.php
//=================================用户配置区=================================

$per=1;     //每次处理的图片数目
$dir1='img';    //输入目录
$dir2='img2';    //输出目录  输出图片会保存于$dir2/$dir1目录下面
$mark_img='www.bKjia.c0m.gif';    //水印图片
$mark_text=''; //水印文字内容  【不支持中文】
$mark_text_size=25;   //水印文字大小
$mark_pos=5;    //水印位置,0为随机,1为顶端居左,2为顶端居中,3为顶端居右,4为中部居左,5为中部居中,6为中部居右,7为底端居左,8为底端居中,9为底端居右;

//=================================用户配置区【结束】=================================

//mark.php加水印文件
function go( $url, $t = 0 )
{
    exit( ( "" );
}

function imagewatermark( $product_img, $waterpos = 0, $waterimage = "", $watertext = "", $textfont = 25, $textcolor = "#ff0000", $newdir = "marked" )
{
    echo "正在处理图片:".$product_img."
";
    $iswaterimage = false;
    $formatmsg = "暂不支持该文件格式,请用图片处理软件将图片转换为gif、jpg、png格式。";
    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( "需要加水印的图片不存在!" );
    }
    if ( $iswaterimage )
    {
        $w = $water_w;
        $h = $water_h;
        $label = "图片的";
    }
    else
    {
        $temp = @imagettfbbox( @ceil( $textfont * 2.5 ), 0, "arial.ttf", $watertext );
        $w = $temp[2] - $temp[6];
        $h = $temp[3] - $temp[7];
        unset( $temp );
        $label = "文字区域";
    }
    if ( $ground_w     {
        echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
    }
    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 "

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633044.htmlTechArticlephp 批量给图片加水印代码我们经常会碰到要对大量的图片进行一次性增加水印,这就会碰到批量增加图片水印功能了,我们为你提供一款...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn