php图片处理
一、创建图片资源
imagecreatetruecolor(width,height)
imagecreatefromgif(图片名称);
imagecreatefromgif(图片名称);
imagecreatefromgif(图片名称);
画出各种图形(圆形、矩形、线段、文字)
imagegif(,图片位置);//此时是保存图片,不输出到浏览器
imagepng(,);
imagejpeg(,);
imagedestroy(图片资源)
二、获取图片的属性
imagesx(res)
imagesy(res)
getimagesize(图片名称);//返回数组,0==width 1==height 2==type
三、透明处理
png jpeg透明色都正常,只有gif不正常
imagecolortransparent();
imagecolorstotal();
imagecolorsforindex();
例如:
<?php
function thumn($background,$width,$height,$newfile){
list($s_w,$s_h)=getimagesize($background);
if($width && $s_w<$s_h){
$width=$s_w/$s_h*$height;
}else{
$height=$s_h/$s_w*$width;
}
$new=imagecreatetruecolor($width,$height);
$img=imagecreatefromgif($background);
$otsc=imagecolortransparent($img);
if($otsc>=0 && $otsc<imagecolorstotal($img)){
$tran=imagecolorsforindex($img,$otsc);
$newt=imagecolorallocate($new,$tran["red"],$tran["green"],$tran["blue"]);
imagefill($new,0,0,$newt);
imagecolortransparent($new,$newt);
}
imagecopyresized($new,$img,0,0,0,0,$width,$height,$s_w,$s_h);
imagegif($new,$newfile);
imagedestroy($new);
imagedestroy($img);
}
thumn("1.gif",200,200,"demo4.gif");
?>
四、图片裁剪
imagecopyresized()
imagecopyresampled()
<?php
function cut($background,$cut_x,$cut_y,$cut_width,$cut_height,$location){
$back=imagecreatefromgif($background);
$new=imagecreatetruecolor($cut_width,$cut_height);
imagecopyresampled($new,$back,0,0,$cut_x,$cut_y,$cut_width,$cut_height,$cut_width,$cut_width);
imagegif($new,$location);
imagedestroy($new);
imagedestroy($back);
}
cut("./demo.gif",10,20,100,100,"./demo7.gif");
?>
五、加水印(文字、图片)
<?php
function mark_text($background,$test,$x,$y){
$back=imagecreatefromgif($background);
$color=imagecolorallocate($back,0,255,0);
//iconv("utf-8","gb2312",$text); 下面的函数只能输出utf-8字体,如果编辑器不是的话,需要使用此函数
imagettftext($back,20,0,$x,$y,$color,"simkai.ttf",$test);
imagegif($back,"./demo33.gif");
imagedestroy($back);
}
mark_text("./demo.gif","细说PHP",100,150);
function mark_pic($background,$waterpic,$x,$y){
$back=imagecreatefromgif($background);
$water=imagecreatefromgif($waterpic);
$w_w=imagesx($water);
$w_h=imagesy($water);
imagecopy($back,$water,$x,$y,0,0,$w_w,$w_h);
imagegif($back,"demo32.gif");
imagedestroy($back);
}
mark_pic("./2.gif","./demo33.gif",0,0);
?>
六、图片旋转
imagerotate--用给定角度旋转图像
<?php
$back=imagecreatefromgif("./demo.gif");
//$color=imagecolorallocate($back,255,20,10);通过自己理解,此变量可放在下面那个函数当作第三个参数用
$new=imagerotate($back,25,0);
imagegif($new,"./demo9.gif");
?>
七、图片翻转
沿X轴
沿Y轴
<?php
function turn_y($background,$newfile){
$back=imagecreatefromgif($background);
$width=imagesx($back);
$height=imagesy($back);
$new=imagecreatetruecolor($width,$height);
for($x=0;$x<$width;$x++){
imagecopy($new,$back,$width-$x-1,0,$x,0,1,$height);
}
imagegif($new,$newfile);
imagedestroy($new);
imagedestroy($back);
}
function turn_x($background,$newfile){
$back=imagecreatefromgif($background);
$width=imagesx($back);
$height=imagesy($back);
$new=imagecreatetruecolor($width,$height);
for($y=0;$y<$height;$y++){
imagecopy($new,$back,0,$height-$y-1,0,$y,$width,1);
}
imagegif($new,$newfile);
imagedestroy($new);
imagedestroy($back);
}
turn_y("./demo.gif","./demo10.gif");
turn_x("./demo.gif","./demo20.gif");
?>
八、图片锐化
<?php
function sharp($background,$degree,$save){
$back=imagecreatefromgif($background);
$b_x=imagesx($back);
$b_y=imagesy($back);
$dst=imagecreatefromgif($background);
for($i=0;$i<$b_x;$i++){
for($j=0;$j<$b_y;$j++){
$b_clr1=imagecolorsforindex($back,imagecolorat($back,$i-1,$j-1));
$b_clr2=imagecolorsforindex($back,imagecolorat($back,$i,$j));
$r=intval($b_clr2["red"]+$degree*($b_clr2["red"]-$b_clr1["red"]));
$g=intval($b_clr2["green"]+$degree*($b_clr2["green"]-$b_clr1["green"]));
$b=intval($b_clr2["blue"]+$degree*($b_clr2["blue"]-$b_clr1["blue"]));
$r=min(255,max($r,0));
$g=min(255,max($g,0));
$b=min(255,max($b,0));
if(($d_clr=imagecolorexact($dst,$r,$g,$b))==-1){
$d_clr=imagecolorallocate($dst,$r,$g,$b);
}
imagesetpixel($dst,$i,$j,$d_clr);
}
}
imagejpeg($dst,$save);
imagedestroy($back);
imagedestroy($dst);
}
sharp("./demo.gif",0.5,"./rh.gif");
?>

浙公网安备 33010602011771号