简体   繁体   English

使用Javascript / jQuery裁剪图像

[英]Using Javascript/jQuery to crop image

I have a page showing several thumbnail images. 我有一个显示几个缩略图的页面。 When the user mouseovers these images, a modal window showing the full image will appear. 当用户将鼠标悬停在这些图像上时,将出现显示完整图像的模态窗口。

Problem: In order to save space, I want to just store 1 version (the original version) of the image on the server, and create the thumbnail "dynamically" on the client side, probably doing a crop (no resize necessary) using javascript/jquery. 问题:为了节省空间,我想在服务器上存储1个版本(原始版本)的图像,并在客户端“动态”创建缩略图,可能使用javascript进行裁剪(无需调整大小) / jQuery的。 Is this possible? 这可能吗?

I have seen (but not tried) those jquery cropping plugins, which seem to have many features like a interactive cropping tool. 我已经看过(但没试过)那些jquery裁剪插件,它们似乎有很多功能,比如交互式裁剪工具。 I dont need these features, just want to crop using javascript. 我不需要这些功能,只想使用javascript进行裁剪。 Most likely cropping with gravity in the center of the image. 最有可能在图像中心重力裁剪。

You can use css to crop image from the center parte. 您可以使用css从中心部分裁剪图像。

See demo 演示

<div class="content">
<img src="http://www.letsgodigital.org/images/producten/2086/testrapport/nikon-p90-picture.jpg" width="400" height="400" />
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

​.content {
width:200px;//require width
height:200px;//require height
overflow:hidden;    
}
.content img {
margin-left:-50%;
 margin-top:-50%;   
}

To get an equivalent of a crop, one css solution is to use overflow:hidden on an outer container of each full sized image. 为了获得相当于一个裁剪,一个css解决方案是使用overflow:hidden在每个全尺寸图像的外部容器上。

Suppose you want 50px x 50px thumbnails: 假设你想要50px x 50px的缩略图:

<div id="thumbnail_1" style="overflow: hidden; width: 50px; height:50px"> 
  <img src="..."/>  <!-- this is the full-size image -->
</div>

<div id="thumbnail_2" style="overflow: hidden; width: 50px; height:50px"> 
  <img src="..."/>
</div>

<div id="thumbnail_3" style="overflow: hidden; width: 50px; height:50px"> 
  <img src="..."/>
</div>

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

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