|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jonasdamher\Simplifyimage\Core; |
|
6
|
|
|
|
|
7
|
|
|
use Jonasdamher\Simplifyimage\Core\ResponseHandler; |
|
8
|
|
|
use Jonasdamher\Simplifyimage\Utils\Path; |
|
9
|
|
|
use Jonasdamher\Simplifyimage\Utils\Contrast; |
|
10
|
|
|
use Jonasdamher\Simplifyimage\Utils\Scale; |
|
11
|
|
|
use Jonasdamher\Simplifyimage\Utils\Crop; |
|
12
|
|
|
use Jonasdamher\Simplifyimage\Utils\Conversion; |
|
13
|
|
|
|
|
14
|
|
|
class Configuration extends ResponseHandler |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
public Path $path; |
|
18
|
|
|
public Contrast $contrast; |
|
19
|
|
|
public Scale $scale; |
|
20
|
|
|
public Crop $crop; |
|
21
|
|
|
public Conversion $conversion; |
|
22
|
|
|
|
|
23
|
|
|
private string $nameInputFile = ''; |
|
24
|
|
|
private string $prefixName = ''; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* to create the image from its default format. |
|
28
|
|
|
*/ |
|
29
|
|
|
protected string $imagecreatefrom = 'imagecreatefrom'; |
|
30
|
|
|
/** |
|
31
|
|
|
* to transform the image to its default format. |
|
32
|
|
|
*/ |
|
33
|
|
|
protected string $transformImage = 'image'; |
|
34
|
|
|
|
|
35
|
|
|
private $oldImageName; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->path = new Path; |
|
40
|
|
|
$this->contrast = new Contrast; |
|
41
|
|
|
$this->scale = new Scale; |
|
42
|
|
|
$this->crop = new Crop; |
|
43
|
|
|
$this->conversion = new Conversion; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// GETS & SETS |
|
47
|
|
|
/** |
|
48
|
|
|
* Return name form input. |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getNameInputFile(): string |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->nameInputFile; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set name form input. |
|
57
|
|
|
* @param string $nameInputFile |
|
58
|
|
|
*/ |
|
59
|
|
|
public function nameImputFile(string $nameInputFile) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->nameInputFile = $nameInputFile; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getPrefixName(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->prefixName; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Header for filename. |
|
71
|
|
|
* Not allow special simbols. |
|
72
|
|
|
* @param string $prefixName |
|
73
|
|
|
*/ |
|
74
|
|
|
public function prefixName(string $prefixName) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->prefixName = $prefixName; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function getOldImageName(): string |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->oldImageName; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Experimental, for remove image. |
|
86
|
|
|
* @example imageForRemove.png |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setOldImageName(string $oldImageName) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->oldImageName = $oldImageName; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// FINAL GETS & SETS |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Upload image in your path. |
|
97
|
|
|
* @param array $image - Current Image for upload. |
|
98
|
|
|
* @param string $target_file - Path where to save. |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function imageUpload(array $image, string $target_file): bool |
|
102
|
|
|
{ |
|
103
|
|
|
try { |
|
104
|
|
|
$ok = true; |
|
105
|
|
|
if (!is_uploaded_file($image['tmp_name']) || !move_uploaded_file($image['tmp_name'], $target_file)) { |
|
106
|
|
|
throw new \Exception('It could not image upload, try again.'); |
|
107
|
|
|
} |
|
108
|
|
|
} catch (\Exception $e) { |
|
109
|
|
|
$ok = false; |
|
110
|
|
|
parent::fail($e->getMessage()); |
|
111
|
|
|
} finally { |
|
112
|
|
|
return $ok; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function response(): array |
|
117
|
|
|
{ |
|
118
|
|
|
return parent::arrayResponse(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
protected function setFilenameResponse(string $filename) |
|
122
|
|
|
{ |
|
123
|
|
|
parent::filename($filename); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|