1

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

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(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

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

2 Answers2

1

The PHP manual is wrong when it states that 0 means lowest possible quality, what it actually means is don't touch the compression.

As to WHY it takes that long to compress your pictures is hard to say, are you hosting this yourself or are you using a cheap webhost?

EJTH
  • 2,178
  • 1
  • 20
  • 25
  • PHP manual?? Do you mean Android? Because i never tried setting 0 in PHP. In Android when i use quality = 0 i get actual image size but when i use quality = 100 i get 5 times bigger image – Archie.bpgc Oct 23 '12 at 12:44
  • Ah okay, a misunderstanding, as you are mentioning PHP functions which also takes a quality parameter from 0 to 100. – EJTH Oct 23 '12 at 12:47
  • But what about your hosting? Since it doesnt seem like you have made any real profiling on the issue, im assuming that what actually takes time is the communication between your app and your server, i might be wrong, if you dont do that imagejpeg() call, do you still have to wait 60-70seconds for the image to send? If not its a PHP issue. – EJTH Oct 23 '12 at 12:50
1

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.

dave.c
  • 10,910
  • 5
  • 39
  • 62