From 4516e7204a0b0104dca1427fc364956b4f0b564d Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Tue, 28 Aug 2012 17:01:41 +0800 Subject: [PATCH] MDL-34633 Repository: Flickr supports small images Conflicts: repository/flickr/lib.php repository/flickr_public/lib.php --- repository/flickr/lib.php | 48 ++++++++++++++++----- repository/flickr_public/lib.php | 71 +++++++++++++++++++++----------- 2 files changed, 84 insertions(+), 35 deletions(-) diff --git a/repository/flickr/lib.php b/repository/flickr/lib.php index cfc1739c247..82a966eb759 100644 --- a/repository/flickr/lib.php +++ b/repository/flickr/lib.php @@ -36,6 +36,11 @@ class repository_flickr extends repository { private $flickr; public $photos; + /** + * Stores sizes of images to prevent multiple API call + */ + static private $sizes = array(); + /** * * @param int $repositoryid @@ -221,18 +226,41 @@ class repository_flickr extends repository { return $this->search('', $page); } + /** + * Return photo url by given photo id + * @param string $photo_id + * @return string + */ public function get_link($photo_id) { - global $CFG; - $result = $this->flickr->photos_getSizes($photo_id); - $url = ''; - if(!empty($result[4])) { - $url = $result[4]['source']; - } elseif(!empty($result[3])) { - $url = $result[3]['source']; - } elseif(!empty($result[2])) { - $url = $result[2]['source']; + $bestsize = $this->get_best_size($photo_id); + if (!isset($bestsize['source'])) { + throw new repository_exception('cannotdownload', 'repository'); } - return $url; + return $bestsize['source']; + } + + /** + * Returns the best size for a photo + * + * @param string $photoid the photo identifier + * @return array of information provided by the API + */ + protected function get_best_size($photoid) { + if (!isset(self::$sizes[$photoid])) { + // Sizes are returned from smallest to greatest. + self::$sizes[$photoid] = $this->flickr->photos_getSizes($photoid); + } + $sizes = self::$sizes[$photoid]; + $bestsize = array(); + if (is_array($sizes)) { + while ($bestsize = array_pop($sizes)) { + // Make sure the source is set. Exit the loop if found. + if (isset($bestsize['source'])) { + break; + } + } + } + return $bestsize; } /** diff --git a/repository/flickr_public/lib.php b/repository/flickr_public/lib.php index b18b4705889..964ac7dc3cd 100644 --- a/repository/flickr_public/lib.php +++ b/repository/flickr_public/lib.php @@ -35,6 +35,11 @@ class repository_flickr_public extends repository { private $flickr; public $photos; + /** + * Stores sizes of images to prevent multiple API call + */ + static private $sizes = array(); + /** * constructor method * @@ -391,18 +396,41 @@ class repository_flickr_public extends repository { return $str; } + /** + * Return photo url by given photo id + * @param string $photo_id + * @return string + */ public function get_link($photo_id) { - global $CFG; - $result = $this->flickr->photos_getSizes($photo_id); - $url = ''; - if(!empty($result[4])) { - $url = $result[4]['source']; - } elseif(!empty($result[3])) { - $url = $result[3]['source']; - } elseif(!empty($result[2])) { - $url = $result[2]['source']; + $bestsize = $this->get_best_size($photo_id); + if (!isset($bestsize['source'])) { + throw new repository_exception('cannotdownload', 'repository'); } - return $url; + return $bestsize['source']; + } + + /** + * Returns the best size for a photo + * + * @param string $photoid the photo identifier + * @return array of information provided by the API + */ + protected function get_best_size($photoid) { + if (!isset(self::$sizes[$photoid])) { + // Sizes are returned from smallest to greatest. + self::$sizes[$photoid] = $this->flickr->photos_getSizes($photoid); + } + $sizes = self::$sizes[$photoid]; + $bestsize = array(); + if (is_array($sizes)) { + while ($bestsize = array_pop($sizes)) { + // Make sure the source is set. Exit the loop if found. + if (isset($bestsize['source'])) { + break; + } + } + } + return $bestsize; } /** @@ -421,21 +449,14 @@ class repository_flickr_public extends repository { $author = $info['owner']['username']; } $copyright = get_string('author', 'repository') . ': ' . $author; - $result = $this->flickr->photos_getSizes($photo_id); - // download link - $source = ''; - // flickr photo page - $url = ''; - if (!empty($result[4])) { - $source = $result[4]['source']; - $url = $result[4]['url']; - } elseif(!empty($result[3])) { - $source = $result[3]['source']; - $url = $result[3]['url']; - } elseif(!empty($result[2])) { - $source = $result[2]['source']; - $url = $result[2]['url']; + + // If we can read the original secret, it means that we have access to the original picture. + if (isset($info['originalsecret'])) { + $source = $this->flickr->buildPhotoURL($info, 'original'); + } else { + $source = $this->build_photo_url($photoid); } + $path = $this->prepare_file($file); $fp = fopen($path, 'w'); $c = new curl; @@ -447,7 +468,7 @@ class repository_flickr_public extends repository { $img->watermark($copyright, array(10,10), array('ttf'=>true, 'fontsize'=>12))->saveas($path); } - return array('path'=>$path, 'url'=>$url, 'author'=>$info['owner']['realname'], 'license'=>$this->license4moodle($info['license'])); + return array('path'=>$path, 'author'=>$info['owner']['realname'], 'license'=>$this->license4moodle($info['license'])); } /**