In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "php how to transfer pictures to base64 format and upload". In daily operation, I believe many people have doubts about how to convert pictures to base64 format and upload in php. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to transfer pictures to base64 format and upload". Next, please follow the editor to study!
Php to achieve the picture to base64 format and upload methods: 1, the picture into base64 format; 2, through ajax to upload the picture to the server; 3, in the server to re-convert the picture format and storage.
This article operating environment: windows10 system, php 7, thinkpad T480 computer.
It is inevitable to deal with image upload in the course of an actual development project. For example, partners who use thinkphp must be familiar with the upload method of import ("@ .ORG.UploadFile"). Today we're going to talk about how to upload pictures using html base64. Let's take a look.
Mainly use the interface of html5 FileReader, since it is html5, I won't say much about the supported browsers.
You can talk about the train of thought, but it's actually quite simple. After selecting a picture, js will first convert the selected image to base64 format, then upload it to the server through ajax, and then convert it to the image on the server side for storage.
Let's look at the front-end code first.
Html part
Js part
$("# imagesfile") .change (function () {var file = this.files [0]); / / use the size attribute to determine that the file size cannot exceed 5m, the advantage of direct judgment from the front end to relieve the pressure on the server. If (file.size > 5 / 1024 / 1024) {alert ("the file you uploaded is too big!" )} / / here comes var reader=new FileReader (); reader.onload = function () {/ / access the generated base64 DataURL var base64 = reader.result; / / print to the console through reader.result, press F12 to view console.log (base64); / / upload picture base64_uploading (base64);} reader.readAsDataURL (file) }); / / AJAX upload base64function base64_uploading (base64Data) {$.ajax ({type: 'POST', url: "upload API path", data: {' base64': base64Data}, dataType: 'json', timeout: 50000, success: function (data) {console.log (data)) }, complete:function () {}, error: function (xhr, type) {alert ('upload timed out, try again');});}
In fact, the front-end code is not complex, mainly using the new FileReader (); interface to convert images, new FileReader (); there are other interfaces, to learn more about the interface used in children's shoes, Google search new FileReader ();
Next, that is the server-side code, the above demo, is written in thinkphp for the framework, but other frameworks are also basically common.
Function base64imgsave ($img) {/ / folder date $ymd = date ("Ymd"); / / Image path address $basedir = 'upload/base64/'.$ymd.''; $fullpath = $basedir; if (! is_dir ($fullpath)) {mkdir ($fullpath,0777,true);} $types = empty ($types)? Array ('jpg',' gif', 'png',' jpeg'): $types; $img = str_replace (array ('_','-'), array ('/','+'), $img); $b64img = substr ($img, 0100); if (preg_match ('/ ^ (data:\ s*image\ / (\ w +); base64,) /', $b64img, $matches)) {$type = $matches [2] If (! in_array ($type, $types)) {return array ('status'= > 1 recording infographic = >' image format is incorrect, only jpg, gif, png, jpeg are supported!' , 'url'= >');} $img = str_replace ($matches [1],'', $img); $img = base64_decode ($img); $photo ='/ '.md5 (date (' YmdHis'). Rand (1000, 9999)).'. $type; file_put_contents ($fullpath.$photo, $img); $ary ['status'] = 1; $ary [' info'] = 'save picture successfully' $ary ['url'] = $basedir.$photo; return $ary;} $ary [' status'] = 0; $ary ['info'] =' Please select a picture to upload'; return $ary;}
The above is the PHP code, the principle is also very simple, get the base64 uploaded by the interface, and then convert it to the image and save it.
Thinkphp 3.2 is used, no database is required, and the PHP environment can be run directly.
The path to the php directory is:
Application\ Home\ Controller\ Base64Controller.class.php
The path to the html directory is:
Application\ Home\ View\ Base64\ imagesupload.html
At this point, the study on "how to convert php pictures to base64 format and upload" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.