add image compressor (50%)
This commit is contained in:
@@ -6,6 +6,7 @@ use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\Image\Image;
|
||||
|
||||
class NextCloudHelper
|
||||
{
|
||||
@@ -63,12 +64,11 @@ class NextCloudHelper
|
||||
{
|
||||
$baseUrl = env('NEXT_CLOUD_URL');
|
||||
$username = env('NEXT_CLOUD_USERNAME');
|
||||
$password= env('NEXT_CLOUD_PASSWORD');
|
||||
$password = env('NEXT_CLOUD_PASSWORD');
|
||||
|
||||
// Ensure the folder exists (or create it)
|
||||
$folderResponse = self::createFolder($baseUrl, $username, $password, $folderPath);
|
||||
|
||||
// Check if folder creation was successful or if it already exists
|
||||
if (!$folderResponse['success']) {
|
||||
return [
|
||||
'success' => false,
|
||||
@@ -81,12 +81,37 @@ class NextCloudHelper
|
||||
$url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/') . '/' . $fileName;
|
||||
|
||||
try {
|
||||
// If fileObject is a stream, we can use it directly
|
||||
// If it's raw content, ensure it's being properly passed
|
||||
// Check if the file is an image
|
||||
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, [
|
||||
'auth' => [$username, $password],
|
||||
'body' => $fileObject, // File content or stream
|
||||
'verify' => false // Disable SSL verification (only for testing purposes)
|
||||
'body' => $fileObject,
|
||||
'verify' => false // Disable SSL verification (only for testing purposes)
|
||||
]);
|
||||
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user