php 给图片增加背景平铺水印代码

 更新时间:2016年11月25日 16:57  点击:1564
如果你想利用php 给图片增加背景平铺水印效果话,必须利用php的一个插件来实例,就是利用imagick,他可以给图片增加背景平铺水印效果哦,下面我们提供一款实例代码。

imagemagic官方去除图片背景的命令行模式:

 代码如下 复制代码
convert -size 140x80 xc:none -fill grey           -gravity northwest -draw "text 10,10 'copyright'"           -gravity southeast -draw "text 5,15 'copyright'"           miff:- |    composite -tile - logo.jpg  wmark_text_tiled.jpg


imagick代码:

 代码如下 复制代码
<?php
$image = new imagick('logo.jpg');
$im = new imagick();
$im->newimage( 140, 80, new imagickpixel( "none" ) );
$draw = new imagickdraw();
$draw->setfillcolor(new imagickpixel( "grey" ));
$draw->setgravity(imagick::gravity_northwest);
$draw->annotation(10,10 ,'copyright');
$draw->setgravity(imagick::gravity_southeast);
$draw->annotation(5,15 ,'copyright');
$im->drawimage( $draw);
$image = $image->textureimage($im);
$image->compositeimage($image,imagick::composite_copy,0,0);
header( "content-type: image/{$image->getimageformat()}" );
$image->writeimage('wmark_text_tiled.jpg');
$image->clear();
$image->destroy();
?>

如果你的机型还没装php_imagick就下载吧,下载地址如下

http://pecl.php.net/package/imagick

本文章提供一款php教程中文汉字验证码生成程序,如果在图片片生成汉字,需要font文件和imagettftext函数,用到的时候大家再网上搜索吧。你要产生随机数,那有mt_rand函数;你还要用到session保存这个随机数;如果需要转成utf-8,需要iconv函数。

 <?php
class simpleimage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == imagetype_jpeg ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == imagetype_gif ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == imagetype_png ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=imagetype_jpeg, $compression=75, $permissions=null) {
if( $image_type == imagetype_jpeg ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == imagetype_gif ) {
imagegif($this->image,$filename);
} elseif( $image_type == imagetype_png ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=imagetype_jpeg) {
if( $image_type == imagetype_jpeg ) {
imagejpeg($this->image);
} elseif( $image_type == imagetype_gif ) {
imagegif($this->image);
} elseif( $image_type == imagetype_png ) {
imagepng($this->image);
}
}
function getwidth() {
return imagesx($this->image);
}
function getheight() {
return imagesy($this->image);
}
function resizetoheight($height) {
$ratio = $height / $this->getheight();
$width = $this->getwidth() * $ratio;
$this->resize($width,$height);
}
function resizetowidth($width) {
$ratio = $width / $this->getwidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getwidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getwidth(), $this->getheight());
$this->image = $new_image;
}
}

$newfile = upload_dir."/icons/".md5($_session['user']->email).".jpg";//上传文件保存的目录
$image = new simpleimage();
$image->load($_files['icons']['tmp_name']);//上传的临时文件名
$image->resizetowidth(80);设置宽度
$image->save($newfile);
?>

php gd库是不可以给gif图片加水印了,如果要利用php给gif图片增加水印的话,就得借助于一款ImageMagick功能的PHP扩展,它可以帮我们完成我们想要的功能哦。
 代码如下 复制代码
<?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);
$image=new imagick();
$animation = new imagick();
$animation->setformat( "gif" );
$image->readimage("old.gif");
$unitl = $image->getimageindex();
$image->writeimages('animation.gif',false);
$delay = $image->getimagedelay();
$filename = 'animation-';
for ($i=0; $i<$unitl; $i++) {
    $thisimage = new imagick();
    $thisimage->readimage($filename.$i.'.gif');
    $thisimage->annotateimage($draw, 0, 12, 0, 'copyright by mpeg');
    $animation->addimage($thisimage);
    $animation->setimagedelay($delay);
}       
header("content-type: image/gif");
echo $animation->getimagesblob();
?>

imagemagick功能的php扩展。使用这个扩展可以使php具备和imagemagick相同的功能。   imagemagick是一套功能强大、稳定而且免费的工具集和开发包,可以用来读、写和处理超过185种基本格式的图片文件,包括流行的tiff, jpeg, gif, png, pdf以及photocd等格式。利用imagemagick,你可以根据web应用程序的需要动态生成图片, 还可以对一个(或一组)图片进行改变大小、旋转、锐化、减色或增加特效等操作,并将操作的结果以相同格式或其它格式保存。

组件下载地址

http://pecl.php.net/package/imagick
http://www.imagemagick.org

一款php 验证码程序函数,原理是利用php gd库再生成随便数字,然后创建一个session与一张数字图片,就成了图形验证码了。
 代码如下 复制代码
function fun_code($sessionname)
{
 header("content-type:image/png");
 session_start();
 $authnum_session = '';
 //$str = 'abcdefghijkmnpqrstuvwxyz1234567890';
 $str = '1234567890';
 $l = strlen($str);
 for($i=1;$i<=4;$i++)
 {
  $num=rand(0,$l-1);
  $authnum_session.= $str[$num];
 }
 $_session[$sessionname]=$authnum_session;
 srand((double)microtime()*1000000);
 $im = imagecreate(50,20);
 $black = imagecolorallocate($im, 0,0,0);
 $white = imagecolorallocate($im, 255,255,255);
 $gray = imagecolorallocate($im, 200,200,200);
 imagefill($im,68,30,$gray);
 for($i=0;$i<3;$i++)
 {
  imageline($im,rand(0,30),rand(0,21),rand(20,40),rand(0,21),$li);
 }
 imagestring($im, 5, 8, 2, $authnum_session, $white);
 for($i=0;$i<90;$i++)
 {
  imagesetpixel($im, rand()%70 , rand()%30 , $gray);
 }
 imagepng($im);
 imagedestroy($im);
}
echo fun_code("code");
在php中要生成中文验证码就必须做与生成验证验证码不一样的操作,因为GD函数只接受UTF8格式编码的文字,所以在用php生成中文验证码时面要前首先要进行编码转换,操作php的iconv可以实例。
 代码如下 复制代码

$ch_str="你要生成中文验证码汉字";
$str=array();
for ($i=0;$i<strlen($ch_str);$i+=3)
{
    $str[]=$ch_str[$i].$ch_str[$i+1].$ch_str[$i+2];
}
//图片的长和高
$image_x=200;
$image_y=100;
$im = imagecreate($image_x,$image_y);
//这里取图片底色为白色
$bkg = imagecolorallocate($im,255,255,255);
//显示的字体样式,这个要把文件放到对应的目录中,如果你没有文件就去window的字体文件中找一个吧。
$fnt = "simfang.ttf";
//为图像分配一些颜色
$white=imagecolorallocate($im,234,185,95);
//在图片上画椭圆弧,指定下坐标点
imagearc($im, 150, 8, 20, 20, 75, 170, $white);
imagearc($im, 180, 7,50, 30, 75, 175, $white);
//在图片上画一条线段,指定下坐标点
imageline($im,20,20,180,30,$white);
imageline($im,20,18,170,50,$white);
imageline($im,25,50,80,50,$white);
//乱点的数量
$noise_num=3000;
$line_num=80;
//各种混乱字符的颜色
$rectangle_color=imagecolorallocate($im,0xaa,0xaa,0xaa);
$noise_color=imagecolorallocate($im,0x00,0x00,0x00);
$font_color=imagecolorallocate($im,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++)
{
    //在一个坐标点上画一个单一像素,这个点上面定义了,是黑色的。
    imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
}

