canva旋转图片 js,JavaScript图片永久旋转

I’m looking for a way to rotate an image locally in the browser and store it locally

I tried to rotate the image with canvas and jquerryrotate. But in boot methods the image is only rotated in the view. As soon as I store these image (e.g. canvas. toDataURL()) I’m getting the original file / rotation.

Is there a way to rotate the image locally in the browser without php/server side interaction?

Thx!

$("#Img").rotate(90);

var dataURL5 = document.getElementById('Img').src;

--> dataURL5 don't contain the rotated image but the original... in the Browser i have the rotated view....

or with canvas

context_tmp.translate(canvas_tmp.width , canvas_tmp.height /canvas_tmp.width );

context_tmp.rotate((Math.PI/2));

canvas is not a requiredment... i only tried it...

in boot

解决方案

You need to extract the image from the canvas using its toDataURL() method.

You cannot extract any image data from src property (but the original URL).

Note: If the original image is from different origin than the page this won't work as CORS kicks in (cross-origin resource sharing). This is by design and intent.

You need to rotate and translate the canvas using its context, then draw the image. Now you can call toDataURL() to get the image data (image will be blank if CORS kicked in).

Example:

/// translate so rotation happens at center of image

ctx.translate(image.width * 0.5, image.height * 0.5);

/// rotate canvas context

ctx.rotate(0.5 * Math.PI); /// 90deg clock-wise

/// translate back so next draw op happens in upper left corner

ctx.translate(-image.width * 0.5, -image.height * 0.5);

/// image will now be drawn rotated

ctx.drawImage(image, 0, 0);

/// get the rotated image (unless CORS kicks in...)

var dataUri = canvas.toDataURL();

I made a fiddle with this code but as CORS is effective for the image I am using it is kind of pointless, but anyways - here goes.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值