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.

146 lines
5.7 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 (Config::get('PYTHON_PIL_PASTE') &&
  78. function_exists(array($image, 'getWatermark')) &&
  79. $image->getWatermark($size)
  80. ) {
  81. $script_file_path = Config::get('PYTHON_PIL_PASTE')->script_file_path;
  82. $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) : '';
  83. ob_start();
  84. exec($script_file_path
  85. . $pil_options
  86. . ' ' . self::getFilePath($image_variant)
  87. . ' ' . Config::get('PATH_WEB_ROOT') . '/' . $image->getWatermark($size));
  88. ob_clean();
  89. }
  90. }
  91. /**
  92. * @param $image ImageVariant|Image
  93. * @param $tmp_file_path string|GreagwarImage
  94. * @param null $width
  95. * @param null $height
  96. * TODO: Сделать возможность передавать параметры для метода _resize()
  97. */
  98. public static function saveImage($image, $tmp_file_path , $width = null, $height = null)
  99. {
  100. if (is_object($tmp_file_path)) {
  101. /**
  102. * @deprecated
  103. */
  104. $greagwar_image = $tmp_file_path;
  105. } else {
  106. $greagwar_image = self::getGreagwarImage($tmp_file_path);
  107. }
  108. if ($width || $height) {
  109. if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
  110. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
  111. } else {
  112. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
  113. }
  114. }
  115. $image->type = $greagwar_image->guessType();
  116. $file_path = $greagwar_image->cacheFile($image->type, $quality = 100, true);
  117. $image->size = filesize($file_path);
  118. $path_parts = pathinfo($file_path);
  119. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  120. $image->filename = $path_parts['basename'];
  121. $image->width = $greagwar_image->width();
  122. $image->height = $greagwar_image->height();
  123. unset($greagwar_image);
  124. }
  125. /**
  126. * @param $image
  127. * @param string $value
  128. * @deprecated
  129. */
  130. public static function brightnessContrast($image, $value = '0x7')
  131. {
  132. $file_path = self::getFilePath($image);
  133. $output = shell_exec("convert {$file_path} -brightness-contrast {$value} {$file_path} 2>&1");
  134. if (isset($output) && $output) {
  135. if (class_exists('ErrorMessage')) {
  136. ErrorMessage::log($output);
  137. }
  138. }
  139. }
  140. }