Skip to content

Commit

Permalink
Add migration and logic for image extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve McKinley committed May 15, 2021
1 parent 72f9891 commit 8b45dcc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
58 changes: 50 additions & 8 deletions GalleryBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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;
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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]);
Expand Down
4 changes: 3 additions & 1 deletion GalleryImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class GalleryImage
public $description;
public $id;
public $rank;
public $extension;
/**
* @var GalleryBehavior
*/
Expand All @@ -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'] : '';
}

/**
Expand All @@ -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);
}
}
20 changes: 20 additions & 0 deletions migrations/m210515_022746_add_image_extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace zxbodya\yii2\galleryManager\migrations;

use yii\db\Schema;
use yii\db\Migration;

class m210515_022746_add_image_extension extends Migration
{
public $tableName = '{{%gallery_image}}';

public function up()
{
$this->addColumn($this->tableName, 'extension', Schema::TYPE_STRING);
}

public function down()
{
$this->dropColumn($this->tableName, 'extension');
}
}

0 comments on commit 8b45dcc

Please sign in to comment.