3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
namespace CodeIgniter\Images;
16
use CodeIgniter\Files\File;
17
use CodeIgniter\Images\Exceptions\ImageException;
20
* Encapsulation of an Image file
22
* @see \CodeIgniter\Images\ImageTest
24
class Image extends File
27
* The original image width in pixels.
34
* The original image height in pixels.
41
* The image type constant.
43
* @see http://php.net/manual/en/image.constants.php
50
* attributes string with size info:
51
* 'height="100" width="200"'
58
* The image's mime type, i.e. image/jpeg
65
* Makes a copy of itself to the new location. If no filename is provided
66
* it will use the existing filename.
68
* @param string $targetPath The directory to store the file in
69
* @param string|null $targetName The new name of the copied file.
70
* @param int $perms File permissions to be applied after copy.
72
public function copy(string $targetPath, ?string $targetName = null, int $perms = 0644): bool
74
$targetPath = rtrim($targetPath, '/ ') . '/';
76
$targetName ??= $this->getFilename();
78
if (empty($targetName)) {
79
throw ImageException::forInvalidFile($targetName);
82
if (! is_dir($targetPath)) {
83
mkdir($targetPath, 0755, true);
86
if (! copy($this->getPathname(), "{$targetPath}{$targetName}")) {
87
throw ImageException::forCopyError($targetPath);
90
chmod("{$targetPath}/{$targetName}", $perms);
96
* Get image properties
98
* A helper function that gets info about the file
102
public function getProperties(bool $return = false)
104
$path = $this->getPathname();
106
if (! $vals = getimagesize($path)) {
107
throw ImageException::forFileNotSupported();
111
IMAGETYPE_GIF => 'gif',
112
IMAGETYPE_JPEG => 'jpeg',
113
IMAGETYPE_PNG => 'png',
114
IMAGETYPE_WEBP => 'webp',
117
$mime = 'image/' . ($types[$vals[2]] ?? 'jpg');
119
if ($return === true) {
122
'height' => $vals[1],
123
'image_type' => $vals[2],
124
'size_str' => $vals[3],
125
'mime_type' => $mime,
129
$this->origWidth = $vals[0];
130
$this->origHeight = $vals[1];
131
$this->imageType = $vals[2];
132
$this->sizeStr = $vals[3];