मेरे पास ऐसी छवियां हैं जो आयाम में काफी बड़ी होंगी और मैं उन्हें अनुपात में कमी करते हुए jQuery के साथ सिकोड़ना चाहता हूं, अर्थात समान पहलू अनुपात।
क्या कोई मुझे किसी कोड की ओर इशारा कर सकता है, या तर्क समझा सकता है?
<img src='image.jpg' width=200>
मेरे पास ऐसी छवियां हैं जो आयाम में काफी बड़ी होंगी और मैं उन्हें अनुपात में कमी करते हुए jQuery के साथ सिकोड़ना चाहता हूं, अर्थात समान पहलू अनुपात।
क्या कोई मुझे किसी कोड की ओर इशारा कर सकता है, या तर्क समझा सकता है?
<img src='image.jpg' width=200>
जवाबों:
Http://ericjuden.com/2009/07/jquery-image-resize/ से कोड के इस टुकड़े पर एक नज़र डालें
$(document).ready(function() {
$('.story-small img').each(function() {
var maxWidth = 100; // Max width for the image
var maxHeight = 100; // Max height for the image
var ratio = 0; // Used for aspect ratio
var width = $(this).width(); // Current image width
var height = $(this).height(); // Current image height
// Check if the current width is larger than the max
if(width > maxWidth){
ratio = maxWidth / width; // get ratio for scaling image
$(this).css("width", maxWidth); // Set new width
$(this).css("height", height * ratio); // Scale height based on ratio
height = height * ratio; // Reset height to match scaled image
width = width * ratio; // Reset width to match scaled image
}
// Check if current height is larger than max
if(height > maxHeight){
ratio = maxHeight / height; // get ratio for scaling image
$(this).css("height", maxHeight); // Set new height
$(this).css("width", width * ratio); // Scale width based on ratio
width = width * ratio; // Reset width to match scaled image
height = height * ratio; // Reset height to match scaled image
}
});
});
max-widthऔर max-heightटू 100%। jsfiddle.net/9EQ5c
मुझे लगता है कि यह वास्तव में अच्छा तरीका है :
/**
* Conserve aspect ratio of the original region. Useful when shrinking/enlarging
* images to fit into a certain area.
*
* @param {Number} srcWidth width of source image
* @param {Number} srcHeight height of source image
* @param {Number} maxWidth maximum available width
* @param {Number} maxHeight maximum available height
* @return {Object} { width, height }
*/
function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) {
var ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);
return { width: srcWidth*ratio, height: srcHeight*ratio };
}
Math.floorवास्तव में एक पिक्सेल सही डिजाइन के साथ मदद करेगा :-)
function imgSizeFit(img, maxWidth, maxHeight){ var ratio = Math.min(1, maxWidth / img.naturalWidth, maxHeight / img.naturalHeight); img.style.width = img.naturalWidth * ratio + 'px'; img.style.height = img.naturalHeight * ratio + 'px'; }
यदि मैं प्रश्न को सही ढंग से समझता हूं, तो आपको इसके लिए jQuery की भी आवश्यकता नहीं है। ग्राहक पर आनुपातिक रूप से सिकुड़ते हुए अकेले सीएसएस के साथ किया जा सकता है: बस इसके सेट max-widthऔर max-heightटू 100%।
<div style="height: 100px">
<img src="http://www.getdigital.de/images/produkte/t4/t4_css_sucks2.jpg"
style="max-height: 100%; max-width: 100%">
</div>
यहाँ है फिडेल: http://jsfiddle.net/9EQ5c/
width: auto; height: auto;आपका कोड चलाने के लिए भी सेट होना था :)
पहलू अनुपात को निर्धारित करने के लिए , आपको लक्ष्य बनाने के लिए एक अनुपात की आवश्यकता होती है।

function getHeight(length, ratio) {
var height = ((length)/(Math.sqrt((Math.pow(ratio, 2)+1))));
return Math.round(height);
}

function getWidth(length, ratio) {
var width = ((length)/(Math.sqrt((1)/(Math.pow(ratio, 2)+1))));
return Math.round(width);
}
इस उदाहरण में मैं 16:10इस विशिष्ट मॉनिटर पहलू अनुपात का उपयोग करता हूं ।
var ratio = (16/10);
var height = getHeight(300,ratio);
var width = getWidth(height,ratio);
console.log(height);
console.log(width);
ऊपर से परिणाम 147और होगा300
वास्तव में मैं सिर्फ इस समस्या में चला गया हूं और मुझे जो समाधान मिला वह अजीब सरल और अजीब था
$("#someimage").css({height:<some new height>})
और चमत्कारिक रूप से छवि को नई ऊंचाई पर आकार दिया जाता है और उसी अनुपात का संरक्षण किया जाता है!
इस समस्या के 4 पैरामीटर हैं
और 3 अलग-अलग सशर्त पैरामीटर हैं
उपाय
आपको बस इतना ही करना है।
//Pseudo code
iX;//current width of image in the client
iY;//current height of image in the client
cX;//configured width
cY;//configured height
fX;//final width
fY;//final height
1. check if iX,iY,cX,cY values are >0 and all values are not empty or not junk
2. lE = iX > iY ? iX: iY; //long edge
3. if ( cX < cY )
then
4. factor = cX/lE;
else
5. factor = cY/lE;
6. fX = iX * factor ; fY = iY * factor ;
यह एक परिपक्व मंच है, मैं आपको इसके लिए कोड नहीं दे रहा हूं :)
<img src="/path/to/pic.jpg" style="max-width:XXXpx; max-height:YYYpx;" >मदद करता है?
ब्राउज़र पहलू अनुपात को बरकरार रखने का ध्यान रखेगा।
max-widthजब छवि की चौड़ाई ऊंचाई से अधिक है और इसकी ऊंचाई आनुपातिक रूप से गणना की जाएगी यानी किक। उसी प्रकारmax-height प्रभाव तब होगा जब ऊंचाई चौड़ाई से अधिक होगी।
इसके लिए आपको किसी jQuery या जावास्क्रिप्ट की आवश्यकता नहीं है।
Ie7 + और अन्य ब्राउज़रों द्वारा समर्थित ( http://caniuse.com/minmaxwh )।
यह सभी संभव अनुपात के साथ छवियों के लिए काम करना चाहिए
$(document).ready(function() {
$('.list img').each(function() {
var maxWidth = 100;
var maxHeight = 100;
var width = $(this).width();
var height = $(this).height();
var ratioW = maxWidth / width; // Width ratio
var ratioH = maxHeight / height; // Height ratio
// If height ratio is bigger then we need to scale height
if(ratioH > ratioW){
$(this).css("width", maxWidth);
$(this).css("height", height * ratioW); // Scale height according to width ratio
}
else{ // otherwise we scale width
$(this).css("height", maxHeight);
$(this).css("width", height * ratioH); // according to height ratio
}
});
});
यहां मेहदीवे के जवाब में सुधार किया गया है। नई चौड़ाई और / या ऊँचाई अधिकतम मान पर सेट नहीं की जा रही थी। एक अच्छा परीक्षण मामला निम्नलिखित है (1768 x 1075 पिक्सेल): http://spacecoastsports.com/wp-content/uploads/2014/06/sportsballs1.png । (प्रतिष्ठा अंक न होने के कारण मैं इस पर ऊपर टिप्पणी नहीं कर पाया।)
// Make sure image doesn't exceed 100x100 pixels
// note: takes jQuery img object not HTML: so width is a function
// not a property.
function resize_image (image) {
var maxWidth = 100; // Max width for the image
var maxHeight = 100; // Max height for the image
var ratio = 0; // Used for aspect ratio
// Get current dimensions
var width = image.width()
var height = image.height();
console.log("dimensions: " + width + "x" + height);
// If the current width is larger than the max, scale height
// to ratio of max width to current and then set width to max.
if (width > maxWidth) {
console.log("Shrinking width (and scaling height)")
ratio = maxWidth / width;
height = height * ratio;
width = maxWidth;
image.css("width", width);
image.css("height", height);
console.log("new dimensions: " + width + "x" + height);
}
// If the current height is larger than the max, scale width
// to ratio of max height to current and then set height to max.
if (height > maxHeight) {
console.log("Shrinking height (and scaling width)")
ratio = maxHeight / height;
width = width * ratio;
height = maxHeight;
image.css("width", width);
image.css("height", height);
console.log("new dimensions: " + width + "x" + height);
}
}
$('#productThumb img').each(function() {
var maxWidth = 140; // Max width for the image
var maxHeight = 140; // Max height for the image
var ratio = 0; // Used for aspect ratio
var width = $(this).width(); // Current image width
var height = $(this).height(); // Current image height
// Check if the current width is larger than the max
if(width > height){
height = ( height / width ) * maxHeight;
} else if(height > width){
maxWidth = (width/height)* maxWidth;
}
$(this).css("width", maxWidth); // Set new width
$(this).css("height", maxHeight); // Scale height based on ratio
});
यदि छवि आनुपातिक है तो यह कोड छवि के साथ आवरण को भर देगा। यदि छवि अनुपात में नहीं है, तो अतिरिक्त चौड़ाई / ऊंचाई फसली हो जाएगी।
<script type="text/javascript">
$(function(){
$('#slider img').each(function(){
var ReqWidth = 1000; // Max width for the image
var ReqHeight = 300; // Max height for the image
var width = $(this).width(); // Current image width
var height = $(this).height(); // Current image height
// Check if the current width is larger than the max
if (width > height && height < ReqHeight) {
$(this).css("min-height", ReqHeight); // Set new height
}
else
if (width > height && width < ReqWidth) {
$(this).css("min-width", ReqWidth); // Set new width
}
else
if (width > height && width > ReqWidth) {
$(this).css("max-width", ReqWidth); // Set new width
}
else
(height > width && width < ReqWidth)
{
$(this).css("min-width", ReqWidth); // Set new width
}
});
});
</script>
अतिरिक्त अस्थायी-var या कोष्ठक के बिना।
var width= $(this).width(), height= $(this).height()
, maxWidth=100, maxHeight= 100;
if(width > maxWidth){
height = Math.floor( maxWidth * height / width );
width = maxWidth
}
if(height > maxHeight){
width = Math.floor( maxHeight * width / height );
height = maxHeight;
}
ध्यान रखें: खोज इंजन इसे पसंद नहीं करते हैं, अगर चौड़ाई और ऊंचाई की विशेषता छवि को फिट नहीं करती है, लेकिन वे जेएस को नहीं जानते हैं।
कुछ परीक्षण और त्रुटि के बाद मैं इस समाधान पर आया:
function center(img) {
var div = img.parentNode;
var divW = parseInt(div.style.width);
var divH = parseInt(div.style.height);
var srcW = img.width;
var srcH = img.height;
var ratio = Math.min(divW/srcW, divH/srcH);
var newW = img.width * ratio;
var newH = img.height * ratio;
img.style.width = newW + "px";
img.style.height = newH + "px";
img.style.marginTop = (divH-newH)/2 + "px";
img.style.marginLeft = (divW-newW)/2 + "px";
}
सीएसएस का उपयोग करके आकार को प्राप्त किया जा सकता है (पहलू अनुपात को बनाए रखना)। यह एक और सरलीकृत जवाब है जो डेन डैस्कलेस्कु के पोस्ट से प्रेरित है।
img{
max-width:200px;
/*Or define max-height*/
}
<img src="http://e1.365dm.com/13/07/4-3/20/alastair-cook-ashes-profile_2967773.jpg" alt="Alastair Cook" />
<img src="http://e1.365dm.com/13/07/4-3/20/usman-khawaja-australia-profile_2974601.jpg" alt="Usman Khawaja"/>
2 चरण:
चरण 1) छवि की मूल चौड़ाई / मूल ऊंचाई के अनुपात की गणना करें।
चरण 2) नई ऊंचाई के अनुरूप नई चौड़ाई प्राप्त करने के लिए नए वांछित ऊंचाई द्वारा मूल_प्रकार / मूल_हाइट अनुपात को गुणा करें।
इस समस्या को सीएसएस द्वारा हल किया जा सकता है।
.image{
max-width:*px;
}
max-widthऔर सेटmax-heightकरें100%।