php imagejpeg 画质,php imagejpeg() function breaks png images

原文:

I have a php function named compressImage() that looks like:

function compressImage($source, $destination, $quality) {

$info = getimagesize($source);

if ($info['mime'] == 'image/jpeg')

$image = imagecreatefromjpeg($source);

elseif ($info['mime'] == 'image/gif')

$image = imagecreatefromgif($source);

elseif ($info['mime'] == 'image/png')

$image = imagecreatefrompng($source);

imagejpeg($image, $destination, $quality);

}

I am trying to compress uploaded images using this function. In a php file named add1.php I call this function like this:

if (!empty($_FILES['image1']['name'])) {

$temp = explode(".", $_FILES["image1"]["name"]);

$newName = date('Ymdhis')."1";

$newName = strval($newName) . "." . end($temp);

$newPath = '../uploads/'.$newName;

move_uploaded_file($_FILES['image1']['tmp_name'], $target_dir . $newName);

compressImage($newPath, $newPath, 60);

$image1 = $newName;

}

And this works when I upload jpeg images and it actually reduces their size. But when I upload png files they are uploaded but are broken and cannot be opened. I do not have any idea what the cause of this problem is. Any help would be appreciated. Thank you

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

You could modify the compressImage so that it generates images in the same format as they are supplied and use the return value from the function for further processing if required.

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'] ) );

}

}

Using the return value

if( compressImage($newPath, $newPath, 60) ){ /* success */ }

else { /* Failure */ }

# Answer 2

In all cases you are writing the image using imagejpeg; also the GIF and PNG files.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值