当前位置:网站首页>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 边栏推荐
- Epoll introduction and principle explanation
- Thread local storage understanding
- WebService details
- Operate attribute chestnut through reflection
- Information theory and coding
- 10. static member variables, static member methods, and pointers to class members
- Download and compile ROS source code
- 2022-1-14
- 2D prefix and
- Targeted and ready to go
猜你喜欢

2021-11-11

There are animation characters interacting with each other when the mouse slides in the web page

Freshman C language summary post (hold change) Part 2 formatted monthly calendar

Garbled code of SecureCRT, double lines, double characters, unable to input (personal detection)

2022-1-14

How are pub and sub connected in ros1?

1. introduction to MySQL database connection pool function technology points

5g core network and core network evolution

Questions not written in the monthly contest

Performance testing -- Interpretation and practice of 16 enterprise level project framework
随机推荐
There are animation characters interacting with each other when the mouse slides in the web page
Google benchmark user manual and examples
Deep learning environment configuration (III) pytorch GPU under Anaconda
Quick sorting C language code + auxiliary diagram + Notes
10. static member variables, static member methods, and pointers to class members
Performance test -- 14 detailed explanation of performance test report and precautions
Download and compile ROS source code
Ansible practice of Nepal graph
Bc117 xiaolele walks up the steps
For Xiaobai who just learned to crawl, you can understand it after reading it
Special exercise split line-----------------------------
MySQL -- how to access the database of a computer in the same LAN (prenatal education level teaching)
Rebirth -- millimeter wave radar and some things I have to say
Array part
5g spectrum
Summary of the first week of winter vacation
Initial structure
//1.16 getchar function
MySQL basic command statement
How to download online printing on Web pages to local PDF format (manual personal test)