flickr-composite.php 5.1 KB

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