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这个已经实现了.但我不知道怎么样才干訪问我上传的原图& ...
随机推荐
- windows程序 UAC设置,程序运行提示使用管理员权限运行的方法
在近期的任务中需要对光盘中的程序运行时获取管理员权限运行程序.这个功能的实现需要改变工程的配置. 在vs2015中,使用鼠标右击解决方案管理器中的工程->属性->链接器->清单文件. ...
- [转]为什么我说ORM是一种反模式
原文地址:http://www.nowamagic.net/librarys/veda/detail/2217 上周我在在上讨论了ORM,在那以后有人希望我澄清我的意思.事实上,我曾经写文章讨论过OR ...
- MFC 控件RadioButton和CheckBox区别
1. 单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox在选中后,通过点击可以变为未选中 2. 一组RadioButton,只能同时选中一个 一组CheckBox,能同时 ...
- Java bean 是个什么概念?
Java语言欠缺属性.事件.多重继承功能.所以,如果要在Java程序中实现一些面向对象编程的常见需求,只能手写大量胶水代码.Java Bean正是编写这套胶水代码的惯用模式或约定.这些约定包括getX ...
- Linux 进程创建二(execve和wait)
三:execve系统调用 int execve(const char *filename, char *const argv[],char *const envp[]); fork创建了一个新的进程, ...
- Python 双向链表 快速排序
1.创建链表: from random import randint class DLinkedNode(object): def __init__(self, data=None, pre=None ...
- HTTP 请求:GET vs. POST
两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST. GET - 从指定的资源请求数据 POST - 向指定的资源提交要处理的数据 GET 基本上用于从服务器获得(取回)数据.注释 ...
- fabric 自动化部署
fabric 项目发布和运维的工作相当机械,频率还蛮高,导致时间浪费在敲大量重复的命令上. 修复bug什么的,测试,提交版本库(2分钟),ssh到测试环境pull部署(2分钟),rsync到线上机器A ...
- cocos2d-x之读开发技术精解
引擎位置(依次往下): 游戏App->逻辑与规则->引擎->运行的平台->硬件接口(驱动运行库API) 1. 渲染框架 CCNode绘制基类(引擎核心类都继承于它,形成一个链表 ...
- linux中使用vi 打开文件时,能显示行号
方法一: 1.显示当前行行号,在VI的命令模式下输入 :nu 2.显示所有行号,在VI的命令模式下输入 :set nu方法二: 使用vi编辑~/.vimrc文件,在该文件中加入一行" ...