class ThumbImages{
/**
* 生成缩略图
* prorate 按比例缩放
* distortion 扭曲型缩图
* cut 最小裁剪后的缩图
* backFill 背景填充图
* clssname ThumbImages
* createtime:2014-1-14下午04:44:28
* author:lhm
* for example
* include 'ThumbImages.class.php'
* $th=new ThumbImages(array('imagePre'=>'',
* 'imagePath'=>'./uploads/thumb/',
* 'echoType'=>'file'));
* $th->setThumb($srcImage, 384, 300, 'prorate');
*/
private $imagepre; //缩略图名称前缀
private $imagepath; //缩略图存放路径
private $srcImage=''; //源图路径
private $newImageName; //缩略图名称
private $imageType; //图片类型
private $echotype; //输出图片类型,link--直接输出到浏览器;file--保存为文件
private $im=''; //临时变量
private $originName=''; //源图名称
private $srcW=''; //源图宽
private $srcH=''; //源图高
private $errorNum=0; //错误号
private $errorMess='';//用来提供错误报告 public function __construct($options = array()){
$allowOption = array('imagepre','imagepath','echotype');
foreach($options as $key=>$val){
$key = strtolower($key);
//查看用户参数中数组的下标是否和成员属性名相同
//if(!in_array($key,get_class_vars(get_class($this)))){
if(!in_array($key, $allowOption)){
$this->errorNum = -3;
$this->errorMess = $this->getError();
continue 1;
}
$this->$key = $val;
}
}
/**
*
* 判断源文件路径、缩略图存放路径是否正确
* 判断GD库是否支持缩略图格式
* 初始化图片高宽、缩略图存放路径、缩略图生成方式
* @param string $srcImage
* @param int $toW
* @param int $toH
* @param string $method
* @param string $echotype
*/
public function setThumb($srcImage = '', $toW = 0, $toH = 0, $method = 'distortion'){
$this->srcImage = $srcImage;
$imageName = explode('/', $this->srcImage);
$this->originName = $imageName[2];
//检查源文件是否存在
if(empty($srcImage) || filetype($srcImage) != 'file'){
$this->errorNum = 4;
$this->errorMess = $this->getError();
return false;
}
//检查文件上传路径
if ($this->echotype == 'file'){
if(!$this->checkImagePath()){
$this->errorMess = $this->getError();
return false;
}
}
$info = '';
$data = getimagesize($this->srcImage, $info);//获取图像大小
$this->srcW = $data[0];//宽
$this->srcH = $data[1];//高
//检查GD库
if(!$this->checkGD($data[2])){
$this->errorMess = $this->getError();
return false;
}
$this->setImageName();//设置缩略图名称
$toFile = $this->imagepath.$this->newImageName;//缩略图存放路径
$return = $this->createImageMethod($method, $toFile, $toW, $toH);
return $return;
}
/**
*
* 初始化缩略图生成方式
* prorate 按比例缩放
* distortion 扭曲型缩图
* cut 最小裁剪后的缩图
* backFill 背景填充图
* @param string $method
* @param string $toFile
* @param int $toW
* @param int $toH
*/
private function createImageMethod($method, $toFile, $toW, $toH){
switch ($method){
case 'prorate':
$return = $this->prorate($toFile, $toW, $toH);
break;
case 'cut':
$return = $this->cut($toFile, $toW, $toH);
break;
case 'backFill':
$return = $this->backFill($toFile, $toW, $toH);
break;
default:
$return = $this->distortion($toFile, $toW, $toH);
}
return $return;
}
//生成扭曲型缩图
function distortion($toFile='', $toW=0, $toH=0){
$cImg=$this->creatImage($this->im, $toW, $toH, 0, 0, 0, 0, $this->srcW, $this->srcH);
return $this->echoImage($cImg, $toFile);
imagedestroy($cImg);
} //生成按比例缩放的缩图
function prorate($toFile, $toW, $toH){
$toWH = $toW / $toH;
$srcWH = $this->srcW / $this->srcH;
if($toWH <= $srcWH){
$ftoW = $toW;
$ftoH = $ftoW * ($this->srcH / $this->srcW);
}else{
$ftoH = $toH;
$ftoW = $ftoH * ($this->srcW / $this->srcH);
}
if($this->srcW > $toW || $this->srcH > $toH){
$cImg = $this->creatImage($this->im, $ftoW, $ftoH, 0, 0, 0, 0, $this->srcW, $this->srcH);
return $this->echoImage($cImg, $toFile);
imagedestroy($cImg);
}else{
$cImg = $this->creatImage($this->im, $this->srcW, $this->srcH, 0, 0, 0, 0, $this->srcW, $this->srcH);
return $this->echoImage($cImg, $toFile);
imagedestroy($cImg);
}
} //生成最小裁剪后的缩图
private function cut($toFile, $toW, $toH){
$toWH = $toW/$toH;
$srcWH = $this->srcW / $this->srcH;
if($toWH <= $srcWH){
$ctoH = $toH;
$ctoW = $ctoH * ($this->srcW / $this->srcH);
}else{
$ctoW = $toW;
$ctoH = $ctoW * ($this->srcH / $this->srcW);
}
$allImg = $this->creatImage($this->im, $ctoW, $ctoH, 0, 0, 0, 0, $this->srcW, $this->srcH);
$cImg = $this->creatImage($allImg, $toW, $toH, 0, 0, ($ctoW-$toW) / 2, ($ctoH-$toH) / 2, $toW, $toH);
return $this->echoImage($cImg, $toFile);
imagedestroy($cImg);
imagedestroy($allImg);
} //生成背景填充的缩图
private function backFill($toFile, $toW, $toH, $bk1=255, $bk2=255, $bk3=255){
$toWH = $toW / $toH;
$srcWH = $this->srcW / $this->srcH;
if($toWH <= $srcWH){
$ftoW = $toW;
$ftoH = $ftoW * ($this->srcH / $this->srcW);
}else{
$ftoH = $toH;
$ftoW = $ftoH*($this->srcW / $this->srcH);
}
if(function_exists("imagecreatetruecolor")){
@$cImg = imagecreatetruecolor($toW,$toH);
if(!$cImg){
$cImg = imagecreate($toW,$toH);
}
}else{
$cImg = imagecreate($toW,$toH);
}
$backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3); //填充的背景颜色
imagefilledrectangle($cImg,0,0,$toW,$toH,$backcolor);
if($this->srcW > $toW || $this->srcH > $toH){
$proImg = $this->creatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
if($ftoW < $toW){
imagecopy($cImg, $proImg, ($toW - $ftoW) / 2, 0, 0, 0, $ftoW, $ftoH);
}else if($ftoH < $toH){
imagecopy($cImg, $proImg, 0, ($toH-$ftoH) / 2, 0, 0, $ftoW, $ftoH);
}else{
imagecopy($cImg, $proImg, 0, 0, 0, 0, $ftoW, $ftoH);
}
}else{
imagecopymerge($cImg, $this->im, ($toW - $ftoW) / 2,($toH - $ftoH) / 2, 0, 0, $ftoW, $ftoH, 100);
}
return $this->echoImage($cImg, $toFile);
imagedestroy($cImg);
}
//创建图像
private function creatImage($img, $creatW, $creatH, $dstX, $dstY, $srcX, $srcY, $srcImgW, $srcImgH){
if(function_exists("imagecreatetruecolor")){
@$creatImg = imagecreatetruecolor($creatW, $creatH);
if($creatImg){
imagecopyresampled($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
}else{
$creatImg=imagecreate($creatW,$creatH);
imagecopyresized($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
}
}else{
$creatImg=imagecreate($creatW, $creatH);
imagecopyresized($creatImg, $img, $dstX, $dstY, $srcX, $srcY, $creatW, $creatH, $srcImgW, $srcImgH);
}
return $creatImg;
} //输出图片,link---只输出,不保存文件。file--保存为文件
function echoImage($img, $toFile){
switch($this->echotype){
case 'link':
if(function_exists('imagejpeg'))
return imagejpeg($img);
else
return imagepng($img);
break;
case 'file':
if(function_exists('imagejpeg'))
return imagejpeg($img,$toFile);
else
return imagepng($img,$toFile);
break;
}
}
/**
*
* 设置随机文件名称
* @access private
* @return string
*/
private function setRandName(){
$fileName = date("YmdHis").rand(100,999);
return $fileName.'.'.$this->imageType;
}
private function setImageName(){
if ($this->imagepre != ''){
$this->newImageName = $this->imagepre.'_'.$this->setRandName();
}else {
$this->newImageName = $this->setRandName();
}
}
/**
*
* 用来检查文件上传路径
* @access private
* @return bool
*/
private function checkImagePath(){
if(empty($this->imagepath)) {
$this->errorNum = -2;
return false;
} if(!file_exists($this->imagepath) || !is_writable($this->imagepath)){
if(!@mkdir($this->imagepath, 0755)){
$this->errorNum = -1;
return false;
}
}
return true;
}
private function checkGD($imageType){
switch ($imageType){
case 1:
if(!function_exists("imagecreatefromgif")){
$this->errorNum = 1;
return false;
}
$this->imageType = 'gif';
$this->im = imagecreatefromgif($this->srcImage);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
$this->errorNum = 2;
return false;
}
$this->imageType = 'jpg';
$this->im = imagecreatefromjpeg($this->srcImage);
break;
case 3:
if(!function_exists("imagecreatefrompng")){
$this->errorNum = 3;
return false;
}
$this->imageType = 'png';
$this->im = imagecreatefrompng($this->srcImage);
break;
default:
$this->errorNum = 0;
return false;
}
return true;
}
/**
*
* 用于获取上传后缩略图片的文件名
* @access public
* @return string
*/
public function getNewImageName(){
return $this->newImageName;
} /**
*
* 获取上传错误信息
* @access private
* @return string
*/
private function getError(){
$str='生成缩略图<font color="red">'.$this->originName.'</font>时出错:'; switch($this->errorNum){
case 4: $str .= '没有找到需要缩略的图片';
break;
case 3: $str .= '你的GD库不能使用png格式的图片,请使用其它格式的图片!';
break;
case 2: $str .= '你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!';
break;
case 1: $str .= '你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!';
break;
case -1: $str .= '建立存放缩略图目录失败,请重新指定缩略图目录';
break;
case -2: $str .= '必须指定缩略图的路径';
break;
case -3: $str .= '初始化参数出错';
break;
default: $str .= '末知错误';
} return $str.'<br>';
}
public function getErrorMsg() {
return $this->errorMess;
}
}

