2

我正在从 facebook 的 graph api 中检索专辑照片。
这段代码可以解决问题,但是我正在尝试选择等于 130 及以上宽度的最小缩略图:130 x 高度:130

意味着宽度或高度必须为 130 及以上,而不是宽度和高度的总和。

注意:数组列表是来自 Facebook 相册的图像变化,因此它们是成比例的。因此,如果它是纵向或横向尺寸,它将相应地按比例缩放。

因此,从print_r下面您可以看到第一个数组中的第 (2) 项符合该描述,但其他数组将是数字 (2) 和 (1),因为这是 130 宽度/高度以上的最小数组

$userURL2 = "https://graph.facebook.com/$albumID/photos?access_token=" . $fb_oAuth_token;
$ch2 = curl_init($userURL2);
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$data2 = curl_exec($ch2);
curl_close($ch2);
$pictures = json_decode($data2, true);

print_r($pictures);

输出自print_r($pictures);

Array
(
        [0] => Array
                (
                        [height] => 288
                        [width] => 460
                        [source] => https://myurl.jpg
                )
        [1] => Array
                (
                        [height] => 200
                        [width] => 320
                        [source] => https://myurl.jpg
                )

        [2] => Array
                (
                        [height] => 130
                        [width] => 180
                        [source] => https://myurl.jpg
                )

        [3] => Array
                (
                        [height] => 81
                        [width] => 130
                        [source] => https://myurl.jpg
                )

)

Array
(
        [0] => Array
                (
                        [height] => 500
                        [width] => 500
                        [source] => https://myurl.jpg
                )

        [1] => Array
                (
                        [height] => 500
                        [width] => 500
                        [source] => https://myurl.jpg
                )

        [2] => Array
                (
                        [height] => 480
                        [width] => 480
                        [source] => https://myurl.jpg
                )


)
Array
(
        [0] => Array
                (
                        [height] => 335
                        [width] => 300
                        [source] => https://myurl.jpg
                )

        [1] => Array
                (
                        [height] => 335
                        [width] => 300
                        [source] => https://myurl.jpg
                )


)

问题:我将如何在 php 中编写这个?

foreach($pictures['data'] as $picture){

    $width = $picture['width'];
    $height = $picture['height'];

    // choose the smallest [source] above width:130 x height:130

}
4

3 回答 3

2

这是给你的一些代码,我们首先设置一个图片数组来比较它具有非常大的尺寸,然后我们循环通过你的数组数组来找到一个宽度和高度都大于 130 的图像,但是面积小于所选图像。

$selectedPicture = array('width' => 10000, 'height'  => 10000, 'source' => '');

foreach($pictures as $album){
    foreach($album as $picture){
        if($picture['width'] > 130 && $picture['height'] > 130 && ($picture['width'] * $picture['height']) < ($selectedPicture['width'] * $selectedPicture['height'])){
            $selectedPicture = $picture;
        }
    }
}
于 2013-07-29T21:43:20.753 回答
2

我相信这可能对你有用。它将返回满足最小宽度/高度的最小图片,如果没有图片满足,则返回 false。

$test = array(
    0 => array(
        'height' => 288,
        'width' => 460,
        'source' => 'https://myurl.jpg'
    ),
    1 => array(
        'height' => 81,
        'width' => 130,
        'source' => 'https://myurl.jpg'
    ),
    2 => array(
        'height' => 200,
        'width' => 320,
        'source' => 'https://myurl.jpg'
    ),
    3 => array(
        'height' => 130,
        'width' => 180,
        'source' => 'https://myurl.jpg'
    ),
    4 => array(
        'height' => 100,
        'width' => 120,
        'source' => 'https://myur5.jpg'
    )
);


$pic = getTheRightPic($test, 130, 130);

var_dump($pic);


function getTheRightPic($pictures, $min_width, $min_height)
{
    //get one to start with
    do
    {
        $win_pic = array_pop($pictures);
    }
    while ( ! checkXY($win_pic, $min_width, $min_height));

    //if none of the pic satisfies return false
    if ($win_pic === false)
    {
        return false;
    }

    foreach ($pictures as $pic)
    {
        $win_pic = comparePics($win_pic, $pic, $min_width, $min_height);
    }

    return $win_pic;
}

function comparePics($original, $compare, $min_width, $min_height)
{
    if ( ! checkXY($compare, $min_width, $min_height))
    {
        return $original;
    }

    //calculate sizes
    $original['size'] = $original['width'] * $original['height'];
    $compare['size']  = $compare['width'] * $compare['height'];

    //return the smaler pic
    if ($original['size'] > $compare['size'])
    {
        return $compare;
    }

    return $original;
}

function checkXY($pic, $min_width, $min_height)
{
    if ($pic['width'] < $min_width && $pic['height'] < $min_height)
    {
        return false;
    }

    return true;
}
于 2013-07-29T22:43:06.010 回答
1

这是你需要的吗?我认为这可行,但我还没有测试过。

$thumbnail = array();                // array to store the smallest thumbnail over 130 x 130 in

foreach($pictures['data'] as $key => $picture){
    if($picture['width'] > 130 && $picture['height'] > 130){        // Check if thumbnail has height AND width over 130.
        $size = $picture['width'] * $picture['height'];     // Store total pixel size since the check for > 130 width and height was already done anyway.
        if(!isset($thumbnail['index'])){               // Check if there's a thumbnail already stored,
            $thumbnail['index'] = $key;                // and if not, store this one
            $thumbnail['size'] = $size;
        } elseif($thumbnail['size'] > $size) {         // Check if currently stored thumbnail is bigger,
            $thumbnail['index'] = $key;                // And if it is, store this one
            $thumbnail['size'] = $size;
        }
    }
}

然后,如果我没记错的话,你应该在 $thumbnail 数组中存储超过 130 x 130 像素的最小缩略图。

e:哦,顺便说一句,我的意思是您的数组引用了存储在原始数组中的缩略图之一。你不会存储整个缩略图,但我想你可以通过添加'url'、'width'和'height'索引来做到这一点。

e2:此外,这考虑到要检查最小的总像素大小。就像马克在他的评论中所说的那样,目前还不清楚您是否希望总像素大小是最小的还是宽度/高度。

于 2013-07-29T21:44:03.253 回答