Skip to content

Commit a871f81

Browse files
update
1 parent c5c8e46 commit a871f81

File tree

6 files changed

+105
-93
lines changed

6 files changed

+105
-93
lines changed

src/ImageAutoresize.php

Lines changed: 85 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use Tamedevelopers\File\Traits\CommonTrait;
88
use 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
}

src/ImageWatermark.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tamedevelopers\File;
66

7+
use Tamedevelopers\Support\Str;
78
use Tamedevelopers\Support\Tame;
89
use Tamedevelopers\File\Traits\CommonTrait;
910
use Tamedevelopers\File\Traits\FileMagicTrait;
@@ -30,6 +31,10 @@ public function watermark($watermarkSource = null, $position = 'bottom-right', $
3031
$this->loop(function($response) use($watermarkSource, $position, $padding) {
3132
foreach($response->uploads as $upload){
3233

34+
// correctly form position
35+
$position = Str::trim($position);
36+
$position = Str::replace(' ', '-', $position);
37+
3338
// resource
3439
$imageSource = $this->getImageResource($upload);
3540

src/Traits/CommonTrait.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ trait CommonTrait{
1717
private function saveImage($gdImage, $filePath, $quality = 50)
1818
{
1919
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
20+
21+
$quality = (int) $quality;
22+
$quality = $quality < 50 || $quality > 100 ? 50 : $quality;
23+
2024
switch ($extension) {
2125
case 'jpg':
2226
case 'jpeg':
2327
// Save as JPEG
24-
@imagejpeg($gdImage, $filePath, 50);
28+
@imagejpeg($gdImage, $filePath, $quality);
2529
break;
2630
case 'png':
2731
// Save as PNG

src/Traits/FileMagicTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private function bucket($upload)
9090
/**
9191
* loop handler
9292
*
93-
* @param Closure $function
93+
* @param Closure $closure
9494
* @return void
9595
*/
9696
private function loop($closure)

src/Traits/FileStorageTrait.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,12 @@ protected function createTimeBaseFolder($uploadDirectory = null)
181181
"now" => $now
182182
];
183183

184+
$year = "{$this->config['baseDir']}{$uploadDirectory}{$time['year']}";
185+
184186
return [
185-
'year' => "{$this->config['baseDir']}{$uploadDirectory}{$time['year']}",
186-
'month' => "{$this->config['baseDir']}{$uploadDirectory}{$time['year']}/{$time['month']}",
187-
'day' => "{$this->config['baseDir']}{$uploadDirectory}{$time['year']}/{$time['month']}/{$time['day']}",
187+
'year' => "{$year}",
188+
'month' => "{$year}/{$time['month']}",
189+
'day' => "{$year}/{$time['month']}/{$time['day']}",
188190
'now' => $time['now']
189191
];
190192
}

tests/index3.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
->save(function($response){
1616

1717
$response
18-
->watermark('test/watermark.png', 'center', 20)
19-
->resize(100, 200)
20-
->compress();
18+
->resize(400, 400)
19+
->watermark('tests/watermark.png', 'top right', 20)
20+
->compress()
21+
;
2122
});
2223

2324
?>

0 commit comments

Comments
 (0)