0

私は、特定の url からさまざまなデータを抽出するのは非常に初心者です。ただし、リストされている幅と高さよりもサイズが大きい画像を抽出する次のコードを見つけました。

このようにして、2つか3つの画像を取得しています。

  1. 最大サイズの 1 つの画像を自動的に取得するにはどうすればよいですか?

  2. 以下のループで言及されているように画像サイズが指定されている場合、画像を1つだけ表示する方法.?

  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
    

私はあなたの助けに感謝します。

4

1 に答える 1

0
$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
于 2013-09-15T12:02:07.820 に答える