@loyd here is the code
public static function StoreImage($image, $fieldName, $path)
{
$thumbData = [];
$imageData = [];
if (isset($image)) {
$fileNameWithExt = $image->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extension = $image->getClientOriginalExtension();
$fileNameToStore = time() . '.' . $extension;
$storagePath = storage_path('app/'.$path);
if(!File::isDirectory($storagePath)) { File::makeDirectory($storagePath, 0777, true, true); }
$imageData = self::generateImage($image, $path, $fileNameToStore);
$thumbData = self::generateThumbnail($image, $path, $fileNameToStore);
} else {
return false;
}
return $data = [
'image' => $imageData,
'thumb_image' => $thumbData,
'type' => mime_content_type($storagePath.$fileNameToStore),
'extension' => strtolower($extension),
];
}
private static function generateThumbnail($image, $path, $fileNameToStore)
{
$fileNameToStore = 'thumb_' . $fileNameToStore;
$image = self::createThumbnail($image, $path, $fileNameToStore, 200, 200);
return $data = [
'imageName' => $fileNameToStore,
'path' => $path,
'size' => $image->filesize(),
];
}
public static function createThumbnail($image, $path, $fileNameToStore, $width, $height)
{
$image = Image::make($image)->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
$image->save(storage_path('app/' . $path . $fileNameToStore)); // at this point my page goes blank
return $image;
}
private static function generateImage($image, $path, $fileNameToStore)
{
// $image = Image::make($image);
// $image->save(storage_path('app/' . $path . $fileNameToStore)); // at this point my page goes blank
$image->storeAs($path, $fileNameToStore);
return $data = [
'imageName' => $fileNameToStore,
'path' => $path,
'size' => filesize(storage_path('app/' . $path . $fileNameToStore)), // $image->filesize(),
];
}
and here is the validation for image
'nullable|image|mimes:jpeg,jpg,png|max:2048' // this is not working saying image must be an image
'nullable|mimes:jpeg,jpg,png|max:2048' // this one is working fine