Logo

dev-resources.site

for different kinds of informations.

Convert images in Webp

Published at
11/7/2023
Categories
laravel
php
images
webp
Author
mawidev
Categories
4 categories in total
laravel
open
php
open
images
open
webp
open
Author
7 person written this
mawidev
open
Convert images in Webp

Allow users to upload an image in JPEG, PNG, JPG, or GIF format, convert it to the WebP format, and save it on your server.

This code performs a series of operations:

  • It checks whether the user has uploaded a valid image.
  • It generates a unique name for the WebP image based on the current time.
  • It converts the uploaded image into an image object.
  • If the conversion is successful, the image is converted to the WebP format and saved on the server.
  • The name of the WebP file is saved in the database.
  • In case of an error, the user receives an error message.
try {
    // Validation for the uploaded image
    $request->validate([
        'img_path' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048'
    ]);

    if ($request->hasFile('img_path')) {
        // Get the uploaded image
        $image = $request->file('img_path');

        // Generate a unique name for the WebP file based on the current time
        $imageName = time() . '.webp';

        // Path where the WebP file will be saved
        $webpPath = public_path('storage/images/' . $imageName);

        // Create an image object from binary data
        $imageResource = imagecreatefromstring(file_get_contents($image->path()));

        // Check if the conversion was successful
        if ($imageResource === false) {
            throw new \Exception("Error during image conversion.");
        }

        // Convert and save the image in WebP format
        if (!imagewebp($imageResource, $webpPath, 90)) {
            throw an \Exception("Error during WebP image saving.");
        }

        // Save the WebP file name in the database
        $imgModel = new Image();
        $imgModel->img_path = $imageName;
        $imgModel->save();

        return redirect()->back();
    }
} catch (\Exception $e) {
    // In case of an error, display an error message to the user
    return redirect()->back()->with('error', $e->getMessage());
}

Enter fullscreen mode Exit fullscreen mode
images Article's
30 articles in total
Favicon
Proxy de imΓ‘genes.
Favicon
Deploying Mancala with Minikube β€” A beginners guide
Favicon
Collect All Requested Images on a Website Using Puppeteer
Favicon
I’m Building a Website Builder Tool, and I Created a Simple Placeholder Image Service Along the Way
Favicon
Enhancing Data Collection Images AI Accuracy and Performance
Favicon
Dynamic watermarking on the JVM
Favicon
Understanding Dockerfile: The Blueprint of Docker Containers and Images
Favicon
A simple Image to text website built with Next.js
Favicon
Error while loading images in Oracle APEX application runtime
Favicon
Base 64
Favicon
Alpaca image generator website Built with ReactJS
Favicon
Use your own neural net to generate images
Favicon
How to calculate RGB values in Python
Favicon
More Fun with AI Generated Images
Favicon
Useful websites for stock images
Favicon
Optimizing Images for Enhanced Website Performance: A Free Tool for Your Website
Favicon
RuGiVi
Favicon
A Simple Approach to LCP Image Optimization with TwicPics Components
Favicon
Fix: Opencart cache images not generated
Favicon
Keeping your fonts in embedded SVG
Favicon
Upload the image with a preview using HTML, CSS & JavaScript
Favicon
Handling Image Uploads in Node with Multer and Cloudinary
Favicon
Before and after image slider in pure CSS
Favicon
Product Page Images Grid
Favicon
Convert images in Webp
Favicon
🎁 Collection of free illustrations for OpenGraph images
Favicon
Create High Quality Images And Videos With GenmoAI
Favicon
Instantly adds nice compression artifacts to your JPEG images
Favicon
React Image Pan And Zoom With Commenting
Favicon
Bring back the Twitter link preview

Featured ones: