当前位置:网站首页>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 
原网站

版权声明
本文为[It workers]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202091754070453.html