简体   繁体   English

使用Javascript中的X,Y坐标裁剪图像

[英]Crop image with X, Y coordinates in Javascript

I'm triying to crop an dataURL image with X and Y coordinates and width and height. 我正在尝试使用X and Y坐标以及width and height.裁剪dataURL图像width and height.
I don't want to resize the image. 我不想调整图像大小。 I just want to get the zone (x,y) and the width and height. 我只想获取区域(x,y)以及宽度和高度。
I've alerdy done this in PHP, but now I'm triying to do it in JS. 我在PHP中做过alerdy,但是现在我正在用JS来做这件事。 Here's my actual code: 这是我的实际代码:

function resizeImage(url, width, height, x, y, callback) {
var canvas = document.createElement("canvas");
      var context = canvas.getContext('2d');
      var imageObj = new Image();

      imageObj.onload = function() {
        var sourceX = 0;
        var sourceY = 0;
        var sourceWidth = imageObj.width;
        var sourceHeight = imageObj.height;
        var destWidth = width;
        var destHeight = height;
        var destX = x;
        var destY = y;

        context.drawImage(imageObj, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight);
        callback(canvas.toDataURL())
      };
      imageObj.src = url;
}

This function just changes the width & height, and that's not the result that it's supposes to do. 此函数只是更改宽度和高度,而并非其应做的结果。

Here's my PHP code (if it can be helpful to better understand me) 这是我的PHP代码(如果它能帮助我更好地理解我)

<?php
class Cropper {
    var $x;
    var $y;
    var $dataURL;
    var $width;
    var $height;
    var $filter;
    function __construct($x, $y, $dataURL, $width, $height, $filter) {
        $this->x = $x;
        $this->y = $y;
        $this->dataURL = $dataURL;
        $this->width = $width;
        $this->height = $height;
        $this->filter = $filter;
    }
    function setHeader() {
        header('Content-Type: image/png');
    }
    function Render() {
        $image = $this->dataURL;
        $image = substr($image, 22);
        $img = imagecreatetruecolor(340, 462);
        $org_img = imagecreatefromstring(base64_decode($image));
        imagecopy($img, $org_img, 0, 0, $this->x, $this->y, $this->width, $this->height);
        if($this->filter == 1) imagefilter($img, IMG_FILTER_GRAYSCALE);
        if($this->filter == 2) {
            imagefilter($img,IMG_FILTER_GRAYSCALE);
            imagefilter($img,IMG_FILTER_COLORIZE,100,50,0);
        }
        ob_start(); 
            imagepng($img);
            imagealphablending($img,true);
            $image_data = ob_get_contents(); 
        ob_end_clean(); 
        $image_data_base64 = base64_encode($image_data);
        imagedestroy($img);
        return 'data:image/png;base64,'.$image_data_base64;
    }
}
?>

I've also made a draw to better understand me: 我也画了一个平局来更好地理解我:

图片

EDIT: sorry, inversed between X and Y 编辑:对不起,X和Y之间倒置

So the function needs to return the yellow area 所以函数需要返回黄色区域
How can I do this? 我怎样才能做到这一点? What's wrong in my code? 我的代码有什么问题? Thanks 谢谢

According to documentation for CanvasRenderingContext2D.drawImage() function: 根据CanvasRenderingContext2D.drawImage()函数的文档:

ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight); ctx.drawImage(image,sx,sy,sWidth,sHeight,dx,dy,dWidth,dHeight);

sx, sy, sWidth, sHeight are parameters of the sub-rectangle of the source image sx,sy,sWidth,sHeight是源图像的子矩形的参数
In your case: parameters sx, sy, sWidth, sHeight should coincide with parameters dx, dy, dWidth, dHeight (in most simple case) 在您的情况下:参数sx,sy,sWidth,sHeight应该与参数dx,dy,dWidth,dHeight一致 (在最简单的情况下)
Change your imageObj.onload handler as shown below: 更改您的imageObj.onload处理程序,如下所示:

...
imageObj.onload = function() {
   context.drawImage(imageObj, x, y, width, height, x, y, width, height);
   callback(canvas.toDataURL())
};
...

The key to preventing distortion seems to be setting the width and height of the canvas element. 防止变形的关键似乎在于设置画布元素的宽度和高度。 I then used the desired cropped or destination width and height for both source and destination, and then used 0, 0 as the starting x, y coordinates for the sub-rectangle, or cropped destination image: 然后,我对源和目标使用了所需的裁剪或目标宽度和高度,然后将0、0用作子矩形或裁剪后的目标图像的起始x,y坐标:

function resizeImage(url, width, height, x, y, callback) {
    var canvas = document.createElement("canvas");
    var context = canvas.getContext('2d');
    var imageObj = new Image();

    // set canvas dimensions

    canvas.width = width;
    canvas.height = height;

    imageObj.onload = function () {
        context.drawImage(imageObj, x, y, width, height, 0, 0, width, height);
        callback(canvas.toDataURL());
    };

    imageObj.src = url;
}

Edit 编辑

Interesting discovery: In addition to cropping, you can use this to add padding to an image. 有趣的发现:除了裁剪之外,您还可以使用它为图像添加填充。

var padding = 10,
    sourceImgWidth = 150,
    sourceImgHeight = 100,
    paddedWidth = sourceImgWidth + padding * 2,
    paddedHeight = sourceImgHeight + padding * 2,
    x = -padding,
    y = -padding;

resizeImage('test.jpg', paddedWidth, paddedHeight, x, y, function (dataURL) {
    paddedImage.src = dataURL;
});

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

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