2021-07-17 05:27:19 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 压缩文件函数调用位置
|
|
|
|
|
*/
|
|
|
|
|
require_once __DIR__ . '/../function.php';
|
2021-11-09 03:43:23 +00:00
|
|
|
|
require_once APP_ROOT . '/application/compress/Imagick/class.Imgcompress.php';
|
|
|
|
|
require_once APP_ROOT . '/application/compress/TinyImg/TinyImg.php';
|
2021-07-17 05:27:19 +00:00
|
|
|
|
require_once APP_ROOT . '/config/api_key.php';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $floder 文件夹
|
2022-01-27 09:25:46 +00:00
|
|
|
|
* @param string 压缩方式Imgcompress / TinyPng
|
2021-07-17 05:27:19 +00:00
|
|
|
|
*/
|
|
|
|
|
function compress($floder, $type = 'Imgcompress', $source = '')
|
|
|
|
|
{
|
|
|
|
|
global $config;
|
|
|
|
|
ini_set('max_execution_time', '0'); // 脚本运行的时间(以秒为单位)0不限制
|
|
|
|
|
|
|
|
|
|
if ($type == 'Imgcompress') {
|
|
|
|
|
|
|
|
|
|
$pic = getFile($floder); // 文件夹路径
|
2022-01-27 09:25:46 +00:00
|
|
|
|
$percent = $config['compress_ratio'] / 100; // 压缩率
|
2021-07-17 05:27:19 +00:00
|
|
|
|
foreach ($pic as $value) {
|
|
|
|
|
$boxImg = $floder . $value;
|
|
|
|
|
// 跳过动态图片
|
|
|
|
|
if (!isAnimatedGif($boxImg)) {
|
2022-01-27 09:25:46 +00:00
|
|
|
|
$img = new Imgcompress($boxImg, $percent);
|
2021-07-17 05:27:19 +00:00
|
|
|
|
$img->compressImg($boxImg);
|
|
|
|
|
echo '<pre>' . $boxImg . '</pre><br />';
|
|
|
|
|
// 释放
|
|
|
|
|
ob_flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 09:25:46 +00:00
|
|
|
|
if ($type == 'TinyPng') {
|
|
|
|
|
if (empty($config['TinyPng_key'])) {
|
|
|
|
|
exit('请先申请TinyPng key并保存再试!');
|
2021-07-17 05:27:19 +00:00
|
|
|
|
}
|
|
|
|
|
$folder = '..' . $config['path'] . $source;
|
|
|
|
|
$tinyImg = new TinyImg();
|
2022-01-27 09:25:46 +00:00
|
|
|
|
$key = $config['TinyPng_key'];
|
2021-07-17 05:27:19 +00:00
|
|
|
|
$input = $folder; //这个文件夹下的文件会被压缩
|
|
|
|
|
$output = $folder; //压缩的结果会被保存到这个文件夹中
|
|
|
|
|
$tinyImg->compressImgsFolder($key, $input, $output);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Test
|
|
|
|
|
$floder = 'D:/phpStudy/WWW/i/2021/05/09/';
|
|
|
|
|
compress($floder, 'TinyImg');
|
|
|
|
|
echo 666;
|
|
|
|
|
*/
|