在将图像发送到服务器之前对其进行压缩

编程入门 行业动态 更新时间:2024-10-26 07:41:16
本文介绍了在将图像发送到服务器之前对其进行压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

ByteArrayOutputStream baos = new ByteArrayOutputStream();bitmappress(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值)的时间.

我没有使用 Bitmap#compress 方法之前是这样,因此以上只是推测.

I am using this code to convert a Bitmap to Base64:

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

and receiving it at the server side, this way:

$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);

Problem:

The actual image size which i am picking from the device gallery to send to the server is 344 kb When i set the quality = 0 and show a spinner dialog util the base64 string is being sent to the server it takes 5 secs to send, and the image received at the server side is 344 Kb but if i am setting quality = 100 it takes 60-70 secs to send, and the image i receive at the server side is 1.7 Mb

Question:

why am i getting the actual size when using quality = 0 and nearly 5 times bigger image when quality = 100

Note:

When i am setting the quality = 100 and change

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

to

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

it takes 60-70 secs to send but the image received on the server side is too small 67 Kb

Thank You

解决方案

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.

更多推荐

在将图像发送到服务器之前对其进行压缩

本文发布于:2023-10-10 22:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479929.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对其   发送到   图像   服务器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!