Php base64 to blob

Convert base64 image string to blob in PHP

Try to save the picture using file_get_contents.

$sql = "INSERT INTO Table(columns, . ) VALUES(. '" . file_get_contents($yourImage) . "')"; 

You can further read THIS thread

Also you can encode the image with base64_encode(if you want to)

More Answer

  • Convert base64 image string to IE compliant image, PHP only
  • How to convert base64 to image in php POST form
  • keditor convert blob generated image URL to base64
  • Convert string to image with PHP
  • JavaFX Chart to Image to Base64 String use in PHP
  • Convert base64 to image resize and encode in php
  • Convert particular String to Array in PHP
  • Use PHP Blob Value from MySQL Database as background image for simple website
  • How to convert base64 string to video in PHP?
  • PHP convert String number with whitespace to Float
  • How to convert string number to int number using php
  • convert php string to json object
  • PHP Convert RGB Array to String variable
  • How to convert PHP string as name of a function and call the function
  • PHP compress/reduce blob image data size
  • convert an array to string php
  • Convert string to number in PHP
  • How to Convert the below string into the array using php
  • PHP Convert image to specific height and width without stretching
  • Unable to convert string to float in php
  • How to Properly Convert String to Associative Array in PHP
  • PHP how to convert a complicated Pattern String into 2 Arrays?
  • PHP string concatenation as image watermark
  • Convert 2 bytes digits eg.’ 0’,’1’ to single byte digits eg. ‘0’,’1′ in a string using php
  • Convert value from HTML input tag as a string in PHP
  • Loop through a string and convert to $_POST in PHP
  • convert a date string using PHP
  • (android + PHP) — convert string to image and store it in database
  • Correctly convert string to UTF-8 via PHP
  • Decode json string using Base64 in php with json_decode
  • Convert PHP String to 1 UTF-8 character
  • Convert md5 in base64 in md5 of 32 characters with PHP
  • PHP Convert string to int and calculate
  • Cannot display blob image from database using PHP
  • Decode image object from binary string in ImageMagick for PHP
  • Convert javascript array into a php unserializable string
  • how to upload image in php using base64
  • PHP to convert string to array
  • Convert PDF portrait to image landscape using Imagemagick and PHP
  • How to convert the object resource into string and store it in a variable in php
  • Convert ambigious date string to unix timestamp in PHP
  • PHP help with image source changing depending on date?
  • A continues series of characters in a string how do I split it at a certain point e.g 30 charaacters in php
  • Display Image to Browser Directly from PC source using PHP
  • string replace php — one word to more words
  • in string spec. symbol, php
  • Hide / Show divs from url string in php
  • Replace string in php
  • Scaling image from php with external tool
  • Need to convert é to e in PHP
Читайте также:  Send email via python

More answer with same ag

  • PHP regex to extract both open parenthesis and the following text
  • How do I remove double quotes from string PHP?
  • Loop over Array in array
  • Can not fetch data from database, when the connect is right
  • How to pass Data From JQuery AJAX to PHP Without submit
  • Preg match all greedy with exception for string
  • How microtime works in php
  • Send php mail from html code
  • array_sum numbers with different comma not equal to zero but long number
  • cannot store in multidimensional session array
  • Create multidimensional array PHP
  • Convert java encryption method in php
  • Access new Config file created in custom classes in Laravel5
  • Can’t Insert Data Into Tables Containing Auto Increment Primary Key Using PHP Prepared Statements
  • PHP Repeat n times inside a function
  • PHP undefined index when adding to SQL
  • How do I show several webpages in PHP with sleep?
  • How to get rid of keys and values ​while leaving some keys and values ​in a multidimensional array
  • failsafe for networked robot
  • Is there a smarter and more important functioning way to display the products from my database in my shop in a way i can sort them later?
  • Generate Laravel URL using URI segments instead of query string
  • DocuSign Embedded returns timed out exception?
  • laravel route:any page expired to inactivity
  • Show information only after complete form
  • login process more than 1 user id
  • Understanding Object Instances and Creation in PHP
  • PHP Variables with the same name
  • Composer Install Package Dependencies
  • Unsupported operand types after PDO conversion
  • Trouble with auto refresh in table
  • How to avoid using eval for a particular string of variables that needs to be executed
  • in_array function, why this doesn’t work?
  • Internal Client on Thruway WAMP2
  • Sort array of objects using object’s value
  • Laravel 5.2 — convert json key values to array
  • Calling Google Maps Api by ajax doesn’t work
  • Curl upload file goes as post variable with path
  • PHPMailer autoload.php not able to open
  • PHP — Detect string if contains only HTML tags or text with HTML tags
  • Undefined variable outside a foreach loop
  • PHP load all lines between 2 lines matching strpos and move to array
  • Using ajax to print «testing» to the screen when you click a label
  • list style image in tcpdf
  • Oracle INSERT ALL with from select result
  • How to receive XML data produced by a website(webservice) in PHP?
  • Cannot connect to ftp through PHP
  • How to create a hierarchical folder from an integer in PHP?
  • How to change name of every file in folder
  • How to format different types of dates into one?
  • Function Code coverage not 100%
Читайте также:  Найти четные элементы массива python

Источник

base64_encode

Эта кодировка предназначена для корректной передачи бинарных данных по протоколам, не поддерживающим 8-битную передачу, например, для отправки тела письма.

Данные, закодированные base64 занимают на 33% больше места по сравнению с оригинальными данными.

Список параметров

Возвращаемые значения

Кодированные данные в виде строки.

Примеры

Пример #1 Пример использования base64_encode()

Результат выполнения данного примера:

0K3RgtC+INC30LDQutC+0LTQuNGA0L7QstCw0L3QvdCw0Y8g0YHRgtGA0L7QutCw

