ci4

Форк
0
/
Image.php 
137 строк · 3.2 Кб
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Images;
15

16
use CodeIgniter\Files\File;
17
use CodeIgniter\Images\Exceptions\ImageException;
18

19
/**
20
 * Encapsulation of an Image file
21
 *
22
 * @see \CodeIgniter\Images\ImageTest
23
 */
24
class Image extends File
25
{
26
    /**
27
     * The original image width in pixels.
28
     *
29
     * @var float|int
30
     */
31
    public $origWidth;
32

33
    /**
34
     * The original image height in pixels.
35
     *
36
     * @var float|int
37
     */
38
    public $origHeight;
39

40
    /**
41
     * The image type constant.
42
     *
43
     * @see http://php.net/manual/en/image.constants.php
44
     *
45
     * @var int
46
     */
47
    public $imageType;
48

49
    /**
50
     * attributes string with size info:
51
     * 'height="100" width="200"'
52
     *
53
     * @var string
54
     */
55
    public $sizeStr;
56

57
    /**
58
     * The image's mime type, i.e. image/jpeg
59
     *
60
     * @var string
61
     */
62
    public $mime;
63

64
    /**
65
     * Makes a copy of itself to the new location. If no filename is provided
66
     * it will use the existing filename.
67
     *
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.
71
     */
72
    public function copy(string $targetPath, ?string $targetName = null, int $perms = 0644): bool
73
    {
74
        $targetPath = rtrim($targetPath, '/ ') . '/';
75

76
        $targetName ??= $this->getFilename();
77

78
        if (empty($targetName)) {
79
            throw ImageException::forInvalidFile($targetName);
80
        }
81

82
        if (! is_dir($targetPath)) {
83
            mkdir($targetPath, 0755, true);
84
        }
85

86
        if (! copy($this->getPathname(), "{$targetPath}{$targetName}")) {
87
            throw ImageException::forCopyError($targetPath);
88
        }
89

90
        chmod("{$targetPath}/{$targetName}", $perms);
91

92
        return true;
93
    }
94

95
    /**
96
     * Get image properties
97
     *
98
     * A helper function that gets info about the file
99
     *
100
     * @return array|bool
101
     */
102
    public function getProperties(bool $return = false)
103
    {
104
        $path = $this->getPathname();
105

106
        if (! $vals = getimagesize($path)) {
107
            throw ImageException::forFileNotSupported();
108
        }
109

110
        $types = [
111
            IMAGETYPE_GIF  => 'gif',
112
            IMAGETYPE_JPEG => 'jpeg',
113
            IMAGETYPE_PNG  => 'png',
114
            IMAGETYPE_WEBP => 'webp',
115
        ];
116

117
        $mime = 'image/' . ($types[$vals[2]] ?? 'jpg');
118

119
        if ($return === true) {
120
            return [
121
                'width'      => $vals[0],
122
                'height'     => $vals[1],
123
                'image_type' => $vals[2],
124
                'size_str'   => $vals[3],
125
                'mime_type'  => $mime,
126
            ];
127
        }
128

129
        $this->origWidth  = $vals[0];
130
        $this->origHeight = $vals[1];
131
        $this->imageType  = $vals[2];
132
        $this->sizeStr    = $vals[3];
133
        $this->mime       = $mime;
134

135
        return true;
136
    }
137
}
138

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.