HTML upload image data to server, PHP receive save picture

Source: Internet
Author: User
Tags php language
Most of the time, we need to save the picture data on the Web or the picture inside the canvas to the server. The HTML5 already provides the available interfaces.


Canvas's Todataurl method, you can export canvas data on canvas to a string format. We just need to transfer the string to the server.

What if the image is an IMG tag?

Very simply, Canvas provides the DrawImage method for drawing img or other canvas data onto your canvas.

Below, let's look at the client's code:

var cc = window.document.getElementById ("Egretcanvas"), var cc2 = document.createelement ("canvas"); Cc2.setattribute (" Width "," "Cc2.setattribute"); var ctx = Cc2.getcontext ("2d"), Ctx.drawimage (cc, 0, 0, 320, 514);
var imgdata:string = cc2["Todataurl"] ();


So the exported string, which contains the prefix " data:image/png;base64," so we need to remove this prefix


Then pass the string to the server, where we choose to use the PHP language to receive the data and save the image.


$imgurl = Str_replace (', ' + ', $_request[' image ');
First, replace the space in the string with the "+" sign.


$savePath = ". /images/123.png "; $image = Base64_decode ($image); File_put_contents ($savePath, $image);

After PHP gets the data, it needs to be Base64 decoded before it can be saved as a picture.



The above describes the HTML upload image data to the server, PHP received save pictures, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.