我正在从facebook的图表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
}
答案 0 :(得分:2)
这里有一些代码,我们首先设置一个图片数组来比较具有非常大的尺寸,然后我们循环通过您的数组数组来找到宽度和高度都大于那么的图像但是该区域小于所选图像。
$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;
}
}
}
答案 1 :(得分:2)
我相信这可能对你有用。它将返回满足最小宽度/高度的最小pic或如果没有图片满足则返回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;
}
答案 2 :(得分: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:此外,这会考虑总像素大小以检查最小值。就像Mark在评论中所说,目前还不清楚你是想要总像素大小是最小的还是宽度/高度。