繁体   English   中英

如何获得页面上所有图像中尺寸最大的图像

[英]how to get the image with largest size among all the images on the page

我是一个新手,可以从给定的URL中提取不同的数据。但是,我发现以下代码可以提取尺寸大于所列宽度和高度的图像。

这样,我得到2或3张图像。

  1. 如何自动获得一张最大尺寸的图像?

  2. 如果在下面的循环中已提到图像大小,如何仅显示一张图像?

  3. 代码比较慢。要花一些时间才能输出,怎么可以更快呢?

  4. 我只是复制了代码。如果对该循环中发生的事件给出一些注释,那么这将对代码的理解有所帮助

      $string = $co->fetch_record ($url2); $image_regex = '/<img[^>]*'.'src=[\\"|\\'](.*)[\\"|\\']/Ui'; preg_match_all($image_regex, $string, $img, PREG_PATTERN_ORDER); $images_array = $img[1]; ?> <div class="images"> <?php $k = 0; for ($i = 0; $i <= sizeof ($images_array); $i++) { if(@$images_array[$i]) { if(@getimagesize (@$images_array[$i])) { list($width, $height, $type, $attr) = getimagesize (@$images_array[$i]); if($width >= 50 && $height >= 50) { echo "<img src='" . @$images_array[$i] . "' width='400' id='" . $k . "' >"; $k++; } } } } ?> <input type="hidden" name="total_images" id="total_images" value="<?php echo --$k ?>"/> </div> <?php 

我将非常感谢您的帮助。

$string = $co->fetch_record ($url2);

$image_regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex, $string, $img, PREG_PATTERN_ORDER);
$images_array = $img[1];
?>

<div class="images">
    <?php
    $k = 0;
    $area = 0;
    $largest_image;
    for ($i = 0; $i <= sizeof ($images_array); $i++) {
        if(@$images_array[$i]) {
            if(@getimagesize (@$images_array[$i])) {
                list($width, $height, $type, $attr) = getimagesize (@$images_array[$i]);
                // calculate current image area
                $latest_area = $width * $height;
                // if current image area is greater than previous
                if($latest_area > $area) {
                    $area = $latest_area;
                    $larest_image = $images_array[$i];
                    $k++;
                }
            }
        }
    }

    // at this point the largest image should be in the $largest_image variable
    // print out $largest_image here

    ?>
    <input type="hidden" name="total_images" id="total_images" value="<?php echo --$k ?>"/>
</div>


<?php

暂无
暂无

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

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