Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 我如何找到h&;将图像的w转换为变量,然后使用这些变量_Variables_Image Manipulation_Javascript - Fatal编程技术网

Variables 我如何找到h&;将图像的w转换为变量,然后使用这些变量

Variables 我如何找到h&;将图像的w转换为变量,然后使用这些变量,variables,image-manipulation,javascript,Variables,Image Manipulation,Javascript,好的,交易如下: 我正在使用一个很好的插件,我从 这一切都很好,但它并没有解决一个特殊的问题:我会让用户上传不同大小和纵横比的图像,所以,下面是他们提供的: function preview(img, selection) { var scaleX = 160 / selection.width; var scaleY = 160 / selection.height; $('#thumbnail + div > img').css({ width: Math.round

好的,交易如下:

我正在使用一个很好的插件,我从

这一切都很好,但它并没有解决一个特殊的问题:我会让用户上传不同大小和纵横比的图像,所以,下面是他们提供的:

function preview(img, selection) { 
 var scaleX = 160 / selection.width; 
 var scaleY = 160 / selection.height; 
 $('#thumbnail + div > img').css({ 
  width: Math.round(scaleX * 500) + 'px', 
  height: Math.round(scaleY * 375) + 'px',
  marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', 
  marginTop: '-' + Math.round(scaleY * selection.y1) + 'px' 
其中,scaleX和scaleY后面的数字是他们在演示中使用的图像的实际像素尺寸。我希望能够让用户上传一个pic,然后让.js创建变量,使这些值成为scaleX和scaleY乘数,这样:

width: Math.round(scaleX * width) + 'px', 
height: Math.round(scaleY * height) + 'px',
我早些时候在网站上发现:

var image=document.getElementById("imageID");
var width=image.offsetWidth;
var height=image.offsetHeight;
那么,像我这样的新手是如何让所有这些东西一起工作的呢

$(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
    }
});
}))