-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.php
133 lines (111 loc) · 3.1 KB
/
Image.php
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Images;
use CodeIgniter\Files\File;
use CodeIgniter\Images\Exceptions\ImageException;
/**
* Encapsulation of an Image file
*/
class Image extends File
{
/**
* The original image width in pixels.
*
* @var float|int
*/
public $origWidth;
/**
* The original image height in pixels.
*
* @var float|int
*/
public $origHeight;
/**
* The image type constant.
*
* @see http://php.net/manual/en/image.constants.php
*
* @var int
*/
public $imageType;
/**
* attributes string with size info:
* 'height="100" width="200"'
*
* @var string
*/
public $sizeStr;
/**
* The image's mime type, i.e. image/jpeg
*
* @var string
*/
public $mime;
/**
* Makes a copy of itself to the new location. If no filename is provided
* it will use the existing filename.
*
* @param string $targetPath The directory to store the file in
* @param string|null $targetName The new name of the copied file.
* @param int $perms File permissions to be applied after copy.
*/
public function copy(string $targetPath, ?string $targetName = null, int $perms = 0644): bool
{
$targetPath = rtrim($targetPath, '/ ') . '/';
$targetName = $targetName ?? $this->getFilename();
if (empty($targetName)) {
throw ImageException::forInvalidFile($targetName);
}
if (! is_dir($targetPath)) {
mkdir($targetPath, 0755, true);
}
if (! copy($this->getPathname(), "{$targetPath}{$targetName}")) {
throw ImageException::forCopyError($targetPath);
}
chmod("{$targetPath}/{$targetName}", $perms);
return true;
}
/**
* Get image properties
*
* A helper function that gets info about the file
*
* @return array|bool
*/
public function getProperties(bool $return = false)
{
$path = $this->getPathname();
if (! $vals = getimagesize($path)) {
throw ImageException::forFileNotSupported();
}
$types = [
IMAGETYPE_GIF => 'gif',
IMAGETYPE_JPEG => 'jpeg',
IMAGETYPE_PNG => 'png',
IMAGETYPE_WEBP => 'webp',
];
$mime = 'image/' . ($types[$vals[2]] ?? 'jpg');
if ($return === true) {
return [
'width' => $vals[0],
'height' => $vals[1],
'image_type' => $vals[2],
'size_str' => $vals[3],
'mime_type' => $mime,
];
}
$this->origWidth = $vals[0];
$this->origHeight = $vals[1];
$this->imageType = $vals[2];
$this->sizeStr = $vals[3];
$this->mime = $mime;
return true;
}
}