【问题标题】:Get images within same size in php在php中获取相同大小的图像
【发布时间】:2018-10-22 04:57:45
【问题描述】:

我创建了一个应用程序来获取 Instagram 标签图像作为输出。我可以加载图像。但我需要获得相同大小的图像。有人帮我解决这个问题吗?

这里是代码...

  <?php
    function scrape_insta_hash($tag) {
        $insta_source = 
    file_get_contents('https://www.instagram.com/explore/tags/'.$tag.'/'); 
        $shards = explode('window._sharedData = ', $insta_source);
        $insta_json = explode(';</script>', $shards[1]); 
        $insta_array = json_decode($insta_json[0], TRUE);
        return $insta_array; 
    }

        $tag = 'savesripada'; 
        $results_array = scrape_insta_hash($tag);
        $limit = 15; 
        $image_array= array(); 
        for ($i=0; $i < $limit; $i++) { 

            $latest_array = $results_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'][$i]['node'];
            $image_data  = '<img src="'.$latest_array['thumbnail_src'].'">'; 
            array_push($image_array, $image_data);
        }


        foreach ($image_array as $image) {
            echo $image;
    }
      ?>

【问题讨论】:

  • instagram.com/explore/tags/savesripada 返回所有尺寸相同的图像。
  • @Yvette Colomb♦ 您删除的那个答案是问题的实际答案。您可以从另一个已标记为重复的问题中删除。

标签: javascript php instagram-api


【解决方案1】:

你可以这样做:

    <?php
    $tag = 'savesripada'; 
    $results_array = scrape_insta_hash($tag);
    $image_array = array_filter(
        $results_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'],
         function($item) {
            $size = $item['node']['dimensions']; // example : [height: 810, width: 1080]
            return $size['height'] === '130' && $size['width'] === '150'; // Adjust your condition if you want another metrix
        });
    var_dump($image_array); // Will print image with exactly 150*130

这里我使用array_filter 来搜索和提取只有高度 == 130 和宽度 == 150 的图像。

【讨论】:

    【解决方案2】:

    以下代码会将图像调整为您想要的大小。

    <?php
    $height = 150;
    $width = 150;
        function scrape_insta_hash($tag) {
                $insta_source =
            file_get_contents('https://www.instagram.com/explore/tags/'.$tag.'/');
                $shards = explode('window._sharedData = ', $insta_source);
                $insta_json = explode(';</script>', $shards[1]);
                $insta_array = json_decode($insta_json[0], TRUE);
                return $insta_array;
            }
    
                $tag = 'savesripada';
                $results_array = scrape_insta_hash($tag);
                $limit = 15;
                $image_array= array();
                for ($i=0; $i < $limit; $i++) {
    
                    $latest_array = $results_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'][$i]['node'];
                    $image_data  = '<img height="'.$height.'" width="'.$width.'" src="'.$latest_array['thumbnail_src'].'">';
                    array_push($image_array, $image_data);
                }
    
    
                foreach ($image_array as $image) {
                    echo $image;
                }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 2018-07-24
      • 1970-01-01
      相关资源
      最近更新 更多