运行方式
php ./coimg.php
coimg.php
<?php
set_time_limit(0);
ini_set("max_execution_time", 0);
ini_set("memory_limit", -1);
// 压缩图片
function c_img($path)
{
$con = @file_get_contents($path);
$img_obj = @imagecreatefromstring($con);
if ($img_obj) {
@imageinterlace($img_obj, 1); //转换为 Progressive JPEG
@imagejpeg($img_obj, $path, 60); //质量设置为60,取值范围:0(最差质量,文件最小)-100(最佳质量,文件最大)
@imagedestroy($img_obj);
}
// echo "imgs ok:" . realpath($path) . "\n";
}
// 判断是否图片
function is_img($filename)
{
if (!@file_exists($filename)) {
return false;
}
$types = ".jpg|.jpeg|.png"; //定义检查的图片类型
$info = @getimagesize($filename);
if (@empty($info)) {
return false;
}
$ext = @image_type_to_extension($info[2]);
$ext = @strtolower($ext);
return @mb_strpos($types, $ext);
}
// 读取目录中的文件
function lists_img($path = "./Uploads")
{
$resource = @opendir($path);
while ($file = @readdir($resource)) {
// 跳过特殊文件夹
if ($file == "." || $file == "..") {
continue;
}
// 拼接新的路径
$path_new = $path . "/" . $file;
// 判断是否文件夹
if (@is_dir($path_new)) {
lists_img($path_new);
continue;
}
// 判断是否为图片
if (is_img($path_new) !== false) {
c_img($path_new);
}
}
@closedir($resource);
echo "paths ok:" . realpath($path) . "\n";
}
lists_img();
echo "all ok" . "\n";