[英]How can I resize images that are loaded onto a page?
So I built a website that loads the homepage of Imgur and pulls the images as full size and then puts them on the homepage of my site. 因此,我建立了一个网站,加载Imgur的主页,并将图像拉为完整大小,然后将它们放在我网站的主页上。 The only problem is that some of the images are huge and I don't know how to resize them.
唯一的问题是一些图像很大,我不知道如何调整它们的大小。 I don't know if I should use CSS, Javascript, or if I can do it in PHP.
我不知道我是否应该使用CSS,Javascript,或者我是否可以在PHP中使用它。
FYI, I'm using the Simple HTML Dom Scraper 仅供参考,我正在使用Simple HTML Dom Scraper
Here's my website: www.probablycats.com 这是我的网站: www.probablycats.com
Here's my PHP code: 这是我的PHP代码:
$imgur = file_get_html('http://www.imgur.com');
foreach($imgur->find('div[class="post"]') as $images)
{
$imagelink = 'http://www.imgur.com/gallery/'.$images->id;
$imagepage = file_get_html($imagelink);
foreach($imagepage->find('#image') as $image2)
{
echo "<div class=\"images\">$image2</div>";
}
} }
Here's my CSS code: 这是我的CSS代码:
html
{
background-color:#D0D0D0;
max-width:100%;
}
.images
{
padding:20px 20px;
float:left;
}
So to clarify, some of the images are huge and go past the width of the screen. 因此,为了澄清,一些图像是巨大的,超过了屏幕的宽度。 How exactly would I resize them?
我究竟会如何调整它们的大小? I can explain more if needed, but if anyone can help me with this I would really appreciate it.
如果需要我可以解释更多,但如果有人能帮助我,我会非常感激。 Thanks!
谢谢!
The easy way is with CSS: 简单的方法是使用CSS:
.images img
{
max-width: 200px;
}
This will render the images in the desired maximum size, while maintaining the original proportions. 这将使图像呈现所需的最大尺寸,同时保持原始比例。 Note: resizing with the HTML width attribute will not maintain original proportions in all browsers , but will not physically resize the images.
注意:使用HTML width属性调整大小不会在所有浏览器中保持原始比例 ,但不会在物理上调整图像大小。 However, since you're referencing the images from Imgur's server, this won't impact your own site's bandwidth.
但是,由于您正在引用Imgur服务器中的图像,因此这不会影响您自己网站的带宽。
To do this with PHP would involve loading the images onto your server, using gd2 or imagick to resize them, saving them, and then referencing the saved images on your server for use in display on your page. 要使用PHP执行此操作,需要将图像加载到服务器上,使用gd2或imagick调整大小,保存它们,然后引用服务器上保存的图像以便在页面上显示。 This may also be a violation of Imgur's T&C
这也可能违反了Imgur的条款和条件
You could specify a width here: 您可以在此处指定宽度:
echo "<div class=\"images\" width=\"150px\">$image2</div>";
However, this will still load the entire image, not technically scale it down. 但是,这仍然会加载整个图像,而不是从技术上缩小它。 So, if the image is huge, it will still load slowly
因此,如果图像很大,它仍然会缓慢加载
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.