dev-resources.site
for different kinds of informations.
Convert images in Webp
Published at
11/7/2023
Categories
laravel
php
images
webp
Author
mawidev
Author
7 person written this
mawidev
open
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());
}
images Article's
30 articles in total
Proxy de imΓ‘genes.
read article
Deploying Mancala with Minikube β A beginners guide
read article
Collect All Requested Images on a Website Using Puppeteer
read article
Iβm Building a Website Builder Tool, and I Created a Simple Placeholder Image Service Along the Way
read article
Enhancing Data Collection Images AI Accuracy and Performance
read article
Dynamic watermarking on the JVM
read article
Understanding Dockerfile: The Blueprint of Docker Containers and Images
read article
A simple Image to text website built with Next.js
read article
Error while loading images in Oracle APEX application runtime
read article
Base 64
read article
Alpaca image generator website Built with ReactJS
read article
Use your own neural net to generate images
read article
How to calculate RGB values in Python
read article
More Fun with AI Generated Images
read article
Useful websites for stock images
read article
Optimizing Images for Enhanced Website Performance: A Free Tool for Your Website
read article
RuGiVi
read article
A Simple Approach to LCP Image Optimization with TwicPics Components
read article
Fix: Opencart cache images not generated
read article
Keeping your fonts in embedded SVG
read article
Upload the image with a preview using HTML, CSS & JavaScript
read article
Handling Image Uploads in Node with Multer and Cloudinary
read article
Before and after image slider in pure CSS
read article
Product Page Images Grid
read article
Convert images in Webp
currently reading
π Collection of free illustrations for OpenGraph images
read article
Create High Quality Images And Videos With GenmoAI
read article
Instantly adds nice compression artifacts to your JPEG images
read article
React Image Pan And Zoom With Commenting
read article
Bring back the Twitter link preview
read article
Featured ones: