@@ -35,6 +35,11 @@ class repository_flickr_public extends repository {
35
private $ flickr ;
35
private $ flickr ;
36
public $ photos ;
36
public $ photos ;
37
37
38
+ /**
39
+ * Stores sizes of images to prevent multiple API call
40
+ */
41
+ static private $ sizes = array ();
42
+
38
/**
43
/**
39
* constructor method
44
* constructor method
40
*
45
*
@@ -207,13 +212,14 @@ public function logout() {
207
212
208
public function license4moodle ($ license_id ) {
213
public function license4moodle ($ license_id ) {
209
$ license = array (
214
$ license = array (
215
+ '0 ' => 'allrightsreserved ' ,
210
'1 ' => 'cc-nc-sa ' ,
216
'1 ' => 'cc-nc-sa ' ,
211
'2 ' => 'cc-nc ' ,
217
'2 ' => 'cc-nc ' ,
212
'3 ' => 'cc-nc-nd ' ,
218
'3 ' => 'cc-nc-nd ' ,
213
'4 ' => 'cc ' ,
219
'4 ' => 'cc ' ,
214
'5 ' => 'cc-sa ' ,
220
'5 ' => 'cc-sa ' ,
215
'6 ' => 'cc-nd ' ,
221
'6 ' => 'cc-nd ' ,
216
- '7 ' => 'allrightsreserved '
222
+ '7 ' => 'other '
217
);
223
);
218
return $ license [$ license_id ];
224
return $ license [$ license_id ];
219
}
225
}
@@ -390,18 +396,41 @@ public function print_search() {
390
return $ str ;
396
return $ str ;
391
}
397
}
392
398
399
+ /**
400
+ * Return photo url by given photo id
401
+ * @param string $photo_id
402
+ * @return string
403
+ */
393
public function get_link ($ photo_id ) {
404
public function get_link ($ photo_id ) {
394
- global $ CFG ;
405
+ $ bestsize = $ this ->get_best_size ($ photo_id );
395
- $ result = $ this ->flickr ->photos_getSizes ($ photo_id );
406
+ if (!isset ($ bestsize ['source ' ])) {
396
- $ url = '' ;
407
+ throw new repository_exception ('cannotdownload ' , 'repository ' );
397
- if (!empty ($ result [4 ])) {
408
+ }
398
- $ url = $ result [4 ]['source ' ];
409
+ return $ bestsize ['source ' ];
399
- } elseif (!empty ($ result [3 ])) {
410
+ }
400
- $ url = $ result [3 ]['source ' ];
411
+
401
- } elseif (!empty ($ result [2 ])) {
412
+ /**
402
- $ url = $ result [2 ]['source ' ];
413
+ * Returns the best size for a photo
414
+ *
415
+ * @param string $photoid the photo identifier
416
+ * @return array of information provided by the API
417
+ */
418
+ protected function get_best_size ($ photoid ) {
419
+ if (!isset (self ::$ sizes [$ photoid ])) {
420
+ // Sizes are returned from smallest to greatest.
421
+ self ::$ sizes [$ photoid ] = $ this ->flickr ->photos_getSizes ($ photoid );
422
+ }
423
+ $ sizes = self ::$ sizes [$ photoid ];
424
+ $ bestsize = array ();
425
+ if (is_array ($ sizes )) {
426
+ while ($ bestsize = array_pop ($ sizes )) {
427
+ // Make sure the source is set. Exit the loop if found.
428
+ if (isset ($ bestsize ['source ' ])) {
429
+ break ;
430
+ }
431
+ }
403
}
432
}
404
- return $ url ;
433
+ return $ bestsize ;
405
}
434
}
406
435
407
/**
436
/**
@@ -420,21 +449,14 @@ public function get_file($photo_id, $file = '') {
420
$ author = $ info ['owner ' ]['username ' ];
449
$ author = $ info ['owner ' ]['username ' ];
421
}
450
}
422
$ copyright = get_string ('author ' , 'repository ' ) . ': ' . $ author ;
451
$ copyright = get_string ('author ' , 'repository ' ) . ': ' . $ author ;
423
- $ result = $ this ->flickr ->photos_getSizes ($ photo_id );
452
+
424
- // download link
453
+ // If we can read the original secret, it means that we have access to the original picture.
425
- $ source = '' ;
454
+ if (isset ($ info ['originalsecret ' ])) {
426
- // flickr photo page
455
+ $ source = $ this ->flickr ->buildPhotoURL ($ info , 'original ' );
427
- $ url = '' ;
456
+ } else {
428
- if (!empty ($ result [4 ])) {
457
+ $ source = $ this ->build_photo_url ($ photoid );
429
- $ source = $ result [4 ]['source ' ];
430
- $ url = $ result [4 ]['url ' ];
431
- } elseif (!empty ($ result [3 ])) {
432
- $ source = $ result [3 ]['source ' ];
433
- $ url = $ result [3 ]['url ' ];
434
- } elseif (!empty ($ result [2 ])) {
435
- $ source = $ result [2 ]['source ' ];
436
- $ url = $ result [2 ]['url ' ];
437
}
458
}
459
+
438
$ path = $ this ->prepare_file ($ file );
460
$ path = $ this ->prepare_file ($ file );
439
$ fp = fopen ($ path , 'w ' );
461
$ fp = fopen ($ path , 'w ' );
440
$ c = new curl ;
462
$ c = new curl ;
@@ -446,7 +468,7 @@ public function get_file($photo_id, $file = '') {
446
$ img ->watermark ($ copyright , array (10 ,10 ), array ('ttf ' =>true , 'fontsize ' =>12 ))->saveas ($ path );
468
$ img ->watermark ($ copyright , array (10 ,10 ), array ('ttf ' =>true , 'fontsize ' =>12 ))->saveas ($ path );
447
}
469
}
448
470
449
- return array ('path ' =>$ path , 'url ' => $ url , ' author ' =>$ info ['owner ' ]['realname ' ], 'license ' =>$ this ->license4moodle ($ info ['license ' ]));
471
+ return array ('path ' =>$ path , 'author ' =>$ info ['owner ' ]['realname ' ], 'license ' =>$ this ->license4moodle ($ info ['license ' ]));
450
}
472
}
451
473
452
/**
474
/**
0 commit comments