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.

143 lines
5.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. $greagwar_image->setCacheDir(Config::get('PATH_WEB_ROOT') . DIRECTORY_SEPARATOR . self::$dir_image_cache);
  13. return $greagwar_image;
  14. }
  15. public static function getFilePath($image)
  16. {
  17. return Config::get('PATH_WEB_ROOT') . DIRECTORY_SEPARATOR . $image->path . DIRECTORY_SEPARATOR . $image->filename;
  18. }
  19. /**
  20. * @param $class string|Image
  21. * @param $file array (ex. ['tmp_name' => '...', 'name' => '...', 'error' => '...'])
  22. * @param $force_create_variants bool
  23. * @return Image
  24. * @throws ErrorException
  25. * TODO: Рефакторинг: убрать параметр $force_create_variants?
  26. */
  27. public static function image($class, $file, $force_create_variants = true)
  28. {
  29. /**
  30. * @var $image Image
  31. */
  32. if (is_null($class) || $class == '') {
  33. throw new ErrorException('Class not defined.');
  34. }
  35. if (!is_object($class) && !class_exists($class)) {
  36. throw new ErrorException('Class "' . $class . '" not exists.');
  37. }
  38. Image::checkSubClass($class);
  39. if (isset($file['error']) && $file['error'] != 0) {
  40. return false;
  41. }
  42. $image = is_object($class) ? $class : new $class;
  43. $image->original_filename = $file['name'];
  44. self::saveImage($image, $file['tmp_name'], $class::getMaxWidth(), $class::getMaxHeight());
  45. if ($force_create_variants) {
  46. $sizes = $image->getSizes();
  47. foreach ($sizes as $size) {
  48. self::imageVariant($image, $size);
  49. }
  50. }
  51. return $image;
  52. }
  53. /**
  54. * @param $image Image
  55. * @param $size string @ex '1200x960'
  56. * @param $greagwar_image GreagwarImage @deprecated
  57. */
  58. public static function imageVariant($image, $size, $greagwar_image = null)
  59. {
  60. unset($greagwar_image);
  61. $image_variant = new ImageVariant();
  62. $size_parts = explode('x', $size);
  63. self::saveImage($image_variant, self::getFilePath($image), $size_parts[0] ? : null, $size_parts[1] ? : null);
  64. $image->variants[$size] = $image_variant;
  65. if (Config::get('PYTHON_PIL_RESIZE')) {
  66. $script_file_path = Config::get('PYTHON_PIL_RESIZE')->script_file_path;
  67. $pil_options = isset(Config::get('PYTHON_PIL_RESIZE')->pil_options) ? ' ' . implode(' ', Config::get('PYTHON_PIL_RESIZE')->pil_options) : '';
  68. ob_start();
  69. exec($script_file_path
  70. . (($size_parts[0])?' --width=' . $size_parts[0]:'')
  71. . (($size_parts[1])?' --height=' . $size_parts[1]:'')
  72. . $pil_options
  73. . ' ' . self::getFilePath($image)
  74. . ' ' . self::getFilePath($image_variant));
  75. ob_clean();
  76. }
  77. if ($image->getWatermark($size) && Config::get('PYTHON_PIL_PASTE')) {
  78. $script_file_path = Config::get('PYTHON_PIL_PASTE')->script_file_path;
  79. $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) : '';
  80. ob_start();
  81. exec($script_file_path
  82. . $pil_options
  83. . ' ' . self::getFilePath($image_variant)
  84. . ' ' . Config::get('PATH_WEB_ROOT') . '/' . $image->getWatermark($size));
  85. ob_clean();
  86. }
  87. }
  88. /**
  89. * @param $image ImageVariant|Image
  90. * @param $tmp_file_path string|GreagwarImage
  91. * @param null $width
  92. * @param null $height
  93. * TODO: Сделать возможность передавать параметры для метода _resize()
  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. if ($width || $height) {
  106. if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
  107. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
  108. } else {
  109. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
  110. }
  111. }
  112. $image->type = $greagwar_image->guessType();
  113. $file_path = $greagwar_image->cacheFile($image->type, $quality = 100, true);
  114. $image->size = filesize($file_path);
  115. $path_parts = pathinfo($file_path);
  116. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  117. $image->filename = $path_parts['basename'];
  118. $image->width = $greagwar_image->width();
  119. $image->height = $greagwar_image->height();
  120. unset($greagwar_image);
  121. }
  122. /**
  123. * @param $image
  124. * @param string $value
  125. * @deprecated
  126. */
  127. public static function brightnessContrast($image, $value = '0x7')
  128. {
  129. $file_path = self::getFilePath($image);
  130. $output = shell_exec("convert {$file_path} -brightness-contrast {$value} {$file_path} 2>&1");
  131. if (isset($output) && $output) {
  132. if (class_exists('ErrorMessage')) {
  133. ErrorMessage::log($output);
  134. }
  135. }
  136. }
  137. }