Image Optimization using artisansweb/image-optimizer Package

It has been 5 years since I am running this blog. Along with this blog, I also run a YouTube channel where I share videos to show how to implement my articles.

I believe in open source. On my website, I don’t run any paid membership, or sell ebooks or courses. I like to keep resources free. With that in mind, I created my first GitHub package artisansweb/image-optimizer which does the task of optimizing images for your website.

I wrote a few articles on image optimization in the past. So far TinyPNG was my favorite and still, it is. But the limitation with TinyPNG is they allow optimizing 500 free images per month. For above 500 images, you will charge per image.

Few previous articles on image optimization:

There are other packages available for this purpose. But they require us to install software like JpegOptim, Optipng, etc. on the machine. That is also not a feasible solution.

In the last year, I was working on a client’s project which has thousands of images that needed to compress. We were looking for open source and efficient service which can do our job. Then we came to know about reSmush.it which is a completely free service for optimizing images.

Though I wrote an article on optimizing images with reSmush.it I feel I can create a GitHub package out of it and I did so. If you read my article on reSmush.it, you will find it needs to add around 25-30 lines of code. But if you go for the package, it can be done in 5-6 lines.

That being said, let’s discuss how to install and use artisansweb/image-optimizer package.

Image Optimization using PHP

First, you need to install the package using the Composer by running the command:

composer require artisansweb/image-optimizer

Upon package installation, you can optimize the image using the code below.

<?php
require_once "vendor/autoload.php";
 
use ArtisansWeb\Optimizer;
 
$img = new Optimizer();
 
$source = 'SOURCE_PATH_OF_IMAGE';
$img->optimize($source);

Here, you need to pass the full directory path of the source image, and your source image gets optimized automatically. Sometimes, you need to keep the original image and store the optimized version in another directory. In that case, you can use a second parameter for the destination.

<?php
$source = 'SOURCE_PATH_OF_IMAGE';
$destination = 'DESTINATION_PATH_OF_IMAGE';
$img->optimize($source, $destination);

Using the above code, you can optimize the images available inside your directory. However, I would recommend optimizing the image on the upload so that you don’t need to perform optimization tasks separately.

The user can optimize the image on the upload as follows.

<?php
require_once "vendor/autoload.php";
 
use ArtisansWeb\Optimizer;
 
if (isset($_POST['submit'])) {
    $img = new Optimizer();

    if (!file_exists('images')) {
        mkdir('images', 0777);
    }

    move_uploaded_file($_FILES['file']['tmp_name'], 'images/'.$_FILES['file']['name']);
    $img->optimize('images/'.$_FILES['file']['name']);
    echo "Image compressed successfully.";
}
?>
 
<form method="post" enctype="multipart/form-data">
    <p><input type="file" name="file" /></p>
    <input type="submit" name="submit" value="Submit" />
</form>

Though reSmush.it compressed billions of images so far, it can be possible their service is down for some reason. I handled this scenario in the package. For some reason, if the package does not get the optimized version from reSmush.it, then as a fallback it uses PHP’s native methods like imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif, imagejpeg for optimization.

I will be glad if you use the package and let me know how it is working in the comment section below. Please share your feedback so we together can improve this package which will help the community.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

5 thoughts on “Image Optimization using artisansweb/image-optimizer Package

  1. Works quite well. Maybe you could allow us to select the quality level of the file. Eg: $img->optimize($source, 78);

Leave a Reply

Your email address will not be published. Required fields are marked *