-
缩略图生成类 追忆
- <?
- class resizeimage
- {
- //图片类型
- var $type;
- //实际宽度
- var $width;
- //实际高度
- var $height;
- //改变后的宽度
- var $resize_width;
- //改变后的高度
- var $resize_height;
- //是否裁图
- var $cut;
- //源图象
- var $srcimg;
- //目标图象地址
- var $dstimg;
- //临时建的图象
- var $im;
- //生成的文件名后缀
- var $extstr;
- function resizeimage($img, $wid, $hei,$extstr,$c=0)
- {
- $this->srcimg = $img;
- $this->resize_width = $wid;
- $this->resize_height = $hei;
- $this->cut = $c;
- $this->extstr = $extstr;
- //图片的类型
- $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);
- //实际图象的比例
- if($this->height>0)
- $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);
- @ImageJpeg($newimg,$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));
- @ImageJpeg($newimg,$this->dstimg);
- }
- }
- else
- //不裁图
- {
- if($ratio>=$resize_ratio)
- {
- $newimg =@imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
- @imagecopyresampled($newimg, $this->im,0,0,0,0, $this->resize_width,($this->resize_width)/$ratio, $this->width, $this->height);
- ImageJpeg($newimg,$this->dstimg);
- }
- if($ratio<$resize_ratio)
- {
- $newimg =@imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
- @imagecopyresampled($newimg, $this->im,0,0,0,0,($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
- @ImageJpeg($newimg,$this->dstimg);
- }
- }
- }
- //初始化图象
- function initi_img()
- {
- $type=strtolower($this->type);//转换成小写,否则不写扩展名生成不了。
- if($type=="jpg"|| $type=="jpeg"|| $type=="jpe")
- {
- $this->im =@imagecreatefromjpeg($this->srcimg);
- }
- if($type=="gif")
- {
- $this->im =@imagecreatefromgif($this->srcimg);
- }
- if($type=="png")
- {
- $this->im =@imagecreatefrompng($this->srcimg);
- }
- if($type=="bmp")
- {
- $this->im = $this->imagecreatefrombmp($this->srcimg);
- }
- }
- function imagecreatefrombmp($p_sFile){
- $file = fopen($p_sFile,"rb");
- $read = fread($file,10);
- while(!feof($file)&&($read<>""))
- $read .= fread($file,1024);
- $temp = unpack("H*",$read);
- $hex = $temp[1];
- $header = substr($hex,0,108);
- if(substr($header,0,4)=="424d"){
- $header_parts = str_split($header,2);
- $width = hexdec($header_parts[19].$header_parts[18]);
- $height = hexdec($header_parts[23].$header_parts[22]);
- unset($header_parts);
- }
- $x =0;
- $y =1;
- $image = imagecreatetruecolor($width,$height);
- $body = substr($hex,108);
- $body_size =(strlen($body)/2);
- $header_size =($width*$height);
- $usePadding =($body_size>($header_size*3)+4);
- for($i=0;$i<$body_size;$i+=3){
- if($x>=$width){
- if($usePadding)
- $i += $width%4;
- $x = 0;
- $y++;
- if($y>$height)
- break;
- }
- $i_pos = $i*2;
- $r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
- $g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
- $b = hexdec($body[$i_pos].$body[$i_pos+1]);
- $color = imagecolorallocate($image,$r,$g,$b);
- imagesetpixel($image,$x,$height-$y,$color);
- $x++;
- }
- unset($body);
- return $image;
- }
- //图象目标地址
- function dst_img()
- {
- $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 = $name.$this->extstr.'.'.$this->type;
- }
- staticfunction get_url($img,$extstr){
- $imgs = explode('.',$img);
- $ext =end($imgs);
- $full_length = strlen($img);
- $type_length = strlen($ext);
- $name_length = $full_length-$type_length;
- $name = substr($img,0,$name_length-1);
- return $name.$extstr.'.'.$ext;
- }
- }
- ?>
-
相关阅读:
学习第五天
第四天学习
学习第三天
学校键盘键位设置
学习第二天
fatal error C1902: 程序数据库管理器不匹配;请检查安装解决
ffmpeg遇到inttypes.h和UINT64_C
<ZZ>linux yum命令详解
<ZZ>Linux rpm 命令参数使用详解[介绍和应用]
转:Windows下WSH/JS实现SVN服务器钩子脚本阻止提交空日志信息和垃圾文件
-
原文地址:https://www.cnblogs.com/phpliu/p/2731133.html
Copyright © 2020-2023
润新知