|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jonasdamher\Simplifyimage; |
|
6
|
|
|
|
|
7
|
|
|
use \Exception; |
|
8
|
|
|
use Jonasdamher\Simplifyimage\Core\Validate; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Upload, modify and delete images the easy way. |
|
12
|
|
|
* |
|
13
|
|
|
* Allows crop and scale images easy. |
|
14
|
|
|
* Allows upload and update one or multiple images. |
|
15
|
|
|
* Allows conversion format image to WEBP, PNG and JPEG. |
|
16
|
|
|
* |
|
17
|
|
|
* @package simplifyimage |
|
18
|
|
|
* @version 0.1 |
|
19
|
|
|
* @license https://github.com/jonasdamher/simplifyimage/blob/master/LICENSE MIT License |
|
20
|
|
|
* |
|
21
|
|
|
* @author Jonás Damián Hernández [jonasdamher] |
|
22
|
|
|
*/ |
|
23
|
|
|
class Image extends Validate |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
private array $image; |
|
27
|
|
|
private string $pathSaveFile; |
|
28
|
|
|
private int $size; |
|
29
|
|
|
private string $format; |
|
30
|
|
|
|
|
31
|
|
|
private function getPropertiesImage() |
|
32
|
|
|
{ |
|
33
|
|
|
$this->image = $_FILES[$this->getNameInputFile()]; |
|
34
|
|
|
|
|
35
|
|
|
$pathAndImageName = $this->path->get() . $this->image['name']; |
|
36
|
|
|
|
|
37
|
|
|
$this->size = (int) $this->image['size']; |
|
38
|
|
|
$this->format = strtolower(pathinfo($pathAndImageName, PATHINFO_EXTENSION)); |
|
39
|
|
|
|
|
40
|
|
|
$extesion = ($this->conversion->get() == 'default' ? $this->format : $this->conversion->get()); |
|
41
|
|
|
|
|
42
|
|
|
$this->image['name'] = basename($this->rename($pathAndImageName) . '.' . $extesion); |
|
43
|
|
|
|
|
44
|
|
|
$this->pathSaveFile = $this->path->get() . $this->image['name']; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function rename(string $path): string |
|
48
|
|
|
{ |
|
49
|
|
|
$fileName = mb_strtolower(pathinfo($path, PATHINFO_FILENAME)); |
|
50
|
|
|
$cleanFileName = $this->getPrefixName() . uniqid() . preg_replace('/\s+||[^a-zA-Z0-9-]/', '', $fileName); |
|
51
|
|
|
$filterFileName = filter_var($cleanFileName, FILTER_SANITIZE_STRING); |
|
52
|
|
|
|
|
53
|
|
|
return $filterFileName; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function modifyImage() |
|
57
|
|
|
{ |
|
58
|
|
|
// createImageFormat |
|
59
|
|
|
$imgFormat = ($this->imagecreatefrom)($this->image['tmp_name']); |
|
60
|
|
|
// Image crop |
|
61
|
|
|
$imgFormat = $this->crop->modify($imgFormat); |
|
62
|
|
|
// Image scale |
|
63
|
|
|
$imgFormat = $this->scale->modify($imgFormat); |
|
64
|
|
|
// Image contrast |
|
65
|
|
|
$imgFormat = $this->contrast->modify($imgFormat); |
|
66
|
|
|
|
|
67
|
|
|
$this->conversion->transform($imgFormat, $this->transformImage, $this->image); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Upload new image. |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
|
|
public function upload(): array |
|
75
|
|
|
{ |
|
76
|
|
|
try { |
|
77
|
|
|
|
|
78
|
|
|
if (!$this->existFileAndPath()) { |
|
79
|
|
|
throw new \Exception(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->getPropertiesImage(); |
|
83
|
|
|
|
|
84
|
|
|
if (!$this->validateImage($this->format, $this->size)) { |
|
85
|
|
|
throw new \Exception(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->modifyImage(); |
|
89
|
|
|
|
|
90
|
|
|
// Verify by modify in image. |
|
91
|
|
|
|
|
92
|
|
|
if (!$this->response()['valid']) { |
|
93
|
|
|
throw new \Exception(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (!$this->imageUpload($this->image, $this->pathSaveFile)) { |
|
97
|
|
|
throw new \Exception(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->setFilenameResponse($this->image['name']); |
|
101
|
|
|
} finally { |
|
102
|
|
|
return $this->response(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Remove image in specify path. |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
|
public function remove(): array |
|
111
|
|
|
{ |
|
112
|
|
|
try { |
|
113
|
|
|
if (!$this->verifyImagePath($this->getOldImageName())) { |
|
114
|
|
|
throw new \Exception("Don't exist image file."); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$imagePath = $this->path->get() . $this->getOldImageName(); |
|
118
|
|
|
|
|
119
|
|
|
if (!unlink($imagePath)) { |
|
120
|
|
|
throw new \Exception("Don't remove image."); |
|
121
|
|
|
} |
|
122
|
|
|
} catch (\Exception $e) { |
|
123
|
|
|
parent::fail($e->getMessage()); |
|
124
|
|
|
} finally { |
|
125
|
|
|
return $this->response(); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|