【问题标题】:dataurl to image for download in phpdataurl 到图像以在 php 中下载
【发布时间】:2013-08-16 08:05:30
【问题描述】:

我正在使用 canvas 并使用以下脚本创建图像,

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();

}

在我的 Download.php 文件中,

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

它正确显示图像。但我想给下载按钮以 jpg 格式或 pdf 格式。

如何使用?

我使用了 base64_decode(); 方法。但是我解决不了。

帮帮我...

【问题讨论】:

    标签: php image html5-canvas base64 data-url


    【解决方案1】:

    试试这个:

    带有链接的 PHP 回显图像

    <?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();
    
    ?>
    

    【讨论】:

      【解决方案2】:

      谢谢大家。但我得到了答案,

      file_put_contents();
      

      但是事情,我不知道如何使用。最后我从这个Answer得到它。

      答案是,

      $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);
      

      但我仍在等待带有图像/pdf格式选项的下载按钮。

      【讨论】:

        猜你喜欢
        • 2015-01-31
        • 1970-01-01
        • 2012-02-06
        • 1970-01-01
        • 2013-05-16
        • 1970-01-01
        • 1970-01-01
        • 2013-11-15
        • 2013-06-19
        相关资源
        最近更新 更多