They Say It.

They say it was easy to do your stuff in PHP. Well, I have no clue how to do it. This is my (supposed to be a) function:

1. Read a jpeg
2. Convert the quality for $x amount to reduce space.
3. Base64 it
4. Check image size.
4. a. If image size > MAX_LEN then go to step (2) with $x lowered.
4. b. else encode with JSON then send it to client.

The objective is to have a BASE64 image with size bound to a certain size.

Already in my knowledge:
1. BASE64 en/decoding
2. Webservice and JSON stuff.
3. Convert via ImageMagicK

Not in my knowledge rite now:
1. Read/Write a file.
2. File length.

Comments are welcome.


  1. to check file size, you can use filesize()
    and if you want to check base64encoded size, just multiply it with (4/3).

    or.. if you add padding and line breaking, use this formula

    base64size = ceil(ceil(original_size * 4/3) / 76)

    substitute 76 with the length for each base64 encoded data.

  2. ah jepe.. you don't even need to read the file. just let the imagemagick read and write the file.

    $limit = 12345;
    $b64size = $limit + 1;
    while ($b64size > $limit) {
    exec("convert -quality $quality $infile $outfile");
    $size = filesize($outfile);
    $b64size = ceil(ceil($size * 4/3)/76);

    $b64data = base64_encode(file_get_contents($outfile));

    cmiiw.. *blind coding* :P

  3. whooo.. i made a mistake :P

    base64size = ceil(original_size * 4/3) + cail(ceil(original_size * 4/3) / 76)

  4. jpmrblood9:20 AM



Post a Comment

Popular Posts