RSS Feed

実験的に開発した小さなプログラムやWebサービスを公開。また、気になった最新IT技術情報をブログ形式でメモ。

Flickr API

Zend Frameworkに付属するデモ:
FlickrのWebサービスを利用するサンプル

Flickrから写真情報を取得して結果を出力します。

動作サンプル : なし (当サーバの都合により)

ソース: /demos/Zend/WebServices/Flickr/flickr-composite.php

  1. < ?php
  2. /**
  3. * Query Yahoo! Web, Image and News searches
  4. */
  5.  
  6. require_once 'Zend/Service/Flickr.php';
  7.  
  8. if (isset($_POST) && strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
  9.     $keywords = strip_tags($_POST['search_term']);
  10. } else {
  11.     $keywords = '';
  12. }
  13.  
  14. ?>
  15. < !DOCTYPE html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  16. <html>
  17. <head>
  18.     <style type="text/css">
  19.         html, body {
  20.             margin: 0px;
  21.             padding: 0px;
  22.             font-family: Tahoma, Verdana, sans-serif;
  23.             font-size: 10px;
  24.         }
  25.  
  26.         h1 {
  27.             margin-top: 0px;
  28.             background-color: darkblue;
  29.             color: white;
  30.             font-size: 16px;
  31.         }
  32.  
  33.         form {
  34.             text-align: center;
  35.         }
  36.  
  37.         label {
  38.             font-weight: bold;
  39.         }
  40.  
  41.         img {
  42.             border: 0px;
  43.             padding: 5px;
  44.         }
  45.  
  46.     #composite {
  47.             text-align: center;
  48.             padding: 25px;
  49.             background-color: black;
  50.             margin-left: auto;
  51.             margin-right: auto;
  52.     }
  53.  
  54.         h2 {
  55.             font-size: 14px;
  56.             color: white;
  57.             text-align: center;
  58.         }
  59.  
  60.         #poweredby {
  61.             clear: both;
  62.         }
  63.     </style>
  64. </head>
  65. <body>
  66.     <h1>Flickr Compositor</h1>
  67.     <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
  68.         <p>
  69.             <label>Search For: <input type="text" name="search_term" value="<?php echo $keywords; ?/>"></label>
  70.             <input type="submit" value="Search!" onclick='this.value="Please Wait..."'/>
  71.         </p>
  72.     </form>
  73. < ?php
  74. if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
  75.     $flickr = new Zend_Service_Flickr('381e601d332ab5ce9c25939570cb5c4b');
  76.  
  77.     try {
  78.         $results = $flickr->tagSearch($keywords, array('per_page' => 50, 'tag_mode' => 'all'));
  79.  
  80.         if ($results->totalResults() > 0) {
  81.             $images = array();
  82.             foreach ($results as $result) {
  83.                 if (isset($result->Medium)) {
  84.                     $images[] = imagecreatefromjpeg($result->Medium->uri);
  85.                     $heights[] = $result->Medium->height;
  86.                     $widths[] = $result->Medium->width;
  87.                 }
  88.             }
  89.             if (sizeof($images) == 0) {
  90.                 echo '<p style="color: orange; font-weight: bold">No Results Found.</p>';
  91.             } else {
  92.                 sort($heights);
  93.                 sort($widths);
  94.                 $max_height = array_pop($heights);
  95.                 $max_width = array_pop($widths);
  96.                 $output = realpath("./temp/") .DIRECTORY_SEPARATOR.mt_rand(). ".jpg";
  97.                 foreach ($images as $key => $image) {
  98.                     if (!file_exists('./temp')) {
  99.                         mkdir("./temp");
  100.                     }
  101.  
  102.                     $tmp = tempnam(realpath('./temp'), 'zflickr');
  103.                     imagejpeg($image, $tmp);
  104.  
  105.                     chmod($tmp, 0777);
  106.  
  107.                     if (file_exists($output)) {
  108.                         passthru("composite -dissolve 20 $tmp $output $output");
  109.                         chmod($output, 0777);
  110.                     } elseif (!isset($previous_image)) {
  111.                         $previous_image = "$tmp";
  112.                     } else {
  113.                         passthru("composite -dissolve 20 $tmp $previous_image $output");
  114.                         chmod($output, 0777);
  115.                     }
  116.                     $image_files[] = $tmp;
  117.                 }
  118.                 foreach ($image_files as $filename) {
  119.                     unlink($filename);
  120.                 }
  121.                 //copy($output, basename($output));
  122.                 //unlink($output);
  123.                 $size = getimagesize($output);
  124.                 $size[0] += 25;
  125.                 $size[1] += 25;
  126.                 echo "<div id='composite' style='width: {$size[0]}px; height: {$size[1]}px;'><img src='temp/" .basename($output). "' alt='" .htmlspecialchars($keywords). "'/><h2>" .ucwords(htmlspecialchars($keywords)). "</h2></div>";
  127.             }
  128.         } else {
  129.             echo '<p style="color: orange; font-weight: bold">No Results Found</p>';
  130.         }
  131.     }
  132.     catch (Zend_Service_Exception $e) {
  133.         echo '<p style="color: red; font-weight: bold">An error occured, please try again later. (' .$e->getMessage(). ')</p>';
  134.     }
  135. }
  136. ?>
  137. <p id="poweredby" style="text-align: center; font-size: 9px;">Powered by the <a href="http://framework.zend.com">Zend Framework</a></p>
  138. </body>
  139. </html>

ソース: /demos/Zend/WebServices/Flickr/flickr-search.php

  1. < ?php
  2. /**
  3. * Query Flickr for a tag and display all of the photos for
  4. * that tag.
  5. */
  6.  
  7. error_reporting(E_ALL);
  8.  
  9. require_once 'Zend/Service/Flickr.php';
  10.  
  11. $flickr = new Zend_Service_Flickr('your api key here');
  12.  
  13. $photos = $flickr->tagSearch('php');
  14.  
  15.  
  16. foreach ($photos as $photo) {
  17.     echo '<img src="' . $photo-/>Thumbnail->uri . '" /> <br />';
  18.     echo $photo->title . "<br />
  19. ";
  20. }
関連記事