简体   繁体   English

使用图像坐标裁剪图像

[英]Cropping image using coordinates of the image

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. 我具有裁剪图像的坐标位置(x1,y1,x2,y2,宽度和高度),我正在将这些值传递给表单的模态元素。 I want to display the cropped image in 150x150 dimensions. 我想以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? 谁能帮我?

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. 您将获得很多技巧。

You can achieve the same effect like this. 您可以达到相同的效果。

HTML: HTML:

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

CSS: CSS:

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

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

Demo 演示

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

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