|
- <?php
-
- namespace common\helpers;
-
- class Image
- {
- const DIR_UPLOADS = 'uploads';
-
- const THUMBNAIL_SMALL = 'small';
- const THUMBNAIL_MEDIUM = 'medium';
- const THUMBNAIL_BIG = 'big';
-
- public static function getThumbnailSmall(string $filenameOriginal, bool $absoluteUrl = false)
- {
- return self::getThumbnail($filenameOriginal, self::THUMBNAIL_SMALL, $absoluteUrl);
- }
-
- public static function getThumbnailMedium(string $filenameOriginal, bool $absoluteUrl = false)
- {
- return self::getThumbnail($filenameOriginal, self::THUMBNAIL_MEDIUM, $absoluteUrl);
- }
-
- public static function getThumbnailBig(string $filenameOriginal, bool $absoluteUrl = false)
- {
- return self::getThumbnail($filenameOriginal, self::THUMBNAIL_BIG, $absoluteUrl);
- }
-
- public static function getThumbnail(string $filenameOriginal, string $thumbnail = self::THUMBNAIL_MEDIUM, bool $absoluteUrl = false)
- {
- $basePath = self::getBasePath($absoluteUrl);
- $thumbnailFilename = self::getThumbnailFilename($filenameOriginal, $thumbnail);
- $thumbnailPath = $basePath . $thumbnailFilename;
- $originalPath = $basePath . $filenameOriginal;
- $defaultImagePath = $basePath . 'default.jpg';
-
- if(self::isPhotoExist($thumbnailFilename)) {
- return $thumbnailPath;
- }
-
- if(self::isPhotoExist($filenameOriginal)) {
- return $originalPath;
- }
-
- return $defaultImagePath;
- }
-
- public static function getBasePath(bool $absoluteUrl = false): string
- {
- $absoluteUrlPrepend = '';
- if($absoluteUrl) {
- $absoluteUrlPrepend = \Yii::$app->urlManagerProducer->getHostInfo();
- }
-
- return $absoluteUrlPrepend . \Yii::$app->urlManagerProducer->baseUrl.'/'.self::DIR_UPLOADS.'/';
- }
-
- public static function getThumbnailFilename(string $filename, string $thumbnail = self::THUMBNAIL_MEDIUM)
- {
- $filenameArray = explode('.', $filename);
- return $filenameArray[0].'-'.$thumbnail.'.'.$filenameArray[1];
- }
-
- public static function isPhotoExist(string $filename): bool
- {
- return file_exists(\Yii::getAlias('@producer').'/web/'.self::DIR_UPLOADS.'/'.$filename);
- }
- }
|