Then, open Postman and set the request URL to http:/127.0.0.1:8000/api/image-compress and change the HTTP method from GET to POST. We are expecting a request as an API, so we will add our controller route in routes/api.php, as in the example below. In this section, we will create a route that will connect with the controller for processing and compressing the image. This will allow the application to store the images there. Now, create a new folder named uploads in the public folder. The image is then resized to a width and height of 150.The path of the image is retrieved using the getRealPath() function.A image validation rule is created, which ensures that uploaded images are no larger than 2048 bytes, and are limited to JPEG, GIF, PNG, and SVG files.In the above code, we notice a couple of things: This is important because the functions required to perform adjustments on images can only be accessed in the Facade module.Īfter that, we can write the functionality to compress and upload images in the compressImage() function. The code imports the Intervention/image Facade in order to use it to retrieve the image and resize it. To start, open a terminal and input the command below to scaffold a Laravel application. Scaffold a Laravel applicationįor the sake of this tutorial, we will be using the Intervention PHP library and the I ntervention/image package with Laravel to upload an image and compress it. The Intervention package combines these two popular libraries and makes image compression easier to understand and implement in a Laravel project. iMagick can create, edit, and compose images and has support for a wide range of images, including JPG, PNG, GIF, and SVG. The GD library allows you to get an image's type and file size, manipulate an image's pixels, and even generate a new image. In Laravel, images are read and manipulated using popular libraries such as GD and iMagick, and other libraries depending on the image type and the functionality you want to have. How does Laravel handle image compression? Here is an article that explains further using examples to show the significant drop in performance for an application that doesn’t compress images. Compressed images help solve this problem as they require less load time. This contributes to an increase in page load time and slow responses. Reduced load time: Assets like images require more time to load in comparison to JSON or text files.With compression, the need is reduced as images can be spliced into half or less their initial storage size. By implementing an image compression algorithm, the cost is cut down as image sizes are significantly reduced. Image upload, over time, builds into a large consumption of storage allocated to run the application and in turn, requires more storage in order for the application to run smoothly. Reduced storage cost: Facebook spends millions of dollars on storage, yearly, in order to store information, a bulk of which is images.This drives a lot of businesses to use third-party servers for storage to reduce the load on production servers because of two key reasons: The importance of reducing storage costs of data/image storage has always been a smart business move. When building a platform that will be accessed by many users, it’s most recommended to use lossy compression against lossless. This isn't an issue for images like display pictures, and this method is used in numerous technologies and products like Meta and its multiple services. Lossy compression can greatly compress images but information will be lost about the image due to the sizing being reduced during compression, For example: the width x height of the image is reduced. Lossless compression comes in handy when the original image data is essential for referential or editing purposes. Lossless compression An algorithm that allows an image to be reduced in size without losing quality. There are two forms of image compression: lossless and lossy. Reducing a file's size reduces both storage and transmission bandwidth requirements. To further break it down, compressing a digital image reduces the number of bytes without degrading the image's quality. Prior experience with Laravel and some command-line experienceĪccording to Wikipedia, image compression is: a type of data compression applied to images to reduce their cost of storage and transmission.In this tutorial, you're going to learn how to compress uploaded images in Laravel using the Intervention/image package. However, by using image compression you can reduce both your data usage and storage needs, as it lets you store image information in a much smaller size! 32 - a/src/MessageHandler/CommentMessageHandler.php +++ b/src/MessageHandler/CommentMessageHandler.Data usage continues to grow while the cost of file storage continuously decreases.
0 Comments
Leave a Reply. |