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.

179 lines
6.8 KiB

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