jquery旋转图像并用同名文件保存(覆盖)?

时间:2016-02-11 10:32:53

标签: javascript jquery html5 canvas

我有一个这样的简单场景:我想旋转图像,并保存它们现有的旧文件。现在,所有功能都已完成,但是当我下载/保存图像时,它总是会创建新的文件名。这是我的代码:

 <div>
        <img src="image/a.png" alt="" id="image" />
        <canvas id="canvas"></canvas>
    </div>
    <p>
        <strong>Rotate Image: </strong>
        <a href="javascript:;" id="resetImage">Reset Image</a>
        <a href="javascript:;" id="rotate90">90&deg;</a>
        <a href="javascript:;" id="rotate180">180&deg;</a>
        <a href="javascript:;" id="rotate270">270&deg;</a>
    </p>

    <a id="download">Download as image</a> 

我的js代码:

<script type="text/javascript">



var img = null, canvas = null;

$(document).ready(function(){
   //  Initialize image and canvas
   img = document.getElementById('image');
   canvas = document.getElementById('canvas');

   if(!canvas || !canvas.getContext){
       canvas.parentNode.removeChild(canvas);
   } else {
       img.style.position = 'absolute';
       img.style.visibility = 'hidden';
   }
   rotateImage(0);

   //  Handle clicks for control links
   $('#resetImage').click(function(){ rotateImage(0); });
   $('#rotate90').click(function(){ rotateImage(90); });
   $('#rotate180').click(function(){ rotateImage(180); });
   $('#rotate270').click(function(){ rotateImage(270); });
});

function rotateImage(degree)
{
    if(document.getElementById('canvas')){
       var cContext = canvas.getContext('2d');
       var cw = img.width, ch = img.height, cx = 0, cy = 0;

       //   Calculate new canvas size and x/y coorditates for image
       switch(degree){
            case 90:
                cw = img.height;
                ch = img.width;
                cy = img.height * (-1);
                break;
            case 180:
                cx = img.width * (-1);
                cy = img.height * (-1);
                break;
            case 270:
                cw = img.height;
                ch = img.width;
                cx = img.width * (-1);
                break;
       }

        //  Rotate image            
        canvas.setAttribute('width', cw);
        canvas.setAttribute('height', ch);
        cContext.rotate(degree * Math.PI / 180);
        cContext.drawImage(img, cx, cy);
    } else {
        //  Use DXImageTransform.Microsoft.BasicImage filter for MSIE
        switch(degree){
            case 0: image.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=0)'; break;
            case 90: image.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=1)'; break;
            case 180: image.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=2)'; break;
            case 270: image.style.filter =    'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)'; break;
        }
    }
}
</script>

谢谢你的回复:)

1 个答案:

答案 0 :(得分:5)

你的问题有2个部分(据我所知)

保存同名文件

所以要保存,您只需将链接href的属性#download更改为图片的dataURL,如下所示:

$('#download').attr('href', canvas.toDataURL())

当然,您必须设置属性download -

的链接

将我们带到第二个问题同名 - 您可以通过设置download属性的值来指定要下载的文件的名称,例如:< / p>

<a id="download" download="you_name_the_file.jpg">Download as image</a> 

完整代码(由于安全原因,此处不起作用,您可以看到实时版本here

var img = null, canvas = null;

$(document).ready(function(){
  //  Initialize image and canvas
  img = document.getElementById('image');
  canvas = document.getElementById('canvas');

  if(!canvas || !canvas.getContext){
    canvas.parentNode.removeChild(canvas);
  } else {
    img.style.position = 'absolute';
    img.style.visibility = 'hidden';
  }
  rotateImage(0);

  //  Handle clicks for control links
  $('#resetImage').click(function(){ rotateImage(0); });
  $('#rotate90').click(function(){ rotateImage(90); });
  $('#rotate180').click(function(){ rotateImage(180); });
  $('#rotate270').click(function(){ rotateImage(270); });
});

function rotateImage(degree)
{
  if(document.getElementById('canvas')){
    var cContext = canvas.getContext('2d');
    var cw = img.width, ch = img.height, cx = 0, cy = 0;

    //   Calculate new canvas size and x/y coorditates for image
    switch(degree){
      case 90:
        cw = img.height;
        ch = img.width;
        cy = img.height * (-1);
        break;
      case 180:
        cx = img.width * (-1);
        cy = img.height * (-1);
        break;
      case 270:
        cw = img.height;
        ch = img.width;
        cx = img.width * (-1);
        break;
    }

    //  Rotate image            
    canvas.setAttribute('width', cw);
    canvas.setAttribute('height', ch);
    cContext.rotate(degree * Math.PI / 180);
    cContext.drawImage(img, cx, cy);
    $('#download').attr('href', canvas.toDataURL())
  } else {
    //  Use DXImageTransform.Microsoft.BasicImage filter for MSIE
    switch(degree){
      case 0: image.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=0)'; break;
      case 90: image.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=1)'; break;
      case 180: image.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=2)'; break;
      case 270: image.style.filter =    'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)'; break;
    }
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <img src="your_image_url.jpg" alt="" id="image" />
  <canvas id="canvas"></canvas>
</div>
<p>
  <strong>Rotate Image: </strong>
  <a href="javascript:;" id="resetImage">Reset Image</a>
  <a href="javascript:;" id="rotate90">90&deg;</a>
  <a href="javascript:;" id="rotate180">180&deg;</a>
  <a href="javascript:;" id="rotate270">270&deg;</a>
</p>

<a id="download" download="mqdefault.jpg">Download as image</a>