php图像处理简单实例
imagecreatetruecolor()返回一个图像标识符代表指定大小的黑色形象。
根据你的php教程和gd版本中函数定义与否。对于php 4.0.6通过4.1.x这个函数总是存在的
,
*/
$im=imagecreatetruecolor(100,100); //创建图像
$string='n'; //定义字符
$white=imagecolorallocate($im,255,255,255); //定义白色
$black=imagecolorallocate($im,0,0,0); //定义黑色
$red=imagecolorallocate($im,255,0,0); //定义红色
//在白色的背景上输出一个黑色的"z",其实是颠倒的n
imagecharup($im,6,20,20,$string,$white);
imagechar($im,2,40,40,"r",$red); //使用红色画出字符
header('content-type: image/png'); //输出头部信息
imagepng($im); //输出png文件
imagedestroy($im); //销毁图像
//
$img=imagecreatetruecolor(400,400); //创建图像
$white=imagecolorallocate($img,255,255,255); //定义白色
$black=imagecolorallocate($img,0,0,0); //定义黑色
imagearc($img,200,200,350,350,0,360,$white); //画椭圆弧
header("content-type: image/png"); //输出头信息
imagepng($img); //输出为png图像
imagedestroy($img); //销毁图像
//
$size=300;
$image=imagecreatetruecolor($size,$size);
//用白色背景加黑色边框画个方框
$back=imagecolorallocate($image,255,255,255);
$border=imagecolorallocate($image,0,0,0);
imagefilledrectangle($image,0,0,$size-1,$size-1,$back);
imagerectangle($image,0,0,$size-1,$size-1,$border);
$yellow_x=100;
$yellow_y=75;
$red_x=120;
$red_y=165;
$blue_x=187;
$blue_y=125;
$radius=150;
//用alpha值分配一些颜色
$yellow=imagecolorallocatealpha($image,255,255,0,75);
$red=imagecolorallocatealpha($image,255,0,0,75);
$blue=imagecolorallocatealpha($image,0,0,255,75);
//画三个交迭的圆
imagefilledellips教程e($image,$yellow_x,$yellow_y,$radius,$radius,$yellow);
imagefilledellipse($image,$red_x,$red_y,$radius,$radius,$red);
imagefilledellipse($image,$blue_x,$blue_y,$radius,$radius,$blue);
//输出header文件头
header('content-type: image/png');
//最后输出结果
imagepng($image);
imagedestroy($image);
//
$im=imagecreate(100,100); //创建图像
$string='php'; //定义字符串
$bg=imagecolorallocate($im,255,255,255); //定义白色
$black=imagecolorallocate($im,0,0,0); //定义黑色
//在左上角输出指定字符
imagechar($im,1,0,0,$string,$black);
header('content-type: image/png'); //输出头部信息
imagepng($im); //输出png文件
imagedestroy($im); //销毁图像
?>
class securecode
{
private static $instance=null;
private $code = '';
private $fontfile;
private $validate;
private $image;
private $specialadd = 'special string for securecode';
private $codeexpire=86400;
private $codecookiename='secure_code';
/**
* 构造方法
*/
private function securecode()
{
$this->fontfile = dirname( __file__ ) . '/arial.ttf';
}
private function __construct()
{
$this->securecode();
}
public static function getinstance()
{
if (self::$instance==null)
self::$instance=new self();
return self::$instance;
}
/**
* 指定字体文件所在路径,默认为当前文件夹下arial.ttf文件
* @param $fontfile 文件路径
* @return void
*/
function loadfont($fontfile)
{
$this->fontfile = $fontfile;
}
/**
* 图片输出方法,在执行本方法前程序不应该有任何形式的输出
* @return void;
*/
function stroke()
{
$this->savecode();
self::sendheader();
imagegif( $this->validate );
imagedestroy( $this->validate );
imagedestroy( $this->image );
}
/**
* 图片保存方法
* @param $filename 保存路径
* @return void
*/
function save($filename)
{
$this->savecode();
imagegif( $this->validate , $filename );
imagedestroy( $this->validate );
imagedestroy( $this->image );
}
/**
* 验证码验证方法
* @param $input 要验证的字符串,即用户的输入内容
* @return boolean 验证结果
*/
function verify($input)
{
$input=strtolower($input);
$targetcode=$this->authcode($input);
$code=$this->getcookie();
if (empty($code)||$code!=$targetcode)
$result= false;
else
$result=true;
$_cookie[$this->codecookiename]='';
setcookie ( $this->codecookiename, '', - 1 );
return $result;
}
/**
* 图片创建方法
* @return void;
*/
function createimage()
{
$this->randcode();
$size = 30;
$width = 90;
$height = 35;
$degrees = array (
rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 )
);
for ($i = 0; $i < 4; ++$i)
{
if (rand() % 2);
else $degrees[$i] = -$degrees[$i];
}
$this->image = imagecreatetruecolor( $size , $size );
$this->validate = imagecreatetruecolor( $width , $height );
$back = imagecolorallocate( $this->image , 255 , 255 , 255 );
$border = imagecolorallocate( $this->image , 0 , 0 , 0 );
imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back );
for ($i = 0; $i < 4; ++$i)
{
$temp = self::rgbtohsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) );
if ($temp[2] > 60) $temp[2] = 60;
$temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
$textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
}
for ($i = 0; $i < 200; ++$i)
{
$randpixelcolor = imagecolorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) );
imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor );
}
$temp = self::rgbtohsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) );
if ($temp[2] < 200) $temp[2] = 255;
$temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
$randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] );
imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
putenv( 'gdfontpath=' . realpath( '.' ) );
// name the font to be used (note the lack of the .ttf extension
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontfile , $this->code[0] );
$this->image = imagerotate( $this->image , $degrees[0] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontfile , $this->code[1] );
$this->image = imagerotate( $this->image , $degrees[1] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontfile , $this->code[2] );
$this->image = imagerotate( $this->image , $degrees[2] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontfile , $this->code[3] );
$this->image = imagerotate( $this->image , $degrees[3] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border );
}
/**
* 获取随机生成的验证码
* @return string 随机验证码,返回的验证码不进行任何处理
*/
function getcode()
{
return $this->code;
}
/**
* 生成随机码方法
* @return void;
*/
protected function randcode()
{
$alphastr = 'abcdefghijklmnpqrstuvwxyz123456789';
$randstr = array (
$alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}
);
$this->code = strtolower( $randstr[0] . $randstr[1] . $randstr[2] . $randstr[3] );
}
/**
* rgb色到hsv色转变方法
* @param $r
* @param $g
* @param $b
* @return array hsv数组
*/
protected static function rgbtohsv($r, $g, $b)
{
$tmp = min( $r , $g );
$min = min( $tmp , $b );
$tmp = max( $r , $g );
$max = max( $tmp , $b );
$v = $max;
$delta = $max - $min;
if ($max != 0) $s = $delta / $max; // s
else
{
$s = 0;
//$h = undefinedcolor;
return;
}
if ($r == $max) $h = ($g - $b) / $delta; // between yellow & magenta
else if ($g == $max) $h = 2 + ($b - $r) / $delta; // between cyan & yellow
else $h = 4 + ($r - $g) / $delta; // between magenta & cyan
$h *= 60; // degrees
if ($h < 0) $h += 360;
return array (
$h, $s, $v
);
}
/**
* 同上一方法功能相反
* @param $h
* @param $s
* @param $v
* @return array rgb数组
*/
protected static function hsvtorgb($h, $s, $v)
{
if ($s == 0)
{
// achromatic (grey)
$r = $g = $b = $v;
return;
}
$h /= 60; // sector 0 to 5
$i = floor( $h );
$f = $h - $i; // factorial part of h
$p = $v * (1 - $s);
$q = $v * (1 - $s * $f);
$t = $v * (1 - $s * (1 - $f));
switch ($i)
{
case 0 :
$r = $v;
$g = $t;
$b = $p;
break;
case 1 :
$r = $q;
$g = $v;
$b = $p;
break;
case 2 :
$r = $p;
$g = $v;
$b = $t;
break;
case 3 :
$r = $p;
$g = $q;
$b = $v;
break;
case 4 :
$r = $t;
$g = $p;
$b = $v;
break;
default : // case 5:
$r = $v;
$g = $p;
$b = $q;
break;
}
return array (
$r, $g, $b
);
}
/**
* 使用cookie保存验证码的方法
* @return void
*/
protected function savecode()
{
$code = $this->authcode($this->code);
$this->setcookie($code);
}
/**
* 验证码cookie值获取方法
* @return string cookie值
*/
protected function getcookie()
{
if (empty( $_cookie[$this->codecookiename] ))
{
return '';
}
else
{
return addslashes($_cookie[$this->codecookiename]);
}
}
/**
* 验证码cookie创建方法
* @param string $code 要保存的验证码
* @return void
*/
protected function setcookie($code)
{
$expire = $this->codeexpire > 0 ? $this->codeexpire + time() : 0;
setcookie( $this->codecookiename , $code, $expire );
}
/**
* 验证码加密方法
* @param string $code 要加密的随机码
* @return mixed 执行结果
*/
protected function authcode($code)
{
return md5($code.$this->specialadd);
}
/**
* 干扰线生成方法
* @param resource $image 图片资源句柄
* @param string $color 干扰线颜色
*/
protected static function imagelinethick($image, $color)
{
$k = rand( 5 , 20 );
for ($px = 0; $px < 400; $px = $px + 1)
{
$y = $k * sin( 0.1 * ($px) ); //$y=200+10*sin(0.1*($px-200));
for ($i = 0; $i < 2; $i++)
{
imagesetpixel( $image , $px , $y + 10 + $i , $color );
}
}
}
/**
* http标头设置方法
* @return void
*/
protected static function sendheader()
{
header( "pragma: no-cache" );
header( "cache-control: max-age=1, s-maxage=1, no-cache, must-revalidate" );
header( 'content-type: image/gif' );
}
}
http://down.111cn.net/down/code/php/qitayuanma/2010/1220/22330.html
我利用了getimagesize来获取原图片的大小然后再x0.5就是把图片/5哦。
array getimagesize ( string $filename [, array &$imageinfo ] )
getimagesize() 函数将测定任何 gif,jpg,png,swf,swc,ps教程d,tiff,bmp,iff,jp2,jpx,jb2,jpc,xbm 或 wbmp 图像文件的大小并返回图像的尺寸以及文件类型和一个可以用于普通 html 文件中 <img> 标记中的 height/width 文本字符串。
如果不能访问 filename 指定的图像或者其不是有效的图像,getimagesize() 将返回 false 并产生一条 e_warning 级的错误
*/
//定义一个文件
$filename='1.jpg';
$percent=0.5;
//发送头部文件
header('content-type: image/jpeg');
//获取图像的大小
list($width,$height)=getimagesize($filename);
//定义新的大小
$new_width=$width * $percent;
$new_height=$height * $percent;
$image_p=imagecreatetruecolor($new_width, $new_height);
/*
: int imagecreate(int x_size, int y_size);
返回值: 整数
内容说明
本函数用来建立一张全空的图形。参数 x_size、y_size 为图形的尺寸,单位为像素 (pixel)。
*/
$image=imagecreatefromjpeg($filename);
/*
resource imagecreatefromjpeg ( string filename ) imagecreatefromjpeg() 返回一图像标识符,代表了从给定的文件名取得的图像。 imagecreatefromjpeg() 在失败时返回一个空字符串,并且输出一条错误信息,不幸地在浏览器中显示为断链接。为减轻调试工作下面的例子会产生一个错误 jpeg:
*/
imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);
/*
):imagecopyresamples() ,其像素插值算法得到的图像边缘比较平滑.质量较好(但该函数的速度比 imagecopyresized() 慢). 两个函数的参数是一样的.如下: imagecopyresampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
*/
//输出图像
imagejpeg($image_p, null, 100);
/*
执行该代码,将把原图像缩放50%,并以新图像输出
*/
在php教程如果想利用它图片处理函数就必须在php.ini里面的gd库开启哦,
*/
//发送头文件
header("content-type: image/png");
//创建图像,如果失败输出内容
$im=@imagecreate(150,50) or die("cannot initialize new gd image stream");
//定义背景颜色
$background_color=imagecolorallocate($im,255,255,255);
//定义文字颜色
$text_color=imagecolorallocate($im,233,14,91);
//在图像上画出文件
imagestring($im,3,5,5,"hello world",$text_color);
//输出图像文件
imagepng($im);
//销毁图像
imagedestroy($im);
/*
该代码的执行结果如图22.5所示:
*/
// 2图片等比例缩小
//定义一个文件
$filename='1.jpg';
//定义缩放百分比
$percent=0.5;
//输出头文件
header('content-type: image/jpeg');
//获取新的大小
list($width,$height)=getimagesize($filename);
$newwidth=$width * $percent;
$newheight=$height * $percent;
//创建图形区域,并载入图像
$thumb=imagecreatetruecolor($newwidth,$newheight);
$source=imagecreatefromjpeg($filename);
//重新调整大小
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//输出图像
imagejpeg($thumb);
/*
执行该代码,将把原图像缩放50%,并以新图像输出
*/
//在图片上写文字
//定义内容
$data='ivborw0kggoaaaansuheugaaabwaaaascamaaab/2u7waaaabl'.
'bmveuaaad///+l2z/daaaasuleqvr4xqwquqoaiaxc2/0vxzdr'.
'ex4ijtrkb7lobnustxsb0jixiamssqnwlsv+wulf4avk9flq2r'.
'8a5hse35q3eo2xp1a1wqkzsgetvdtkdqaaaabjru5erkjggg==';
//对内容进行base64编码
$data=base64_decode($data);
//根据字符串新建图像
$im=imagecreatefromstring($data);
if($im!== false)
{
//如果成功创建,则输出图像
header('content-type: image/png');
imagepng($im);
}
else
{
//如果创建失败,则输出内容
echo 'an error occured.';
}
/*
该代码的执行结果如图:22.4所示:
*/
//在图片上写文字
header("content-type: image/png");
//创建图像,如果失败输出内容
$im=@imagecreate(100,50) or die("cannot initialize new gd image stream");
//定义背景颜色
$background_color=imagecolorallocate($im,255,255,255);
//定义文字颜色
$text_color=imagecolorallocate($im,233,14,91);
//在图像上画出文件
imagestring($im,1,5,5,"a simple text string",$text_color);
//输出图像文件
imagepng($im);
//销毁图像
imagedestroy($im);
/*
执行该代码将生成一个jpeg图像。
并输出指定字符串
*/
在php在要生成高清的图片必须用 imagecreatetruecolor函数来做,下面看它的用法 imagecreatetruecolor(int x,int y)建立的是一幅大小为 和 y的黑色图像,它所举的例子并没用给生成的像素添加背景颜色,而是直接用imagecolorallocate()建立了一个画图的颜色
//创建图像
$im=imagecreatetruecolor(100,100);
//将背景设为红色
$red=imagecolorallocate($im,255,0,0);
imagefill($im,0,0,$red);
//输出图像
header('content-type: image/png');
imagepng($im);
imagedestroy($im);
/*
执行该代码,将生成背景为红色的图形。
*/
//代码二
//创建真彩色图像
$img=imagecreatetruecolor(400,400);
//通过循环执行操作
for($i=10;$i<=350;$i=$i+20)
{
//定义颜色
$color=imagecolorallocate($img,200,50,$i);
//画出椭圆
imageellips教程e($img,200,200,350,$i,$color);
}
//输出图像
header("content-type: image/png");
imagepng($img);
//销毁图像
imagedestroy($img);
/*
该代码的执行结果如图:22.7所示:
*/
//代码三
//创建真彩色图像
$img=imagecreatetruecolor(200,200);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$blue=imagecolorallocate($img,0,0,255);
//在图像上画图
imagearc($img,100,100,50,150,360,0,$red);
imagearc($img,100,100,150,50,0,360,$blue);
//输出图像
header("content-type: image/png");
imagepng($img);
//销毁图像
imagedestroy($img);
/*
该代码的的执行结果如图22.6所示:
*/
//实例四
//发送头文件
header("content-type: image/png");
//创建图像,如果失败输出内容
$im=imagecreatetruecolor(500,500); //创建图像
//定义背景颜色
$black=imagecolorallocate($im,0,0,0);
//定义线颜色
$color=imagecolorallocate($im,0,255,255);
//在图像上画出虚线
imageline($im,1,1,450,450,$color);
//输出图像文件
imagepng($im);
//销毁图像
imagedestroy($im);
相关文章
- 这篇文章主要介绍了Windows批量搜索并复制/剪切文件的批处理程序实例,需要的朋友可以参考下...2020-06-30
BAT批处理判断服务是否正常运行的方法(批处理命令综合应用)
批处理就是对某对象进行批量的处理,通常被认为是一种简化的脚本语言,它应用于DOS和Windows系统中。这篇文章主要介绍了BAT批处理判断服务是否正常运行(批处理命令综合应用),需要的朋友可以参考下...2020-06-30- file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
- 这篇文章主要介绍了C#多线程中的异常处理操作,涉及C#多线程及异常的捕获、处理等相关操作技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了postgresql 中的时间处理小技巧(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
- 这篇文章主要介绍了Python同时处理多个异常的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-29
C#异常处理中try和catch语句及finally语句的用法示例
这篇文章主要介绍了C#异常处理中try和catch语句及finally语句的用法示例,finally语句的使用涉及到了C#的垃圾回收特性,需要的朋友可以参考下...2020-06-25- 今天小编就为大家分享一篇python 实现将Numpy数组保存为图像,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
- 这篇文章主要介绍了python如何用moviepy对视频进行简单的处理,帮助大家更好的利用python处理视频,感兴趣的朋友可以了解下...2021-03-11
- 这篇文章主要介绍了C#图像亮度调整的方法,涉及C#操作图像亮度的相关技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章介绍了C#异常处理,有需要的朋友可以参考一下...2020-06-25
- 这篇文章主要介绍了C#图像透明度调整的方法,涉及C#操作图像透明度的相关技巧,需要的朋友可以参考下...2020-06-25
- 这篇文章主要给大家介绍了关于sql server日志处理不当造成的隐患的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用sql server具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-07-11
- 这篇文章主要介绍了go语言中的Carbon库时间处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-05
- Spring MVC是Spring系列框架中使用频率最高的部分。不管是Spring Boot还是传统的Spring项目,只要是Web项目都会使用到Spring MVC部分。因此程序员一定要熟练掌握MVC部分。本篇博客简要分析Spring MVC处理一个请求的流程。...2021-02-06
- 今天小编就为大家分享一篇Python-numpy实现灰度图像的分块和合并方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
- 这篇文章主要为大家详细介绍了python opencv实现图像配准与比较,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
- C++ 提供了异常机制,让我们能够捕获运行时错误,本文就详细的介绍了C++异常处理入门,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09
- 这篇文章主要介绍了使用opencv识别图像红色区域,并输出红色区域中心点坐标,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-03
C#事务处理(Execute Transaction)实例解析
这篇文章主要介绍了C#事务处理(Execute Transaction)实例解析,对于理解和学习事务处理有一定的帮助,需要的朋友可以参考下...2020-06-25