imagejpeg不压缩图像文件

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

你能告诉我原因吗?</ p>

  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);
imaged estroy($ 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);
返回$ files_picture;
}
</ code> </ pre>
</ div>

展开原文

原文

I would like to compress image file uploaded by users. However, the size of the image file after imagejpeg is the same size before imagejpeg.

Could you tell me why?

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;
}

doulu3808
doulu3808 我想你应该先用move_uploaded_file移动上传的文件。因为如果您写入临时目录,它会导致一些权限问题。
2 年多之前 回复
doukuilian8365
doukuilian8365 我会再次尝试确认,谢谢!
2 年多之前 回复
dotcraq3249
dotcraq3249 是的,jpeg图像尺寸已更改为400x400,质量已压缩至30%。
2 年多之前 回复
douxuqiao6394
douxuqiao6394 这张照片真的被压缩了吗?
2 年多之前 回复
dongtingxiao4697
dongtingxiao4697 你的脚本有效(在我的电脑上)。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