for($i=0;$i<$line_num;$i++)
{
    $line_color=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    //在两个坐标点间画一条线,颜色在上面定义
    imageline($im,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);   
}
$randnum=rand(0,count($str)-4);
//保持是偶数
if ($randnum%2)
{
    $randnum+=1;   
}
$str1=$str[$randnum].$str[$randnum+1];
for ($i=0;$i<2;$i++)
{
    imagettftext($im, rand(28,32), rand(0,70), rand(($image_x/4)*$i+$image_x/10,($image_x/4)*$i+$image_x/8), rand($image_y/2+$image_y/10,$image_y/2+$image_y/5), $font_color, $fnt, $str[$randnum+$i]);   
}
imagepng($im);
imagedestroy($im);

//生成中文验证码二
$str="中文汉字";
$image_x=110;
$image_y=110;
$im = imagecreate($image_x,$image_y);
$bkg = imagecolorallocate($im,255,255,255);
$fnt = "hb.ttf"; //显示的字体样式
$white=imagecolorallocate($im,234,185,95);
imagearc($im, 150, 8, 20, 20, 75, 170, $white);
imagearc($im, 180, 7,50, 30, 75, 175, $white);
imageline($im,20,20,180,30,$white);
imageline($im,20,18,170,50,$white);
imageline($im,25,50,80,50,$white);
$noise_num=3000;
$line_num=80;
imagecolorallocate($im,0xff,0xff,0xff);
$rectangle_color=imagecolorallocate($im,0xaa,0xaa,0xaa);
$noise_color=imagecolorallocate($im,0x00,0x00,0x00);
$font_color=imagecolorallocate($im,0x00,0x00,0x00);
$line_color=imagecolorallocate($im,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++)
imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
for($i=0;$i<$line_num;$i++)
imageline($im,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
$randnum=rand(0,strlen($str)-4);
if ($randnum%2)$randnum+=1;
$str1=substr($str,$randnum,4);
$str2 = iconv("gb2312","utf-8",$str1);//验证汉字在$str1里面

imagettftext($im, rand(28,32), rand(0,70), rand(25,27), rand(70,100), $font_color, $fnt, $str2);
imagepng($im);
imagedestroy($im);

//把汉字放在数组
/*
gd函数只接受utf8格式编码的文字,所以在写文字前首先要进行编码转换。php自带的iconv和mbstring库都可以完成这项工作
*/

$randcode=array('宠');
$codetable=array();
$fp=fopen("gb2312.txt","r");
while($line=fgets($fp))
    $codetable[hexdec(substr($line,0,6))]=substr($line,7,6);
fclose($fp); 

//gb2312转utf8
function gb2utf8($gbstr)
{
    global $codetable;
    if(trim($gbstr)=="")
        return $gbstr;
    $ret="";
    $utf8="";
    while($gbstr)
    {
        if(ord(substr($gbstr,0,1))>127)
        {
            $thisw=substr($gbstr,0,2);
            $gbstr=substr($gbstr,2,strlen($gbstr));
            $utf8="";
            @$utf8=u2utf8(hexdec($codetable[hexdec(bin2hex($thisw))-0x8080]));

 

            if($utf8!="")
            for($i=0;$i<strlen($utf8);$i+=3)
                $ret.=chr(substr($utf8,$i,3));
        }
        else
        {
            $ret.=substr($gbstr,0,1);
            $gbstr=substr($gbstr,1,strlen($gbstr));
        }
    }
    return $ret;

 

//unicode转utf8
function u2utf8($c)
{
    $str="";
    if($c<0x80)
        $str.=$c;
    elseif($c<0x800)
    {
        $str.=(0xc0|$c>>6);
        $str.=(0x80|$c&0x3f);
    }
    elseif($c<0x10000)
    {
        $str.=(0xe0|$c>>12);
        $str.=(0x80|$c>>6&0x3f);
        $str.=(0x80|$c&0x3f);
    }
    elseif($c<0x200000)
    {
        $str.=(0xf0|$c>>18);
        $str.=(0x80|$c>>12&0x3f);

 

        $str.=(0x80|$c>>6&0x3f);
        $str.=(0x80|$c&0x3f);
    }
    return $str;

//生成附加码
function create_excode($length)
{
 global $randcode;
    header("content-type: image/png");
 $image_x=$length*30;    //图片宽度
 $image_y=40;            //图片高度
    $noise_num=80*$length;   //杂点数量
    $line_num=$length-2;      //干扰线数量
 $image=imagecreate($image_x,$image_y);
 imagecolorallocate($image,0xff,0xff,0xff);                  //设定背景颜色
 $rectangle_color=imagecolorallocate($image,0xaa,0xaa,0xaa); //边框颜色
 $noise_color=imagecolorallocate($image,0x00,0x00,0x00);     //杂点颜色
 $font_color=imagecolorallocate($image,0x00,0x00,0x00);      //字体颜色
 $line_color=imagecolorallocate($image,0x33,0x33,0x33);      //干扰线颜色

 //加入杂点
    for($i=0;$i<$noise_num;$i++)
  imagesetpixel($image,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);

 $font_face="simkai.ttf";    //字体
    $x=2;
    $session_code='';
    for($i=0;$i<$length;$i++)
    {
        $code=$randcode[mt_rand(0,count($randcode)-1)];
     imagettftext($image,18,mt_rand(-6,6),$x,29,$font_color,$font_face,gb2utf8($code));
        $x+=30;
        $session_code.=$code;
    }
    @session_start();
    $_session['excode']=$session_code;  //把附加码的值放在session中

 
    //加入干扰线
    for($i=0;$i<$line_num;$i++)
     imageline($image,mt_rand(0,$image_x),mt_rand(0,$image_y),
                    mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
 imagerectangle($image,0,0,$image_x-1,$image_y-1,$rectangle_color);  //加个边框
 imagepng($image);
 imagedestroy($image);
}
create_excode(6);

 

// 使用的时候直接用html语法:<img src="excode.php">调用就可以了,在服务端做验证时取session存储的验证字符与用户提交的字符进行比较,相同则通过验证

[!--infotagslink--]

相关文章

  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • ps把文字背景变透明的操作方法

    ps软件是现在非常受大家喜欢的一款软件,有着非常不错的使用功能。这次文章就给大家介绍下ps把文字背景变透明的操作方法,喜欢的一起来看看。 1、使用Photoshop软件...2017-07-06
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

    jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13
  • 利用JS实现点击按钮后图片自动切换的简单方法

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • Photoshop枪战电影海报图片制作教程

    Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • 使用PHP下载CSS文件中的图片的代码

    共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
  • PHP swfupload图片上传的实例代码

    PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
  • C#中图片旋转和翻转(RotateFlipType)用法分析

    这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • ps怎么制作图片阴影效果

    ps软件是现在很多人比较喜欢的,有着非常不错的使用效果,这次文章就给大家介绍下ps怎么制作图片阴影效果,还不知道制作方法的赶紧来看看。 ps图片阴影效果怎么做方法/...2017-07-06
  • OpenCV如何去除图片中的阴影的实现

    这篇文章主要介绍了OpenCV如何去除图片中的阴影的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
  • C#将图片和字节流互相转换并显示到页面上

    本文主要介绍用C#实现图片转换成字节流,字节流转换成图片,并根据图片路径返回图片的字节流,有需要的朋友可以参考下...2020-06-25
  • JavaScript 如何禁止用户保存图片

    这篇文章主要介绍了JavaScript 如何禁止用户保存图片,帮助大家完成需求,更好的理解和使用JavaScript,感兴趣的朋友可以了解下...2020-11-19
  • php上传图片学习笔记与心得

    我们在php中上传文件就必须使用#_FILE变量了,这个自动全局变量 $_FILES 从 PHP 4.1.0 版本开始被支持。在这之前,从 4.0.0 版本开始,PHP 支持 $HTTP_POST_FILES 数组。这...2016-11-25