doudou130216
2013-08-14 11:31 阅读 44
已采纳

dataurl to image可在php下载

I'm creating image using canvas and using following script,

function getImage() {
    var canvas1 = document.getElementById("images");

    if (canvas1.getContext) {
        var ctx = canvas1.getContext("2d");
        var myImage = canvas1.toDataURL("image/jpg");
    }
    $('<form action="download.php" method="POST">' + 
    '<input type="hidden" name="aid" value="' + myImage + '">' +
    '</form>').submit();

}

And in my Download.php file is,

<?php $img = $_POST['aid']; 
    echo "<img src=".$img.">";
?>

it showing image correctly. But i wanna give download button with jpg format or pdf format.

How i can use?

I used base64_decode(); method. But i cant solve.

Help me...

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doumu1951 doumu1951 2013-08-14 12:20

    Thanks for all. but I got answer using,

    file_put_contents();
    

    But thing, i dont know how to use. Finally i got it from this Answer.

    Answer is,

    $data = 'data:image/png;base64,AAAFBfj42Pj4';
    
    list($type, $data) = explode(';', $data);
    list(, $data)      = explode(',', $data);
    $data = base64_decode($data);
    
    file_put_contents('/tmp/image.png', $data);
    

    But still i'm waiting for download button with option of image/pdf format.

    点赞 7 评论 复制链接分享
  • doushouxie7064 doushouxie7064 2013-08-14 12:06

    Try this:

    PHP echo'ed image with link

    <?php
    
    $img = $_POST['aid'];
    echo "<a href='download_image.php'><img src=".$img."></a>";
    
    ?>
    

    download_image.php

    <?php
    
    $img = "myimage.jpg";
    
    // fix for IE catching or PHP bug issue
    header("Pragma: public");
    header("Expires: 0"); // set expiration time
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    // browser must download file from server instead of cache
    
    // force download dialog
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    
    // use the Content-Disposition header to supply a recommended filename and 
    // force the browser to display the save dialog. 
    header("Content-Disposition: attachment; filename=".basename($img).";");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($img));
    
    readfile("$img");
    exit();
    
    ?>
    
    点赞 4 评论 复制链接分享

相关推荐