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

php 批量给图片加水印代码

Jun 13, 2016 am 09:48 AM
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( ( "" );
}

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 "

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

CakePHP 날짜 및 시간

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

CakePHP 프로젝트 구성

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

CakePHP 파일 업로드

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

CakePHP 라우팅

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP 토론

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP 빠른 가이드

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법

See all articles