简体   繁体   English

缩放图片

[英]Scaling pictures

I'm displaying pictures within a div that is 350px x 350px big. 我正在显示350px x 350px大的div中的图片。 People can upload pictures to my fileserver and a destination link to that picture is created in a database as well as an id. 人们可以将图片上传到我的文件服务器,并在数据库中创建指向该图片的目标链接以及ID。 The php document getting the link is in the root folder and all pictures are saved in a subfolder called pictures. 获取链接的php文档位于根文件夹中,所有图片均保存在名为图片的子文件夹中。 I show the picture using something like this: 我使用以下方式显示图片:

$piclink = "SELECT `link` FROM `pictures` WHERE `id=`.$id.`";

The $id variable is stored when clicking a link from the previous page. 单击上一页中的链接时,将存储$id变量。

My problem: Not all pictures are the same format. 我的问题:并非所有图片都具有相同的格式。 I want to display them so that the max dimension is 350px, the other dimension according to proportion. 我想显示它们,以便最大尺寸为350px,其他尺寸按比例显示。 How do I do this? 我该怎么做呢? Simply doing this: 只需这样做:

echo "<img href=" . $piclink . " height='350px' width='350px'/>"

will stretch the picture to 350x350 thus breaking proportions. 会将图片拉伸到350x350,从而打破比例。 Any help appreciated. 任何帮助表示赞赏。

To retain the aspect ratio, you can retrieve the image dimensions and apply the ratio using the 350px maximums. 要保留宽高比,您可以检索图像尺寸并使用最大350px的像素来应用宽高比。

Here's an example: 这是一个例子:

HTML HTML

<div id="img-holder"></div>

Code

var img = new Image();
img.onload = function () {
    alert('Orignal width:'+img.width+', height:'+img.height);
    var width, height;
    if (img.width > img.height) {
        width = (img.width > 350 ? 350 : img.width);
        height = img.height * (350 / img.width);
    } else {
        height = (img.height > 350 ? 350 : img.height);
        width = img.width * (350 / img.height);
    }
    img.width=width;
    img.height=height;
    $("#img-holder").append(img);
}
img.src = "http://mps.thebeautyquotient.com/images/val4a.jpeg"

Here's the jsfiddle: http://jsfiddle.net/aN6Ec/ 这是jsfiddle: http : //jsfiddle.net/aN6Ec/

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

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