7
7
use Tamedevelopers \File \Traits \CommonTrait ;
8
8
use Tamedevelopers \File \Traits \FileMagicTrait ;
9
9
10
-
11
- class ImageAutoresize {
12
-
10
+ class ImageAutoresize {
13
11
use FileMagicTrait, CommonTrait;
14
12
15
13
/**
@@ -18,130 +16,132 @@ class ImageAutoresize {
18
16
* @param int|null $width The desired width of the resized image (in pixels).
19
17
* @param int|null $height The desired height of the resized image (in pixels).
20
18
*
21
- * @return bool
19
+ * @return bool
22
20
* - Returns true on success or false on failure.
23
21
*/
24
22
public function resize ($ width = null , $ height = null )
25
23
{
26
- $ this ->loop (function ($ response ) use ($ width , $ height ) {
27
- foreach ($ response ->uploads as $ upload ){
28
-
29
- // resource
24
+ $ this ->loop (function ($ response ) use ($ width , $ height ) {
25
+ foreach ($ response ->uploads as $ upload ) {
26
+ // Resource
30
27
$ imageSource = $ this ->getImageResource ($ upload );
31
28
32
29
// GdImage object
33
30
$ gdImage = $ response ->createGDImage ($ imageSource , $ upload ['fullPath ' ]);
34
31
35
- // if not an instance of GdImage
36
- if (!$ gdImage instanceof \GdImage){
32
+ // If not an instance of GdImage
33
+ if (!$ gdImage instanceof \GdImage) {
37
34
return ;
38
35
}
39
-
36
+
40
37
// Get the dimensions of the source image.
41
38
$ source_width = imagesx ($ gdImage );
42
39
$ source_height = imagesy ($ gdImage );
43
-
44
- // get image aspect ratio
45
- [$ width , $ height ] = $ this ->imageAspectRatio ($ imageSource , $ width , $ height );
40
+
41
+ // Calculate the closest possible fit dimensions
42
+ [$ new_width , $ new_height ] = $ this ->imageAspectRatio (
43
+ [$ source_width , $ source_height ],
44
+ $ width ,
45
+ $ height
46
+ );
46
47
47
48
// Create a new image with the adjusted dimensions
48
- $ resizedImage = @imagecreatetruecolor ($ width , $ height );
49
+ $ resizedImage = @imagecreatetruecolor ($ new_width , $ new_height );
49
50
50
51
// Check if the destination image resource was created successfully
51
52
if ($ resizedImage ) {
52
-
53
- // Get the image extension
54
- $ imageExtension = pathinfo ($ upload ['fullPath ' ], PATHINFO_EXTENSION );
55
-
56
- // Check if the image extension is 'png'
57
- if ($ imageExtension === 'png ' ){
58
- // Check if the source image has transparency
59
- $ sourceHasTransparency = $ this ->isImageTransparent ($ gdImage );
60
-
61
- // Enable alpha blending and save alpha channel
62
- imagesavealpha ($ resizedImage , true );
63
-
64
- // If the source image doesn't have transparency, fill the resized image with the source image's background color
65
- if (!$ sourceHasTransparency ) {
66
- $ background_color = $ this ->getBackgroundColor ($ gdImage );
67
- $ background_color = imagecolorallocate ($ resizedImage , $ background_color [0 ], $ background_color [1 ], $ background_color [2 ]);
68
- imagefill ($ resizedImage , 0 , 0 , $ background_color );
69
- } else {
70
- // Fill with a transparent color (instead of white)
71
- $ transparent_color = imagecolorallocatealpha ($ resizedImage , 0 , 0 , 0 , 127 );
72
- imagefill ($ resizedImage , 0 , 0 , $ transparent_color );
73
- }
74
- }
53
+ $ this ->prepareImageForTransparency ($ gdImage , $ resizedImage , $ upload );
75
54
76
55
// Perform the image resizing operation
77
- imagecopyresampled ($ resizedImage , $ gdImage , 0 , 0 , 0 , 0 , $ width , $ height , $ source_width , $ source_height );
56
+ imagecopyresampled (
57
+ $ resizedImage ,
58
+ $ gdImage ,
59
+ 0 ,
60
+ 0 ,
61
+ 0 ,
62
+ 0 ,
63
+ $ new_width ,
64
+ $ new_height ,
65
+ $ source_width ,
66
+ $ source_height
67
+ );
78
68
}
79
-
80
- // save copy of image
69
+
70
+ // Save a copy of the resized image
81
71
$ this ->saveImage (
82
72
$ resizedImage ,
83
- $ upload ['fullPath ' ]
73
+ $ upload ['fullPath ' ],
74
+ 60
84
75
);
85
76
86
- // run bucket method
77
+ // Run bucket method for further processing
87
78
$ this ->bucket ($ upload );
88
79
}
89
80
});
90
- }
81
+ }
91
82
92
83
/**
93
84
* Automatically adjusts image size to fit into the provided dimensions.
94
85
*
95
- * @param string $imageSource The path to the image file .
96
- * @param int $width The desired width of the image .
97
- * @param int $height The desired height of the image .
86
+ * @param array $imageSource An array with original width and height .
87
+ * @param int|null $max_width The maximum desired width.
88
+ * @param int|null $max_height The maximum desired height.
98
89
*
99
- * @return array
90
+ * @return array
100
91
* - An array containing the adjusted width and height of the image.
101
92
*/
102
- private function imageAspectRatio ($ imageSource , $ width , $ height )
93
+ private function imageAspectRatio (array $ imageSource , $ max_width , $ max_height )
103
94
{
104
- // Get the original size of the image
105
- list ($ original_width , $ original_height ) = $ imageSource ;
95
+ [$ original_width , $ original_height ] = $ imageSource ;
106
96
107
- // Calculate the aspect ratio of the original image
97
+ // Aspect ratio of the original image
108
98
$ aspect_ratio = $ original_width / $ original_height ;
109
99
110
- // If both width and height are provided, adjust the image to fit
111
- if ($ width && $ height ) {
112
- // Calculate the aspect ratio of the desired dimensions
113
- $ desired_aspect_ratio = $ width / $ height ;
114
-
115
- // If the original aspect ratio is wider than the desired aspect ratio, adjust the width
116
- if ($ aspect_ratio > $ desired_aspect_ratio ) {
117
- $ height = $ width / $ aspect_ratio ;
118
- }
119
- // If the original aspect ratio is taller than the desired aspect ratio, adjust the height
120
- else {
121
- $ width = $ height * $ aspect_ratio ;
100
+ if ($ max_width && $ max_height ) {
101
+ // Adjust based on the more restrictive dimension
102
+ if (($ max_width / $ max_height ) > $ aspect_ratio ) {
103
+ $ max_width = (int ) round ($ max_height * $ aspect_ratio );
104
+ } else {
105
+ $ max_height = (int ) round ($ max_width / $ aspect_ratio );
122
106
}
107
+ } elseif ($ max_width ) {
108
+ $ max_height = (int ) round ($ max_width / $ aspect_ratio );
109
+ } elseif ($ max_height ) {
110
+ $ max_width = (int ) round ($ max_height * $ aspect_ratio );
111
+ } else {
112
+ $ max_width = $ original_width ;
113
+ $ max_height = $ original_height ;
123
114
}
124
- // If only width is provided, adjust the height to maintain aspect ratio
125
- elseif ($ width ) {
126
- $ height = $ width / $ aspect_ratio ;
127
- }
128
- // If only height is provided, adjust the width to maintain aspect ratio
129
- elseif ($ height ) {
130
- $ width = $ height * $ aspect_ratio ;
131
- }
132
- // If no dimensions are provided, return the original size of the image
133
- else {
134
- return array (
135
- $ original_width ,
136
- $ original_height
137
- );
138
- }
139
115
140
- // Return the adjusted dimensions as an array
141
- return array (
142
- (int ) round ($ width ),
143
- (int ) round ($ height )
144
- );
116
+ return [$ max_width , $ max_height ];
117
+ }
118
+
119
+ /**
120
+ * Prepare image for transparency support if applicable.
121
+ *
122
+ * @param \GdImage $gdImage The original GD image.
123
+ * @param \GdImage $resizedImage The destination GD image.
124
+ * @param array $upload The upload data.
125
+ *
126
+ * @return void
127
+ */
128
+ private function prepareImageForTransparency ($ gdImage , $ resizedImage , $ upload )
129
+ {
130
+ $ imageExtension = pathinfo ($ upload ['fullPath ' ], PATHINFO_EXTENSION );
131
+
132
+ if ($ imageExtension === 'png ' ) {
133
+ $ sourceHasTransparency = $ this ->isImageTransparent ($ gdImage );
134
+
135
+ imagesavealpha ($ resizedImage , true );
136
+
137
+ if (!$ sourceHasTransparency ) {
138
+ $ background_color = $ this ->getBackgroundColor ($ gdImage );
139
+ $ background_color = imagecolorallocate ($ resizedImage , $ background_color [0 ], $ background_color [1 ], $ background_color [2 ]);
140
+ imagefill ($ resizedImage , 0 , 0 , $ background_color );
141
+ } else {
142
+ $ transparent_color = imagecolorallocatealpha ($ resizedImage , 0 , 0 , 0 , 127 );
143
+ imagefill ($ resizedImage , 0 , 0 , $ transparent_color );
144
+ }
145
+ }
145
146
}
146
-
147
147
}
0 commit comments