PHP implementation saves the canvas image in HTML5 to the server

Source: Internet
Author: User
This article mainly introduces the PHP implementation of the HTML5 canvas image saved to the server method, can be implemented to save the canvas image to the server function, is a very practical skill, the need for friends can refer to the next

The example in this article describes how the PHP implementation saves the canvas image in HTML5 to the server. Share to everyone for your reference. The implementation method is as follows:

First, the question:

When HTML5 was not popular a few years ago, our project manager asked me to ask the project review experts to use a pen to electronically sign the tablet at the end of the review. This requires our review software to provide such a function: Open the browser, login, enter the review comments page, the bottom of the page has a block area, the user here with a touch pen signature, and then this signature will remain on the server.

Such a demand at the time was to make me a lot of trouble, but now think of it, if it is done with HTML5 canvas, it is too simple. This is accomplished in the article "How to put pictures and save as pictures in HTML5 canvas."

Second, the solution:

Before I said a lot of how to save the canvas image as a picture and download methods, but these methods are to save the picture to the client, and our signature requirement is to save the content of the canvas to the server side, how to implement?

Actually very simple, after reading the following PHP code, I believe you will feel very simple.

<?php//requires PHP5 define (' Upload_dir ', ' images/'); $img = $_post[' img ']; $img = Str_replace (' data:image/png;base64, ', ', $img); $img = Str_replace (', ' + ', $img); $data = Base64_decode ($img); $file = Upload_dir. Uniqid (). '. png '; $success = File_put_contents ($file, $data); Print $success? $file: ' Unable to save the file. '? >

Uploaded from the Web page to the server side of the image is the base64_encode transcoding data URL format, the server side of the database is decoded by Base64_decode, saved as a file.

Perhaps one day you will need to use it, think it is very useful to collect a bit!

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.