代码如下 | 复制代码 |
class resizeimage { //图片类型 var $type; //实际宽度 var $width; //实际高度 var $height; //改变后的宽度 var $resize_width; //改变后的高度 var $resize_height; //是否裁图 var $cut; //源图象 var $srcimg; //目标图象地址 var $dstimg; //圆角源 var $corner; var $im; function resizeimage($img, $corner, $wid, $hei,$c, $corner_radius, $angle) { $this->srcimg = $img; $this->corner = $corner; $this->resize_width = $wid; $this->resize_height = $hei; $this->cut = $c; $this->corner_radius = $corner_radius; $this->angle = $angle; //图片的类型 $this->type = substr(strrchr($this->srcimg,"."),1); //初始化图象 $this->initi_img(); //目标图象地址 $this -> dst_img(); //-- $this->width = imagesx($this->im); $this->height = imagesy($this->im); //生成图象 $this->newimg(); imagedestroy ($this->im); } function newimg() { //改变后的图象的比例 $resize_ratio = ($this->resize_width)/($this->resize_height); //实际图象的比例 $ratio = ($this->width)/($this->height); if(($this->cut)=="1") //裁图 { if($ratio>=$resize_ratio) //高度优先 { $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height); $tmp = $this->rounded_corner($newimg,$this->resize_width); imagepng ($tmp,$this->dstimg); } if($ratio<$resize_ratio) //宽度优先 { $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio)); $tmp = $this->rounded_corner($newimg); imagepng ($tmp,$this->dstimg); } } else |
在很多地方我们都会看到把ip地址生成在图片哦,今天我们就来告诉你如何利用php 把ip生成在图片上哦,好了下面我们就来看看实例吧。
代码如下 | 复制代码 |
$img = imagecreate(180,50); $ip = $_server['remote_addr']; imagecolortransparent($img,$bgcolor); $bgcolor = imagecolorallocate($img, 0x2c,0x6d,0xaf); // 背景颜色 $shadow = imagecolorallocate($img, 250,0,0); // 阴影颜色 $textcolor = imagecolorallocate($img, oxff,oxff,oxff); // 字体颜色 imagettftext($img,10,0,78,30,$shadow,"c:/windows/fonts/tahoma.ttf",$ip); //显示背景 imagettftext($img,10,0,25,28,$textcolor,"c:/windows/fonts/tahoma.ttf","your ip is".$ip); // 显示ip imagepng($img); imagecreatefrompng($img); imagedestroy($img); |
其实就利用了php生成图片功能,生成验证码也经常会用到这样的功能哦。
本款php生成验证码代码原理生成随机数-->创建图片-->随机数写进图片-->保存在session中 ,看一下流程验证码图片生成 通知浏览器将要输出PNG图片 准备好随机数发生器种子 srand((double)microtime()*1000000); 将四位整数验证码绘入图片代码如下 | 复制代码 |
/*===================== //验证码图片生成 session_start(); ?> |
代码如下 | 复制代码 |
$directory = 'gallery'; $allowed_types=array('jpg','jpeg','gif','png'); $dir_handle = @opendir($directory) or die("there is an error with your image directory!"); while ($file = readdir($dir_handle)) $title = implode('.',$file_parts); closedir($dir_handle); |
图象缩略函数
参数说明:
代码如下 | 复制代码 |
$srcfile 原图地址; $dir 新图目录 $thumbwidth 缩小图宽最大尺寸 $thumbheitht 缩小图高最大尺寸 $ratio 默认等比例缩放 为1是缩小到固定尺寸。 */ function makethumb($srcfile,$dir,$thumbwidth,$thumbheight,$ratio=0) { //判断文件是否存在 if (!file_exists($srcfile))return false; //生成新的同名文件,但目录不同 $imgname=explode('/',$srcfile); $arrcount=count($imgname); $dstfile = $dir.$imgname[$arrcount-1]; //缩略图大小 $tow = $thumbwidth; $toh = $thumbheight; if($tow < 40) $tow = 40; if($toh < 45) $toh = 45; //获取图片信息 $im =''; if($data = getimagesize($srcfile)) { if($data[2] == 1) { $make_max = 0;//gif不处理 if(function_exists("imagecreatefromgif")) { $im = imagecreatefromgif($srcfile); } } elseif($data[2] == 2) { if(function_exists("imagecreatefromjpeg")) { $im = imagecreatefromjpeg($srcfile); } } elseif($data[2] == 3) { if(function_exists("imagecreatefrompng")) { $im = imagecreatefrompng($srcfile); } } } if(!$im) return ''; $srcw = imagesx($im); $srch = imagesy($im); $towh = $tow/$toh; $srcwh = $srcw/$srch; if($towh <= $srcwh){ $ftow = $tow; $ftoh = $ftow*($srch/$srcw); } else { $ftoh = $toh; $ftow = $ftoh*($srcw/$srch); } if($ratio){ $ftow = $tow; $ftoh = $toh; } //缩小图片 if($srcw > $tow || $srch > $toh || $ratio) { if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh)) { imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch); } elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh)) { imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch); } else { return ''; } if(function_exists('imagejpeg')) { imagejpeg($ni, $dstfile); } elseif(function_exists('imagepng')) { imagepng($ni, $dstfile); } }else { //小于尺寸直接复制 copy($srcfile,$dstfile); } imagedestroy($im); if(!file_exists($dstfile)) { return ''; } else { return $dstfile; } } |
?>