ImageManagerGetPath.php
4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace backend\components\imagemanager\components;
use Yii;
use yii\base\Component;
use backend\components\imagemanager\models\ImageManager;
use yii\base\InvalidConfigException;
use yii\db\Connection;
class ImageManagerGetPath extends Component {
/**
* @var null|string $mediaPath Folder path in which the images are stored
*/
public $mediaPath = null;
/**
* @var string $cachePath cache path where store the resized images (relative from webroot (index.php))
*/
public $cachePath = "assets/imagemanager";
/**
* @var boolean $useFilename use original filename in generated cache file
*/
public $useFilename = true;
/**
* @var boolean $useFilename use original filename in generated cache file
*/
public $absoluteUrl = false;
/**
* @var string The DB component name that the image model uses
* This defaults to the default Yii DB component: Yii::$app->db
* If this component is not set, the model will default to DB
*/
public $databaseComponent = 'db';
/**
* Init set config
*/
public function init() {
parent::init();
// initialize the compontent with the configuration loaded from config.php
\Yii::$app->set('imageresize', [
'class' => 'noam148\imageresize\ImageResize',
'cachePath' => $this->cachePath,
'useFilename' => $this->useFilename,
'absoluteUrl' => $this->absoluteUrl,
]);
if (is_callable($this->databaseComponent)) {
// The database component is callable, run the user function
$this->databaseComponent = call_user_func($this->databaseComponent);
}
// Check if the user input is correct
$this->_checkVariables();
}
/**
* Get the path for the given ImageManager_id record
* @param int $ImageManager_id ImageManager record for which the path needs to be generated
* @param int $width Thumbnail image width
* @param int $height Thumbnail image height
* @param string $thumbnailMode Thumbnail mode
* @return null|string Full path is returned when image is found, null if no image could be found
*/
public function getImagePath($ImageManager_id, $width = 400, $height = 400, $thumbnailMode = "outbound") {
//default return
$return = null;
$mImageManager = ImageManager::findOne($ImageManager_id);
//check if not empty
if ($mImageManager !== null) {
//set crop mode
$mode = $thumbnailMode == "outbound" ? "outbound" : "inset";
$sMediaPath = null;
if ($this->mediaPath !== null) {
$sMediaPath = $this->mediaPath;
}
$sFileExtension = pathinfo($mImageManager->fileName, PATHINFO_EXTENSION);
//get image file path
$sImageFilePath = $sMediaPath . '/' . $mImageManager->id . '_' . $mImageManager->fileHash . '.' . $sFileExtension;
//check file exists
if (file_exists($sImageFilePath)) {
$return = \Yii::$app->imageresize->getUrl($sImageFilePath, $width, $height, $mode, null, $mImageManager->fileName);
} else {
$return = null; //isset(\Yii::$app->controller->module->assetPublishedUrl) ? \Yii::$app->controller->module->assetPublishedUrl. "/img/img_no-image.png" : null;
}
}
return $return;
}
/**
* Check if the user configurable variables match the criteria
* @throws InvalidConfigException
*/
private function _checkVariables() {
// Check to make sure that the $databaseComponent is a string
if (! is_string($this->databaseComponent)) {
throw new InvalidConfigException("Image Manager Component - Init: Database component '$this->databaseComponent' is not a string");
}
// Check to make sure that the $databaseComponent object exists
if (Yii::$app->get($this->databaseComponent, false) === null) {
throw new InvalidConfigException("Image Manager Component - Init: Database component '$this->databaseComponent' does not exists in application configuration");
}
// Check to make sure that the $databaseComponent is a yii\db\Connection object
if (($databaseComponentClassName = get_class(Yii::$app->get($this->databaseComponent))) !== ($connectionClassName = Connection::className())) {
throw new InvalidConfigException("Image Manager Component - Init: Database component '$this->databaseComponent' is not of type '$connectionClassName' instead it is '$databaseComponentClassName'");
}
}
}