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这个已经实现了.但我不知道怎么样才干訪问我上传的原图& ...
随机推荐
- active mq 配置
<transportConnectors> <!-- DOS protection, limit concurrent connections to 1000 and frame s ...
- LeetCode207. Course Schedule
Description There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses m ...
- linux命令 — lsof 查看进程打开那些文件 或者 查看文件给那个进程使用
lsof命令是什么? 可以列出被进程所打开的文件的信息.被打开的文件可以是 1.普通的文件,2.目录 3.网络文件系统的文件,4.字符设备文件 5.(函数)共享库 6.管道,命名管道 7.符号链 ...
- 大数据实战centos 6.7安装mysql5.7
https://www.cnblogs.com/jr1260/p/6590232.html
- DeepLearnToolbox-master代码学习笔记
卷积神经网络(CNN)博大精深,网上资料浩如烟海,让初学者无从下手.笔者以为,学习编程还是从代码实例入们最好.目前,学习CNN最好的代码实例就是,DeepLearnToolbox-master,不用装 ...
- 表单中method="post"什么意思?
表示表单中的数据以"post"方法传递.Form提供了两种数据传输的方式——get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的 ...
- Cut the rope
http://acm.nyist.net/JudgeOnline/problem.php?pid=651 描述We have a rope whose length is L. We will cut ...
- 检测手机中是否安装了google地图,没有则提示安装,并跳转到地图查找特定的地点
/** * 检测手机中是否安装了某个特定的app,若没有提示安装 */ PackageInfo name_2 = null; try { // 若没有这个包名会异常 name_2 = getPacka ...
- Mac下使用安装MongoDB
1.安装方法1:Mac电脑上面安装很简单,直接下载需要的版本解压即可: 下载网址 https://www.mongodb.com/download-center?jmp=nav#community 方 ...
- 不在要DLL中使用C++全局对象
如题,原因很简单,DLL不会调用该对象的析构函数. 果断被微软坑了.