PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/core/admin/form-field-types/process/_photo-process.php

https://github.com/philp/BigTree-CMS
PHP | 151 lines | 113 code | 21 blank | 17 comment | 49 complexity | 9ea31de317c4ab6296835ca9a0037065 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?
  2. $failed = false;
  3. // Let's check the minimum requirements for the image first before we store it anywhere.
  4. $image_info = getimagesize($temp_name);
  5. $iwidth = $image_info[0];
  6. $iheight = $image_info[1];
  7. $itype = $image_info[2];
  8. $channels = $image_info["channels"];
  9. // If the minimum height or width is not meant, do NOT let the image through. Erase the change or update from the database.
  10. if ((isset($options["min_height"]) && $iheight < $options["min_height"]) || (isset($options["min_width"]) && $iwidth < $options["min_width"])) {
  11. $fails[] = array("field" => $options["title"], "error" => "Image uploaded did not meet the minimum size of ".$options["min_width"]."x".$options["min_height"]);
  12. $failed = true;
  13. }
  14. // If it's not a valid image, throw it out!
  15. if ($itype != IMAGETYPE_GIF && $itype != IMAGETYPE_JPEG && $itype != IMAGETYPE_PNG) {
  16. $fails[] = array("field" => $options["title"], "error" => "An invalid file was uploaded. Valid file types: JPG, GIF, PNG.");
  17. $failed = true;
  18. }
  19. // See if it's CMYK
  20. if ($channels == 4) {
  21. $fails[] = array("field" => $options["title"], "error" => "A CMYK encoded file was uploaded. Please upload an RBG image.");
  22. $failed = true;
  23. }
  24. // Do EXIF Image Rotation
  25. $already_created_first_copy = false;
  26. if ($itype == IMAGETYPE_JPEG && function_exists("exif_read_data")) {
  27. $exif = exif_read_data($temp_name);
  28. $o = $exif['Orientation'];
  29. if ($o == 3 || $o == 6 || $o == 8) {
  30. $first_copy = $site_root."files/".uniqid("temp-").".jpg";
  31. $source = imagecreatefromjpeg($temp_name);
  32. if ($o == 3) {
  33. $source = imagerotate($source,180,0);
  34. } elseif ($o == 6) {
  35. $source = imagerotate($source,270,0);
  36. } else {
  37. $source = imagerotate($source,90,0);
  38. }
  39. imagejpeg($source,$first_copy);
  40. imagedestroy($source);
  41. $already_created_first_copy = true;
  42. }
  43. }
  44. $value = "";
  45. if (!$failed) {
  46. // Make a temporary copy to be used for thumbnails and crops.
  47. $itype_exts = array(IMAGETYPE_PNG => ".png", IMAGETYPE_JPEG => ".jpg", IMAGETYPE_GIF => ".gif");
  48. if (!$already_created_first_copy) {
  49. $first_copy = $temp_name;
  50. }
  51. // Let's crush this png.
  52. if ($itype == IMAGETYPE_PNG && $upload_service->optipng) {
  53. $first_copy = $site_root."files/".uniqid("temp-").".png";
  54. move_uploaded_file($temp_name,$first_copy);
  55. exec($upload_service->optipng." ".$first_copy);
  56. }
  57. // Let's crush the gif and see if we can make it a PNG.
  58. if ($itype == IMAGETYPE_GIF && $upload_service->optipng) {
  59. $first_copy = $site_root."files/".uniqid("temp-").".gif";
  60. move_uploaded_file($temp_name,$first_copy);
  61. exec($upload_service->optipng." ".$first_copy);
  62. if (file_exists(substr($first_copy,0,-3)."png")) {
  63. unlink($first_copy);
  64. $first_copy = substr($first_copy,0,-3)."png";
  65. $name_parts = BigTree::pathInfo($name);
  66. $name = $name_parts["filename"].".png";
  67. }
  68. }
  69. // Let's trim the jpg.
  70. if (!$already_created_first_copy && $itype == IMAGETYPE_JPEG && $upload_service->jpegtran) {
  71. $first_copy = $site_root."files/".uniqid("temp-").".jpg";
  72. move_uploaded_file($temp_name,$first_copy);
  73. exec($upload_service->jpegtran." -copy none -optimize -progressive $first_copy > $first_copy-trimmed");
  74. unlink($first_copy);
  75. $first_copy = $first_copy."-trimmed";
  76. }
  77. list($iwidth,$iheight,$itype,$iattr) = getimagesize($first_copy);
  78. $temp_copy = $site_root."files/".uniqid("temp-").$itype_exts[$itype];
  79. BigTree::copyFile($first_copy,$temp_copy);
  80. // Upload the original to the proper place.
  81. $value = $upload_service->upload($first_copy,$name,$options["directory"]);
  82. if (!$value) {
  83. $fails[] = array("field" => $options["title"], "error" => "Could not upload file. The destination is not writable.");
  84. unlink($temp_copy);
  85. unlink($first_copy);
  86. $failed = true;
  87. }
  88. }
  89. // If we didn't fail, let's check on crops and thumbnails.
  90. if (!$failed) {
  91. $pinfo = BigTree::pathInfo($value);
  92. // Handle Crops
  93. foreach ($options["crops"] as $crop) {
  94. $cwidth = $crop["width"];
  95. $cheight = $crop["height"];
  96. // Check to make sure each dimension is greater then or equal to, but not both equal to the crop.
  97. if (($iheight >= $cheight && $iwidth > $cwidth) || ($iwidth >= $cwidth && $iheight > $cheight)) {
  98. $crops[] = array(
  99. "image" => $temp_copy,
  100. "directory" => $options["directory"],
  101. "name" => $pinfo["basename"],
  102. "width" => $cwidth,
  103. "height" => $cheight,
  104. "prefix" => $crop["prefix"],
  105. "thumbs" => $crop["thumbs"]
  106. );
  107. // If it's the same dimensions, let's see if they're looking for a prefix for whatever reason...
  108. } elseif ($iheight == $cheight && $iwidth == $cwidth) {
  109. if (is_array($crop["thumbs"])) {
  110. foreach ($crop["thumbs"] as $thumb) {
  111. $temp_thumb = $site_root."files/".uniqid("temp-").$itype_exts[$itype];
  112. BigTree::createThumbnail($temp_copy,$temp_thumb,$thumb["width"],$thumb["height"]);
  113. // We use replace here instead of upload because we want to be 100% sure that this file name doesn't change.
  114. $upload_service->replace($temp_thumb,$thumb["prefix"].$pinfo["basename"],$options["directory"]);
  115. }
  116. }
  117. $upload_service->upload($temp_copy,$crop["prefix"].$pinfo["basename"],$options["directory"],false);
  118. }
  119. }
  120. // Handle thumbnailing
  121. if (is_array($options["thumbs"])) {
  122. foreach ($options["thumbs"] as $thumb) {
  123. $temp_thumb = $site_root."files/".uniqid("temp-").$itype_exts[$itype];
  124. BigTree::createThumbnail($temp_copy,$temp_thumb,$thumb["width"],$thumb["height"]);
  125. // We use replace here instead of upload because we want to be 100% sure that this file name doesn't change.
  126. $upload_service->replace($temp_thumb,$thumb["prefix"].$pinfo["basename"],$options["directory"]);
  127. }
  128. }
  129. }
  130. ?>