当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
随机推荐
Circular linked list---------Joseph problem
v-bind用法:类动态绑定对象 数组 style样式 及函数方法
12.什么是JS
uniapp | 开发中遇到的兼容性问题(待续)
阿里云服务器如何使用admin账户登录
猴子选大王(约瑟环问题)
由中序遍历和后序遍历得到前序遍历(树的遍历)
[phpunit/php-timer]一个用于代码执行时间的计时器
js 原型和原型链
宝塔邮局邮箱设置成功后能发送不能接收问题处理
[trendsoft/capital]金额转中文大写库
js __proto__、prototype、constructor的关系
PHP 给图片添加全图水印
js eventLoop 事件循环机制
canvas--饼状图
TypeScript 错误 error TS2469、error TS2731 解决办法
js基础知识
14.JS语句和注释,变量和数据类型
微信小程序实现文本安全监测
三元判断再三元判断