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.

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