使用图像的坐标裁剪图像

时间:2021-12-28 00:49:23

I have the coordinates position of a cropped image (x1,y1,x2,y2,width and height) I am passing these value to modal element of a form. I want to display the cropped image in 150x150 dimensions.

我有一个裁剪图像的坐标位置(x1,y1,x2,y2,宽度和高度)我将这些值传递给窗体的模态元素。我想以150x150尺寸显示裁剪后的图像。

I tried the following code ,

我尝试了以下代码,

var rx = xsize / (x2-x1);
var ry = ysize / (y2-y1);

document.getElementById('logo').style.width=Math.round(rx * boundx) + 'px';//No I18N
document.getElementById('logo').style.height=Math.round(ry * boundy) + 'px';//No I18N
document.getElementById('logo').style.marginLeft= '-' + Math.round(rx * x1) + 'px';//No I18N
document.getElementById('logo').style.marginTop= '-' + Math.round(ry * y1)+ 'px';//No I18N

Where logo is the ID of my image tag.

徽标是我的图片代码的ID。

By using this code cropped image is not correct.

使用此代码裁剪的图像不正确。

Can anyone help me?

谁能帮我?

2 个解决方案

#1


1  

Use this technique for image cropping in HTML5

使用此技术在HTML5中进行图像裁剪

For playing with images try to use canvas, operation on canvas are quite easy.

对于玩图像尝试使用画布,在画布上操作相当容易。

Go to --> http://www.html5canvastutorials.com/tutorials/html5-canvas-image-crop/

转到 - > http://www.html5canvastutorials.com/tutorials/html5-canvas-image-crop/

You will get plenty of tricks.

你会得到很多技巧。

#2


0  

You can achieve the same effect like this.

你可以达到同样的效果。

HTML:

<div class="crop">
    <img class="photo" src="http://placekitten.com/400/200" />
</div>

CSS:

.crop {
    overflow: hidden;
    width: 150px; /* crop width */
    height: 150px; /* crop height */
}

.photo {
    margin-top: -20px; /* start x */
    margin-left: -50px; /* start y */
}

Demo

#1


1  

Use this technique for image cropping in HTML5

使用此技术在HTML5中进行图像裁剪

For playing with images try to use canvas, operation on canvas are quite easy.

对于玩图像尝试使用画布,在画布上操作相当容易。

Go to --> http://www.html5canvastutorials.com/tutorials/html5-canvas-image-crop/

转到 - > http://www.html5canvastutorials.com/tutorials/html5-canvas-image-crop/

You will get plenty of tricks.

你会得到很多技巧。

#2


0  

You can achieve the same effect like this.

你可以达到同样的效果。

HTML:

<div class="crop">
    <img class="photo" src="http://placekitten.com/400/200" />
</div>

CSS:

.crop {
    overflow: hidden;
    width: 150px; /* crop width */
    height: 150px; /* crop height */
}

.photo {
    margin-top: -20px; /* start x */
    margin-left: -50px; /* start y */
}

Demo