add image compressor (50%)

This commit is contained in:
Jagad R R
2024-12-31 12:38:14 +07:00
parent 86639eb246
commit 3f7b9d0eb5
3 changed files with 167 additions and 76 deletions
+31 -6
View File
@@ -6,6 +6,7 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Spatie\Image\Image;
class NextCloudHelper class NextCloudHelper
{ {
@@ -63,12 +64,11 @@ class NextCloudHelper
{ {
$baseUrl = env('NEXT_CLOUD_URL'); $baseUrl = env('NEXT_CLOUD_URL');
$username = env('NEXT_CLOUD_USERNAME'); $username = env('NEXT_CLOUD_USERNAME');
$password= env('NEXT_CLOUD_PASSWORD'); $password = env('NEXT_CLOUD_PASSWORD');
// Ensure the folder exists (or create it) // Ensure the folder exists (or create it)
$folderResponse = self::createFolder($baseUrl, $username, $password, $folderPath); $folderResponse = self::createFolder($baseUrl, $username, $password, $folderPath);
// Check if folder creation was successful or if it already exists
if (!$folderResponse['success']) { if (!$folderResponse['success']) {
return [ return [
'success' => false, 'success' => false,
@@ -81,12 +81,37 @@ class NextCloudHelper
$url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/') . '/' . $fileName; $url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/') . '/' . $fileName;
try { try {
// If fileObject is a stream, we can use it directly // Check if the file is an image
// If it's raw content, ensure it's being properly passed if (Str::endsWith($fileName, ['.jpeg', '.png', '.jpg', '.gif', '.svg', '.heic', '.heif'])) {
// Save the uploaded file or raw content to a temporary path
$tempPath = tempnam(sys_get_temp_dir(), 'image_') . '.' . pathinfo($fileName, PATHINFO_EXTENSION);
if (is_string($fileObject)) {
// If fileObject is raw content, write it to the temp file
file_put_contents($tempPath, $fileObject);
} elseif ($fileObject instanceof \Illuminate\Http\UploadedFile) {
// If fileObject is an UploadedFile, move it to the temp file
$fileObject->move(dirname($tempPath), basename($tempPath));
}
// Optimize the image
Image::load($tempPath)
->optimize()
->quality(50)
->save();
// Replace fileObject with the optimized file content
$fileObject = file_get_contents($tempPath);
// Remove the temporary file
unlink($tempPath);
}
// Upload the file
$response = $client->request('PUT', $url, [ $response = $client->request('PUT', $url, [
'auth' => [$username, $password], 'auth' => [$username, $password],
'body' => $fileObject, // File content or stream 'body' => $fileObject,
'verify' => false // Disable SSL verification (only for testing purposes) 'verify' => false // Disable SSL verification (only for testing purposes)
]); ]);
return [ return [
+1 -1
View File
@@ -18,8 +18,8 @@
"nino/laravel-nextcloud-fs": "^2.0", "nino/laravel-nextcloud-fs": "^2.0",
"phpoffice/phpspreadsheet": "^3.5", "phpoffice/phpspreadsheet": "^3.5",
"singlequote/laravel-webdav": "^1.1", "singlequote/laravel-webdav": "^1.1",
"spatie/image": "^3.7",
"spatie/laravel-html": "^3.11", "spatie/laravel-html": "^3.11",
"spatie/laravel-image-optimizer": "^1.8",
"spatie/laravel-menu": "^4.2", "spatie/laravel-menu": "^4.2",
"spatie/laravel-permission": "^6.10", "spatie/laravel-permission": "^6.10",
"yajra/laravel-datatables-oracle": "11.0" "yajra/laravel-datatables-oracle": "11.0"
Generated
+135 -69
View File
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "21b0b8595f584053b4b3e6a9267a2a47", "content-hash": "868009e8371e3394bb5e17f99038a6b8",
"packages": [ "packages": [
{ {
"name": "barryvdh/laravel-dompdf", "name": "barryvdh/laravel-dompdf",
@@ -4856,6 +4856,79 @@
}, },
"time": "2024-06-07T12:41:19+00:00" "time": "2024-06-07T12:41:19+00:00"
}, },
{
"name": "spatie/image",
"version": "3.7.4",
"source": {
"type": "git",
"url": "https://github.com/spatie/image.git",
"reference": "d72d1ae07f91a3c1230e064acd4fd8c334ab237b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/image/zipball/d72d1ae07f91a3c1230e064acd4fd8c334ab237b",
"reference": "d72d1ae07f91a3c1230e064acd4fd8c334ab237b",
"shasum": ""
},
"require": {
"ext-exif": "*",
"ext-json": "*",
"ext-mbstring": "*",
"php": "^8.2",
"spatie/image-optimizer": "^1.7.5",
"spatie/temporary-directory": "^2.2",
"symfony/process": "^6.4|^7.0"
},
"require-dev": {
"ext-gd": "*",
"ext-imagick": "*",
"laravel/sail": "^1.34",
"pestphp/pest": "^2.28",
"phpstan/phpstan": "^1.10.50",
"spatie/pest-plugin-snapshots": "^2.1",
"spatie/pixelmatch-php": "^1.0",
"spatie/ray": "^1.40.1",
"symfony/var-dumper": "^6.4|7.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\Image\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Manipulate images with an expressive API",
"homepage": "https://github.com/spatie/image",
"keywords": [
"image",
"spatie"
],
"support": {
"source": "https://github.com/spatie/image/tree/3.7.4"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
},
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2024-10-07T09:03:34+00:00"
},
{ {
"name": "spatie/image-optimizer", "name": "spatie/image-optimizer",
"version": "1.8.0", "version": "1.8.0",
@@ -4989,74 +5062,6 @@
], ],
"time": "2024-10-18T14:37:21+00:00" "time": "2024-10-18T14:37:21+00:00"
}, },
{
"name": "spatie/laravel-image-optimizer",
"version": "1.8.0",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-image-optimizer.git",
"reference": "024752cba691fee3cd1800000b6aa3da3b8b2474"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-image-optimizer/zipball/024752cba691fee3cd1800000b6aa3da3b8b2474",
"reference": "024752cba691fee3cd1800000b6aa3da3b8b2474",
"shasum": ""
},
"require": {
"laravel/framework": "^8.0|^9.0|^10.0|^11.0",
"php": "^8.0",
"spatie/image-optimizer": "^1.2.0"
},
"require-dev": {
"orchestra/testbench": "^6.23|^7.0|^8.0|^9.0",
"phpunit/phpunit": "^9.4|^10.5"
},
"type": "library",
"extra": {
"laravel": {
"aliases": {
"ImageOptimizer": "Spatie\\LaravelImageOptimizer\\Facades\\ImageOptimizer"
},
"providers": [
"Spatie\\LaravelImageOptimizer\\ImageOptimizerServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Spatie\\LaravelImageOptimizer\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Optimize images in your Laravel app",
"homepage": "https://github.com/spatie/laravel-image-optimizer",
"keywords": [
"laravel-image-optimizer",
"spatie"
],
"support": {
"source": "https://github.com/spatie/laravel-image-optimizer/tree/1.8.0"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
}
],
"time": "2024-02-29T10:55:08+00:00"
},
{ {
"name": "spatie/laravel-menu", "name": "spatie/laravel-menu",
"version": "4.2.0", "version": "4.2.0",
@@ -5328,6 +5333,67 @@
], ],
"time": "2023-04-12T10:02:12+00:00" "time": "2023-04-12T10:02:12+00:00"
}, },
{
"name": "spatie/temporary-directory",
"version": "2.2.1",
"source": {
"type": "git",
"url": "https://github.com/spatie/temporary-directory.git",
"reference": "76949fa18f8e1a7f663fd2eaa1d00e0bcea0752a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/temporary-directory/zipball/76949fa18f8e1a7f663fd2eaa1d00e0bcea0752a",
"reference": "76949fa18f8e1a7f663fd2eaa1d00e0bcea0752a",
"shasum": ""
},
"require": {
"php": "^8.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\TemporaryDirectory\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alex Vanderbist",
"email": "alex@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
}
],
"description": "Easily create, use and destroy temporary directories",
"homepage": "https://github.com/spatie/temporary-directory",
"keywords": [
"php",
"spatie",
"temporary-directory"
],
"support": {
"issues": "https://github.com/spatie/temporary-directory/issues",
"source": "https://github.com/spatie/temporary-directory/tree/2.2.1"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
},
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2023-12-25T11:46:58+00:00"
},
{ {
"name": "spatie/url", "name": "spatie/url",
"version": "2.4.0", "version": "2.4.0",