ShowImage.php
1.62 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
<?php
namespace common\behaviors;
use common\modules\file\components\UploaderComponent;
use yii;
use yii\base\Behavior;
class ShowImage extends Behavior
{
function minImg($dir, $width, $height=null){
if(empty($dir)){
return '/images/avatar-bg.png';
}
if($width=='original'){
$preg = '/\/(.[^\/]*)$/';
preg_match('/\.(.[^.]*)$/', $dir, $type);
if(isset($type[1])){
$dir = preg_replace( $preg, '/original.'.$type[1], $dir);
}
} else {
$preg = '/\/(.[^\/]*)$/';
preg_match('/\.(.[^.]*)$/', $dir, $type);
if(isset($type[1])){
$dir = preg_replace( $preg, '/'.$width.'x'.$height.'.'.$type[1], $dir);
$storage = dirname(yii\helpers\Url::to('@storage'));
$filename = $storage.$dir;
if (!file_exists($filename)) {
$original = $storage.dirname($dir).'/original.'.$type[1];
if (file_exists($original)) {
$resizer = new UploaderComponent();
$resizer->resizeImg($width,$height, $original,$filename);
} else {
throw new \Exception("Файл $original не существует");
}
}
}
}
return $dir;
}
function ShowGallery($array){
$gallery = explode(',', $array );
if(is_array($gallery)){
array_splice($gallery,-1);
return $gallery;
} else {
return [];
}
}
}