-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBasicImageHelperWithGD.php
101 lines (89 loc) · 2.5 KB
/
BasicImageHelperWithGD.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
<?php
/**
* Project codeigniter-basic-helper
* Created by PhpStorm
* User: 713uk13m <dev@nguyenanhung.com>
* Copyright: 713uk13m <dev@nguyenanhung.com>
* Date: 12/02/2023
* Time: 23:05
*/
namespace nguyenanhung\CodeIgniter\BasicHelper;
class BasicImageHelperWithGD
{
protected $image;
protected $imageFormat;
public function load($imageFile)
{
$imageInfo = getImageSize($imageFile);
$this->imageFormat = $imageInfo[2];
if ($this->imageFormat === IMAGETYPE_JPEG) {
$this->image = imagecreatefromjpeg($imageFile);
} elseif ($this->imageFormat === IMAGETYPE_GIF) {
$this->image = imagecreatefromgif($imageFile);
} elseif ($this->imageFormat === IMAGETYPE_PNG) {
$this->image = imagecreatefrompng($imageFile);
}
}
public function save($imageFile, $imageFormat = IMAGETYPE_JPEG, $compression = 75, $permissions = null)
{
if ($imageFormat == IMAGETYPE_JPEG) {
imagejpeg($this->image, $imageFile, $compression);
} elseif ($imageFormat == IMAGETYPE_GIF) {
imagegif($this->image, $imageFile);
} elseif ($imageFormat == IMAGETYPE_PNG) {
imagepng($this->image, $imageFile);
}
if ($permissions != null) {
chmod($imageFile, $permissions);
}
}
public function getWidth()
{
return imagesx($this->image);
}
public function getHeight()
{
return imagesy($this->image);
}
public function resizeToHeight($height)
{
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resized($width, $height);
}
public function resizeToWidth($width)
{
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resized(
$width,
$height
);
}
public function scale($scale)
{
$width = $this->getWidth() * $scale / 100;
$height = $this->getheight() * $scale / 100;
$this->resized(
$width,
$height
);
}
protected function resized($width, $height)
{
$newImage = imagecreatetruecolor($width, $height);
imagecopyresampled(
$newImage,
$this->image,
0,
0,
0,
0,
$width,
$height,
$this->getWidth(),
$this->getHeight()
);
$this->image = $newImage;
}
}