团子利用ai写的一份基于PHP图片压缩源码(关键是能跑握艹)

团子利用ai写的一份基于PHP图片压缩源码(关键是能跑握艹)-寻梦者开发网
团子利用ai写的一份基于PHP图片压缩源码(关键是能跑握艹)
此内容为免费阅读,请登录后查看
0
免费阅读

第一个版本:

<?php
function compressImage($sourcePath, $destinationPath, $quality) {
// 获取原始图片的信息
list($width, $height, $type) = getimagesize($sourcePath);
// 根据图片类型创建相应的图像资源
switch ($type) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($sourcePath);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($sourcePath);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($sourcePath);
break;
default:
return false; // 不支持的图片类型
}
// 压缩图片并保存
imagejpeg($image, $destinationPath, $quality);
// 释放图像资源
imagedestroy($image);
return true;
}
// 使用示例
$sourcePath = 'path/to/source/image.jpg'; // 原始图片路径
$destinationPath = 'path/to/destination/image_compressed.jpg'; // 压缩后图片保存路径
$quality = 80; // 压缩质量,范围通常为0-100
if (compressImage($sourcePath, $destinationPath, $quality)) {
echo '图片压缩成功!';
} else {
echo '图片压缩失败!';
}
?>
<?php
function compressImage($sourcePath, $destinationPath, $quality) {
    // 获取原始图片的信息
    list($width, $height, $type) = getimagesize($sourcePath);
    // 根据图片类型创建相应的图像资源
    switch ($type) {
        case IMAGETYPE_JPEG:
            $image = imagecreatefromjpeg($sourcePath);
            break;
        case IMAGETYPE_PNG:
            $image = imagecreatefrompng($sourcePath);
            break;
        case IMAGETYPE_GIF:
            $image = imagecreatefromgif($sourcePath);
            break;
        default:
            return false; // 不支持的图片类型
    }
    // 压缩图片并保存
    imagejpeg($image, $destinationPath, $quality);
    // 释放图像资源
    imagedestroy($image);
    return true;
}
// 使用示例
$sourcePath = 'path/to/source/image.jpg'; // 原始图片路径
$destinationPath = 'path/to/destination/image_compressed.jpg'; // 压缩后图片保存路径
$quality = 80; // 压缩质量,范围通常为0-100
if (compressImage($sourcePath, $destinationPath, $quality)) {
    echo '图片压缩成功!';
} else {
    echo '图片压缩失败!';
}
?>
<?php function compressImage($sourcePath, $destinationPath, $quality) { // 获取原始图片的信息 list($width, $height, $type) = getimagesize($sourcePath); // 根据图片类型创建相应的图像资源 switch ($type) { case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($sourcePath); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($sourcePath); break; case IMAGETYPE_GIF: $image = imagecreatefromgif($sourcePath); break; default: return false; // 不支持的图片类型 } // 压缩图片并保存 imagejpeg($image, $destinationPath, $quality); // 释放图像资源 imagedestroy($image); return true; } // 使用示例 $sourcePath = 'path/to/source/image.jpg'; // 原始图片路径 $destinationPath = 'path/to/destination/image_compressed.jpg'; // 压缩后图片保存路径 $quality = 80; // 压缩质量,范围通常为0-100 if (compressImage($sourcePath, $destinationPath, $quality)) { echo '图片压缩成功!'; } else { echo '图片压缩失败!'; } ?>

 

在上述示例中,compressImage函数接受三个参数:原始图片的路径、压缩后图片的保存路径以及压缩质量(范围为0-100)。函数首先使用getimagesize函数获取原始图片的信息,然后根据图片类型创建相应的图像资源。接下来,使用imagejpeg函数将图像资源压缩并保存到指定路径。最后,释放图像资源并返回压缩结果。 请注意,上述示例仅支持JPEG、PNG和GIF格式的图片。如果你需要支持其他格式,可以根据需要扩展switch语句中的情况。 此外,你还可以根据需要对压缩算法进行调整,例如使用imagepngimagegif函数来压缩PNG或GIF图片。

第二个版本对上面的代码进行一些优化,以提高其灵活性和效率。以下是一个改进后的版本:

<?php
/**
* 压缩图片并保存
*
* @param string $sourcePath 原始图片路径
* @param string $destinationPath 压缩后图片保存路径
* @param int $quality 压缩质量(0-100)
* @param string $type 图片类型(可选,默认为null,根据文件扩展名自动检测)
* @return bool 成功返回true,失败返回false
*/
function compressImage($sourcePath, $destinationPath, $quality = 80, $type = null) {
// 如果未指定图片类型,则根据文件扩展名自动检测
if ($type === null) {
$type = pathinfo($sourcePath, PATHINFO_EXTENSION);
}
// 根据图片类型创建图像资源
switch ($type) {
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($sourcePath);
break;
case 'png':
$image = imagecreatefrompng($sourcePath);
break;
case 'gif':
$image = imagecreatefromgif($sourcePath);
break;
default:
return false; // 不支持的图片类型
}
// 压缩图片并保存
$success = false;
switch ($type) {
case 'jpg':
case 'jpeg':
$success = imagejpeg($image, $destinationPath, $quality);
break;
case 'png':
$success = imagepng($image, $destinationPath, $quality);
break;
case 'gif':
$success = imagegif($image, $destinationPath);
break;
}
// 释放图像资源
imagedestroy($image);
return $success;
}
// 使用示例
$sourcePath = 'path/to/source/image.jpg';
$destinationPath = 'path/to/destination/image_compressed.jpg';
$quality = 80;
if (compressImage($sourcePath, $destinationPath, $quality)) {
echo '图片压缩成功!';
} else {
echo '图片压缩失败!';
}
?>
<?php
/**
 * 压缩图片并保存
 *
 * @param string $sourcePath 原始图片路径
 * @param string $destinationPath 压缩后图片保存路径
 * @param int $quality 压缩质量(0-100)
 * @param string $type 图片类型(可选,默认为null,根据文件扩展名自动检测)
 * @return bool 成功返回true,失败返回false
 */
function compressImage($sourcePath, $destinationPath, $quality = 80, $type = null) {
    // 如果未指定图片类型,则根据文件扩展名自动检测
    if ($type === null) {
        $type = pathinfo($sourcePath, PATHINFO_EXTENSION);
    }
    // 根据图片类型创建图像资源
    switch ($type) {
        case 'jpg':
        case 'jpeg':
            $image = imagecreatefromjpeg($sourcePath);
            break;
        case 'png':
            $image = imagecreatefrompng($sourcePath);
            break;
        case 'gif':
            $image = imagecreatefromgif($sourcePath);
            break;
        default:
            return false; // 不支持的图片类型
    }
    // 压缩图片并保存
    $success = false;
    switch ($type) {
        case 'jpg':
        case 'jpeg':
            $success = imagejpeg($image, $destinationPath, $quality);
            break;
        case 'png':
            $success = imagepng($image, $destinationPath, $quality);
            break;
        case 'gif':
            $success = imagegif($image, $destinationPath);
            break;
    }
    // 释放图像资源
    imagedestroy($image);
    return $success;
}
// 使用示例
$sourcePath = 'path/to/source/image.jpg';
$destinationPath = 'path/to/destination/image_compressed.jpg';
$quality = 80;
if (compressImage($sourcePath, $destinationPath, $quality)) {
    echo '图片压缩成功!';
} else {
    echo '图片压缩失败!';
}
?>
<?php /** * 压缩图片并保存 * * @param string $sourcePath 原始图片路径 * @param string $destinationPath 压缩后图片保存路径 * @param int $quality 压缩质量(0-100) * @param string $type 图片类型(可选,默认为null,根据文件扩展名自动检测) * @return bool 成功返回true,失败返回false */ function compressImage($sourcePath, $destinationPath, $quality = 80, $type = null) { // 如果未指定图片类型,则根据文件扩展名自动检测 if ($type === null) { $type = pathinfo($sourcePath, PATHINFO_EXTENSION); } // 根据图片类型创建图像资源 switch ($type) { case 'jpg': case 'jpeg': $image = imagecreatefromjpeg($sourcePath); break; case 'png': $image = imagecreatefrompng($sourcePath); break; case 'gif': $image = imagecreatefromgif($sourcePath); break; default: return false; // 不支持的图片类型 } // 压缩图片并保存 $success = false; switch ($type) { case 'jpg': case 'jpeg': $success = imagejpeg($image, $destinationPath, $quality); break; case 'png': $success = imagepng($image, $destinationPath, $quality); break; case 'gif': $success = imagegif($image, $destinationPath); break; } // 释放图像资源 imagedestroy($image); return $success; } // 使用示例 $sourcePath = 'path/to/source/image.jpg'; $destinationPath = 'path/to/destination/image_compressed.jpg'; $quality = 80; if (compressImage($sourcePath, $destinationPath, $quality)) { echo '图片压缩成功!'; } else { echo '图片压缩失败!'; } ?>

在这个版本中,我们做了以下几点优化:

  1. 增加了$type参数,允许用户指定图片类型。如果未指定,则根据文件扩展名自动检测。
  2. 改进了compressImage函数,使其能够根据图片类型选择正确的压缩函数(imagejpegimagepngimagegif)。
  3. 提高了代码的可读性和可维护性,通过添加注释和类型提示。 请注意,这个优化版本仍然假设你正在处理的是本地文件系统中的文件。如果你需要从URL或其他源加载图片,你可能需要添加额外的逻辑来处理这些情况。此外,对于大文件或高流量的网站,你可能还需要考虑内存使用情况和执行时间,并可能需要使用更高级的图像处理库或工具。

希望能够帮助到有需要的你!

© 版权声明
THE END
喜欢就支持一下吧
点赞11赞赏 分享
Don’t let your dreams be dreams.
不要让你的梦想只是想想而已
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容