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.

148 lines
5.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. if ($force_create_variants) {
  48. $sizes = $image->getSizes();
  49. foreach ($sizes as $size) {
  50. self::imageVariant($image, $size);
  51. }
  52. }
  53. return $image;
  54. }
  55. /**
  56. * @param $image Image
  57. * @param $size string @ex '1200x960'
  58. * @param $greagwar_image GreagwarImage @deprecated
  59. */
  60. public static function imageVariant($image, $size, $greagwar_image = null)
  61. {
  62. unset($greagwar_image);
  63. $image_variant = new ImageVariant();
  64. $size_parts = explode('x', $size);
  65. self::saveImage($image_variant, self::getFilePath($image), $size_parts[0] ? : null, $size_parts[1] ? : null);
  66. $image->variants[$size] = $image_variant;
  67. if (Config::get('PYTHON_PIL_RESIZE')) {
  68. $script_file_path = Config::get('PYTHON_PIL_RESIZE')->script_file_path;
  69. $pil_options = isset(Config::get('PYTHON_PIL_RESIZE')->pil_options) ? ' ' . implode(' ', Config::get('PYTHON_PIL_RESIZE')->pil_options) : '';
  70. // ob_start();
  71. passthru($script_file_path
  72. . (($size_parts[0])?' --width=' . $size_parts[0]:'')
  73. . (($size_parts[1])?' --height=' . $size_parts[1]:'')
  74. . $pil_options
  75. . ' ' . self::getFilePath($image)
  76. . ' ' . self::getFilePath($image_variant));
  77. // ob_clean();
  78. }
  79. if (Config::get('PYTHON_PIL_PASTE') &&
  80. function_exists(array($image, 'getWatermark')) &&
  81. $image->getWatermark($size)
  82. ) {
  83. $script_file_path = Config::get('PYTHON_PIL_PASTE')->script_file_path;
  84. $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) : '';
  85. // ob_start();
  86. passthru($script_file_path
  87. . $pil_options
  88. . ' ' . self::getFilePath($image_variant)
  89. . ' ' . Config::get('PATH_WEB_ROOT') . '/' . $image->getWatermark($size));
  90. // ob_clean();
  91. }
  92. }
  93. /**
  94. * @param $image ImageVariant|Image
  95. * @param $tmp_file_path string|GreagwarImage
  96. * @param null $width
  97. * @param null $height
  98. * TODO: Сделать возможность передавать параметры для метода _resize()
  99. */
  100. public static function saveImage($image, $tmp_file_path , $width = null, $height = null)
  101. {
  102. if (is_object($tmp_file_path)) {
  103. /**
  104. * @deprecated
  105. */
  106. $greagwar_image = $tmp_file_path;
  107. } else {
  108. $greagwar_image = self::getGreagwarImage($tmp_file_path);
  109. }
  110. if ($width || $height) {
  111. if (!(ImageVariant::getIsClass($image)) && Image::getIsSubClass($image)) {
  112. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = true);
  113. } else {
  114. $greagwar_image->resize($width, $height, 0xffffff, $force = false, $rescale = false, $crop = false);
  115. }
  116. }
  117. $image->type = $greagwar_image->guessType();
  118. $file_path = $greagwar_image->cacheFile($image->type, $quality = 100, true);
  119. $image->size = filesize($file_path);
  120. $path_parts = pathinfo($file_path);
  121. $image->path = preg_replace('#^' . Config::get('PATH_WEB_ROOT') . '/#', '', $path_parts['dirname']);
  122. $image->filename = $path_parts['basename'];
  123. $image->width = $greagwar_image->width();
  124. $image->height = $greagwar_image->height();
  125. unset($greagwar_image);
  126. }
  127. /**
  128. * @param $image
  129. * @param string $value
  130. * @deprecated
  131. */
  132. public static function brightnessContrast($image, $value = '0x7')
  133. {
  134. $file_path = self::getFilePath($image);
  135. $output = shell_exec("convert {$file_path} -brightness-contrast {$value} {$file_path} 2>&1");
  136. if (isset($output) && $output) {
  137. if (class_exists('ErrorMessage')) {
  138. ErrorMessage::log($output);
  139. }
  140. }
  141. }
  142. }