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.

174 lines
6.6 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. function_exists(array($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. passthru($script_file_path
  75. . $pil_options
  76. . ' ' . self::getFilePath($image_variant)
  77. . ' ' . Config::get('PATH_WEB_ROOT') . '/' . $image->getWatermark($size));
  78. // ob_clean();
  79. }
  80. self::defineSizeWidthAndHeight($image_variant);
  81. $image->variants[$size] = $image_variant;
  82. }
  83. /**
  84. * @param $image ImageVariant|Image
  85. * @param $tmp_file_path string|GreagwarImage
  86. * @param null $width
  87. * @param null $height
  88. * TODO: Возможно, стоит искоренить GreagwarImage
  89. */
  90. public static function saveImage($image, $tmp_file_path , $width = null, $height = null)
  91. {
  92. if (is_object($tmp_file_path)) {
  93. /**
  94. * @deprecated
  95. */
  96. $greagwar_image = $tmp_file_path;
  97. } else {
  98. $greagwar_image = self::getGreagwarImage($tmp_file_path);
  99. }
  100. $image->type = $greagwar_image->guessType();
  101. $hash = $greagwar_image->getHash($image->type, $quality = 100);
  102. $file_path = $greagwar_image->generateFileFromhash($hash) . '.' . $image->type;
  103. if (Image::getIsSubClass($image)) {
  104. if (is_uploaded_file($tmp_file_path)) {
  105. move_uploaded_file($tmp_file_path, $file_path);
  106. } else {
  107. copy($tmp_file_path, $file_path);
  108. }
  109. } else {
  110. if (Config::get('PYTHON_PIL_RESIZE')) {
  111. $script_file_path = Config::get( 'PYTHON_PIL_RESIZE' )->script_file_path;
  112. $pil_options = isset( Config::get( 'PYTHON_PIL_RESIZE' )->pil_options ) ? ' ' . implode( ' ', Config::get( 'PYTHON_PIL_RESIZE' )->pil_options ) : '';
  113. // ob_start();
  114. $code = null;
  115. $command = $script_file_path
  116. . ( ( $width ) ? ' --width=' . $width : '' )
  117. . ( ( $height ) ? ' --height=' . $height : '' )
  118. . $pil_options
  119. . ' ' . $tmp_file_path
  120. . ' ' . $file_path;
  121. //ob_start();
  122. passthru( 'exec 2>&1; ' . $command, $code );
  123. if ($code !== 0) {
  124. throw new ErrorException('Command PYTHON_PIL_RESIZE exit with code "' . $code . '": ' . $command . PHP_EOL . 'Std out: ' . ob_get_clean() );
  125. }
  126. // ob_clean();
  127. } else {
  128. if ($width || $height) {
  129. if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
  130. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
  131. } else {
  132. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
  133. }
  134. $file_path = $greagwar_image->cacheFile($image->type, $quality = 100, true);
  135. }
  136. }
  137. }
  138. $path_parts = pathinfo($file_path);
  139. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  140. $image->filename = $path_parts['basename'];
  141. unset($greagwar_image);
  142. }
  143. private static function defineSizeWidthAndHeight($image)
  144. {
  145. $file_path = self::getFilePath($image);
  146. $imagesize = getimagesize($file_path);
  147. $image->width = $imagesize[0];
  148. $image->height = $imagesize[1];
  149. $image->size = filesize($file_path);
  150. }
  151. /**
  152. * @param $image
  153. * @param string $value
  154. * @deprecated
  155. */
  156. public static function brightnessContrast($image, $value = '0x7')
  157. {
  158. $file_path = self::getFilePath($image);
  159. $output = shell_exec("convert {$file_path} -brightness-contrast {$value} {$file_path} 2>&1");
  160. if (isset($output) && $output) {
  161. if (class_exists('ErrorMessage')) {
  162. ErrorMessage::log($output);
  163. }
  164. }
  165. }
  166. }