Hello i want to compress an image in laravel but i keep getting error, this is my code.
I have made this code in the resource:
'images' => $this->convertToBase64($this->getImages($this->coordination_id)->pluck('image')) ?? '', by this code i get the name of the image example file/name.jpeg
Then at convertToBase64 i do this code:
public function convertToBase64($imagePaths)
{
$encodedImages = [];
if ($imagePaths) {
foreach ($imagePaths as $image) {
$imagePath = Storage::get('public/' . $image);
$compressedImagePath = Helpers::compressImage($imagePath, 'files/', 80);
$encodedImage = base64_encode(Storage::get('public/' . $compressedImagePath));
$encodedImages[] = $encodedImage;
break;
}
}
return $encodedImages;
}
And for the compressImage i have this code from internet that i found:
public static function compressImage($image, $path, $quality)
{
$fileName = time().'_'.$image->getClientOriginalName();
$compressedImage = Image::make($image)
->encode('jpg', $quality);
Storage::disk('public')->put($path . $fileName, $compressedImage);
$completedPath = $path . $fileName;
return $completedPath;
}
The compressImage code works well no need to be changed but the code above is new and is making the error:
"message": "Call to a member function getClientOriginalName() on string",
"exception": "Error",```