77use Tamedevelopers \File \Traits \CommonTrait ;
88use Tamedevelopers \File \Traits \FileMagicTrait ;
99
10-
11- class ImageAutoresize {
12-
10+ class ImageAutoresize {
1311 use FileMagicTrait, CommonTrait;
1412
1513 /**
@@ -18,130 +16,132 @@ class ImageAutoresize {
1816 * @param int|null $width The desired width of the resized image (in pixels).
1917 * @param int|null $height The desired height of the resized image (in pixels).
2018 *
21- * @return bool
19+ * @return bool
2220 * - Returns true on success or false on failure.
2321 */
2422 public function resize ($ width = null , $ height = null )
2523 {
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
3027 $ imageSource = $ this ->getImageResource ($ upload );
3128
3229 // GdImage object
3330 $ gdImage = $ response ->createGDImage ($ imageSource , $ upload ['fullPath ' ]);
3431
35- // if not an instance of GdImage
36- if (!$ gdImage instanceof \GdImage){
32+ // If not an instance of GdImage
33+ if (!$ gdImage instanceof \GdImage) {
3734 return ;
3835 }
39-
36+
4037 // Get the dimensions of the source image.
4138 $ source_width = imagesx ($ gdImage );
4239 $ 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+ );
4647
4748 // Create a new image with the adjusted dimensions
48- $ resizedImage = @imagecreatetruecolor ($ width , $ height );
49+ $ resizedImage = @imagecreatetruecolor ($ new_width , $ new_height );
4950
5051 // Check if the destination image resource was created successfully
5152 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 );
7554
7655 // 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+ );
7868 }
79-
80- // save copy of image
69+
70+ // Save a copy of the resized image
8171 $ this ->saveImage (
8272 $ resizedImage ,
83- $ upload ['fullPath ' ]
73+ $ upload ['fullPath ' ],
74+ 60
8475 );
8576
86- // run bucket method
77+ // Run bucket method for further processing
8778 $ this ->bucket ($ upload );
8879 }
8980 });
90- }
81+ }
9182
9283 /**
9384 * Automatically adjusts image size to fit into the provided dimensions.
9485 *
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.
9889 *
99- * @return array
90+ * @return array
10091 * - An array containing the adjusted width and height of the image.
10192 */
102- private function imageAspectRatio ($ imageSource , $ width , $ height )
93+ private function imageAspectRatio (array $ imageSource , $ max_width , $ max_height )
10394 {
104- // Get the original size of the image
105- list ($ original_width , $ original_height ) = $ imageSource ;
95+ [$ original_width , $ original_height ] = $ imageSource ;
10696
107- // Calculate the aspect ratio of the original image
97+ // Aspect ratio of the original image
10898 $ aspect_ratio = $ original_width / $ original_height ;
10999
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 );
122106 }
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 ;
123114 }
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- }
139115
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+ }
145146 }
146-
147147}
0 commit comments