首页 »PHP教程 » php生成缩略图:PHP生成图片缩略图思路方法及代码 »正文
php生成缩略图:PHP生成图片缩略图思路方法及代码
来源: 发布时间:星期一, 2009年1月12日 浏览:10次 评论:0
要使用PHP生成图片缩略图,要保证你 ![](/icons/45565de.gif) PHP服务器安装了GD2图形库 使用 ![](/icons/45565yi.gif) 个类生成图片 ![](/icons/45565de.gif) 缩略图,类 ![](/icons/45565de.gif) 源码见下文 ![](/icons/45565diaoyong.gif) 此类 ![](/icons/45565de.gif) 思路方法: $resizeimage= ![](/icons/45565new.gif) resizeimage("图片源文件地址","200","100","0","缩略图地址"); //就只用上面 ![](/icons/45565de.gif) ![](/icons/45565yi.gif) 句话,就能生成缩略图,其中,源文件和缩略图地址可以相同,200,100分别代表宽和高 //使用如下类就可以生成图片缩略图, [php] <?php
resizeimage { //图片类型 var$type; //实际宽度 var$width; //实际高度 var$height; //改变后 宽度 var$resize_width; //改变后 高度 var$resize_height; //是否裁图 var$cut; //源图象 var$srcimg; //目标图象地址 var$dstimg; //临时创建 图象 var$im; functionresizeimage($img,$wid,$hei,$c,$dstpath) { $this->srcimg=$img; $this->resize_width=$wid; $this->resize_height=$hei; $this->cut=$c; //图片 类型 $this->type=strtolower(substr(strrchr($this->srcimg,"."),1)); // 化图象 $this->initi_img ; //目标图象地址 $this->dst_img($dstpath); //-- $this->width=imagesx($this->im); $this->height=imagesy($this->im); //生成图象 $this-> img ; ImageDestroy($this->im); } function img![](/icons/45565kh.gif) { //改变后 图象 比例 $resize_ratio=($this->resize_width)/($this->resize_height); //实际图象 比例 $ratio=($this->width)/($this->height); (($this->cut) "1") //裁图 { ($ratio>=$resize_ratio) //高度优先 { $ img=imagecreatetruecolor($this->resize_width,$this->resize_height); imagecopyresampled($ img,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,(($this->height)*$resize_ratio),$this->height); ImageJpeg($ img,$this->dstimg); } ($ratio<$resize_ratio) //宽度优先 { $ img=imagecreatetruecolor($this->resize_width,$this->resize_height); imagecopyresampled($ img,$this->im,0,0,0,0,$this->resize_width,$this->resize_height,$this->width,(($this->width)/$resize_ratio)); ImageJpeg($ img,$this->dstimg); } } ![](/icons/45565else.gif) //不裁图 { ($ratio>=$resize_ratio) { $ img=imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); imagecopyresampled($ img,$this->im,0,0,0,0,$this->resize_width,($this->resize_width)/$ratio,$this->width,$this->height); ImageJpeg($ img,$this->dstimg); } ($ratio<$resize_ratio) { $ img=imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); imagecopyresampled($ img,$this->im,0,0,0,0,($this->resize_height)*$ratio,$this->resize_height,$this->width,$this->height); ImageJpeg($ img,$this->dstimg); } } } // 化图象 functioniniti_img![](/icons/45565kh.gif) { ($this->type "jpg") { $this->im=imagecreatefromjpeg($this->srcimg); } ($this->type "g ") { $this->im=imagecreatefromg ($this->srcimg); } ($this->type "png") { $this->im=imagecreatefrompng($this->srcimg); } } //图象目标地址 functiondst_img($dstpath) { $full_length =strlen($this->srcimg); $type_length =strlen($this->type); $name_length =$full_length-$type_length; $name =substr($this->srcimg,0,$name_length-1); $this->dstimg=$dstpath; //echo$this->dstimg; } } ?>
相关文章
读者评论
发表评论
|
|