Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing base64 Images

I have multiple Images - saved as Base64 Strings and now i want to resize these images to get thumbnails of them...

Best would be using Javascript (Node-Server) to resize them, but it would be possible to resize them with php, too.

Thanks in advance

like image 631
Lukas Olsen Avatar asked Sep 01 '12 23:09

Lukas Olsen


People also ask

Does Base64 encoding reduce size of image?

Download size increase Although Base64 is a relatively efficient way of encoding binary data it will, on average still increase the file size for more than 25%. This not only increases your bandwidth bill, but also increases the download time.

Does Base64 encoding increase size?

Encoded size increase This means that the Base64 version of a string or file will be at least 133% the size of its source (a ~33% increase). The increase may be larger if the encoded data is small.

Is it good to store images as Base64?

You can encode and decode on the fly (without knowing the total size of the data). While base64 is fine for transport, do not store your images base64 encoded. Base64 provides no checksum or anything of any value for storage. Base64 encoding increases the storage requirement by 33% over a raw binary format.


2 Answers

I agree to the method from Jon Hanna: Do Parsing the Base64code then load it to GD Image before Resample. However to get it back as data it is not as easy as I though. On php in GAE it will need to enable output buffering by setting output_buffering = "On" in php.ini file.

Here I explain the step in detail.

This doc is taken as reference to Create Image Resource using the Parsing of Base64code: http://php.net/manual/en/function.imagecreatefromstring.php

// Create image resource from Base64code
$data64 = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
        . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
        . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
        . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$image = imagecreatefromstring(base64_decode($data64));

This is an image resource which can be directly put to the Resample function: http://php.net/manual/en/function.imagecopyresampled.php

// Resample
$image_p = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_w, $new_h, $org_w, $org_h);

The result is also an image resource. To get it as a data we need Buffering.
See how to create a base64encoded string from image resource

// Buffering
ob_start();
imagepng($image_p);
$data = ob_get_contents();
ob_end_clean();

Using doc below I set a GCS bucket on my project as a website so I can Store & Display it directly: https://cloud.google.com/storage/docs/website-configuration#tips

//Store & Display
$context = stream_context_create([
   'gs' =>[
        'acl'=> 'public-read', 
        'Content-Type' => 'image/jpeg', 
        'enable_cache' => true, 
        'enable_optimistic_cache' => true,
        'read_cache_expiry_seconds' => 300,
    ]
]);
file_put_contents("gs://mybucket/resample/image.jpeg", $data, false, $context); 
header("Location: http://mybucket/resample/image.jpeg");
like image 63
Chetabahana Avatar answered Sep 28 '22 10:09

Chetabahana


Your best bet is to use PHPThumb in PHP.

An alternative is to invoke ImageMagick however you prefer:

  • http://coffeeshopped.com/2009/01/creating-image-thumbnails-using-php-and-imagemagick

  • http://www.hacksparrow.com/node-js-image-processing-and-manipulation.html

like image 37
paulsm4 Avatar answered Sep 28 '22 08:09

paulsm4