I have function for image upload:
public function upload(UploadImageRequest $request)
{
$storagePath=Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
$image = $request->file('image')->storeAs('public/images', time().'_'.$request->file('image')->getClientOriginalName());
$url = Storage::url($image);
$image = Image::make([]);
$image->path = $storagePath.$url;
$image->url = "$url";
$image->saveOrFail();
return Response::json($image, ResponseHttp::HTTP_CREATED);
}
It's work fine, but I want to add fucntionality if frontend send me picture in base64. How it decode?
To decode base64 image use this -
$decode = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image));
Hope this will help you.
This is the value of variable which carrying an image. How can i save this to a server location using php?
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAD6CAYAAABXq7VOAAAgAElEQVR4XtS9CYxd13km+N1331asKpKSqMUyRVKkZNmSKImWRO1yvMTxFnlL4nacOE46PdONbkyjke6ZpCeTNKaBwWCABgaDAaYTTAODwaDTthVLXmJttqx9sSVZtkTtmy1RFCVxJ6vqbfcOvn8559z37itWSXSQLpuqqldvucs5//d/379lf/zHZ5cNZOBXGf6Tyc........
You can try this code.
$data = 'write your image code here';
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents('image.png', $data);
I try an API to get video content. The problem is, the return of the api is binary. How can I get a video file?
When I want to get an image, I just run this code:
$response = curl_exec($ch);
$aa = base64_encode($response);
$data = 'data:image/jpeg;base64,'.$aa;
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents('image2.jpg', $data);
but I think it doesn't work when I want to convert into video.
I am using the below code from the library
https://github.com/antimatter15/jsgif.
I tried to convert the code given below to a gif image file but could not achieve it.
var encoder = new GIFEncoder();
encoder.setRepeat(0);
encoder.start();
var st = '|100:167.1,D,Pencil,3,#000|100:167.1,U|100:167.1,D|102:163.1,M|135:125.1|206:97.1|289:99.1|323:134.1|3\
16:252.1|205:331.1|234:260.1|504:102.1|581:92.1|631:95.1|635:132.1|512:209.1,U|385:29.1,D,,,#66c|384:29.1,M|384:40.1|378:84.\
1|365:168.1|360:215.1|354:269.1|354:300.1|357:313.1|405:319.1|450:314.1|507:291.1|546:260.1|534:223.1|487:171.1|418:119.1|29\
0:131.1|220:201.1|168:284.1|157:339.1|170:353.1|256:308.1|348:212.1|390:118.1|399:77.1|397:68.1|298:82.1|176:168.1|90:265.1|\
70:290.1|70:292.1|250:150.1|465:14.1|594:-42.9|606:-39.9|550:29.1|442:170.1|335:322.1|258:430.1|258:430.1,U';
var frames_array = st.split('|');
$.each(frames_array,function(index,value) {
encoder.addFrame(value);
});
encoder.finish();
var binary_gif = encoder.stream().getData();
var data_url = 'data:image/gif;base64,'+encode64(binary_gif);
What I get for data_url is as given below
data:image/gif;base64,fDEwMDoxNjcuMSxELFBlbmNpbCwzLCMwMDB8MTAwOjE2Ny4xLFV8MTAwOjE2Ny4xLER8MTAyOjE2My4xLE18MTM1OjEyNS4xfDIwNjo5Ny4xfDI4OTo5OS4xfDMyMzoxMzQuMXwzMTY6MjUyLjF8MjA1OjMzMS4xfDIzNDoyNjAuMXw1MDQ6MTAyLjF8NTgxOjkyLjF8NjMxOjk1LjF8NjM1OjEzMi4xfDUxMjoyMDkuMSxVfDM4NToyOS4xLEQsLCwjNjZjfDM4NDoyOS4xLE18Mzg0OjQwLjF8Mzc4Ojg0LjF8MzY1OjE2OC4xfDM2MDoyMTUuMXwzNTQ6MjY5LjF8MzU0OjMwMC4xfDM1NzozMTMuMXw0MDU6MzE5LjF8NDUwOjMxNC4xfDUwNzoyOTEuMXw1NDY6MjYwLjF8NTM0OjIyMy4xfDQ4NzoxNzEuMXw0MTg6MTE5LjF8MjkwOjEzMS4xfDIyMDoyMDEuMXwxNjg6Mjg0LjF8MTU3OjMzOS4xfDE3MDozNTMuMXwyNTY6MzA4LjF8MzQ4OjIxMi4xfDM5MDoxMTguMXwzOTk6NzcuMXwzOTc6NjguMXwyOTg6ODIuMXwxNzY6MTY4LjF8OTA6MjY1LjF8NzA6MjkwLjF8NzA6MjkyLjF8MjUwOjE1MC4xfDQ2NToxNC4xfDU5NDotNDIuOXw2MDY6LTM5Ljl8NTUwOjI5LjF8NDQyOjE3MC4xfDMzNTozMjIuMXwyNTg6NDMwLjF8MjU4OjQzMC4xLFU=
I need to convert this encoded code to an gif image file. How can I achieve it further
try this , may this will help you
<?php
function base64url_decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
?>
Use this (PHP pseudo-code): http://php.net/manual/en/function.imagecreatefromstring.php
function imagegif_from_base64($base64)
{
return imagecreatefromstring(base64_decode(substr($base64, 22) /* remove the "data:image/gif;base64," string */));
}
related get image from base64 string
AS you are using jsgif library you You can do like this to decode image
$imgstr = 'data:image/gif;base64,fDEwMDoxNjcuMSxELFBlbmNpbCwzLCMwMDB8MTAwOjE2Ny4xLFV8MTAwOjE2Ny4xLER8MTAyOjE2My4xLE18MTM1OjEyNS4xfDIwNjo5Ny4xfDI4OTo5OS4xfDMyMzoxMzQuMXwzMTY6MjUyLjF8MjA1OjMzMS4xfDIzNDoyNjAuMXw1MDQ6MTAyLjF8NTgxOjkyLjF8NjMxOjk1LjF8NjM1OjEzMi4xfDUxMjoyMDkuMSxVfDM4NToyOS4xLEQsLCwjNjZjfDM4NDoyOS4xLE18Mzg0OjQwLjF8Mzc4Ojg0LjF8MzY1OjE2OC4xfDM2MDoyMTUuMXwzNTQ6MjY5LjF8MzU0OjMwMC4xfDM1NzozMTMuMXw0MDU6MzE5LjF8NDUwOjMxNC4xfDUwNzoyOTEuMXw1NDY6MjYwLjF8NTM0OjIyMy4xfDQ4NzoxNzEuMXw0MTg6MTE5LjF8MjkwOjEzMS4xfDIyMDoyMDEuMXwxNjg6Mjg0LjF8MTU3OjMzOS4xfDE3MDozNTMuMXwyNTY6MzA4LjF8MzQ4OjIxMi4xfDM5MDoxMTguMXwzOTk6NzcuMXwzOTc6NjguMXwyOTg6ODIuMXwxNzY6MTY4LjF8OTA6MjY1LjF8NzA6MjkwLjF8NzA6MjkyLjF8MjUwOjE1MC4xfDQ2NToxNC4xfDU5NDotNDIuOXw2MDY6LTM5Ljl8NTUwOjI5LjF8NDQyOjE3MC4xfDMzNTozMjIuMXwyNTg6NDMwLjF8MjU4OjQzMC4xLFU=';
$new_data=explode(";",$imgstr);
$type=$new_data[0];
$imgdata=explode(",",$new_data[1]);
$imgstr = base64_decode($imgdata[1]);
// or choose any name instead sample.gif
file_put_contents("sample.gif", base64_decode($imgstr));
If you just wanna use PHP try this:
$str = 'data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAA...';
$data = str_replace('data:image/gif;base64,', '', $str);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data); // base64 decoded image data
$source_img = imagecreatefromstring($data);
$file = uniqid() .'.gif';
$success = file_put_contents($file, $data);
This will save the .gif image in the same folder the php file is located.
my data
this1
this2
this3
this4
what I want
$keepit[0]='this1';
$keepit[1]='this2';
$keepit[2]='this3';
$keepit[3]='this4';
data uri
data:text/plain;charset=utf-8;base64,dGhpczENCnRoaXMyDQp0aGlzMw0KdGhpczQ=
Is it possible to do this?
You can do this with normal file methods using the data:// protocol:
$data = file('data://text/plain;charset=utf-8;base64,dGhpczENCnRoaXMyDQp0aGlzMw0KdGhpczQ=');
Alternatively though you can pull out the base64 encoded string and decode it yourself:
$plaintext = base64_decode('dGhpczENCnRoaXMyDQp0aGlzMw0KdGhpczQ=');
If the encoded string can be decoded this will work:
$file_path = ''; // change this.
$fp = fopen($file_path, 'rb');
$contents = fread($handle, filesize($file_path));
fclose($fp);
$data_uri = preg_split('/,/', $contents);
$encoded = $data_uri[1];
$decoded = base64_decode($encoded);
var_dump($decoded);