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);
Related
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 made simple text editor and now working on image upload and image manager. I have set up manager to read .json file with all images and it works ok. The problem is for php script to actually write newly added images to that json.
$file = "images.json";
$arr_data = array();
foreach(glob('/uploads/*') as $image) {
$arr_data = array(
'link' => $image,
'tag' => 'images',
);
}
$jsondata = file_get_contents($file);
$arr_data = json_decode($jsondata, true);
array_push($arr_data,$jsondata);
$jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);
file_put_contents($file, $jsondata));
I am getting
Warning: array_push() expects parameter 1 to be array
even tho array data is provided. How to solve this?
If you are starting with an empty file i.e. images.json then the first time you run these 2 lines
$jsondata = file_get_contents($file);
$arr_data = json_decode($jsondata, true);
the second line will change $arr_data into a boolean probably. As json_decode() will fail to convert nothing into an array.
So add this to initialize the file for use
<?php
$file = "images.json";
file_put_contents($file, '[]'); // init the file
You are also reusing the $arr_data variable so amend this also and you are overwriting the new array as well
$file = "images.json";
file_put_contents($file, '[]'); // init the file
$arr_data = array();
foreach(glob('/uploads/*') as $image) {
// amended to not reuse $arr_data
// ameded to not overwrite the array as you build it
$new_arr[] = array( 'link' => $image, 'tag' => 'images');
}
$jsondata = file_get_contents($file);
$arr_data = json_decode($jsondata, true);
array_push($arr_data,$new_arr);
$jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);
file_put_contents($file, $jsondata));
I want to save a base64 string as an image (.png or .jpg) on my server.
The string I get looks like this:
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQA[...]"
I tried this:
$data = $_POST['form_new_data'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents("/userImgs/pic.jpg",$data);
but it is not working (maybe because the base64 string is a jpeg?).. Please help..
try this
$data = $_POST['form_new_data'];
file_put_contents('img.jpg', base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data)));
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.
i have a problem with my website many hackers came to me and stealing members cookies and redirecting to their website . I searched a little bit and i found a script that blocks xss exploiting but i am new to php and i don't know how to use it.
I tried to use include and the name of the php file.
This script:
/*
* XSS filter
*
* This was built from numerous sources
* (thanks all, sorry I didn't track to credit you)
*
* It was tested against *most* exploits here: http://ha.ckers.org/xss.html
* WARNING: Some weren't tested!!!
* Those include the Actionscript and SSI samples, or any newer than Jan 2011
*
*
* TO-DO: compare to SymphonyCMS filter:
* https://github.com/symphonycms/xssfilter/blob/master/extension.driver.php
* (Symphony's is probably faster than my hack)
*/
function xss_clean($data)
{
// Fix &entity\n;
$data = str_replace(array('&','<','>'), array('&','<','>'), $data);
$data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
$data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
$data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');
// Remove any attribute starting with "on" or xmlns
$data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);
// Remove javascript: and vbscript: protocols
$data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);
// Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);
// Remove namespaced elements (we do not need them)
$data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);
do
{
// Remove really unwanted tags
$old_data = $data;
$data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
}
while ($old_data !== $data);
// we are done...
return $data;
}
How to use it ? Please explain where to put it ?
You need to include this file, then wherever you read anything from the client (or to be more precise: where you output your client's input) you need to replace $_GET['something'] with xss_clean($_GET['something']) and $_POST['sth'] with xss_clean($_POST['sth'])