PHP implements the method of saving canvas images in HTML5 to the server _php tips

Source: Internet
Author: User

The example in this article describes how the PHP implementation will save the canvas image to the server in HTML5. Share to everyone for your reference. The implementation methods are 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 sign the tablet electronically with a pen at the end of the review. This requires us to review the software to provide such a function: Open the browser, login, enter the review comments page, the bottom of the page there is a block area, the user here with a stylus to sign, and then this signature will be maintained on the server.

Such a demand at that time is let me a lot of trouble, but now think of it, if with HTML5 canvas realize, it is too simple. This is accomplished in the article "putting pictures in HTML5 canvas and saving them as pictures."

Second, the solution:

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

Actually very simple, read the following PHP code, I believe you will also feel very simple.

Copy Code code as follows:
<?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. '
?>

From the Web page uploaded to the server side of the picture is the base64_encode transcoding data URL format, in the server side of the Base64_decode to decode, save into a file.

Perhaps one day you will also need to use it, feel very useful words to collect it!

I hope this article will help you with your PHP program design.

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.