当前位置:网站首页>PHP Base64 image processing Encyclopedia
PHP Base64 image processing Encyclopedia
2022-06-23 02:13:00 【It workers】
<?php
header('Content-type:text/html;charset=utf-8');
function image_base64($image_file) {
// getimagesize Get the attribute value of the image and return an array , Indexes 0 Corresponding picture width , Indexes 1 Corresponding picture height
/*
* getimagesize Get the attribute value of the image and return an array , here $image_info['mime'] The corresponding value is the string "image/jpeg"
* Indexes 0 Given is the pixel value of the image width
* Indexes 1 What's given is the pixel value of the image height
* Indexes 2 What's given is the type of image , The number returned is , among 1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
* Indexes 3 Given is a string of width and height , Can be used directly HTML Of <image> label
* Indexes bits Given is the number of bits for each color of the image , Binary format
* Indexes channels Given is the channel value of the image ,RGB The default image is 3
* Indexes mime What's given is the image of MIME Information , This information can be used in HTTP Content-type Send the correct message in the header message , Such as :
* header("Content-type: image/jpeg");
*/
$image_info = getimagesize ( $image_file );
// Combine into base64 code
// chunk_split take base64_encode() The output of is converted to conform to RFC 2045 Semantic string . It will be in every chunklen( The default is 76) Insert... After characters end( Default is space " ")
// Not here chunk_split Function processing is OK , about <img> The label display image does not affect
// The array in the double quotation marks of the string is {} Expand , You can normally display the contents in the string
$base64_image = "data:{$image_info['mime']};base64," . chunk_split ( base64_encode ( file_get_contents ( $image_file ) ) );
return $base64_image;
}
$image_file = './test.jpg';
$base64_image = image_base64 ( $image_file );
// Acquired base64 Code for data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEBLAEsAAD/4RVFRXhpZgAATU0AKgAAAAgACgEPAA......
?>
<img src="<?php echo $base64_image;?>" />
<?php
/**
* Decompile data/base64 Stream data and create picture files
*
* @param string $base64_image base64 Data flow
* @param string $put_url Directory for storing picture files , No slash after the path /
* @param string $fileName Picture file name ( No file suffix )
* @return mixed Returns the image address or boolean type that can be accessed in the browser
*/
function base64DecImg($base64_image, $put_url, $fileName) {
// The browser accesses the current path URL
$__URL__ = 'localhost/test/';
try {
// Division base64 code , Get the header encoding part
$headData = explode ( ';', $base64_image );
// Then obtain the suffix information of the original file before encoding
$postfix = explode ( '/', $headData [0] );
// Determine whether the source file is a picture
if (strstr ( $postfix [0], 'image' )) {
// Judge whether it is jpeg picture , And assign the correct suffix
$postfix = $postfix [1] == 'jpeg' ? 'jpg' : $postfix [1];
// The full path and extension of the image to be combined
// DIRECTORY_SEPARATOR Directory separator , because win And linux The directory separator is different ,PHP Returns the correct directory separator according to the current system .windows return \ or /,linux return /
$file_url = $put_url . DIRECTORY_SEPARATOR . $fileName . '.' . $postfix;
// Get rid of $base64_image The header content in the code , Get the contents of the file encoding
$base64Arr = explode(",",$base64_image);
// the base64_decode decode
$image_decode = base64_decode ($base64Arr[1] );
try {
// Composite file
file_put_contents ( $file_url, $image_decode );
// Return the picture address that can be accessed in the browser
return $__URL__ . $file_url;
} catch ( Exception $e ) {
return false;
}
}
} catch ( Exception $e ) {
return false;
}
return false;
}
// ./ Indicates the path of the current page
echo base64DecImg ( $base64_image, "./", "test2" );
?>
data:image/*;base64 Namely Data URI scheme.
Data URI scheme Is in RFC2397 As defined in , The purpose is to put some small data , Directly embedded in the web page , So there's no need to load... From an external file
for example :
data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEBLAEsAAD/4RVFRXhpZgAATU0AKgAAAAgACgEPAA......
base64 In code ,data Indicates the name of the contract that gets the data ,image/jpeg Is the data type name ,base64 It's the way data is encoded , This file follows the comma base64 Encoded data
at present ,Data URI scheme Supported types are :
data:, Text data
data:text/plain, Text data
data:text/html,HTML Code
data:text/html;base64,base64 Coded HTML Code
data:text/css,CSS Code
data:text/css;base64,base64 Coded CSS Code
data:text/javascript,Javascript Code
data:text/javascript;base64,base64 Coded Javascript Code
data:image/gif;base64,base64 Coded gif Picture data
data:image/png;base64,base64 Coded png Picture data
data:image/jpeg;base64,base64 Coded jpeg Picture data
data:image/x-icon;base64,base64 Coded icon Picture data
base64 In short , It puts some 8-bit Translate data into standard ASCII character , There are many free ones on the Internet base64 Coding and decoding tools 边栏推荐
- C language games: sanziqi (simple version) implementation explanation
- Data analysis method - user group analysis
- Hello
- Quick sorting C language code + auxiliary diagram + Notes
- Third order magic cube formula
- You must know the type and method of urllib
- II Data preprocessing
- Thread local storage understanding
- Interviewer: what is the difference between SSH and SSM frameworks? How to choose??
- Ugui empty button implementation
猜你喜欢

MySQL -- how to access the database of a computer in the same LAN (prenatal education level teaching)

Reptile lesson 1

Three methods for solving Fibonacci sequence feibonacci (seeking rabbit) - program design

Rebirth -- C language and the story I have to tell (text)

Ansible practice of Nepal graph

C language games: sanziqi (simple version) implementation explanation

Stop automatically after MySQL starts (unable to start)

Circuit analysis (circuit principle)

Performance test -- 14 detailed explanation of performance test report and precautions

Custom shapes for ugui skill learning
随机推荐
Unique in Pimpl_ PTR compilation errors and Solutions
2021-11-11
Interviewer: why does TCP shake hands three times and break up four times? Most people can't answer!
8 vertical centering methods
Game (sanziqi & minesweeping)
"Initial C language" (Part 1)
For Xiaobai who just learned to crawl, you can understand it after reading it
[CodeWars]Matrix Determinant
Nebula operator cloud practice
Function part
Ch340 and PL2303 installation (with link)
Arm assembly syntax
Primary pointer part
C. Number of Pairs-Codeforces Round #725 (Div. 3)
An interesting example of relaxed memory models
Branch and loop statements (including goto statements) -part2
How to make word notes beautiful
Stop automatically after MySQL starts (unable to start)
Quick sort method
Performance testing -- Interpretation and practice of 16 enterprise level project framework