<?php
/**
*图片压缩到指定的体积大小
* $original_file 原始图片路径
* $resized_file 压缩后图片路径
* $max_file_size 压缩大小 单位kb
* $image_quality 图片质量 1-100
*/
function compresstosize($original_file, $resized_file, $max_file_size,$image_quality=100) {
$original_image = imagecreatefromjpeg($original_file);
do {
$temp_stream = fopen('php://temp', 'w+');
$saved = imagejpeg($original_image, $temp_stream, $image_quality--);
rewind($temp_stream);
$fstat = fstat($temp_stream);
fclose($temp_stream);
$file_size = $fstat['size'];
}
while (($file_size > $max_file_size) && ($image_quality >= 0));
if (-1 == $image_quality) {
die("无法压缩这么小");
// echo "Unable to get the file that small. Best I could do was $file_size bytes at image quality 0.\n";
} else {
// echo "Successfully resized $original_file to $file_size bytes using image quality $image_quality. Resized file saved as $resized_file.\n";
imagejpeg($original_image, $resized_file, $image_quality + 1);
}
}
$rand = rand(1, 10000000);
compresstosize("bg.jpg","resized.jpg","10000");
echo "<h2>压缩到指定大小kb的图</h2>"; echo "<img src='resized.jpg?rand={$rand}'>";
网友回复
gpt-image2能直接将图片转成分层透明的psd设计文件?
claude code、codex、gemini cli如何切换国内大模型使用?
蒸馏最强ai大模型是中小ai模型低成本升级的最好通道?
arena.ai上为啥没有最新的claude4.7及gpt5.5呢?
ai大模型公司为啥开始大量招聘文科生了?
cloudflared如何在低版本centos6或7上安装?
bfwsoa框架如何开启异步缓存与异步任务模式?
selenium如何获取网页js加载渲染后的真实dom结构?
go编写的Eino与python编写的langchain如何选择?
LangChain如何编写多个agent协同工作的代码?