Смотрите также

  • base64_decode() — Декодирует данные, закодированные MIME base64
  • chunk_split() — Разбивает строку на фрагменты
  • convert_uuencode() — Кодирует строку в формат uuencode
  • » RFC 2045 раздел 6.8

User Contributed Notes 37 notes

For anyone interested in the ‘base64url’ variant encoding, you can use this pair of functions:

function base64url_encode ( $data ) <
return rtrim ( strtr ( base64_encode ( $data ), ‘+/’ , ‘-_’ ), ‘=’ );
>

function base64url_decode ( $data ) <
return base64_decode ( str_pad ( strtr ( $data , ‘-_’ , ‘+/’ ), strlen ( $data ) % 4 , ‘=’ , STR_PAD_RIGHT ));
>
?>

gutzmer at usa dot net’s ( http://php.net/manual/en/function.base64-encode.php#103849 ) base64url_decode() function doesn’t pad longer strings with ‘=’s. Here is a corrected version:

function base64url_encode ( $data ) return rtrim ( strtr ( base64_encode ( $data ), ‘+/’ , ‘-_’ ), ‘=’ );
>

function base64url_decode ( $data ) return base64_decode ( strtr ( $data , ‘-_’ , ‘+/’ ) . str_repeat ( ‘=’ , 3 — ( 3 + strlen ( $data )) % 4 ));
>

// proof
for( $i = 0 , $s = » ; $i < 24 ; ++ $i , $s .= substr ( " $i " , - 1 ))$base64_encoded = base64_encode ( $s );
$base64url_encoded = base64url_encode ( $s );
$base64url_decoded = base64url_decode ( $base64url_encoded );
$base64_restored = strtr ( $base64url_encoded , ‘-_’ , ‘+/’ )
. str_repeat ( ‘=’ ,
3 — ( 3 + strlen ( $base64url_encoded )) % 4
);
echo » $s
$base64url_decoded
$base64_encoded
$base64_restored
$base64url_encoded

» ;
>
?>

In PHP 7, the padding issue with base64_decode() is no more — the following is totally fine:

function base64_encode_url($string) return str_replace([‘+’,’/’,’=’], [‘-‘,’_’,»], base64_encode($string));
>

function base64_decode_url($string) return base64_decode(str_replace([‘-‘,’_’], [‘+’,’/’], $string));
>

Checked here with random_bytes() and random lengths:

Base64 encoding of large files.

Base64 encoding converts triples of eight-bit symbols into quadruples of six-bit symbols. Reading the input file in chunks that are a multiple of three bytes in length results in a chunk that can be encoded independently of the rest of the input file. MIME additionally enforces a line length of 76 characters plus the CRLF. 76 characters is enough for 19 quadruples of six-bit symbols thus representing 19 triples of eight-bit symbols. Reading 57 eight-bit symbols provides exactly enough data for a complete MIME-formatted line. Finally, PHP’s default buffer size is 8192 bytes — enough for 143 MIME lines’ worth of input.

So if you read from the input file in chunks of 8151 (=57*143) bytes you will get (up to) 8151 eight-bit symbols, which encode as exactly 10868 six-bit symbols, which then wrap to exactly 143 MIME-formatted lines. There is no need to retain left-over symbols (either six- or eight-bit) from one chunk to the next. Just read a chunk, encode it, write it out, and go on to the next chunk. Obviously the last chunk will probably be shorter, but encoding it is still independent of the rest.

while(! feof ( $input_file ))
$plain = fread ( $input_file , 57 * 143 );
$encoded = base64_encode ( $plain );
$encoded = chunk_split ( $encoded , 76 , «\r\n» );
fwrite ( $output_file , $encoded );
>

?>

Conversely, each 76-character MIME-formatted line (not counting the trailing CRLF) contains exactly enough data for 57 bytes of output without needing to retain leftover bits that need prepending to the next line. What that means is that each line can be decoded independently of the others, and the decoded chunks can then be concatenated together or written out sequentially. However, this does make the assumption that the encoded data really is MIME-formatted; without that assurance it is necessary to accept that the base64 data won’t be so conveniently arranged.

Unfortunately my «function» for encoding base64 on-the-fly from 2007 [which has been removed from the manual in favor of this post] had 2 errors!
The first led to an endless loop because of a missing «$feof»-check, the second caused the rare mentioned errors when encoding failed for some reason in larger files, especially when
setting fgets($fh, 2) for example. But lower values then 1024 are bad overall because they slow down the whole process, so 4096 will be fine for all purposes, I guess.
The error was caused by the use of «empty()».

Here comes the corrected version which I have tested for all kind of files and length (up to 4,5 Gb!) without any error:

$fh = fopen ( ‘Input-File’ , ‘rb’ );
//$fh2 = fopen(‘Output-File’, ‘wb’);

if (! $eof ) <
if (! feof ( $fh )) <
$row = fgets ( $fh , 4096 );
> else <
$row = » ;
$eof = true ;
>
>

if ( $cache !== » )
$row = $cache . $row ;
elseif ( $eof )
break;

$b64 = base64_encode ( $row );
$put = » ;

if ( strlen ( $b64 ) < 76 ) <
if ( $eof ) <
$put = $b64 . «\n» ;
$cache = » ;
> else <
$cache = $row ;
>

> elseif ( strlen ( $b64 ) > 76 ) <
do <
$put .= substr ( $b64 , 0 , 76 ). «\n» ;
$b64 = substr ( $b64 , 76 );
> while ( strlen ( $b64 ) > 76 );

$cache = base64_decode ( $b64 );

if ( $put !== » ) <
echo $put ;
//fputs($fh2, $put);
//fputs($fh2, base64_decode($put)); // for comparing
>
>

Источник

Оцените статью