eladcm
eladcm

Reputation: 582

Convert base64 to image resize and encode in php

I am using html5 file api to preview a image in browser and then saving it to database as base64 string using ajax.

I need to reduce the quality in order to resize the image.

<?php

$img = $_POST['img'];
$i = explode(",",$img);

$img = $i[1];
$img = imagecreatefromstring[$img];
$rimg = imagejpeg($img,XXXXXXX, 60);

?>

xxx=> how could I turned it back to base64?

thanks in advance.

Upvotes: 0

Views: 2422

Answers (1)

gia
gia

Reputation: 757

From http://php.net/manual/en/function.imagejpeg.php

 /*
 * imageXXX() only has two options, save as a file, or send to the browser.
 * It does not provide you the oppurtunity to manipulate the final GIF/JPG/PNG file stream
 * So I start the output buffering, use imageXXX() to output the data stream to the browser,
 * get the contents of the stream, and use clean to silently discard the buffered contents.
 */
ob_start();

switch ($image_type)
{
    case 1: imagegif($tmp); break;
    case 2: imagejpeg($tmp, NULL, 100);  break; // best quality
    case 3: imagepng($tmp, NULL, 0); break; // no compression
    default: echo ''; break;
}

$final_image = ob_get_contents();

ob_end_clean();

return $final_image;

or save to the file, then load the file as binary stream, base 64 the binary and delete the file

Upvotes: 2

Related Questions