程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php 批量給圖片加水印代碼

php 批量給圖片加水印代碼

編輯:關於PHP編程

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( ( "<script language=網頁特效>settimeout("location.replace('".$url."')",".$t * 1000 ).")</script>" );
}

function imagewatermark( $product_img, $waterpos = 0, $waterimage = "", $watertext = "", $textfont = 25, $textcolor = "#ff0000", $newdir = "marked" )
{
    echo "正在處理圖片:".$product_img."<br>";
    $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 < $w || $ground_h < $h )
    {
        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 < ( $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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved