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.

183 lines
7.1 KiB

  1. <?php
  2. namespace dimti\Image;
  3. /**
  4. * Class Upload
  5. * @version 0.2
  6. */
  7. abstract class Upload
  8. {
  9. public static $dir_image_cache = 'media/cache/images';
  10. public static function &getGreagwarImage($file_path)
  11. {
  12. $greagwar_image = new \GreagwarImage($file_path);
  13. $config_upload = \Majestic\Config::get('Upload');
  14. $dir_image_cache = $config_upload ? $config_upload->dir_image_cache : self::$dir_image_cache;
  15. $greagwar_image->setCacheDir(\Majestic\Config::get('PATH_WEB_ROOT') . DIRECTORY_SEPARATOR . $dir_image_cache);
  16. return $greagwar_image;
  17. }
  18. public static function getFilePath($image)
  19. {
  20. return \Majestic\Config::get('PATH_WEB_ROOT') . DIRECTORY_SEPARATOR . $image->path . DIRECTORY_SEPARATOR . $image->filename;
  21. }
  22. /**
  23. * @param $class string|Image
  24. * @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'error' => '...'])
  25. * @param $force_create_variants bool
  26. * @return Image
  27. * @throws ErrorException
  28. * TODO: Рефакторинг: убрать параметр $force_create_variants?
  29. */
  30. public static function image($class, $file, $force_create_variants = true)
  31. {
  32. /**
  33. * @var $image Image
  34. */
  35. if (is_null($class) || $class == '') {
  36. throw new \ErrorException('Class not defined.');
  37. }
  38. if (!is_object($class) && !class_exists($class)) {
  39. throw new \ErrorException('Class "' . $class . '" not exists.');
  40. }
  41. Image::checkSubClass($class);
  42. if (isset($file['error']) && $file['error'] != 0) {
  43. return false;
  44. }
  45. $image = is_object($class) ? $class : new $class;
  46. $image->original_filename = $file['name'];
  47. self::saveImage($image, $file['tmp_name'], $class::getMaxWidth(), $class::getMaxHeight());
  48. self::defineSizeWidthAndHeight($image);
  49. if ($force_create_variants) {
  50. $sizes = $image->getSizes();
  51. foreach ($sizes as $size) {
  52. self::imageVariant($image, $size);
  53. }
  54. }
  55. return $image;
  56. }
  57. /**
  58. * @param $image Image
  59. * @param $size string @ex '1200x960'
  60. * @param $greagwar_image \GreagwarImage @deprecated
  61. */
  62. public static function imageVariant($image, $size, $greagwar_image = null)
  63. {
  64. unset($greagwar_image);
  65. $image_variant = new ImageVariant();
  66. $size_parts = explode('x', $size);
  67. self::saveImage($image_variant, self::getFilePath($image), $size_parts[0] ? : null, $size_parts[1] ? : null);
  68. if (\Majestic\Config::get('PYTHON_PIL_PASTE') &&
  69. method_exists($image, 'getWatermark') &&
  70. $image->getWatermark($size)
  71. ) {
  72. $script_file_path = \Majestic\Config::get('PYTHON_PIL_PASTE')->script_file_path;
  73. $pil_options = (isset(\Majestic\Config::get('PYTHON_PIL_PASTE')->pil_options) && \Majestic\Config::get('PYTHON_PIL_PASTE')->pil_options) ? ' ' . implode(' ', \Majestic\Config::get('PYTHON_PIL_PASTE')->pil_options) : '';
  74. ob_start();
  75. $code = null;
  76. $command = $script_file_path
  77. . $pil_options
  78. . ' ' . self::getFilePath($image_variant)
  79. . ' ' . \Majestic\Config::get('PATH_WEB_ROOT') . '/' . $image->getWatermark($size);
  80. passthru('exec 2>&1; ' . $command, $code);
  81. if ($code !== 0) {
  82. throw new \ErrorException('Command PYTHON_PIL_PASTE exit with code "' . $code . '": ' . $command . PHP_EOL . 'Std out: ' . ob_get_clean() );
  83. }
  84. ob_end_clean();
  85. }
  86. self::defineSizeWidthAndHeight($image_variant);
  87. $image->variants[$size] = $image_variant;
  88. }
  89. /**
  90. * @param $image ImageVariant|Image
  91. * @param $tmp_file_path string|\GreagwarImage
  92. * @param null $width
  93. * @param null $height
  94. * TODO: Возможно, стоит искоренить GreagwarImage
  95. */
  96. public static function saveImage($image, $tmp_file_path , $width = null, $height = null)
  97. {
  98. if (is_object($tmp_file_path)) {
  99. /**
  100. * @deprecated
  101. */
  102. $greagwar_image = $tmp_file_path;
  103. } else {
  104. $greagwar_image = self::getGreagwarImage($tmp_file_path);
  105. }
  106. $image->type = $greagwar_image->guessType();
  107. if (!Image::getIsSubClass($image)) {
  108. if ( !( ImageVariant::getIsClass( $image ) ) && Image::getIsSubClass( $image ) ) {
  109. $greagwar_image->resize( $width, $height, 0xffffff, $force = false, $rescale = false, $crop = true );
  110. } else {
  111. $greagwar_image->resize( $width, $height, 0xffffff, $force = false, $rescale = false, $crop = false );
  112. }
  113. }
  114. $hash = $greagwar_image->getHash($image->type, $quality = 100);
  115. $file_path = $greagwar_image->generateFileFromhash($hash) . '.' . $image->type;
  116. if (Image::getIsSubClass($image)) {
  117. if (is_uploaded_file($tmp_file_path)) {
  118. move_uploaded_file($tmp_file_path, $file_path);
  119. } else {
  120. copy($tmp_file_path, $file_path);
  121. }
  122. } else {
  123. if (\Majestic\Config::get('PYTHON_PIL_RESIZE')) {
  124. $script_file_path = \Majestic\Config::get( 'PYTHON_PIL_RESIZE' )->script_file_path;
  125. $pil_options = isset( \Majestic\Config::get( 'PYTHON_PIL_RESIZE' )->pil_options ) ? ' ' . implode( ' ', \Majestic\Config::get( 'PYTHON_PIL_RESIZE' )->pil_options ) : '';
  126. // ob_start();
  127. $code = null;
  128. $command = $script_file_path
  129. . ( ( $width ) ? ' --width=' . $width : '' )
  130. . ( ( $height ) ? ' --height=' . $height : '' )
  131. . $pil_options
  132. . ' ' . $tmp_file_path
  133. . ' ' . $file_path;
  134. ob_start();
  135. passthru( 'exec 2>&1; ' . $command, $code );
  136. if ($code !== 0) {
  137. throw new \ErrorException('Command PYTHON_PIL_RESIZE exit with code "' . $code . '": ' . $command . PHP_EOL . 'Std out: ' . ob_get_clean() );
  138. }
  139. ob_end_clean();
  140. } else {
  141. if ($width || $height) {
  142. $file_path = $greagwar_image->cacheFile($image->type, $quality = 100, true);
  143. }
  144. }
  145. }
  146. $path_parts = pathinfo($file_path);
  147. $image->path = preg_replace('#^' . \Majestic\Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  148. $image->filename = $path_parts['basename'];
  149. self::defineSizeWidthAndHeight($image);
  150. unset($greagwar_image);
  151. }
  152. private static function defineSizeWidthAndHeight($image)
  153. {
  154. $file_path = self::getFilePath($image);
  155. $imagesize = getimagesize($file_path);
  156. $image->width = $imagesize[0];
  157. $image->height = $imagesize[1];
  158. $image->size = filesize($file_path);
  159. }
  160. /**
  161. * @param $image
  162. * @param string $value
  163. * @deprecated
  164. */
  165. public static function brightnessContrast($image, $value = '0x7')
  166. {
  167. $file_path = self::getFilePath($image);
  168. $output = shell_exec("convert {$file_path} -brightness-contrast {$value} {$file_path} 2>&1");
  169. if (isset($output) && $output) {
  170. if (class_exists('ErrorMessage')) {
  171. \ErrorMessage::log($output);
  172. }
  173. }
  174. }
  175. }