第一个版本:
<?phpfunction 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-100if (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
语句中的情况。 此外,你还可以根据需要对压缩算法进行调整,例如使用imagepng
或imagegif
函数来压缩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 '图片压缩失败!'; } ?>
在这个版本中,我们做了以下几点优化:
- 增加了
$type
参数,允许用户指定图片类型。如果未指定,则根据文件扩展名自动检测。 - 改进了
compressImage
函数,使其能够根据图片类型选择正确的压缩函数(imagejpeg
、imagepng
或imagegif
)。 - 提高了代码的可读性和可维护性,通过添加注释和类型提示。 请注意,这个优化版本仍然假设你正在处理的是本地文件系统中的文件。如果你需要从URL或其他源加载图片,你可能需要添加额外的逻辑来处理这些情况。此外,对于大文件或高流量的网站,你可能还需要考虑内存使用情况和执行时间,并可能需要使用更高级的图像处理库或工具。
希望能够帮助到有需要的你!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容