PHP缩略图类的更多相关文章

  1. pdo文字水印类,验证码类,缩略图类,logo类

    文字水印类 image.class.php <?php /** * webrx.cn qq:7031633 * @author webrx * @copyright copyright (c) ...

  2. php四个常用类封装 :MySQL类、 分页类、缩略图类、上传类;;分页例子;

    Mysql类 <?php /** * Mysql类 */ class Mysql{ private static $link = null;//数据库连接 /** * 私有的构造方法 */ pr ...

  3. 【PHP缩略图类】手机照片不能生成缩略图问题以及解决方式

    [本文原创,谢绝转载] 一.出现的问题 这几天做了手机上传照片并裁出缩略图的接口的測试,发现无论怎么.生成的缩略图都是一片漆黑.:-( 然后就把这个缩略图类单拿出来进行測试,发现仅仅要是手机拍出来的照 ...

  4. PHP原生写的生成图片缩略图类

    PHP原生写的生成图片缩略图类,本文以京东商品图片为例,分别生成三种不同尺寸的图片.调用方法很简单只要传参数高度和宽度,及新图片的名称. 引入缩略图类 include_once 'ImageResiz ...

  5. php-验证码类-PDO类-缩略图类

    Verify.class.php 验证码类 <?php class Verify{ const VERIFY_TYPE_NUM=1; const VERIFY_TYPE_EN=2; const ...

  6. .NET 等宽、等高、等比例、固定宽高生成缩略图 类

    #region 根据原图片生成等比缩略图 /// <summary> /// 根据源图片生成缩略图 /// </summary> /// <param name=&quo ...

  7. ThinkPHP3验证码、文件上传、缩略图、分页(自定义工具类、session和cookie)

    验证码 TP框架中自带了验证码类 位置:Think/verify.class.php 在LoginController控制器中创建生存验证码的方法 login.html登陆模板中 在LoginCont ...

  8. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  9. php生成图片缩略图,支持png透明

    注:此功能依赖GD2图形库 PHP生成缩略图类   <?php /* * desc: Resize Image(png, jpg, gif) * author: 十年后的卢哥哥(http://w ...

随机推荐

  1. 孤荷凌寒自学python第二十天python的匿名函数与偏函数

    孤荷凌寒自学python第二十天python的匿名函数与偏函数 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python为使函数的使用更加方便高效,可以使用两种特殊的函数简化语句书写. 一 ...

  2. Windows后续处理工作

    1.远程桌面开启,应预先开启windows防火墙,并放行“远程桌面”(TCP 3389)端口,防止用户自行开启防火墙时操作错误. 2.防火墙高级安全-需放行ICMP 3.补丁更新,更新完重启 4.本地 ...

  3. shell之netstat命令

      语 法:netstat [-acCeFghilMnNoprstuvVwx] [-A<网络类型>][--ip] 补充说明:利用netstat指令可让你得知整个Linux系统的网络情况. ...

  4. jQuery基础知识点(上)

    jQuery是一个优秀的.轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF1.5+, Safari 2.0+, Opera 9.0+),而jQuery2.0及后续版本将不再支 ...

  5. [NC189C]硬币游戏

    题目大意:有$4n$个硬币,放在$2n$个位置(即放成两排),有两个人,轮流取.第一个人取上面的,第二个人取下面的,每个人只可以取两个人都没取过的位置.若硬币正面向上,为$1$,反面为$0$.把取得的 ...

  6. Gcd反应堆 (pgcd)

    Gcd反应堆 (pgcd) 题目描述 不知什么时候起,TA突然对gcd产生了浓厚的兴趣,于是他为此编写了个程序,输入分别不大于m,n (1<m,n<=10^7)的两个数,就能得出gcd(m ...

  7. HDU1520 树形DP入门

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. code forces 996D Suit and Tie

    D. Suit and Tie time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  9. windows 系统下TortoiseGit 的安装

    git 常用的使用工具: Windows 支持下载安装TortoiseGit Mac 下 推荐使用 sourcetree Windows下的 TortoiseGit 安装 1. 进入 小乌龟官网 To ...

  10. laravel 学习笔记 —— 神奇的服务容器

    转载自:https://www.insp.top/learn-laravel-container 容器,字面上理解就是装东西的东西.常见的变量.对象属性等都可以算是容器.一个容器能够装什么,全部取决于 ...