A simple php website which displays quotes on pictures of pandas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.8 KiB

  1. <?php
  2. function wordWrapAnnotation(&$image, &$draw, $text, $maxWidth)
  3. {
  4. $words = explode(" ", $text);
  5. $lines = array();
  6. $i = 0;
  7. $lineHeight = 0;
  8. while($i < count($words) )
  9. {
  10. $currentLine = $words[$i];
  11. if($i+1 >= count($words))
  12. {
  13. $lines[] = $currentLine;
  14. break;
  15. }
  16. //Check to see if we can add another word to this line
  17. $metrics = $image->queryFontMetrics($draw, $currentLine . ' '
  18. . $words[$i+1]);
  19. while($metrics['textWidth'] <= $maxWidth)
  20. {
  21. //If so, do it and keep doing it!
  22. $currentLine .= ' ' . $words[++$i];
  23. if($i+1 >= count($words))
  24. break;
  25. $metrics = $image->queryFontMetrics($draw, $currentLine . ' '
  26. . $words[$i+1]);
  27. }
  28. //We can't add the next word to this line, so loop to the next line
  29. $lines[] = $currentLine;
  30. $i++;
  31. //Finally, update line height
  32. if($metrics['textHeight'] > $lineHeight)
  33. $lineHeight = $metrics['textHeight'];
  34. }
  35. return array($lines, $lineHeight);
  36. }
  37. function displayImage($quote, $person, $imageLoc)
  38. {
  39. $draw = new ImagickDraw();
  40. $image = new Imagick();
  41. $image->readImage($imageLoc);
  42. /* Green text */
  43. $draw->setFillColor("rgb(0,255,0)");
  44. /* Font properties */
  45. $draw->setFont('Bookman-DemiItalic');
  46. $fontsize = 0.05 * $image->getimagewidth();
  47. $draw->setFontSize( "$fontsize" );
  48. $xpos = $image->getimagewidth()/4;
  49. $ypos = $image->getimageheight()/4;
  50. $msg = '"' . $quote . '"' . " - $person";
  51. list($lines, $lineHeight) = wordWrapAnnotation($image,
  52. $draw, $msg, $image->getimagewidth() /2);
  53. for($i = 0; $i < count($lines); $i++)
  54. $image->annotateImage($draw, $xpos, $ypos + $i*$lineHeight,
  55. 0, $lines[$i]);
  56. /* Give image a format */
  57. $image->setImageFormat('png');
  58. echo '<img src="data:image/jpg;base64,'.base64_encode(
  59. $image->getImageBlob()).'" alt="" width="100%"/>';
  60. }
  61. function printHalf($query)
  62. {
  63. $r = mysqli_query ($dbc, $query);
  64. while($row = mysqli_fetch_array($r))
  65. {
  66. $q2 = "select name from people where person_id='" .
  67. $row['person_id'] . "' limit 1";
  68. $r2 = mysqli_query($dbc, $q2);
  69. while($row2 = mysqli_fetch_array($r2))
  70. {
  71. $images = glob('../img/*');
  72. displayImage($row['quote'], $row2['name'],
  73. $images[rand(0, count($images) - 1)]);
  74. }
  75. }
  76. echo '</div>';
  77. }
  78. echo '<div class="w3-row-padding w3-center w3-margin-top">';
  79. echo '<div class="w3-half">';
  80. echo '<h1 class="w3-text-teal w3-center">Random Quote</h1>';
  81. $q ="select * from quote where visibility=true order by rand() limit 1";
  82. $r = mysqli_query ($dbc, $q);
  83. while($row = mysqli_fetch_array($r))
  84. {
  85. $q2 = "select name from people where person_id='" . $row['person_id']
  86. . "' limit 1";
  87. $r2 = mysqli_query($dbc, $q2);
  88. while($row2 = mysqli_fetch_array($r2))
  89. {
  90. $images = glob('../img/*');
  91. displayImage($row['quote'], $row2['name'],
  92. $images[rand(0, count($images) - 1)]);
  93. }
  94. }
  95. echo '</div>';
  96. echo '<div class="w3-half">';
  97. echo '<h1 class="w3-text-teal w3-center">Most Recent</h1>';
  98. $q ="select * from quote where visibility=true order by creation_date
  99. desc limit 1";
  100. $r = mysqli_query ($dbc, $q);
  101. while($row = mysqli_fetch_array($r))
  102. {
  103. $q2 = "select name from people where person_id='" . $row['person_id']
  104. . "' limit 1";
  105. $r2 = mysqli_query($dbc, $q2);
  106. while($row2 = mysqli_fetch_array($r2))
  107. {
  108. $images = glob('../img/*');
  109. displayImage($row['quote'], $row2['name'],
  110. $images[rand(0, count($images) - 1)]);
  111. }
  112. }
  113. echo '</div>';
  114. echo '</div>';
  115. echo '<div class="w3-row-padding w3-center w3-margin-top">';
  116. echo '<div class="w3-half row-center">';
  117. if(isset($_POST['panda']))
  118. {
  119. displayImage($_POST['panda_quote'], $_POST['panda_name'],
  120. $images[rand(0, count($images) - 1)]);
  121. }
  122. echo '<h1 class="w3-text-teal w3-center">Create a panda quote!</h1>';
  123. echo '<form action="index.php" method ="post" class="w3-container w3-card-4">
  124. <div class="w3-group">
  125. <input class="w3-input" type="text" name="panda_quote" required>
  126. <label class="w3-label w3-validate">Quote</label>
  127. </div>
  128. <div class="w3-group">
  129. <input class="w3-input" type="text" name="panda_name" required>
  130. <label class="w3-label w3-validate">Person\'s Name</label>
  131. </div>
  132. <p><input type="submit" name="Submit" value="Create Panda Quote"
  133. class="w3-padding-16 w3-hover-dark-grey w3-btn-block w3-center-align" /></p>
  134. <input type="hidden" name="panda" value="TRUE" />
  135. </form>';
  136. echo '</div>';
  137. echo '<div class="w3-half w3-container">';
  138. //profile
  139. include('user/profile.php');
  140. echo '</div>';
  141. echo '</div>';
  142. ?>