flickr
API Flickr, une autre façon de gerer ses photos.
Flickr qu'est-ce ?
Flickr est un site communautaire de photographies récemment racheté par Yahoo. Il permet de stocker et présenter ses photos sans trop de souci.
De nombreux outils permettent d'uploader ses photos rapidement et facilement (par ici).
Flickr met à disposition une API permettant d'utiliser flickr depuis son site web. La documentation de cette API se trouve ici.
Pré requis :
Pour stocker ses photos sur Flickr, un compte Flickr ou Yahoo est indispensable (un compte Yahoo.fr faisant aussi l'affaire). Pour utiliser l'API, il est nécessaire de récupérer une clef d'API en allant sur http://flickr.com/services/api/key.gne. Une fois cette clef obtenue, il suffit d'aller sur http://www.flickr.com/services/api/registered_keys.gne et d'obtenir l'API_secret en cliquant sur "Edit configuration". C'est sur ce même panneau qu'il faut déclarer l'URL de call-back nécessaire à toute authentification.
La classe Flickr :
Flickr propose plus de 70 méthodes, la classe Flickr en implémente quelques-unes. Chaque méthode de cette classe retourne une chaîne XML.
Les classes User, Album et Photo :
Celles-ci stockent les propriétés respectives des users, albums et photos. Avec de nombreuses méthodes ces classes procurent une bonne base d'outils pour la création d'un album photo.
Ces classes fonctionnent indifféremment en PHP4 et PHP5.
Exemple d'utilisation minimale
Les Méthodes
- User:: User - User - Nouvelle instance.
- User::getId - User - Récupérer l'Id de l'utilisateur.
- User::getName - User - Récupérer le pseudo de l'utilisateur.
- User::getRealName - User - Récupérer le nom réel de l'utilisateur.
- User::getLocation - User - Récupérer la localisation de l'utilisateur.
- User::getImgBuddyIcon - User - Récupérer l'avatar de l'utilisateur.
- User::getProfilUrl - User - Récupérer l'url Flickr du profil de l'utilisateur.
- User::getPhotosUrl - User - Récupérer l'url Flickr des photos de l'utilisateur.
- User::getPhotosCount - User - Récupérer le nombre de photos de l'utilisateur.
- User::getAlbumsCount - User - Récupérer le nombre d'albums de l'utilisateur.
- User::getAlbumsList - User - Récupérer la liste des albums de l'utilisateur.
- Album:: Album - Album - Nouvelle instance.
- Album::getId - Album - Récupérer l'Id d'un album.
- Album::getTitle - Album - Récupérer le titre d'un album.
- Album::getDescription - Album - Récupérer la description d'un album.
- Album::getPhotosCount - Album - Récupérer le nombre de photos dans un album.
- Album::getOwnerName - Album - Récupérer le pseudo du propriétaire d'un album.
- Album::getOwnerRealName - Album - Récupérer le nom réel du propriétaire d'un album.
- Album::getOwnerLocation - Album - Récupérer la localisation du propriétaire d'un album.
- Album::getOwnerBuddyIcon - Album - Récupérer l'avatar du propriétaire d'un album.
- Album::getOwnerOtherAlbums - Album - Récupérer la liste des autres albums du propriétaire d'un album.
- Album::getPhotos - Album - Récupérer la liste des photos d'un album.
- Photo:: Photo - Photo - Nouvelle instance.
- Photo::getId - Photo - Récupérer l'Id d'une photo.
- Photo::getSecret - Photo - Récupérer le code d'une photo.
- Photo::getTitle - Photo - Récupérer le titre d'une photo.
- Photo::getDescription - Photo - Récupérer la description d'une photo.
- Photo::getOwnerName - Photo - Récupérer le pseudo du propriétaire d'une photo.
- Photo::getOwnerRealName - Photo - Récupérer le nom réel du propriétaire d'une photo.
- Photo::getOwnerLocation - Photo - Récupérer la localisation du propriétaire d'une photo.
- Photo::getTakenDate - Photo - Récupérer la date de prise d'une photo.
- Photo::getTakenHour - Photo - Récupérer l'heure de prise d'une photo.
- Photo::getExif Infos - Photo - Récupérer les infos Exif d'une photo.
- Photo::getTags - Photo - Récupérer les mots clés associés à une photo.
- Photo::getNextPhotoId - Photo - Récupérer l'Id de la photo suivante d'une photo.
- Photo::getNextPhotoSecret - Photo - Récupérer le code de la photo suivante d'une photo.
- Photo::getNextPhotoTitle - Photo - Récupérer le titre de la photo suivante d'une photo.
- Photo::getNextPhotoImg - Photo - Récupérer l'image thumb de la photo suivante d'une photo.
- Photo::getPreviousPhotoId - Photo - Récupérer l'Id de la photo précédente d'une photo.
- Photo::getPreviousPhotoSecret - Photo - Récupérer le code de la photo précédente d'une photo.
- Photo::getPreviousPhotoTitle - Photo - Récupérer le titre de la photo précédente d'une photo.
- Photo::getPreviousPhotoImg - Photo - Récupérer l'image thumb de la photo précédente d'une photo.
- Album::getImgLarge - Photo - Récupérer la photo grand format.
- Album::getImgMedium - Photo - Récupérer la photo format moyen.
- Album::getImgSmall - Photo - Récupérer la photo petit format.
- Album::getImgThumb - Photo - Récupérer la photo format Thumb.
- Album::getImgSquare - Photo - Récupérer la photo format vignette.
- Flickr::Flickr - Flickr - Nouvelle instance.
- Flickr::peoplegetPublicPhotos - Flickr - People : Récupérer les photos publiques d'un user.
- Flickr::peoplegetInfos - Flickr - People : Récupérer les infos d'un user
- Flickr::photosgetRecent - Flickr - Photos : Récupérer les photos récentes.
- Flickr::photosgetInfo - Flickr - Photos : Récupérer les informations générales d'une photo.
- Flickr::photosgetExif - Flickr - Photos : Récupérer les informations Exif d'une photo.
- Flickr::photosetsgetList - Flickr - Photoset : Récupérer les albums d'un user
- Flickr::photosetsgetInfo - Flickr - Photoset : Récupérer les informations d'un album.
- Flickr::photosetsgetPhotos - Flickr - Photoset : Récupérer les photos d'un album.
- Flickr::photosetsgetContext - Flickr - Photoset : Récupérer les photos précédentes et suivantes.
- Flickr:: getAuthUrl - Flickr - Auth : Récupérer une Url d'authentification.
- Flickr:: getFrobToken - Flickr - Auth : Récupérer un code d'authentification.
Exemples
- Exemple : Informations sur un user.
- Exemple : Afficher les photos d'un album.
- Exemple : Afficher une photo.
La Source
/**
* Class User
* pour API Flickr
* PHP 4
*
* @package Flickr 0.2a
* @author Alain NICOLAS (mcAllan)
* @version 0.2a (2005/11)
*
*/
class User {
var $userId;
var $id;
var $nsid;
var $username;
var $realname;
var $location;
var $iconserver;
var $urlAvatar;
var $isadmin;
var $ispro;
var $count;
var $profileurl;
var $photosurl;
var $nbAlb = 0;
var $album = array();
var $inAlbum = false;
var $api;
/**
* Constructeur
*
* @param string $api objet API flickr
* @param string $userId id user
*
*/
function User($api, $userId){
$this->userId = $userId;
$this->api = $api;
$str = $api->peoplegetInfo($this->userId);
$this->parseXml($str);
$str = $api->photosetsgetList($this->userId);
$this->parseXml($str);
$this->inAlbum = false;
$this->setUrlFirstPhoto();
$this->setUrlAvatar();
}
/**
* Retourne l'Id de l'utilisateur
*
* @return string
*/
function getId(){
return $this->id;
}
/**
* Retourne le pseudo de l'utilisateur
*
* @return string
*/
function getName(){
return $this->str2Html($this->username);
}
/**
* Retourne le nom réel de l'utilisateur
*
* @return string
*/
function getRealName(){
return $this->str2Html($this->realname);
}
/**
* Retourne la localisation de l'utilisateur
*
* @return string
*/
function getLocation(){
return $this->str2Html($this->location);
}
/**
* Retourne l'avatar de l'utilisateur
*
* @return string
*/
function getImgBuddyIcon(){
return $this->setImg($this->urlAvatar, $this->username);
}
/**
* Retourne l'url Flickr du profil de l'utilisateur
*
* @return string
*/
function getProfilUrl(){
return $this->profileurl;
}
/**
* Retourne l'url Flickr des photos de l'utilisateur
*
* @return string
*/
function getPhotosUrl(){
return $this->photosurl;
}
/**
* Retourne le nombre de photos de l'utilisateur
*
* @return string
*/
function getPhotosCount(){
return $this->count;
}
/**
* Retourne le nombre d'albums de l'utilisateur
*
* @return string
*/
function getAlbumsCount(){
return $this->nbAlb;
}
/**
* Retourne la liste des albums de l'utilisateur
*
* @return array
*/
function getAlbumsList(){
$out = array();
if($this->nbAlb != 0){
$i = 0;
foreach($this->album as $album){
$out[$i]['id'] = $album->id;
$out[$i]['title'] = $this->str2Html($album->title);
$out[$i]['description'] = $this->str2Html($album->description);
$out[$i]['nbPhotos'] = $album->photos;
$out[$i]['firstPhotoImg'] = $this->setImg($album->urlFirstPhoto, $album->title);
$i ++;
}
}else{
$out[0]['id'] = '';
$out[0]['title'] = '';
$out[0]['description'] = '';
$out[0]['nbPhotos'] =
$out[0]['firstPhotoImg'] = '';
}
return $out;
}
/**
* Url avatar
*
*/
function setUrlAvatar(){
$this->urlAvatar = 'http://photos' . $this->iconserver . '.flickr.com/buddyicons/' . $this->nsid . '.jpg';
}
/**
* Url des firstPhots de chaque album
*
*/
function setUrlFirstPhoto(){
if($this->nbAlb != 0){
foreach($this->album as $key=>$album){
$server = $album->server;
$id = $album->primary;
$secret = $album->secret;
$this->album[$key]->urlFirstPhoto = 'http://static.flickr.com/'.$server.'/'.$id.'_'.$secret.'_t.jpg';
}
}
}
/**
* Construit une image HTML type <img src... />
*
* @param string $url Url de l'image
* @param string $alt Texte alternatif de l'image
*
* @return string chaine convertie
*/
function setImg($url, $alt){
return '<img src="' . $url . '" alt="' . $this->str2Html($alt) . '" />';
}
/**
* Converti une chaine UTF8 en HTML
*
* @param string $str chaine à convertir
*
* @return string chaine convertie
*/
function str2Html($str){
return htmlentities($str,0,'UTF-8');
}
/**
* Parser XML
*
* @param string $str chaine xml à parser
*
*/
function parseXml($str){
$this->resParser = xml_parser_create();
xml_set_object($this->resParser,$this);
xml_set_element_handler($this->resParser, "startElement", "endElement");
xml_set_character_data_handler($this->resParser, "characterData");
$this->strXmlData = xml_parse($this->resParser, $str);
xml_parser_free($this->resParser);
}
function startElement($parser, $name, $attribs) {
$this->vartemp = strtolower($name);
if($name=='PERSON'){
if (sizeof($attribs)) {
foreach($attribs as $key=>$value) {
$key = strtolower($key);
$this->$key = $value;
}
}
}
if($name=='PHOTOSET'){
if (sizeof($attribs)) {
foreach($attribs as $key=>$value) {
$key = strtolower($key);
$this->album[$this->nbAlb]->$key = $value;
}
}
$this->inAlbum = true;
$this->nbAlb++;
}
}
function endElement($parser, $name) {
$this->vartemp = '';
if($name=='PHOTOSET') {
$this->inAlbum = false;
}
}
function characterData($parser, $data) {
$prop = $this->vartemp;
if(!empty($this->vartemp) && ord(trim($data))!=0){
if($this->inAlbum){
$this->album[$this->nbAlb - 1]->$prop .= $data;
}else{
$this->$prop .= $data;
}
}
}
/**
* Fin du Parser XML
**/
}
?>
<?php
/**
* Class Album
* pour API Flickr
* PHP 4
*
* @package Flickr 0.2a
* @author Alain NICOLAS (mcAllan)
* @version 0.2a (2005/11)
*
*/
class Album {
var $albumId;
var $id;
var $primary;
var $title;
var $description;
var $urlFirstPhoto;
var $ownerName;
var $ownerRealName;
var $ownerLocation;
var $ownerUrlAvatar;
var $photoCount = 0;
var $xmlParser;
var $strXmlData;
var $otherAlbums = false;
var $ownerAlbum = array();
var $photos = array();
/**
* Constructeur
*
* @param string $api objet API flickr
* @param string $albumId id de l'album
* @param string $userId id user
*
*/
function Album($api, $albumId, $userId){
$this->albumId = $albumId;
$owner = new User($api, $userId);
$this->ownerName = $owner->username;
$this->ownerRealName = $owner->realname;
$this->ownerLocation = $owner->location;
$this->ownerUrlAvatar = $owner->urlAvatar;
$nb=0;
foreach($owner->album as $album){
if($album->id != $albumId){
$this->otherAlbums = true;
$this->ownerAlbum[$nb]= $album;
$nb++;
}else{
$this->title = $album->title;
$this->description = $album->description;
$this->urlFirstPhoto = $album->urlFirstPhoto;
}
}
if($albumId == 'ALL'){
$str = $api->peoplegetPublicPhotos($userId);
$this->title = '';
$this->description = '';
$this->urlFirstPhoto = '';
}elseif($albumId == 'RECENT'){
$str = $api->photosgetRecent();
$this->title = '';
$this->description = '';
$this->urlFirstPhoto = '';
}else{
$str = $api->photosetsgetPhotos($albumId);
}
$this->parseXml($str);
$this->initPhotoUrls();
}
/**
* Retourne l'Id de l'album
*
* @return string
*/
function getId(){
return $this->str2Html($this->id);
}
/**
* Retourne le titre de l'album
*
* @return string HTML
*/
function getTitle(){
return $this->str2Html($this->title);
}
/**
* Retourne la description de l'album
*
* @return string HTML
*/
function getDescription(){
return $this->str2Html($this->description);
}
/**
* Retourne le nombre de photos dans l'album
*
* @return string HTML
*/
function getPhotoCount(){
return $this->photoCount;
}
/**
* Retourne le nom réel du propriétaire de l'album
*
* @return string HTML
*/
function getOwnerRealName(){
return $this->str2Html($this->ownerRealName);
}
/**
* Retourne le pseudo du propriétaire de l'album
*
* @return string HTML
*/
function getOwnerName(){
return $this->str2Html($this->ownerName);
}
/**
* Retourne la localisation du propriétaire de l'album
*
* @return string HTML
*/
function getOwnerLocation(){
return $this->str2Html($this->ownerLocation);
}
/**
* Retourne l'avatar du propriétaire de l'album
*
* @return string HTML
*/
function getOwnerBuddyIcon(){
return $this->setImg($this->ownerUrlAvatar, $this->ownerName);
}
/**
* Retourne la liste des autres albums du propriétaire de l'album
*
* @return array
*/
function getOwnerOtherAlbums(){
$out = array();
if($this->otherAlbums){
$i = 0;
foreach($this->ownerAlbum as $album){
$out[$i]['id'] = $this->str2Html($album->id);
$out[$i]['title'] = $this->str2Html($album->title);
$out[$i]['description'] = $this->str2Html($album->description);
$out[$i]['nbPhotos'] = $album->photos;
$out[$i]['firstPhotoImg'] = $this->setImg($album->urlFirstPhoto, $album->title);
$i ++;
}
}else{
$out[0]['id'] = '';
$out[0]['title'] = '';
$out[0]['description'] = '';
$out[$i]['nbPhotos'] = 0;
$out[0]['firstPhotoImg'] = '';
}
return $out;
}
/**
* Retourne la liste des photos de l'album
*
* @return array
*/
function getPhotos(){
$out = array();
$i = 0;
foreach($this->photos as $photo){
$out[$i]['id'] = $this->str2Html($photo->id);
$out[$i]['title'] = $this->str2Html($photo->title);
$out[$i]['imgSquare'] = $this->setImg($photo->squareUrl, $photo->title);
$out[$i]['imgThumb'] = $this->setImg($photo->thumbUrl, $photo->title);
$out[$i]['imgSmall'] = $this->setImg($photo->smallUrl, $photo->title);
$out[$i]['imgMedium'] = $this->setImg($photo->mediumUrl, $photo->title);
$out[$i]['imgLarge'] = $this->setImg($photo->largeUrl, $photo->title);
$i ++;
}
return $out;
}
/**
* Construit les urls des différentes grosseurs des photos
*
*
*/
function initPhotoUrls(){
foreach($this->photos as $key=>$photo){
$this->photos[$key]->mediumUrl = 'http://static.flickr.com/'.$photo->server.'/'.$photo->id.'_'.$photo->secret.'.jpg';
$this->photos[$key]->thumbUrl = 'http://static.flickr.com/'.$photo->server.'/'.$photo->id.'_'.$photo->secret.'_t.jpg';
$this->photos[$key]->smallUrl = 'http://static.flickr.com/'.$photo->server.'/'.$photo->id.'_'.$photo->secret.'_m.jpg';
$this->photos[$key]->largeUrl = 'http://static.flickr.com/'.$photo->server.'/'.$photo->id.'_'.$photo->secret.'_b.jpg';
$this->photos[$key]->squareUrl = 'http://static.flickr.com/'.$photo->server.'/'.$photo->id.'_'.$photo->secret.'_s.jpg';
}
}
/**
* Converti une chaine UTF8 en HTML
*
* @param string $str chaine à convertir
*
* @return string chaine convertie
*/
function str2Html($str){
return htmlentities($str,0,'UTF-8');
}
/**
* Construit une image HTML type <img src... />
*
* @param string $url Url de l'image
* @param string $alt Texte alternatif de l'image
*
* @return string chaine convertie
*/
function setImg($url, $alt){
return '<img src="' . $url . '" alt="' . $this->str2Html($alt) . '" />';
}
/**
* Parser XML
*
* @param string $str chaine xml à parser
*
*/
function parseXml($str){
$this->resParser = xml_parser_create();
xml_set_object($this->resParser,$this);
xml_set_element_handler($this->resParser, "startElement", "endElement");
xml_set_character_data_handler($this->resParser, "characterData");
$this->strXmlData = xml_parse($this->resParser, $str);
xml_parser_free($this->resParser);
}
function startElement($parser, $name, $attribs) {
if($name=='PHOTOS'){
if (sizeof($attribs)) {
foreach($attribs as $key=>$value) {
$key = strtolower($key);
$this->$key = $value;
}
}
}
if($name=='PHOTOSET'){
if (sizeof($attribs)) {
foreach($attribs as $key=>$value) {
$key = strtolower($key);
$this->$key = $value;
}
}
}
if($name=='PHOTO'){
if (sizeof($attribs)) {
foreach($attribs as $key=>$value) {
$key = strtolower($key);
$this->photos[$this->photoCount]->$key = $value;
}
}
$this->photoCount++;
}
}
function endElement($parser, $name) {
}
function characterData($parser, $data) {
}
/**
* Fin du Parser XML
**/
}
?>
<?php
/**
* Class Photo
* pour API Flickr
* PHP 4
*
* @package Flickr 0.2a
* @author Alain NICOLAS (mcAllan)
* @version 0.2a (2005/11)
*
*/
class Photo{
var $id;
var $secret;
var $server;
var $title;
var $description;
var $nsid;
var $username;
var $realname;
var $location;
var $taken;
var $ispublic;
var $isfriend;
var $isfamily;
var $largeUrl;
var $mediumUrl;
var $thumbUrl;
var $smallUrl;
var $squareUrl;
var $prevPhoto;
var $nextPhoto;
var $inTag = false;
var $nbTag = 0;
var $tag = array();
var $inExif = false;
var $nbExif = 0;
var $exif = array();
/**
* Constructeur
*
* @param string $api objet API flickr
* @param string $photo id de la photo
*
*/
function Photo($api, $photoId, $setId=''){
$str = $api->photosgetInfo($photoId);
$this->parseXml($str);
$this->initUrls();
$str = $api->photosgetExif($photoId, $this->secret);
$this->parseXml($str);
if(!empty($setId)){
$str = $api->photosetsgetContext($photoId, $setId);
$this->parseXml($str);
}else{
$this->prevPhoto->id = 0;
$this->nextPhoto->id = 0;
}
}
/**
* Retourne l'Id de la photo
*
* @return string
*/
function getId(){
return $this->id;
}
/**
* Retourne le code de la photo
*
* @return string
*/
function getSecret(){
return $this->secret;
}
/**
* Retourne le titre de la photo
*
* @return string HTML
*/
function getTitle(){
return $this->str2Html($this->title);
}
/**
* Retourne la description de la photo
*
* @return string HTML
*/
function getDescription(){
return $this->str2Html($this->description);
}
/**
* Retourne le nom réel du propriétaire de la photo
*
* @return string HTML
*/
function getOwnerRealName(){
return $this->str2Html($this->realname);
}
/**
* Retourne le pseudo du propriétaire de la photo
*
* @return string HTML
*/
function getOwnerName(){
return $this->str2Html($this->username);
}
/**
* Retourne la localisation du propriétaire de la photo
*
* @return string HTML
*/
function getOwnerLocation(){
return $this->str2Html($this->location);
}
/**
* Retourne l'heure de prise de vue de la photo
*
* @return string
*/
function getTakenHour(){
return date('H:i:s', strtotime($this->taken));
}
/**
* Retourne la date de prise de vue de la photo
*
* @return string
*/
function getTakenDate(){
return date('d-M-Y', strtotime($this->taken));
}
/**
* Retourne les infos exif d'une photo si il y en a
*
* @return array
*/
function getExifInfos(){
$out = array();
if($this->nbExif !=0){
$i = 0;
foreach($this->exif as $info){
$out[$i]['label'] = $this->str2Html($info->label);
$out[$i]['raw'] = $this->str2Html($info->raw);
if(isset($info->clean)){
$out[$i]['clean'] = $this->str2Html($info->clean);
}else{
$out[$i]['clean'] = ' ';
}
$i ++;
}
}else{
$out[0]['label'] = 'no exif';
$out[0]['raw'] = '';
$out[0]['clean'] = '';
}
return $out;
}
/**
* Retourne la liste des tags d'une photo si il y en a
*
* @return array
*/
function getTags(){
if($this->nbTag != 0){
$out = array();
foreach($this->tag as $tag){
array_push($out, $this->str2Html($tag->tag));
}
}else{
$out = array('no tag');
}
return $out;
}
/**
* Retourne l'Id de l'image suivante d'une photo si existe
*
* @return string
*/
function getNextPhotoId(){
if($this->nextPhoto->id != 0){
return $this->nextPhoto->id;
}else{
return false;
}
}
/**
* Retourne le secret de l'image suivante d'une photo si existe
*
* @return string
*/
function getNextPhotoSecret(){
if($this->nextPhoto->id != 0){
return $this->nextPhoto->secret;
}else{
return 0;
}
}
/**
* Retourne le titre de l'image suivante d'une photo (Thumb) si existe
*
* @return string HTML
*/
function getNextPhotoTitle(){
if($this->nextPhoto->id != 0){
return $this->str2Html($this->nextPhoto->title);
}else{
return '';
}
}
/**
* Retourne l'image suivante d'une photo (Thumb) si existe
*
* @return string HTML
*/
function getNextPhotoImg(){
if($this->nextPhoto->id != 0){