Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-34633 Repository: Flickr supports small images

  • Loading branch information...
commit 82bf5b10b9e3e4078e18960c3ab762cf8a9ef8a8 1 parent 668ba1e
Frédéric Massart FMCorz authored
Showing with 75 additions and 33 deletions.
  1. +33 −9 repository/flickr/lib.php
  2. +42 −24 repository/flickr_public/lib.php
42 repository/flickr/lib.php
View
@@ -38,6 +38,11 @@ class repository_flickr extends repository {
public $photos;
/**
+ * Stores sizes of images to prevent multiple API call
+ */
+ static private $sizes = array();
+
+ /**
*
* @param int $repositoryid
* @param object $context
@@ -228,16 +233,35 @@ public function get_listing($path = '', $page = '') {
* @return string
*/
private function build_photo_url($photoid) {
- $result = $this->flickr->photos_getSizes($photoid);
- $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($photoid);
+ if (!isset($bestsize['source'])) {
+ throw new repository_exception('cannotdownload', 'repository');
+ }
+ 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 $url;
+ return $bestsize;
}
public function get_link($photoid) {
66 repository/flickr_public/lib.php
View
@@ -42,6 +42,11 @@ class repository_flickr_public extends repository {
public $photos;
/**
+ * Stores sizes of images to prevent multiple API call
+ */
+ static private $sizes = array();
+
+ /**
* constructor method
*
* @global object $CFG
@@ -403,16 +408,35 @@ public function print_search() {
* @return string
*/
private function build_photo_url($photoid) {
- $result = $this->flickr->photos_getSizes($photoid);
- $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($photoid);
+ if (!isset($bestsize['source'])) {
+ throw new repository_exception('cannotdownload', 'repository');
+ }
+ 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 $url;
+ return $bestsize;
}
public function get_link($photoid) {
@@ -435,29 +459,23 @@ public function get_file($photoid, $file = '') {
$author = $info['owner']['username'];
}
$copyright = get_string('author', 'repository') . ': ' . $author;
- $result = $this->flickr->photos_getSizes($photoid);
- // 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);
}
+
$result = parent::get_file($source, $file);
$path = $result['path'];
+
if (!empty($this->usewatermarks)) {
$img = new moodle_image($path);
$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']));
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.