2 回答
TA贡献1735条经验 获得超5个赞
您可以修改compressImage它,使其生成与提供的格式相同的图像,并在需要时使用函数的返回值进行进一步处理。
function compressImage( $source=false, $destination=false, $quality=80, $filters=false ) {
$info = getimagesize( $source );
switch( $info['mime'] ){
case 'image/jpeg':
/* Quality: integer 0 - 100 */
if( !is_int( $quality ) or $quality < 0 or $quality > 100 ) $quality=80;
$image = imagecreatefromjpeg( $source );
return imagejpeg( $image, $destination, $quality );
case 'image/gif':
$image = imagecreatefromgif( $source );
return imagegif( $image, $destination );
case 'image/png':
/* Quality: Compression integer 0(none) - 9(max) */
if( !is_int( $quality ) or $quality < 0 or $quality > 9 )$quality=6;
$image = imagecreatefrompng( $source );
return imagepng( $image, $destination, $quality, $filters );
case 'image/webp':
/* Quality: Compression 0(lowest) - 100(highest) */
if( !is_int( $quality ) or $quality < 0 or $quality > 100 )$quality=80;
$image=imagecreatefromwebp( $source );
return imagewebp( $image, $destination, $quality );
case 'image/bmp':
/* Quality: Boolean for compression */
if( !is_bool( $quality ) )$quality=true;
$image=imagecreatefrombmp( $source );
return imagebmp( $image, $destination, $quality );
default:exit( sprintf( 'Unknown type: %s',$info['mime'] ) );
}
}
使用返回值
if( compressImage($newPath, $newPath, 60) ){ /* success */ }
else { /* Failure */ }
- 2 回答
- 0 关注
- 162 浏览
添加回答
举报