Skip to content

Commit 232d6a8

Browse files
author
Fredrick Peter
committed
version upgrade and internal error fix
1 parent fe16c32 commit 232d6a8

14 files changed

+331
-260
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [First](#first)
2121
* [Get](#get)
2222
* [Form](#form)
23+
* [Unlink](#unlink)
2324
* [Usage](#usage)
2425
* [INPUT HTML STRUCTURE](#input-html-structure)
2526
* [Driver](#driver)
@@ -134,8 +135,7 @@ config_file(
134135
'limit' => 1,
135136
'mime' => 'images', // video|audio|files|images|general_image|general_media|general_file
136137
'size' => 2097152, // 2mb
137-
'baseUrl' => domain(),
138-
'baseDir' => base_path(),
138+
'baseDir' => 'public',
139139
'driver' => 'local',
140140
'structure' => 'default', // default|year|month|day
141141
'generate' => true, // will always generate a unique() name for each uploaded file
@@ -234,6 +234,22 @@ $upload->get();
234234
});
235235
```
236236

237+
### Unlink
238+
- Accepts two param as string. This method uses the base path automatically.
239+
- [mandatory] `$fileToUnlink` Path to file, you want to unlink.
240+
- [optional] `$checkFile`. This will check if `$fileToUnlink` is not same, before.
241+
242+
```
243+
->save(function($response){
244+
245+
$userAvatar;
246+
247+
$response->unlink("public/images/{$userAvatar}", "avatar.svg");
248+
249+
<!-- the above will only unlink when value is not avatar.svg -->
250+
});
251+
```
252+
237253
## Usage
238254

239255
### INPUT HTML STRUCTURE

src/File.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ static public function name($name = null)
7373
/**
7474
* Begin Upload Validation
7575
*
76-
* @param callable $function
76+
* @param Closure|null $closure
7777
* @return $this
7878
*/
79-
public function validate(callable $function = null)
79+
public function validate($closure = null)
8080
{
8181
$this->proceedToValidate();
8282

8383
if(!$this->success){
84-
$this->callback($function);
84+
$this->callback($closure);
8585
}
8686

8787
return $this;
@@ -90,10 +90,10 @@ public function validate(callable $function = null)
9090
/**
9191
* Begin Upload Confirmation
9292
*
93-
* @param callable $function
93+
* @param Closure|null $closure
9494
* @return $this
9595
*/
96-
public function save(callable $function = null)
96+
public function save($closure = null)
9797
{
9898
$this->ignoreIfValidatorHasBeenCalled();
9999

@@ -103,7 +103,7 @@ public function save(callable $function = null)
103103
}
104104

105105
$this->proceedToSave()
106-
->callback($function);
106+
->callback($closure);
107107
}
108108

109109
return $this;
@@ -221,6 +221,8 @@ public function folder($folder)
221221
* Set Folder Structure Creation
222222
*
223223
* @param string $structure
224+
* - Keys [default, year, month, day]
225+
*
224226
* @return $this
225227
*/
226228
public function structure($structure)
@@ -420,6 +422,23 @@ public function form()
420422
->all();
421423
}
422424

425+
/**
426+
* Unlink File from Server
427+
*
428+
* @param string $fileToUnlink
429+
* @param string|null $checkFile
430+
* [optional] File to check against before unlinking
431+
*
432+
* @return void
433+
*/
434+
static public function unlink(string $fileToUnlink, $checkFile = null)
435+
{
436+
Tame::unlinkFile(
437+
base_path($fileToUnlink),
438+
$checkFile
439+
);
440+
}
441+
423442
/**
424443
* Echo `json_encode` with response and message
425444
*

src/FileStorage.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ private function migrate()
130130
// new generated file name
131131
$generateName = $this->file->generate($config['generate']);
132132

133+
// create base directory if not exist
134+
$this->createBaseDirectory();
135+
133136
// Create parent storage folders
134137
$this->createParentFolder($this->folder);
135138

@@ -145,7 +148,6 @@ private function migrate()
145148
'driver' => $this->config['driver'],
146149
]);
147150
}
148-
149151

150152
/**
151153
* Get Class Associated with drivers, for selected Cloud storage

src/ImageAutoresize.php

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public function resize($width = null, $height = null)
3131

3232
// GdImage object
3333
$gdImage = $response->createGDImage($imageSource, $upload['fullPath']);
34+
35+
// if not an instance of GdImage
36+
if(!$gdImage instanceof \GdImage){
37+
return;
38+
}
3439

3540
// Get the dimensions of the source image.
3641
$source_width = imagesx($gdImage);
@@ -44,13 +49,39 @@ public function resize($width = null, $height = null)
4449

4550
// Check if the destination image resource was created successfully
4651
if ($resizedImage) {
47-
$white_color = imagecolorallocate($resizedImage, 255, 255, 255);
48-
imagefill($resizedImage, 0, 0, $white_color);
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+
}
75+
76+
// Perform the image resizing operation
4977
imagecopyresampled($resizedImage, $gdImage, 0, 0, 0, 0, $width, $height, $source_width, $source_height);
5078
}
5179

5280
// save copy of image
53-
$this->saveImage($resizedImage, $upload['fullPath']);
81+
$this->saveImage(
82+
$resizedImage,
83+
$upload['fullPath']
84+
);
5485

5586
// run bucket method
5687
$this->bucket($upload);
@@ -112,41 +143,5 @@ private function imageAspectRatio($imageSource, $width, $height)
112143
(int) round($height)
113144
);
114145
}
115-
116-
/**
117-
* Save an image resource to a file
118-
*
119-
* @param mixed $imageSource The image resource to save
120-
* @param string $filePath The path to save the image
121-
*
122-
* @return void
123-
*/
124-
private function saveImage($imageSource, $filePath)
125-
{
126-
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
127-
switch ($extension) {
128-
case 'jpg':
129-
case 'jpeg':
130-
// Save as JPEG
131-
@imagejpeg($imageSource, $filePath);
132-
break;
133-
case 'png':
134-
// Save as PNG
135-
@imagepng($imageSource, $filePath);
136-
break;
137-
case 'gif':
138-
// Save as GIF
139-
@imagegif($imageSource, $filePath);
140-
break;
141-
case 'webp':
142-
// Save as WebP
143-
@imagewebp($imageSource, $filePath);
144-
break;
145-
}
146-
147-
// Free up memory.
148-
imagedestroy($imageSource);
149-
}
150-
151146

152147
}

src/ImageCompress.php

Lines changed: 10 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -30,92 +30,23 @@ public function compress($quality = 50)
3030

3131
// GdImage object
3232
$gdImage = $response->createGDImage($imageSource, $upload['fullPath']);
33+
34+
// if not an instance of GdImage
35+
if(!$gdImage instanceof \GdImage){
36+
return;
37+
}
3338

3439
// save copy of image
35-
$this->saveImage($gdImage, $upload['fullPath'], $quality);
40+
$this->saveImage(
41+
$gdImage,
42+
$upload['fullPath'],
43+
$quality
44+
);
3645

3746
// run bucket method
3847
$this->bucket($upload);
3948
}
4049
});
4150
}
42-
43-
/**
44-
* Save an image resource to a file
45-
*
46-
* @param mixed $gdImage The image resource to save
47-
* @param string $filePath The path to save the image
48-
* @param int $quality The image quality (0-100, applicable for JPEG and WebP)
49-
*
50-
* @return void
51-
*/
52-
public function saveImage($gdImage, $filePath, $quality = 50)
53-
{
54-
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
55-
switch ($extension) {
56-
case 'jpg':
57-
case 'jpeg':
58-
// Save as JPEG
59-
@imagejpeg($gdImage, $filePath, 50);
60-
break;
61-
case 'png':
62-
// Save as PNG
63-
$this->allowBlending($gdImage);
64-
$value = (int) round(9 * (100 - $quality) / 90);
65-
@imagepng($gdImage, $filePath, $value);
66-
break;
67-
case 'gif':
68-
// Save as GIF
69-
$this->allowBlending($gdImage);
70-
@imagegif($gdImage, $filePath);
71-
break;
72-
case 'webp':
73-
// Save as WebP
74-
$this->allowBlending($gdImage);
75-
@imagewebp($gdImage, $filePath, $quality);
76-
break;
77-
}
78-
79-
// Free up memory.
80-
imagedestroy($gdImage);
81-
}
82-
83-
/**
84-
* Apply blending mode
85-
*
86-
* @param mixed $gdImage The image resource to save
87-
* @return void
88-
*/
89-
private function allowBlending($gdImage)
90-
{
91-
if($this->isImageTransparent($gdImage)){
92-
imageAlphaBlending($gdImage, true);
93-
imageSaveAlpha($gdImage, true);
94-
}
95-
}
96-
97-
/**
98-
* Check if image is transparent
99-
*
100-
* @param mixed $im
101-
* @return bool
102-
*/
103-
private function isImageTransparent($gdImage)
104-
{
105-
$width = imagesx($gdImage); // Get the width of the image
106-
$height = imagesy($gdImage); // Get the height of the image
107-
108-
// We run the image pixel by pixel and as soon as we find a transparent pixel we stop and return true.
109-
for($i = 0; $i < $width; $i++) {
110-
for($j = 0; $j < $height; $j++) {
111-
$rgba = imagecolorat($gdImage, $i, $j);
112-
if(($rgba & 0x7F000000) >> 24) {
113-
return true;
114-
}
115-
}
116-
}
117-
118-
return false;
119-
}
12051

12152
}

src/ImageWatermark.php

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

55
namespace Tamedevelopers\File;
66

7-
use Tamedevelopers\File\ImageCompress;
7+
use Tamedevelopers\Support\Tame;
88
use Tamedevelopers\File\Traits\CommonTrait;
99
use Tamedevelopers\File\Traits\FileMagicTrait;
1010

@@ -23,18 +23,23 @@ class ImageWatermark {
2323
*
2424
* @param int $padding Padding in pixels (applied to all positions except 'center')
2525
*
26-
* @return bool True if the watermark was successfully added, false otherwise.
26+
* @return void
2727
*/
2828
public function watermark($watermarkSource = null, $position = 'bottom-right', $padding = 15)
2929
{
30-
$this->loop(function($response) use($watermarkSource, $position, $padding){
30+
$this->loop(function($response) use($watermarkSource, $position, $padding) {
3131
foreach($response->uploads as $upload){
3232

3333
// resource
3434
$imageSource = $this->getImageResource($upload);
3535

3636
// GdImage object
3737
$gdImage = $response->createGDImage($imageSource, $upload['fullPath']);
38+
39+
// if not an instance of GdImage
40+
if(!$gdImage instanceof \GdImage){
41+
return;
42+
}
3843

3944
// Get the dimensions of the source image.
4045
$source_width = imagesx($gdImage);
@@ -43,6 +48,11 @@ public function watermark($watermarkSource = null, $position = 'bottom-right', $
4348
// full path to watermark image
4449
$watermarkImage = base_path($watermarkSource);
4550

51+
// if watermark file doesn't exists
52+
if(!Tame::exists($watermarkImage)){
53+
return;
54+
}
55+
4656
// watermark GdImage object
4757
$watermarkGDImage = $response->createGDImage(getimagesize($watermarkImage), $watermarkImage);
4858

@@ -64,7 +74,7 @@ public function watermark($watermarkSource = null, $position = 'bottom-right', $
6474

6575
// save image to path
6676
if($check){
67-
(new ImageCompress)->saveImage(
77+
$this->saveImage(
6878
gdImage: $gdImage,
6979
filePath: $upload['fullPath']
7080
);

0 commit comments

Comments
 (0)