简体   繁体   English

重命名从HTML5画布创建的图像

[英]Renaming an image created from an HTML5 canvas

I have made a simple canvas and save it as an image. 我做了一个简单的画布并将其保存为图像。 I have done this with the help of this code: 我在这段代码的帮助下做到了这一点:

 var canvas = document.getElementById("mycanvas");
 var img    = canvas.toDataURL("image/png");

and pop up the created image with this: 并用以下方法弹出创建的图像:

document.write('<img src="'+img+'"/>');

But its name is always a weird one. 但它的名字总是很奇怪。 I want to rename the image name like faizan.jpg etc. How can I do this? 我想重命名像faizan.jpg等图像名称。我该怎么做?

To put it simply, you can't. 简单来说,你做不到。 When you call the toDataURL method on an HTMLCanvasElement it generates a string representation of the image as a Data URL. 在HTMLCanvasElement上调用toDataURL方法时,它会生成图像的字符串表示形式作为数据URL。 Thus if you try to save the image, the browser gives it a default filename (eg Opera saves it as default.png if the Data URL was a png file). 因此,如果您尝试保存图像,浏览器会为其提供默认文件名(例如,如果数据URL是png文件,Opera会将其保存为default.png)。

Many workarounds exist. 存在许多变通方法。 The simplest one is to make an AJAX call to a server, save the image on the server-side and return the URL of the saved image which can then be accessed and saved on the client-side: 最简单的方法是对服务器进行AJAX调用,在服务器端保存图像并返回保存图像的URL,然后可以在客户端访问和保存:

function saveDataURL(canvas) {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (request.readyState === 4 && request.status === 200) {
            window.location.href = request.responseText;
        }
    };
    request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    request.open("POST", "saveDataURL.php", true);
    request.send("dataURL=" + canvas.toDataURL());
}

To save the image on the server side, use the following PHP script: 要在服务器端保存映像,请使用以下PHP脚本:

$dataURL = $_POST["dataURL"];
$encodedData = explode(',', $dataURL)[1];
$decodedData = base64_decode($encodedData);
file_put_contents("images/faizan.png", $decodedData);
echo "http://example.com/images/faizan.png";

Got this working 100%! 得到这个100%的工作! Just had to do a little debugging to the above answer. 只需要对上面的答案做一点调试。 Here's the working code: 这是工作代码:

The JavaScript: JavaScript:

var saveDataURL = function(canvas) {
  var dataURL = document.getElementById(canvas).toDataURL();
  var params = "dataURL=" + encodeURIComponent(dataURL);

  var request = new XMLHttpRequest();
  request.open("POST", "/save-data-url.php", true);
  request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  window.console.log(dataURL);    

  request.onreadystatechange = function () {
    if (request.readyState === 4 && request.status === 200) {
      window.console.log(request.responseText);
    }
  };

  request.send(params);
}

/scripts/save-data-url.php: /scripts/save-data-url.php:

<?php
  $dataURL = $_POST["dataURL"];
  $encodedData = explode(',', $dataURL);
  $encodedData = $encodedData[1];
  $decodedData = base64_decode($encodedData);
  file_put_contents("images/log.txt", $encodedData);
  file_put_contents("images/test.png", $decodedData);
  echo "http://www.mywebsite.com/images/test.png";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM