Thinkphp processes base64 images

Source: Internet
Author: User
{Code...} How to apply the {code...} to the example given by tp, and how to use thinkphp to process the $ result? For example, I want to change the width and height of a piece and then upload it to the server. Thank you!
$ Url = ''// network Image address; $ curl = curl_init ($ url); curl_setopt ($ curl, CURLOPT_URL,''); curl_setopt ($ curl, CURLOPT_REFERER, ''); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); $ result = curl_exec ($ curl); // header ('content-type: image/JPEG '); // echo $ result; return $ result;

How to apply the example given by tp

$ Image = \ think \ Image: open ('. /image.png '); // returns the image width $ width = $ image-> width (); // returns the image height $ height = $ image-> height (); // the type of the returned image $ type = $ image-> type (); // The mime type of the returned image $ mime = $ image-> mime (); // returned image size array 0 image width 1 image height $ size = $ image-> size ();

How can I use thinkphp to process the $ result? For example, I want to change the width and height of a piece and then upload it to the server.
Thank you!

Reply content:
$ Url = ''// network Image address; $ curl = curl_init ($ url); curl_setopt ($ curl, CURLOPT_URL,''); curl_setopt ($ curl, CURLOPT_REFERER, ''); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); $ result = curl_exec ($ curl); // header ('content-type: image/JPEG '); // echo $ result; return $ result;

How to apply the example given by tp

$ Image = \ think \ Image: open ('. /image.png '); // returns the image width $ width = $ image-> width (); // returns the image height $ height = $ image-> height (); // the type of the returned image $ type = $ image-> type (); // The mime type of the returned image $ mime = $ image-> mime (); // returned image size array 0 image width 1 image height $ size = $ image-> size ();

How can I use thinkphp to process the $ result? For example, I want to change the width and height of a piece and then upload it to the server.
Thank you!

This is the native package I installed on the internet. for your reference, it is actually very simple. it is to decode the base64 bit and save it locally.
I will generate the following code.
As for the image size you mentioned, I am using it. you can either use the js plug-in before Upload to allow users to complete the upload, or use PHP to operate the image after you generate it locally. Only local files can be operated on by the server.

/*** Save the 64-bit encoded image */function saveBase64Image ($ base64_image_content) {if (preg_match ('/^ (data: \ s * image \/(\ w + ); base64,)/', $ base64_image_content, $ result) {// Image suffix $ type = $ result [2]; // save location -- image name $ image_name = date ('his '). str_pad (mt_rand (1, 99999), 5, '0', STR_PAD_LEFT ). ". ". $ type; $ image_url = '/uploads/image /'. date ('ymmd '). '/'. $ image_name; if (! Is_dir (dirname ('. '. $ image_url) {mkdir (dirname ('. '. $ image_url); chmod (dirname ('. '. $ image_url), 0777); umask ($ oldumask);} // decodes $ decode = base64_decode (str_replace ($ result [1], '', $ base64_image_content )); if (file_put_contents ('. '. $ image_url, $ decode) {$ data ['code'] = 0; $ data ['imagename'] = $ image_name; $ data ['URL'] = $ image_url; $ data ['MSG '] =' saved successfully! ';} Else {$ data ['code'] = 1; $ data ['imagename'] = ''; $ data ['URL'] = ''; $ data ['MSG '] = 'image saving failed! ';}} Else {$ data ['code'] = 1; $ data ['imagename'] = ''; $ data ['URL'] = ''; $ data ['MSG '] = 'base64 image format error! ';} Return $ data ;}

Save images as image.png
Use the image class of thinnkphp to delete the temporary image.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.