Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get image from base64 string

Tags:

php

I am trying to take a base64 encoded string and return it as an image in php using $_POST. On line one if I use $_POST['imgdata'] it returns error from the preg_match if i were hard code the base64 string instead of using $_POST it all works and returns the image. how can i make this work by using the $_POST

works

$imgstr = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAAAoCAYAAAC7HLUcAAADtUlEQVR4Xu2aLZYqMRCFMxuAFaARg8WwAnaABQEWgwUBFoMFARaDRqAxWBBoVgAbmPcu7/Q7PX3SP0k16TTcnDNqOpXKrXykKsmXUurn7x+bvQJf9l3Z03cFEFwCIosSAZHp53VvAiIPDwGRa+itBQIiDw0BkWvorQUCIg8NAZFr6K0FAiIPDQGRa+itBQIiDw0BkWvorQUCIg8NAZFr6K0FAiIPDQGRa+itBTEg7XZb1Wo11Wg0VKVS+T/Rx+Ohzuezut1uar/feyVAp9NR8Dvaer2ejZ8ExEa1kvSxBgQLbDQaPeFIa4BkPp97AQr8XiwWWpfr9XraVHT/JyA2qpWkjxUg+AWeTqfGU5xMJmq73Rr3y6vD9/e32mw2v3a6sG0CkpfS72PHGBAsst1uZ60A0pjj8Wjd37Yj0j/AAf/jGgGxVfd9+xkDgp0DO0i0oeZYLpfqcrk8F+FgMND+UqMeGQ6HzhVdr9eq1WoljktAnIfF+wGNATmdTtqFH02fsBixKHUgNZtNp8LEQR11goA4DUspBjMCJC69wu6hW/TX6zXPYthKUF29BH/DJ26BYQJiJfFbdzICJFAinKoAGiy4aPGN063D4VDoDqIDGr7iRE13yEBA3nqtW03OCpAsI+EoVXfX4KoGAaA4TIjezXS7XVWtVrXpHwHJEtnP+uYlgCTl/C5OseJOrII6Ka4+IiCftfizzDZXQLAwcXmoO+WCM652D93uhbRqtVo9NSEgWZYGv4ECuQGClAYLM+6eAce/SG9QA7yy9fv9J6ThhvoIu0e4htKdsHEHeWVkymk7F0BQa8xms9gbalwM4u7j1XDoTqx0YHIHKediLcJrMSC6X+zwRJDWIL1x0XR3NBj/fr//Gh67nS4NDPzUncol+M+3WC6CW9AYIkCSinEssvF47PSBYty9i6m22PEMXvYSEFOBS/S9NSBJcLhKqaI6E5ASrbySuGoFSFJaFS2IXepAQFyq/RljGQOS9po3y0td5PoonvNuBCRvRWnPGJAsr2LTZH3VZWHaa93AL0AePQrG/4K6A0W9AcCsQdICXuL/GwES977KdP6vAiSrHzzmzaoUvzMCJO1IN6ucBCSrUvyuaAWMAIl7gGg6CQJiqhi/L0oBI0CKctLzcVmDeB4giXsERKLev74ERK6htxYIiDw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcW/gDZOWY4lzJl1QAAAABJRU5ErkJggg==';

does not work

$imgstr = $_POST['imgdata'];

full code

$imgstr = $_POST['imgdata'];   
// Grab the MIME type and the data with a regex for convenience
if (!preg_match('/data:([^;]*);base64,(.*)/', $imgstr, $matches)) {
    die("error");
}

// Decode the data
$content = base64_decode($matches[2]);

// Output the correct HTTP headers (may add more if you require them)
header('Content-Type: '.$matches[1]);
header('Content-Length: '.strlen($content));

// Output the actual image data
echo $content;
like image 508
Daniel Avatar asked Jul 20 '12 00:07

Daniel


2 Answers

As was noted the "+" should not be missed, the rest is straight forward. Use $_REQUEST if you are not sure is it post or get.

// requires php5
define('UPLOAD_DIR', 'images/');
$img = $_REQUEST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
like image 63
Logic Wreck Avatar answered Oct 19 '22 21:10

Logic Wreck


This is the One., Would you like the following i think.,

<?php
$imgstr = 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAAAoCAYAAAC7HLUcAAADtUlEQVR4Xu2aLZYqMRCFMxuAFaARg8WwAnaABQEWgwUBFoMFARaDRqAxWBBoVgAbmPcu7/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ERK6htxYIiDw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcWCIg8NARErqG3FgiIPDQERK6htxYIiDw0BESuobcW/gDZOWY4lzJl1QAAAABJRU5ErkJggg==';
$new_data=explode(";",$imgstr);
$type=$new_data[0];
$data=explode(",",$new_data[1]);
header("Content-type:".$type);
echo base64_decode($data[1]);
?>

The above code will generates the output as per your wish.,Try this..This may help you.

Sorry For The Delay Reply.,Now Only I Recover My StackOverFlow Account After a long time.

Note : Ignore the empty space's in before the starting '' tag on the PHP file you are using for image decode.,If any empty is on that file means it throws Error message and it doesn't create or show the original image after the decodes.

like image 38
John Peter Avatar answered Oct 19 '22 21:10

John Peter