Flicker Photo Library With AngularJS

Introduction

Flicker provides collections of photographs. It also provides API. We can integrate this API to our website and display different photographs.

Below is the Flicker Image Search Endpoint.

  1. var endPoint = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=4ef2fe2affcdd6e13218f5ddd0e2500d&tags=" + searchField + "%2C+&per_page=" + perPage + "&page=" + currentPage + "&format=json&nojsoncallback=1";   

Data Service to Call Flicker Photo Search API -

  1. var app = angular.module("flickerApp");  
  2.   
  3. app.factory('flickerDataService', ['$http''$q'function ($http, $q) {  
  4.   
  5.     function getSearchData(searchField, perPage, currentPage) {  
  6.         var endPoint = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=4ef2fe2affcdd6e13218f5ddd0e2500d&tags=" + searchField + "%2C+&per_page=" + perPage + "&page=" + currentPage + "&format=json&nojsoncallback=1";  
  7.         var req = {  
  8.             method: 'GET',  
  9.             url: endPoint  
  10.         }  
  11.   
  12.         var deferred = $q.defer();  
  13.         $http(req).success(function (data) {  
  14.             deferred.resolve(data);  
  15.         })  
  16.           .error(function (err) {  
  17.               console.log('Error retrieving data');  
  18.               deferred.reject(err);  
  19.           });  
  20.         return deferred.promise;  
  21.     }  
  22.   
  23.     return {  
  24.         getSearchData: getSearchData  
  25.     };  
  26. }]);  
Then, you will get Flicker API JSON response, as shown below.
  1. "photos": { "page": 1, "pages""34477""perpage": 15, "total""517154",   
  2.     "photo": [  
  3.       { "id""29391554603""owner""11741544@N05""secret""548817b7f5""server""5161""farm": 6, "title""1933 Rolls Royce""ispublic": 1, "isfriend": 0, "isfamily": 0 },  
  4.       { "id""29391552033""owner""11741544@N05""secret""4381911dea""server""5643""farm": 6, "title""1933 Rolls Royce""ispublic": 1, "isfriend": 0, "isfamily": 0 }  
  5.     ] }, "stat""ok" }  
Now, create URL of each image by using above JSON properties.
  1. controller.getFlickerData = function () {  
  2.         controller.isLoading = true;  
  3.         flickerDataService.getSearchData(controller.searchValue, controller.perPage, controller.currentIndex).then(function (data) {  
  4.             controller.flickerData = data.photos.photo;  
  5.             controller.createImageUrl(controller.flickerData);  
  6.             controller.isLoading = false;  
  7.         }).catch(function (data) {  
  8.             controller.errorMessage = "Oops! Something went wrong.";  
  9.             controller.isLoading = false;  
  10.         });  
  11.     }  
  1. controller.createImageUrl = function (flickerData) {  
  2.       controller.urlData = [];  
  3.       var url = "";  
  4.       if (!angular.isUndefined(flickerData) && flickerData.length > 0) {  
  5.           controller.mainImageUrl = "https://farm" + flickerData[0].farm + ".staticflickr.com/" + flickerData[0].server + "/" + flickerData[0].id + "_" + flickerData[0].secret + ".jpg";  
  6.   
  7.           for (var i = 0; i < flickerData.length; i++) {  
  8.               url = "https://farm" + flickerData[i].farm + ".staticflickr.com/" + flickerData[i].server + "/" + flickerData[i].id + "_" + flickerData[i].secret + ".jpg";  
  9.               controller.urlData.push({ id: i, url: url });  
  10.           }  
  11.       }  
  12.   }  
In the above code, this line will create image URL -
  1. controller.mainImageUrl = "https://farm" + flickerData[0].farm + ".staticflickr.com/" + flickerData[0].server + "/" + flickerData[0].id + "_" + flickerData[0].secret + ".jpg";  
Now, bind the image URL, like below, to display on HTML page.
  1. <ul>  
  2.        <li ng-repeat="item in homeController.urlData" ng-click="homeController.GetImage(item.id)" style="cursor: pointer;">  
  3.            <img src="{{item.url}}" class="js-gallery__image" />  
  4.        </li>  
  5.    </ul>  
For more details, I have attached zipped project. Download it and follow the below steps for the execution of project.
  1. Node.js should be installed and integrated into command prompt. Downloading source.
  2. Navigate to folder "FlickerApp".
  3. Here, you will find startServer.bat file.
  4. Click on this file. It will execute the Server.
  5. Go to browser and type "localhost:8080".
  6. The web app will open.

Conclusion - By using flicker search API, you can get images from different locations and create image URL to show.

Ebook Download
View all
Learn
View all