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.

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