ImageHelper.php
1.79 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
<?php
namespace artbox\core\helpers;
use yii\base\InvalidConfigException;
use Yii;
/**
* Class ImageHelper
*
* @package artbox\core\helpers
*/
class ImageHelper
{
/**
* Parse $given path and creates image manipulator instance
*
* @param $path
*
* @return ImageManipulator
* @throws \yii\base\InvalidConfigException
*/
public static function set($path)
{
self::checkDir();
$path = Yii::getAlias($path);
if (!file_exists($path) || empty($path)) {
$path = Yii::getAlias('@frontend/web/img/no-image.png');
}
$matches = [];
if (!preg_match("~^(.+\/)([a-zA-Z0-9_-]+)\.(jpg|jpeg|gif|png)$~i", $path, $matches)) {
throw new InvalidConfigException('Wrong file path');
}
/**
* @var ImageManipulator $obj
*/
$obj = Yii::createObject(
[
'class' => ImageManipulator::className(),
'fullPath' => $matches[ 0 ],
'path' => $matches[ 1 ],
'name' => $matches[ 2 ],
'extension' => $matches[ 3 ],
]
);
return $obj;
}
/**
* Check if helper directory exists, if not - create it
*/
protected static function checkDir()
{
$dir = Yii::getAlias('@storage/helper');
$stor = Yii::getAlias('@storage');
if (!is_dir($stor)) {
mkdir($stor);
}
if (!is_dir($dir)) {
mkdir($dir);
}
}
}