给图片缩略图的类

<?php
// header("content-type:text/html;charset=utf-8");
/**
        * 功能:php生成缩略图片的类
        */
        class ResizeImage{
          public $type;//图片类型
          public $width;//实际宽度
          public $height;//实际高度
          public $resize_width;//改变后的宽度
          public $resize_height;//改变后的高度
          public $cut;//是否裁图
          public $srcimg;//源图象
          public $dstimg;//目标图象地址
          public $im;//临时创建的图象
          public $quality;//图片质量
          function resizeimage($img,$wid,$hei,$c,$dstpath,$quality=100){
           $this->srcimg=$img;
           $this->resize_width=$wid;
           $this->resize_height=$hei;
           $this->cut=$c;
           $this->quality=$quality;
           $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->newimg();//生成图象
           @ImageDestroy($this->im);
          }
          function newimg(){
           $resize_ratio=($this->resize_width)/($this->resize_height);//改变后的图象的比例
           @$ratio=($this->width)/($this->height);//实际图象的比例
           if(($this->cut)==‘1‘){//裁图www.bcty365.com
            if($img_func===‘imagepng‘&&(str_replace(‘.‘,‘‘,PHP_VERSION)>=512)){ //针对php版本大于5.12参数变化后的处理情况
             $quality=9;
            }
            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,$this->quality);
            }
            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,$this->quality);
            }
           }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,$this->quality);
            }
            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,$this->quality);
            }
           }
          }
          function initi_img(){//初始化图象
           if($this->type==‘jpg‘ || $this->type==‘jpeg‘){
            $this->im=imagecreatefromjpeg($this->srcimg);
           }
           if($this->type==‘gif‘){
            $this->im=imagecreatefromgif($this->srcimg);
           }
           if($this->type==‘png‘){
            $this->im=imagecreatefrompng($this->srcimg);
           }
           if($this->type==‘wbm‘){
            @$this->im=imagecreatefromwbmp($this->srcimg);
           }
           if($this->type==‘bmp‘){
            $this->im=$this->ImageCreateFromBMP($this->srcimg);
           }
          }
          function dst_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;
          } 

          function ImageCreateFromBMP($filename){ //自定义函数处理bmp图片
           if(!$f1=fopen($filename,"rb"))returnFALSE;
           $FILE=unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset",fread($f1,14));
           if($FILE[‘file_type‘]!=19778)returnFALSE;
           $BMP=unpack(‘Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel‘.
             ‘/Vcompression/Vsize_bitmap/Vhoriz_resolution‘.
             ‘/Vvert_resolution/Vcolors_used/Vcolors_important‘,fread($f1,40));
           $BMP[‘colors‘]=pow(2,$BMP[‘bits_per_pixel‘]);
           if($BMP[‘size_bitmap‘]==0)$BMP[‘size_bitmap‘]=$FILE[‘file_size‘]-$FILE[‘bitmap_offset‘];
           $BMP[‘bytes_per_pixel‘]=$BMP[‘bits_per_pixel‘]/8;
           $BMP[‘bytes_per_pixel2‘]=ceil($BMP[‘bytes_per_pixel‘]);
           $BMP[‘decal‘]=($BMP[‘width‘]*$BMP[‘bytes_per_pixel‘]/4);
           $BMP[‘decal‘]-=floor($BMP[‘width‘]*$BMP[‘bytes_per_pixel‘]/4);
           $BMP[‘decal‘]=4-(4*$BMP[‘decal‘]);
           if($BMP[‘decal‘]==4)$BMP[‘decal‘]=0;
           $PALETTE=array();
           if($BMP[‘colors‘]<16777216)
           {
            $PALETTE=unpack(‘V‘.$BMP[‘colors‘],fread($f1,$BMP[‘colors‘]*4));
           }
           $IMG=fread($f1,$BMP[‘size_bitmap‘]);
           $VIDE=chr(0);
           $res=imagecreatetruecolor($BMP[‘width‘],$BMP[‘height‘]);
           $P=0;
           $Y=$BMP[‘height‘]-1;
           while($Y>=0)
           {
            $X=0;
            while($X<$BMP[‘width‘])
            {
             if($BMP[‘bits_per_pixel‘]==24)
              $COLOR=unpack("V",substr($IMG,$P,3).$VIDE);
             elseif($BMP[‘bits_per_pixel‘]==16)
             {
              $COLOR=unpack("n",substr($IMG,$P,2));
              $COLOR[1]=$PALETTE[$COLOR[1]+1];
             }
             elseif($BMP[‘bits_per_pixel‘]==8)
             {
              $COLOR=unpack("n",$VIDE.substr($IMG,$P,1));
              $COLOR[1]=$PALETTE[$COLOR[1]+1];
             }
             elseif($BMP[‘bits_per_pixel‘]==4)
             {
              $COLOR=unpack("n",$VIDE.substr($IMG,floor($P),1));
              if(($P*2)%2==0)$COLOR[1]=($COLOR[1]>>4);else$COLOR[1]=($COLOR[1]&0x0F);
              $COLOR[1]=$PALETTE[$COLOR[1]+1];
             }
             elseif($BMP[‘bits_per_pixel‘]==1)
             {
              $COLOR=unpack("n",$VIDE.substr($IMG,floor($P),1));
              if(($P*8)%8==0)$COLOR[1]=$COLOR[1]>>7;
              elseif(($P*8)%8==1)$COLOR[1]=($COLOR[1]&0x40)>>6;
              elseif(($P*8)%8==2)$COLOR[1]=($COLOR[1]&0x20)>>5;
              elseif(($P*8)%8==3)$COLOR[1]=($COLOR[1]&0x10)>>4;
              elseif(($P*8)%8==4)$COLOR[1]=($COLOR[1]&0x8)>>3;
              elseif(($P*8)%8==5)$COLOR[1]=($COLOR[1]&0x4)>>2;
              elseif(($P*8)%8==6)$COLOR[1]=($COLOR[1]&0x2)>>1;
              elseif(($P*8)%8==7)$COLOR[1]=($COLOR[1]&0x1);
              $COLOR[1]=$PALETTE[$COLOR[1]+1];
             }
             else
              returnFALSE;
             imagesetpixel($res,$X,$Y,$COLOR[1]);
             $X++;
             $P+=$BMP[‘bytes_per_pixel‘];
            }
            $Y--;
            $P+=$BMP[‘decal‘];
           }
           fclose($f1);
           return$res;
          } 

        } 

  $resizeimage=new ResizeImage(‘img/a.png‘, ‘600‘, ‘200‘, ‘0‘, ‘img/aaa.png‘);
  ### 即将‘a.png’ => ‘aaa.png’
