View source
<?php
namespace Drupal\flickr_api\Service;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
class Helpers {
use StringTranslationTrait;
public function __construct(TranslationInterface $stringTranslation) {
$this->stringTranslation = $stringTranslation;
}
public function isNsid($id) {
return preg_match('/^\\d+@N\\d+$/', $id);
}
public function photoSizes() {
return [
's' => [
'label' => 'Square',
'description' => $this
->t('s: 75 px square'),
'width' => 75,
'height' => 75,
],
't' => [
'label' => 'Thumbnail',
'description' => $this
->t('t: 100px on longest side'),
'width' => 100,
'height' => 67,
],
'q' => [
'label' => 'Large Square',
'description' => $this
->t('q: 150px square'),
'width' => 150,
'height' => 150,
],
'm' => [
'label' => 'Small',
'description' => $this
->t('m: 240px on longest side'),
'width' => 240,
'height' => 240,
],
'n' => [
'label' => 'Small 320',
'description' => $this
->t('n: 320px on longest side'),
'width' => 320,
'height' => 320,
],
'-' => [
'label' => 'Medium',
'description' => $this
->t('-: 500px on longest side'),
'width' => 500,
'height' => 500,
],
'z' => [
'label' => 'Medium 640',
'description' => $this
->t('z: 640px on longest side'),
'width' => 640,
'height' => 640,
],
'c' => [
'label' => 'Medium 800',
'description' => $this
->t('c: 800px on longest side'),
'width' => 800,
'height' => 800,
],
'b' => [
'label' => 'Large',
'description' => $this
->t('b: 1024px on longest side'),
'width' => 1024,
'height' => 1024,
],
'h' => [
'label' => 'Large 1600',
'description' => $this
->t('h: 1600px on longest side'),
'width' => 1600,
'height' => 1600,
],
'k' => [
'label' => 'Large 2048',
'description' => $this
->t('k: 2048px on longest side'),
'width' => 2048,
'height' => 2048,
],
'o' => [
'label' => 'Original',
'description' => $this
->t('o: Original image'),
'width' => 2048,
'height' => 2048,
],
'x' => [
'label' => 'slideshow',
'description' => $this
->t('x: Full featured responsive slideshow (for group, set and user IDs only)'),
],
'y' => [
'label' => 'Simple slideshow',
'description' => $this
->t('y: Basic responsive slideshow (for set and user IDs only)'),
],
];
}
public function inArrayR($needle, array $haystack, $strict = FALSE) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || is_array($item) && self::inArrayR($needle, $item, $strict)) {
return TRUE;
}
}
return FALSE;
}
public function photoImgUrl($photo, $size = NULL, $format = NULL) {
$farm = isset($photo['farm']) ? $photo['farm'] : 1;
$server = $photo['server'];
$id = isset($photo['primary']) ? $photo['primary'] : $photo['id'];
$secret = $photo['secret'];
$suffix = $size ? "_{$size}." : '.';
$suffix = $size == '-' ? '.' : $suffix;
$extension = $size == 'o' ? $format : 'jpg';
return "https://farm{$farm}.static.flickr.com/{$server}/{$id}_{$secret}" . $suffix . $extension;
}
}