Saving exported image on a server

Deprecated
The information contained in this article is deprecated and is here as an archive only. Refer to the links further down the page, or use search option to find up-to-date information.

IMPORTANT NOTICE! The information provided in this article is deprecated. The export scripts, described in this tutorial were replaced by a better alternative since version 3.14. Please refer to this tutorial for further information.

Instead of offering the user to save the image to his hard drive, you might want it to be saved on your web server. As our script allows adding custom click handlers, it's not that difficult to do this. In the sample below we added custom click handler:

chart.exportConfig = {
    menuItems: [{
        icon: 'amcharts/images/export.png',
        format: 'png',
        onclick: function(a) {
            var output = a.output({
                format: 'png',
                output: 'datastring'
            }, function(data) {
                console.log(data)
            });
        }
    }]
}

Now, if you click the export button, the script will output image data to your browser's console.

If you are experienced programmer, most likely you know what to do from this point - you should pass this data string to some script which would save it as an image. You have a wide selection of options here and you will need some additional library for this. We will use jQuery, as it is one of the most popular.

So, include jQuery lib to your HTML:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

And add these lines instead of console.log(data):

$.post("save.php", {
 imageData: encodeURIComponent(data)
},
function(msg) {
 alert("image saved");
});

Of course, instead of alerting some message, most likely you'll want to do something else.

Now, create save.php file and add these lines to it:

<?php

$data = urldecode($_POST['imageData']);
list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);
file_put_contents('image.png', $data);

?>

Upload all the files to your server, click export button and, if everything is correct - the image should be saved to your server.

Here is a working example illustrating saving image on the server.