/resources/scripts/php/image.php

https://github.com/grickit/kredimage · PHP · 149 lines · 129 code · 19 blank · 1 comment · 27 complexity · 83cda2680c8cea2c116abe2200c97fed MD5 · raw file

  1. <?php
  2. include("secrets.php");
  3. function loadImage($location) { // Load image from file
  4. $contents = file_get_contents($location);
  5. return imagecreatefromstring($contents);
  6. }
  7. function getImageInfo($location) { return getimagesize($location); } // Get metadata about the image
  8. function getImageType($location) { // Extract the filetype from the metadata
  9. $image_info = getImageInfo($location);
  10. return $image_info[2];
  11. }
  12. function getImageMime($location) { // Convert filetype to mimetype
  13. $image_info = getImageInfo($location);
  14. return image_type_to_mime_type($image_info[2]);
  15. }
  16. function getImageWidth($image) { return imagesx($image); } // Get the width of the image
  17. function getImageHeight($image) { return imagesy($image); } // Get the height of the image
  18. function resizeImage($image,$image_type,$width,$height) { // Resize the image
  19. $resized = imagecreatetruecolor($width,$height); # Create a blank canvas
  20. $transparency = imagecolortransparent($image);
  21. if($transparency >= 0) { # If the image already has a transparent color, inject it into the canvas
  22. $transparent_color = imagecolorsforindex($image,$transparency);
  23. $transparency = imagecolorallocate($resized,$transparent_color['red'],$transparent_color['green'],$transparent_color['blue']);
  24. imagefill($resized,0,0,$transparency);
  25. imagecolortransparent($resized,$transparency);
  26. }
  27. elseif($image_type == IMAGETYPE_PNG) { # All pngs need a base transparency
  28. $transparency_color = imagecolorallocatealpha($resized,0,0,0,127);
  29. imagefill($resized,0,0,$transparency_color);
  30. }
  31. imagecopyresampled($resized,$image,0,0,0,0,$width,$height,getImageWidth($image),getImageHeight($image)); # Copy the image onto the canvas
  32. return $resized;
  33. }
  34. function resizeImageToWidth($image,$image_type,$width) { // Resize the image proportionally to a specific width
  35. $percent = $width / getImageWidth($image);
  36. $height = getImageHeight($image) * $percent;
  37. return resizeImage($image,$image_type,$width,$height);
  38. }
  39. function resizeImageToHeight($image,$image_type,$height) { // Resize the image proportionally to a specific height
  40. $percent = $height / getImageHeight($image);
  41. $width = getImageWidth($image) * $percent;
  42. return resizeImage($image,$image_type,$width,$height);
  43. }
  44. function resizeImageToNumber($image,$image_type,$number) { // Resize the image proportionally so that the largest dimension equals a given number
  45. if ((getImageWidth($image) > $number) || (getImageHeight($image) > $number)) { # This function is used blindly, and may not be needed
  46. if (getImageWidth($image) >= getImageHeight($image)) { # Do we use width or height?
  47. return resizeImageToWidth($image,$image_type,$number);
  48. }
  49. else {
  50. return resizeImageToHeight($image,$image_type,$number);
  51. }
  52. }
  53. else {
  54. return $image;
  55. }
  56. }
  57. function outputImage($image,$image_type) { // Put the image to the browser
  58. if($image_type == IMAGETYPE_JPEG) { imagejpeg($image); }
  59. elseif($image_type == IMAGETYPE_GIF) { imagegif($image); }
  60. elseif($image_type == IMAGETYPE_PNG) { imagepng($image); }
  61. }
  62. function saveImage($image,$image_type,$location) { // Save the image to a file
  63. if($image_type == IMAGETYPE_JPEG) { imagejpeg($image,$location); }
  64. elseif($image_type == IMAGETYPE_GIF) { imagegif($image,$location); }
  65. elseif($image_type == IMAGETYPE_PNG) { imagepng($image,$location); }
  66. }
  67. function imageHeaders($location,$etag) {
  68. if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($location))) {
  69. header('HTTP/1.1 304 Not Modified');
  70. }
  71. header("Cache-Control: private");
  72. header("Pragma: ");
  73. header("Expires: ");
  74. header("Content-Type: ");
  75. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($location)) . ' GMT');
  76. header("Etag: ".$etag);
  77. }
  78. function loadAndOutputImage($location,$etag) { // Load an image and output it
  79. imageHeaders($location,$etag);
  80. echo file_get_contents($location);
  81. exit();
  82. }
  83. function loadResizeOutputAndSaveImage($location,$number,$location2,$etag) { // Load an image, resize it, output it, and save it
  84. imageHeaders($location,$etag);
  85. $image = loadImage($location);
  86. $image_type = getImageType($location);
  87. $image = resizeImageToNumber($image,$image_type,$number);
  88. imagealphablending($image,false);
  89. imagesavealpha($image,true);
  90. outputImage($image,$image_type);
  91. saveImage($image,$image_type,$location2);
  92. exit();
  93. }
  94. if (isset($_GET['id'])) {
  95. $id = $_GET['id'];
  96. if (isset($_GET['json'])) { # Want json?
  97. // TODO: Echo information about the image in JSON format
  98. $image = loadImage($full_directory.$id);
  99. header('Content-type: application/json');
  100. echo "{\n";
  101. echo " \"image ".$id."\": {\n";
  102. echo " \"width\": \"".getImageWidth($image)."\"\n";
  103. echo " \"height\": \"".getImageheight($image)."\"\n";
  104. echo " }\n";
  105. echo "}\n";
  106. exit();
  107. }
  108. else {
  109. if (isset($_GET['small'])) { # Want small?
  110. if (file_exists($small_directory.$id)) { # Already have small?
  111. loadAndOutputImage($small_directory.$id,"small".$id);
  112. }
  113. else {
  114. loadResizeOutputAndSaveImage($full_directory.$id,1000,$small_directory.$id,"small".$id); # Make small
  115. }
  116. }
  117. elseif (isset($_GET['thumb'])) { # Want thumbnail?
  118. if (file_exists($thumb_directory.$id)) { # Already have thumbnail?
  119. loadAndOutputImage($thumb_directory.$id,"thumb".$id);
  120. }
  121. else {
  122. loadResizeOutputAndSaveImage($full_directory.$id,120,$thumb_directory.$id,"thumb".$id); # Make thumbnail
  123. }
  124. }
  125. else {
  126. loadAndOutputImage($full_directory.$id,"full".$id);
  127. }
  128. }
  129. }
  130. ?>