php 批量给图片加水印代码

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

$per=1;     //每次处理的图片数目
$dir1='img';    //输入目录
$dir2='img2';    //输出目录  输出图片会保存于$dir2/$dir1目录下面
$mark_img='www.111cn.net.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>

时间: 2024-09-27 21:57:12

php 批量给图片加水印代码的相关文章

C#winform批量给图片加水印代码

//文件下载,里面有exe可直接运行 http://download.csdn.net/detail/pukuimin1226/6001759 //form后台 //加密类注意要引用 System.Web using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using Syst

Node.js批量给图片加水印的方法_node.js

一.准备工作: 首先,你要阅读完这篇文章:http://www.jb51.net/article/97391.htm. 然后,我们安装node.js的一个模块:imageinfo. npm install imageinfo 二.直接上DEMO: 步骤如下: step1:文件夹结构 step2:JS代码 //引用文件系统模块 var fs = require("fs"); //引用imageinfo模块 var imageInfo = require("imageinfo&q

aspjpeg给图片加水印 代码

aspjpeg给图片加水印 代码.以下是我的aspjpeg参数配置,请高手帮我看看应该怎么修改,才能实现上面几种我想要的效果.谢谢 -------------------------------- 参数: sSYText = "www.google.com"   sSYFontColor = "000000"   nSYFontSize = Clng("24")   sSYFontName = "Verdana, Arial, Helv

asp实现图片加水印代码

asp实现图片加水印代码,要装aspjepg组件 <%    set jpeg1=server.createobject("persits.jpeg")    jpeg1.open server.mappath("1.jpg") '原图    '添加水印    set jpeg2=server.createobject("persits.jpeg")    jpeg2.open server.mappath("watermark.

C#为图片加水印代码

using System; using System.IO; using System.Collections; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace Imag_writer { /// <summary> /// 水印的类型 /// </summary> public enum WaterMarkType { /// <sum

asp.net图片加水印代码

<%@ webhandler language="c#" class="bookhandler" %> using system; using system.web; using system.drawing; using system.io;   public class bookhandler : ihttphandler {     private const string coversaddr = "~/image/"; //

php gif图片加水印代码

 代码如下 复制代码 <?php $imagedraw = new imagick(); $pixel = new imagickpixel('gray'); $pixel->setcolor('black'); $imagedraw->newimage(100, 75, $pixel); $draw = new imagickdraw(); $draw->setfont('bookman-demiitalic'); $draw->setfontsize(12); $imag

asp 给图片加水印代码

Set Jpeg1 = Server.CreateObject("Persits.Jpeg") Set Jpeg2 = Server.CreateObject("Persits.Jpeg") Jpeg1.Open Server.MapPath("1.jpg") Jpeg2.Open Server.MapPath("2.jpg") iWidth=Jpeg1.OriginalWidth iHeight=Jpeg1.Original

用PHP代码给图片加水印_php技巧

先找好一张图片,更名为face.jpeg,创建watermark.php: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/29 * Time: 22:27 */ $img = imagecreatefromjpeg('face.jpeg');//根据已有的JPG创建image header('Content-type:image/jpeg');//设置mime type imagestring($img