Home  >  Article  >  Backend Development  >  Image upload in php yii2 interface

Image upload in php yii2 interface

WBOY
WBOYOriginal
2016-08-04 09:19:372035browse

Now I am providing an interface to ios, and I have encountered some image upload problems. Now I can receive the base64-encoded data from ios. Do I need to decode it? What decoding process is required to upload the image to the server and obtain the URL from the server to store it in the database? Ask God for guidance. I wrote the interface under the yii2 framework

Reply content:

Now I am providing an interface to ios, and I have encountered some image upload problems. Now I can receive the base64-encoded data from ios. Do I need to decode it? What decoding process is required to upload the image to the server and obtain the URL from the server to store it in the database? Ask God for guidance. I wrote the interface under the yii2 framework

I used base64
yii2 processing when doing canvas image synthesis on the front end
1. Read the base64 encoded data => $data
2.$data = base64_decode($data);//data is binary data
3. Assume there is an upload folder in the web directory, and the file name is md5(microtime(true)).'.jpg' => $filename
4.file_put_contents(Yii::$app->basePath.'/web /upload/'.$filename,$data);
5.url = Yii::$app->urlManager->createAbsoluteUrl(['/']).'upload/'.$filename

Image upload in php yii2 interfaceImage upload in php yii2 interface

The first picture is the base64-encoded data sent from ios, and the second picture is the data output after decoding by my base64_decode(). These are my simulated http transfers on postman. Why is the decoded data garbled?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn