当前位置:网站首页>PHP image compression to specified size
PHP image compression to specified size
2022-08-02 03:53:00 【auspi12341】
<?php
/**
* Please keep the URL for sharing.尊重别人劳动成果.谢谢.
* 图片压缩类:通过缩放来压缩.如果要保持源图比例,把参数$percent保持为1即可.
* 即使原比例压缩,也可大幅度缩小.数码相机4M图片.也可以缩为700KB左右.If scaled down,The volume will be smaller.
* 结果:可保存、可直接显示.
*/
class imgcompress
{
private $src;
private $image;
private $imageinfo;
private $percent = 0.5;
/**
* 图片压缩
*
* @param $src 源图
* @param float $percent
* 压缩比例
*/
public function __construct($src, $percent = 1)
{
$this->src = $src;
$this->percent = $percent;
}
/**
* HD compressed pictures
*
* @param string $saveName
* Provide image name(Can be without extension,Use the sourcemap extension)用于保存.Or directly display without providing the file name
* @param int $maxFileSize
* 目标文件大小
*/
public function compressImg($saveName = '')
{
$this->_openImage();
if (! empty($saveName))
$this->_saveImage($saveName); // 保存
else
$this->_showImage();
return true;
}
/**
* 内部:打开图片
*/
private function _openImage()
{
list ($width, $height, $type, $attr) = getimagesize($this->src);
$this->imageinfo = array(
'width' => $width,
'height' => $height,
'type' => image_type_to_extension($type, false),
'attr' => $attr
);
$fun = "imagecreatefrom" . $this->imageinfo['type'];
$this->image = $fun($this->src);
$this->_thumpImage();
}
/**
* 内部:操作图片
*/
private function _thumpImage()
{
// $this->percent = $this->imageinfo['width'] ;
$new_width = $this->imageinfo['width'] * $this->percent;
$new_height = $this->imageinfo['height'] * $this->percent;
// $new_width = 1200;
// $new_height = 900;
$image_thump = imagecreatetruecolor($new_width, $new_height);
// 将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
imagecopyresampled($image_thump, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->imageinfo['width'], $this->imageinfo['height']);
imagedestroy($this->image);
$this->image = $image_thump;
// $this->image = imagerotate($this->image, 270, 0);
}
/**
* 输出图片:Use to save picturessaveImage()
*/
private function _showImage()
{
header('Content-Type: image/' . $this->imageinfo['type']);
$funcs = "image" . $this->imageinfo['type'];
$funcs($this->image);
}
/**
* 保存图片到硬盘:
*
* @param string $dstImgName
* 1、You can specify a string name without a suffix,Use the sourcemap extension .2、Directly specify the target image name with an extension.
*/
private function _saveImage($dstImgName)
{
if (empty($dstImgName))
return false;
$allowImgs = [
'.jpg',
'.jpeg',
'.png',
'.bmp',
'.wbmp',
'.gif'
]; // If the target image name has a suffix, use the target image extension 后缀,如果没有,Use the extension of the source image
$dstExt = strrchr($dstImgName, ".");
$sourseExt = strrchr($this->src, ".");
if (! empty($dstExt))
$dstExt = strtolower($dstExt);
if (! empty($sourseExt))
$sourseExt = strtolower($sourseExt);
// There is a specified target name extension
if (! empty($dstExt) && in_array($dstExt, $allowImgs)) {
$dstName = $dstImgName;
} elseif (! empty($sourseExt) && in_array($sourseExt, $allowImgs)) {
$dstName = $dstImgName . $sourseExt;
} else {
$dstName = $dstImgName . $this->imageinfo['type'];
}
$funcs = "image" . $this->imageinfo['type'];
$funcs($this->image, $dstName);
}
/**
* 销毁图片
*/
public function __destruct()
{
if(!empty($this->image)){
imagedestroy($this->image);
}
}
/**
* Compress the image to the specified size
* @param string $to_source
* The path to save after compression
* @param number $maxFileSize
* Size to compress
* @return boolean
*/
public function getimg($to_source, $maxFileSize = 200)
{
$source_size = filesize($this->src) / 1024;
if ($source_size < $maxFileSize) {
copy($this->src, $to_source);
return true;
} else {
$ys = true;
$i = 0;
while ($ys) {
clearstatcache();
$this->percent = 0.7;
// $percent = 0.7;
if ($i > 0) {
$this->src = $to_source;
}
$image = $this->compressImg($to_source);
$to_source_size = filesize($to_source) / 1024;
// var_dump($to_source_size);
$ys = $to_source_size > $maxFileSize;
$i ++;
}
// echo "压缩次数:" . $i;
}
return true;
}
}
边栏推荐
- 每日五道面试题总结 22/7/23
- ES6数组的扩展方法map、filter、reduce、fill和数组遍历for…in for…of arr.forEach
- 稳定好用的短连接生成平台,支持API批量生成
- 数组的高级操作
- SQL classification, DQL (Data Query Language), and corresponding SQL query statement demonstration
- MySql高级 -- 约束
- page load process
- [vite] Failed to parse source for import analysis because the content contains invalid JS syntax.
- 每日面试题 2022/7/28
- 3.PHP数据类型、常量、字符串和运算符
猜你喜欢
TCP通信程序
The Error in the render: "TypeError: always read the properties of null '0' (reading)" Error solution
uniapp | 使用npm update更新后编译报错问题
三元判断再三元判断
面试总结 22/7/22 面试中的重点
Circular linked list---------Joseph problem
SQL:DDL、DML、DQL、DCL相应介绍以及演示
点名系统和数组元素为对象的排序求最大值和最小值
阿里云服务器如何使用admin账户登录
ES6数组的扩展方法map、filter、reduce、fill和数组遍历for…in for…of arr.forEach
随机推荐
二维码生成API接口,可以直接作为A标签连接
ES6数组的扩展方法map、filter、reduce、fill和数组遍历for…in for…of arr.forEach
Customer Rating Control
微信小程序云开发之券码领取,怎么防止用户领取到相同的数据?
The querystring module
--fs模块--
[symfony/finder]最好用的文件操作库
js作用域与闭包
6.24今日学习
vim编辑模式
PHP8.2 version release administrator and release plan
页面加载流程
每日五道面试题总结 22/7/19
[trendsoft/capital]金额转中文大写库
Query the indexes of all tables in the database and parse them into sql
The Error in the render: "TypeError: always read the properties of null '0' (reading)" Error solution
线程池(线程池介绍与使用)
13.JS输出内容和语法
PHP8.2将会有哪些新东西?
TCP通信程序