diff --git a/GalleryBehavior.php b/GalleryBehavior.php index 54e52e7..1b6630e 100644 --- a/GalleryBehavior.php +++ b/GalleryBehavior.php @@ -172,7 +172,7 @@ public function getImages() $query = new \yii\db\Query(); $imagesData = $query - ->select(['id', 'name', 'description', 'rank']) + ->select('*') ->from($this->tableName) ->where(['type' => $this->type, 'ownerId' => $this->getGalleryId()]) ->orderBy(['rank' => 'asc']) @@ -187,21 +187,21 @@ public function getImages() return $this->_images; } - protected function getFileName($imageId, $version = 'original') + protected function getFileName($imageId, $version = 'original', $extension = null) { return implode( '/', [ $this->getGalleryId(), $imageId, - $version . '.' . $this->extension, + $version . '.' . $this->getImageExtension($imageId, $extension), ] ); } - public function getUrl($imageId, $version = 'original') + public function getUrl($imageId, $version = 'original', $extension = null) { - $path = $this->getFilePath($imageId, $version); + $path = $this->getFilePath($imageId, $version, $extension); if (!file_exists($path)) { return null; @@ -215,12 +215,12 @@ public function getUrl($imageId, $version = 'original') $suffix = ''; } - return $this->url . '/' . $this->getFileName($imageId, $version) . $suffix; + return $this->url . '/' . $this->getFileName($imageId, $version, $extension) . $suffix; } - public function getFilePath($imageId, $version = 'original') + public function getFilePath($imageId, $version = 'original', $extension = null) { - return $this->directory . '/' . $this->getFileName($imageId, $version); + return $this->directory . '/' . $this->getFileName($imageId, $version, $extension); } /** @@ -273,6 +273,25 @@ public function getGalleryId() } } + /** + * Get Image Extension + * @return string + */ + public function getImageExtension($imageId, $extension = null) + { + if (!empty($extension)) { + return $extension; + } elseif (!empty($this->extension)) { + return $this->extension; + } else { + $image = (new Query()) + ->select('*') + ->from($this->tableName) + ->where(['id' => $imageId]) + ->one(); + return isset($image['extension']) ? $image['extension'] : ''; + } + } private function createFolders($filePath) { @@ -336,6 +355,29 @@ public function addImage($fileName) ['id' => $id] )->execute(); + $schema = $db->getTableSchema($this->tableName); + if (isset($schema->columns['extension'])) { + $imageType = getimagesize($fileName)[2]; + switch ($imageType) { + case IMAGETYPE_JPEG: + $extension = 'jpg'; + break; + case IMAGETYPE_PNG: + $extension = 'png'; + break; + + default: + $extension = ''; + break; + } + $db->createCommand() + ->update( + $this->tableName, + ['extension' => $extension], + ['id' => $id] + )->execute(); + } + $this->replaceImage($id, $fileName); $galleryImage = new GalleryImage($this, ['id' => $id]); diff --git a/GalleryImage.php b/GalleryImage.php index 76db224..3e0dbea 100644 --- a/GalleryImage.php +++ b/GalleryImage.php @@ -8,6 +8,7 @@ class GalleryImage public $description; public $id; public $rank; + public $extension; /** * @var GalleryBehavior */ @@ -26,6 +27,7 @@ function __construct(GalleryBehavior $galleryBehavior, array $props) $this->description = isset($props['description']) ? $props['description'] : ''; $this->id = isset($props['id']) ? $props['id'] : ''; $this->rank = isset($props['rank']) ? $props['rank'] : ''; + $this->extension = isset($props['extension']) ? $props['extension'] : ''; } /** @@ -35,6 +37,6 @@ function __construct(GalleryBehavior $galleryBehavior, array $props) */ public function getUrl($version) { - return $this->galleryBehavior->getUrl($this->id, $version); + return $this->galleryBehavior->getUrl($this->id, $version, $this->extension); } } diff --git a/migrations/m210515_022746_add_image_extension.php b/migrations/m210515_022746_add_image_extension.php new file mode 100644 index 0000000..ad2baf0 --- /dev/null +++ b/migrations/m210515_022746_add_image_extension.php @@ -0,0 +1,20 @@ +addColumn($this->tableName, 'extension', Schema::TYPE_STRING); + } + + public function down() + { + $this->dropColumn($this->tableName, 'extension'); + } +}