No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

Image.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace common\helpers;
  3. class Image
  4. {
  5. const DIR_UPLOADS = 'uploads';
  6. const THUMBNAIL_SMALL = 'small';
  7. const THUMBNAIL_MEDIUM = 'medium';
  8. const THUMBNAIL_BIG = 'big';
  9. public static function getThumbnailSmall(string $filenameOriginal, bool $absoluteUrl = false)
  10. {
  11. return self::getThumbnail($filenameOriginal, self::THUMBNAIL_SMALL, $absoluteUrl);
  12. }
  13. public static function getThumbnailMedium(string $filenameOriginal, bool $absoluteUrl = false)
  14. {
  15. return self::getThumbnail($filenameOriginal, self::THUMBNAIL_MEDIUM, $absoluteUrl);
  16. }
  17. public static function getThumbnailBig(string $filenameOriginal, bool $absoluteUrl = false)
  18. {
  19. return self::getThumbnail($filenameOriginal, self::THUMBNAIL_BIG, $absoluteUrl);
  20. }
  21. public static function getThumbnail(string $filenameOriginal, string $thumbnail = self::THUMBNAIL_MEDIUM, bool $absoluteUrl = false)
  22. {
  23. $basePath = self::getBasePath($absoluteUrl);
  24. $thumbnailFilename = self::getThumbnailFilename($filenameOriginal, $thumbnail);
  25. $thumbnailPath = $basePath . $thumbnailFilename;
  26. $originalPath = $basePath . $filenameOriginal;
  27. return self::isThumbnailExist($thumbnailFilename) ? $thumbnailPath : $originalPath;
  28. }
  29. public static function getBasePath(bool $absoluteUrl = false): string
  30. {
  31. $absoluteUrlPrepend = '';
  32. if($absoluteUrl) {
  33. $absoluteUrlPrepend = \Yii::$app->urlManagerProducer->getHostInfo();
  34. }
  35. return $absoluteUrlPrepend . \Yii::$app->urlManagerProducer->baseUrl.'/'.self::DIR_UPLOADS.'/';
  36. }
  37. public static function getThumbnailFilename(string $filename, string $thumbnail = self::THUMBNAIL_MEDIUM)
  38. {
  39. $filenameArray = explode('.', $filename);
  40. return $filenameArray[0].'-'.$thumbnail.'.'.$filenameArray[1];
  41. }
  42. public static function isThumbnailExist(string $thumbnailFilename): bool
  43. {
  44. return file_exists(\Yii::getAlias('@producer').'/web/'.self::DIR_UPLOADS.'/'.$thumbnailFilename);
  45. }
  46. }