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这个已经实现了.但我不知道怎么样才干訪问我上传的原图& ...
随机推荐
- Vue 组件4 动态组件
动态组件 通过使用保留的<component>元素,动态的绑定到它的is特性,我们让多个组件同时使用同一个挂载点,并动态切换: var vm = new Vue({ el: '#examp ...
- Hibernate集合映射
可以在Hibernate中映射持久类的集合元素. 您需要从以下类型之一声明持久类中的集合类型: java.util.List java.util.Set java.util.SortedSet jav ...
- 2017 css新特性
2017年要学习的三个CSS新特性 这是翻译的一篇文章,原文是:3 New CSS Features to Learn in 2017,翻译的不是很好,如有疑问欢迎指出. 新的一年,我们有一系列新的东 ...
- unity 多选枚举
首先是自定义 using UnityEngine; using System.Collections; using UnityEditor; public class EnumFlagsAttribu ...
- android自定义View_4——自定义属性的格式选择
reference - if it references another resource id (e.g, "@color/my_color", "@layout/my ...
- 夏日炎炎 python写个天气预报
东南地区连续突破历史,江浙沪除了包邮之外的另一福利-桑拿也已到手.这样的日子里是应该每日关注天气主义降暑避免出现热疾病,python包含比 较多的网络应用类这样就方便了一些网络应用的操作,之外还有些可 ...
- Python_Selenium之鼠标右键
Python_Selenium之鼠标右键 一.步骤: (以百度为例)获取百度网址 找到需要右键的元素(定位),xpath表达式为“//*[@id='lg']/img” 然后,右键选择“在新标签页中打开 ...
- Python简单的线程池
class ThreadPool(object): def __init__(self, max_num=20): # 创建一个队列,队列里最多只能有10个数据 self.queue = queue. ...
- Log4j 使用
源博客 http://www.cnblogs.com/alipayhutu/archive/2012/06/21/2558249.html#3159794 [1]从零开始 a). 新建Java Pro ...
- VC编译那些事儿
转载自:http://blog.csdn.net/wowolook/article/details/8077153 最近又被ms的编译选译纠结了一下,运行程序是老是弹出0x14b1 or 71 ...