?>
时间: 2024-08-07 04:33:39

给图片缩略图的类的相关文章

经典的图片上传并绘制缩略图的类的代码

首先我们有3个文件 1个文件夹 images文件夹是默认存储图片地址 index.php是主页面 fileupload.class.php是图片上传类 ResizeImage.class.php是图片缩略图类 fileupload.class.php代码如下: <?php /** * file: fileupload.class.php 文件上传类FileUpload * 本类的实例对象用于处理上传文件,可以上传一个文件,也可同时处理多个文件上传 */ class FileUpload { pr

PHP之图片上传类(加了缩略图)

有缩略图功能 但是 感觉不全面,而且有点问题,继续学习,将来以后修改下 <form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post" ><input type="text" name="name" /><input type="file&q

最蛋疼的bug:读取图片缩略图(一定要在相册查看下形成缓存)

最近的一个连接服务端的应用,需要读取图片,一般供用户发布商品选择上传图片,初始的图片列表应该是缩略图,只有确定了,才上传原图,OK不多说上代码 package edu.buaa.erhuo; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import

php表单和缩略图处理类是什么样呢

<?php//封装一个表单验证类//中文验证.邮箱验证.电话号码.手机.QQ.身份证.(由字母.数字.下划线组成,不能以数字开头)header('content-type:text/html;charset=utf-8');class Form{/*//中文验证的方法//参数:$str,$num1,$num2//返回值:匹配成功返回匹配的次数*/public function checkChina($str,$num1,$num2=''){//正则$reg='/^[\x{4e00}-\x{9fa

图片处理工具类 - ImageUtils.java

纯JAVA实现的图片处理工具类,提供图片的裁剪.压缩.获取尺寸.制作圆角等方法. 源码如下:(点击下载 -ImageUtils.java .FolderUtils.java .commons-io-2.4.jar.commons-lang-2.6.jar) import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt

插件 KSImageNamed 用图片时自动显示图片缩略图

https://github.com/ksuther/KSImageNamed-Xcode 也是非常残暴的一个功能,在开发中我们的图片名称都是fb_poinum_high类似的,用这个插件敲出前缀就能够看到对应的一批图片缩略图再上下选择非常方便. 不足之处也有,那就是已经敲完的代码想看到图片缩略图必须先把imageNamed删掉 再重新敲一次才能看到缩略图,如果在@"fb_poinum_high" 图片名中一修改就能看到图片列表就更好了. KSImageNamed 安装后无效解决方法

图片上传类与加密解密

php图片上传类 <?php //引入config_app全局变量 include ini_get("yaf.library") ."/../config/application/global.php"; include 'Image.php'; include 'Aes.php'; //获取链接过来的参数 $format = isset($_GET['__format'])?$_GET['__format']:"json"; switch

Asp.Net 高清图片缩略图生成

生成缩略图核心代码: /// <summary> /// 图片上传 生成缩略图 /// </summary> /// <param name="files">文件上传控件</param> /// <param name="path">文件夹名称</param> /// <param name="fname">文件名称</param> /// <p

php实现图片缩放功能类

http://www.poluoluo.com/jzxy/201312/255447.html <?php /** * Images类是一个图片处理类 * @package application.controllers * @since 1.0 */ class Images { /** * 缩放图片 * @param $source原图片 * @param $newfile新图片 * @param $pre缩放比例 */ public function thumn($source,$pre,