1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Models; |
12
|
|
|
|
13
|
|
|
use Zemit\Models\Base\AbstractFile; |
14
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
15
|
|
|
use Phalcon\Validation\Validator\StringLength\Max; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class File |
19
|
|
|
* |
20
|
|
|
* @property EmailFile $EmailFileEntity |
21
|
|
|
* @property User $UserEntity |
22
|
|
|
* |
23
|
|
|
* @method EmailFile getEmailFileEntity($params = null) |
24
|
|
|
* @method User getUserEntity($params = null) |
25
|
|
|
* |
26
|
|
|
* @package Zemit\Models |
27
|
|
|
*/ |
28
|
|
|
class File extends AbstractFile |
29
|
|
|
{ |
30
|
|
|
protected $deleted = self::NO; |
31
|
|
|
|
32
|
|
|
public function initialize() |
33
|
|
|
{ |
34
|
|
|
parent::initialize(); |
35
|
|
|
|
36
|
|
|
$this->hasOne('id', EmailFile::class, 'fileId', ['alias' => 'EmailFileEntity']); |
37
|
|
|
$this->belongsTo('userId', User::class, 'id', ['alias' => 'UserEntity']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function validation() |
41
|
|
|
{ |
42
|
|
|
$validator = $this->genericValidation(); |
43
|
|
|
|
44
|
|
|
$validator->add('key', new Max(['max' => 50, 'message' => $this->_('length-exceeded')])); |
45
|
|
|
$validator->add('path', new Max(['max' => 120, 'message' => $this->_('length-exceeded')])); |
46
|
|
|
$validator->add('type', new Max(['max' => 100, 'message' => $this->_('length-exceeded')])); |
47
|
|
|
$validator->add('typeReal', new Max(['max' => 100, 'message' => $this->_('length-exceeded')])); |
48
|
|
|
$validator->add('extension', new Max(['max' => 6, 'message' => $this->_('length-exceeded')])); |
49
|
|
|
$validator->add('name', new Max(['max' => 100, 'message' => $this->_('length-exceeded')])); |
50
|
|
|
$validator->add('nameTemp', new Max(['max' => 120, 'message' => $this->_('length-exceeded')])); |
51
|
|
|
$validator->add('size', new Max(['max' => 45, 'message' => $this->_('length-exceeded')])); |
52
|
|
|
$validator->add('createdBy', new PresenceOf(['message' => $this->_('required')])); |
53
|
|
|
|
54
|
|
|
return $this->validate($validator); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return null if file not found, or the path |
59
|
|
|
* |
60
|
|
|
* @param $fileName |
61
|
|
|
* @param null $category |
|
|
|
|
62
|
|
|
* |
63
|
|
|
* @return null|string |
64
|
|
|
*/ |
65
|
|
|
public function getFilePath($fileName = null) |
66
|
|
|
{ |
67
|
|
|
$fileName ??= $this->getPath(); |
68
|
|
|
$filePath = null; |
|
|
|
|
69
|
|
|
|
70
|
|
|
$config = $this->getDI()->get('config'); |
71
|
|
|
$filePath = $config->app->dir->files . $fileName; |
72
|
|
|
if (!file_exists($filePath)) { |
73
|
|
|
$filePath = null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $filePath; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Compress an image |
81
|
|
|
* |
82
|
|
|
* @param null $destination |
|
|
|
|
83
|
|
|
* @param null $source |
|
|
|
|
84
|
|
|
* @param int $quality |
85
|
|
|
* @param int $maxWidth |
86
|
|
|
* @param int $maxHeight |
87
|
|
|
* @param string $returnMethodName |
88
|
|
|
* |
89
|
|
|
* @return string|bool Return a string on success or false |
90
|
|
|
*/ |
91
|
|
|
public function compressImage($destination = null, $source = null, $quality = 60, $maxWidth = 1920, $maxHeight = 1024, $returnMethodName = 'imagepng') |
92
|
|
|
{ |
93
|
|
|
$source ??= $this->getFilePath(); |
94
|
|
|
$destination ??= $source; |
95
|
|
|
|
96
|
|
|
if (empty($source)) { |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$info = getimagesize($source); |
101
|
|
|
|
102
|
|
|
$imageWidth = $info[0]; |
103
|
|
|
$imageHeight = $info[1]; |
104
|
|
|
$imageSize['width'] = $imageWidth; |
|
|
|
|
105
|
|
|
$imageSize['height'] = $imageHeight; |
106
|
|
|
if ($imageWidth > $maxWidth || $imageHeight > $maxHeight) { |
107
|
|
|
if ($imageWidth > $imageHeight) { |
108
|
|
|
$imageSize['height'] = floor(($imageHeight / $imageWidth) * $maxWidth); |
109
|
|
|
$imageSize['width'] = $maxWidth; |
110
|
|
|
} |
111
|
|
|
else { |
112
|
|
|
$imageSize['width'] = floor(($imageWidth / $imageHeight) * $maxHeight); |
113
|
|
|
$imageSize['height'] = $maxHeight; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
switch (mb_strtolower($info['mime'])) { |
118
|
|
|
case 'image/jpg': |
119
|
|
|
case 'image/jpeg': |
120
|
|
|
$image = imagecreatefromjpeg($source); |
121
|
|
|
break; |
122
|
|
|
case 'image/gif': |
123
|
|
|
$image = imagecreatefromgif($source); |
124
|
|
|
break; |
125
|
|
|
case 'image/png': |
126
|
|
|
$image = imagecreatefrompng($source); |
127
|
|
|
break; |
128
|
|
|
default: |
129
|
|
|
$image = false; |
130
|
|
|
break; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($image) { |
134
|
|
|
$newImage = imagecreatetruecolor($imageSize['width'], $imageSize['height']); |
135
|
|
|
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $imageSize['width'], $imageSize['height'], $imageWidth, $imageHeight); |
136
|
|
|
|
137
|
|
|
$exif = @exif_read_data($source); |
138
|
|
|
if (!empty($exif['Orientation'])) { |
139
|
|
|
switch ($exif['Orientation']) { |
140
|
|
|
case 8: |
141
|
|
|
$newImage = imagerotate($newImage, 90, 0); |
142
|
|
|
break; |
143
|
|
|
case 3: |
144
|
|
|
$newImage = imagerotate($newImage, 180, 0); |
145
|
|
|
break; |
146
|
|
|
case 6: |
147
|
|
|
$newImage = imagerotate($newImage, -90, 0); |
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$returnMethodName($newImage, $destination, $returnMethodName === 'imagejpg' ? $quality : ceil($quality / 10)); |
153
|
|
|
|
154
|
|
|
return $destination; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|