Wednesday, 20 September 2023

laravel make image thumbnail intervantion

 if ($request->hasFile('image')) {
            $ds = DIRECTORY_SEPARATOR;
            $image = $request->file('image');

            // Validate the uploaded image
            $validatedData = $request->validate([
                'image' => 'required|image|max:2048', // Max size in KB (2MB)
            ]);

            try {
                // Store the original image in the public directory
                $imageName = $image->hashName();
                $imagePath = $image->move(public_path('admin' . $ds . 'uploads' . $ds . 'category_images'), $imageName);

                // Create a resized thumbnail (100x100 pixels)
                $thumbnailName = $imageName;
                $thumbnailPath = public_path("admin" . $ds . "uploads" . $ds . "category_thumbnails");
                if (!file_exists($thumbnailPath)) {
                    mkdir($thumbnailPath, 0777, true);
                }

                $thumbnail = Image::make($imagePath)
                    ->fit(100, 100);
                $thumbnail->save($thumbnailPath . "" . $ds . "" . $thumbnailName);

                // Destroy the Intervention Image instance to free up memory
                $thumbnail->destroy();

                $thumbnailPath = $thumbnailPath . "" . $ds . "" . $thumbnailName;

                $validatedData['image'] = $imagePath;
                $validatedData['thumbnail'] = $thumbnailPath;

                return redirect()->back()->with('status', 'Created successfully');
            } catch (\Exception $e) {
                return redirect()->back()->with('status', 'An error occurred while processing the image.');
            }
        }

No comments:

Post a Comment

Laravel Export data to csv

 use Illuminate\Http\Response; // Define a function to export data to CSV function exportToCSV($exportData, $columns) {     $filename = ...