PHP生成缩略图,控制图片质量,支持.png .jpg .gif
namespace common\components; class ResizeImageHelper
{
public $type;//图片类型
public $width;//实际宽度
public $height;//实际高度
public $resize_width;//改变后的宽度
public $resize_height;//改变后的高度
public $cut;//是否裁图
public $srcimg;//源图象
public $dstimg;//目标图象地址
public $im;//临时创建的图象
public $quality;//图片质量
public $img_array = array('jpg', 'png', 'gif'); /**
* 生成保持原图纵横比的缩略图,支持.png .jpg .gif
* 缩略图类型统一为.png格式
* $img 原图像文件名称
* $wid 缩略图宽
* $hei 缩略图高
* $c 是否剪裁
* $dstpath 缩略图文件名称,为空覆盖原图像文件
* $quality 质量最大100
* @return bool
*/
public function createThumbnail($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->type = $this->checkFileType($this->srcimg);//更为严格的检测图片类型
if (!in_array($this->type, $this->img_array)) {
return '';
}
$this->initi_img();//初始化图象
$this->dst_img($dstpath);//目标图象地址
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
$this->newimg();//生成图象
ImageDestroy($this->im);
return true;
} private function newimg()
{
$resize_ratio = ($this->resize_width) / ($this->resize_height);//改变后的图象的比例
$ratio = ($this->width) / ($this->height);//实际图象的比例
if (($this->cut) == '1') {//裁图
if (function_exists('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);
}
}
} private function initi_img()
{//初始化图象
if ($this->type == 'jpg') {
$this->im = imagecreatefromjpeg($this->srcimg);
}
if ($this->type == 'gif') {
$this->im = imagecreatefromgif($this->srcimg);
}
if ($this->type == 'png') {
$this->im = imagecreatefrompng($this->srcimg);
}
} private 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;
} //读取文件前几个字节 判断文件类型
private function checkFileType($filename)
{
$file = fopen($filename, 'rb');
$bin = fread($file, 2); //只读2字节
fclose($file);
$strInfo = @unpack("c2chars", $bin);
$typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
switch ($typeCode) {
case 7790:
$fileType = 'exe';
break;
case 7784:
$fileType = 'midi';
break;
case 8297:
$fileType = 'rar';
break;
case 255216:
$fileType = 'jpg';
break;
case 7173:
$fileType = 'gif';
break;
case 6677:
$fileType = 'bmp';
break;
case 13780:
$fileType = 'png';
break;
default:
$fileType = 'unknown' . $typeCode;
break;
}
if ($strInfo['chars1'] == '-1' && $strInfo['chars2'] == '-40') {
return 'jpg';
}
if ($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80') {
return 'png';
}
return $fileType;
} }
调用示例
$resizeimage = new ResizeImageHelper();
$resizeimage->CreateThumbnail('path/../1.png', 1440,400, 1, 'path/../2.png', 80);
PHP生成缩略图,控制图片质量,支持.png .jpg .gif的更多相关文章
- php生成缩略图
<?php /** * 生成缩略图函数(支持图片格式:gif.jpeg.png和bmp) * @author ruxing.li * @param string $src 源图片路径 * @pa ...
- php 图片操作类,支持生成缩略图,添加水印,上传缩略图
<?php class Image { //类开始 public $originimage = ""; //源图片文件地址 public $image ...
- C#上传图片同时生成缩略图,控制图片上传大小。
#region 上传图片生成缩略图 /// <summary> /// 上传图片 /// </summary> /// <param name="sender& ...
- 使用Uploadify实现上传图片生成缩略图例子,实时显示进度条
不了解Uploadify的,先看看前一篇详细说明 http://www.cnblogs.com/XuebinDing/archive/2012/04/26/2470995.html Uploadify ...
- Android_Bitmap_图片的二次采样并生成缩略图
1.Bitmap概述 Android系统支持几种图片(.png (preferred), .jpg (acceptable), .gif (discouraged)), 其中Bitmap位图#ffff ...
- ngx_image_thumb模块生成缩略图
ngx_image_thumb是nginx中用来生成缩略图的模块. 编译前确定已经安装了libcurl-dev libgd2-dev libpcre-dev gd-devel pcre-devel l ...
- PHP图片裁剪_图片缩放_PHP生成缩略图
在制作网页过程中,为了排版整齐美观,对网页中的图片处理成固定大小尺寸的图片,或是要截去图片边角中含有水印的图片,对于图片量多,每天更新大量图,靠人工PS处理是不现实的,那么有没有自动处理图片的程序了! ...
- yii php 图片上传与生成缩略图
今天需要做图片上传与生成缩略图的功能,把代码进行记录如下: html 视图 ($pic_action_url = $this->createAbsoluteUrl('h ...
- nginx利用image_filter动态生成缩略图
转自:http://www.nginx.cn/2160.html "我如今是有些图片须要生成缩略图.这个如今加了image_filter这个已经实现了.但我不知道怎么样才干訪问我上传的原图& ...
随机推荐
- Mac显示和隐藏隐藏文件
显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:defaults write com.apple.finder A ...
- linux设备号分配
参考:http://blog.chinaunix.net/uid-24460251-id-2606762.htmlhttp://blog.csdn.net/zjjyliuweijie/article/ ...
- 个人博客开发之xadmin 布局和后台样式
项目源码下载:http://download.vhosts.cn 一. xadmin 后台配置注册信息 1. 在apps 的blogs 和 users 两个app中添加adminx.py文件 vim ...
- easyUI combox静态动态联动
easyUI重写了select,取而代之的是combobox,有如下几种方式可以创建一个combobox 1.使用select标签,并加上class="easyui-combobox&quo ...
- 关于fork()父子进程返回值的问题
我们都知道,父进程fork()之后返回值为子进程的pid号,而子进程fork()之后的返回值为0.那么,现在就有一个问题了,子进程fork()的返回值是怎么来的?如果子进程又执行了一遍fork()函数 ...
- php 字符串截取
$str="3,22,11,444,33,1,3455,33,22,444,55,66,77,88,99,554336,"; echo substr($str,0,strlen($ ...
- wchar与char字符转换的探究
在Xcode 模拟器环境下.測试wchar_t与char的转换: void convert_test() { setlocale(LC_ALL, "zh_CN.UTF-8"); c ...
- 使用nginx cache缓存网站数据实践
Nginx本身就有缓存功能,能够缓存静态对象,比如图片.CSS.JS等内容直接缓存到本地,下次访问相同对象时,直接从缓存即可,无需访问后端静态服务器以及存储存储服务器,可以替代squid功能. 1 ...
- JDK的命令具体解释操作
JDK的命令具体解释1 rmic 功能说明: rmic 为远程对象生成 stub 和 skeleton. 语法: rmic [ options ] package-qualified-class-na ...
- linux 大量的TIME_WAIT解决办法(转)
发现存在大量TIME_WAIT状态的连接tcp 0 0 127.0.0.1:3306 127.0.0.1:41378 TIME ...