Data URL draws pictures locally instead of loading them from the server, so it saves HTTP connections and accelerates web pages.
Syntax:
data:image/jpg; Declaration of data protocol and type name
base64, The encoding form is base64
/9j/4AAQSkZ…… Base64 encoding result
Data URL generation method (php):
Note: This method is suitable for small pictures. Don’t consider large pictures. In addition, browsers below IE8 do not support this method. . Using this method will increase the burden on the client's CPU and memory. In short, it has advantages and disadvantages.
So how do we transfer the images in Data URL format on the website into actual images?
In fact, it is very simple. We transfer the image content, which is the src part, to the background and save it.
$img_content // 图片内容 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)){ $type = $result[2]; $new_file = "./test.{$type}"; if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content)))){ echo '新文件保存成功:', $new_file; } }
For more PHP implementation of image generation and saving of data URL, please pay attention to the PHP Chinese website for related articles!