在将图像发送到服务器之前压缩图像

时间:2012-10-23 12:19:41

标签: php android base64

我正在使用此代码将位图转换为 Base64

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, **quality**, baos);
byte[] b = baos.toByteArray();
base64code = Base64.encodeToString(b, Base64.DEFAULT);

并在服务器端接收它,这样:

$strImage = preg_replace('!\s*!', '', trim($this->input->post('image')));
        $thefile = base64_decode($strImage);
        $img = imagecreatefromstring($thefile);
        //header('Content-Type: image/jpeg');
        header('Content-Type: bitmap; charset=utf-8');
        imagesavealpha($img, true);
        imagejpeg($img,'./images/temp/testing.jpg',100);
        imagedestroy($img);

问题:

我从设备库中选择要发送到服务器的实际图像大小 344 kb 当我设置 quality = 0 并显示微调器对话框util base64 字符串发送到服务器需要 5秒才能发送,服务器端收到的图像 344 Kb 但是我设置质量= 100 需要 60-70秒才能发送,而我在服务器端收到的图片 1.7 Mb

问题:

质量= 100

时,为什么在使用 quality = 0 5倍图像时获得实际尺寸?

注意:

当我设置质量= 100 并更改

imagejpeg($img,'./images/temp/testing.jpg',100);

imagejpeg($img,'./images/temp/testing.jpg',10);

发送 60-70秒,但服务器端收到的图片太小 67 Kb

谢谢

2 个答案:

答案 0 :(得分:1)

当PHP指出0表示可能的最低质量时,PHP手册是错误的,它实际上意味着不接触压缩。

至于为什么压缩你的照片需要很长时间才很难说,你自己主持这个还是使用便宜的虚拟主机?

答案 1 :(得分:1)

我认为@EJTH是正确的,0可能意味着根本不重新压缩图像。任何其他值(1-100)可能首先将您的图像转换为位图(这将非常大)然后压缩到目标质量jpeg。这种重新压缩需要一段时间才能完成,因此对于0以外的值,您可以看到60-70秒。

之前我没有使用过Bitmap#compress方法,所以上面就是推测。