imagejpeg不压缩图像文件

时间:2018-02-17 14:41:57

标签: php resize compression imagejpeg

我想压缩用户上传的图片文件。但是,imagejpeg之后的图像文件的大小与imagejpeg之前的大小相同。

你能告诉我为什么吗?

function compressImage($files_picture){
   $file_type = $files_picture['type']; // image/jpeg
   $original_picture_info =getimagesize($files_picture['tmp_name']);
   $original_picture_width = $original_picture_info[0];
   $original_picture_height = $original_picture_info[1];
   $new_picture_width = 400;
   $new_picture_height = 400;
   $canvas = imagecreatetruecolor($new_picture_width, $new_picture_height);



    if($file_type == 'image/jpeg'){
        $original_image = imagecreatefromjpeg($files_picture['tmp_name']);
        imagecopyresampled($canvas, $original_image, 0,0,0,0, $new_picture_width, $new_picture_height, $original_picture_width, $original_picture_height);
        imagedestroy($original_image);
        imagejpeg($canvas, $files_picture['tmp_name'], 30);
    }elseif($file_type == 'image/png'){
        $original_image = imagecreatefrompng($files_picture['tmp_name']);
        imagecopyresampled($canvas, $original_image, 0,0,0,0, $new_picture_width, $new_picture_height, $original_picture_width, $original_picture_height);
        imagedestroy($original_image);
        imagepng($canvas, $files_picture['tmp_name'], 30);
    }elseif($file_type == 'imagegif'){
        $original_image = imagecreatefromgif($files_picture['tmp_name']);
        imagecopyresampled($canvas, $original_image, 0,0,0,0, $new_picture_width, $new_picture_height, $original_picture_width, $original_picture_height);
        imagedestroy($original_image);
        imagegif($canvas, $files_picture['tmp_name'], 30);
    }
    imagedestroy($canvas);
    return $files_picture;
}

0 个答案:

没有答案