在将图像发送到服务器之前对其进行压缩 [英] Compressing an image before sending it to the Server

查看:55
本文介绍了在将图像发送到服务器之前对其进行压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码将位图转换为 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:位图; charset = utf-8');imagesavealpha($ img,true);imagejpeg($ img,'./images/temp/testing.jpg',100);imagedestroy($ img); 

问题:

我从设备库中选择要发送到服务器的实际图像大小为 344 kb ,当我设置 quality = 0 并显示微调器对话框实用程序时 base64 字符串被发送到服务器需要花费 5秒的时间,并且服务器端收到的图像为 344 Kb ,但如果我将 quality = 100 设置为需要 60-70秒进行发送,而在服务器端收到的图像为 1.7 Mb

问题:

为什么我使用 quality = 0 时会得到实际尺寸,而当 quality = 100

时会比图像大近 5倍

注意:

当我设置 quality = 100 并进行更改

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

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

发送消息需要 60-70秒,但是服务器端接收到的图像太小 67 Kb

谢谢

解决方案

我认为@EJTH是正确的,因为0可能意味着完全不重新压缩图像.任何其他值(1-100)可能首先是将图像转换为位图(这将非常大),然后压缩为目标质量jpeg.重新压缩需要花费一定的处理时间,因此您看到60到70秒(非0值)的时间.

我没有使用 解决方案

I would think that @EJTH is correct in that 0 might mean to not re-compress the image at all. Any other value (1-100) is probably first converting your image to a bitmap (which would be very large) then compressing to the target quality jpeg. This re-compression takes processing time to complete so you see 60-70 secs for values other than 0.

I've not used the Bitmap#compress method before though, so the above is speculation.

这篇关于在将图像发送到服务器之前对其进行压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