简体   繁体   English

如何调整加载到页面上的图像的大小?

[英]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.

相关问题 我如何在 PHP 中使用 imagescale 调整图像大小 - How i can resize Images with imagescale in PHP 如何将$ _GET变量重定向到另一个页面? - How can I redirect $_GET variables onto another page? 如何上传/调整数码相机拍摄的图像? - How can I upload/resize images taken with a digital camera? 如何将目录中的图像和文件回显到页面php - How to echo images and files within a directory onto a page php 如何在加载的页面上动态更改图像,并在每个图像上附加一个链接 - How to dynamically change images on a loaded page with a link attached to each images 如何在表格内的同一页面上显示用户搜索结果? - How can I display user search results onto the same page inside a table? 如何在PHP中将大量图像复制到画布上? - how do I copy a lot of images onto a canvas in PHP? 如果图像标签是从数据库动态创建的,如何使用javascript或php确定何时加载图像? - how can I use javascript or php to determine when images are loaded if image tags are dynamically created from database? 将图像上传到子页面中的表单后,如何在主页上重新加载图像? - How can I reload images on a main page after uploading images to a form in the sub page? 当页面被重定向时,如何在正在加载的页面的 innerhtml 中使用 javascript 变量? - As a page is redirected how can I use a javascript variable in the innerhtml of the page being loaded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM