Image Optimization using artisansweb/image-optimizer Package

It has been 4 years now I am running this blog. Along with this blog, I also running a YouTube channel where I share videos which shows how to implement my articles.

I believe in open source. On my website I don’t run any paid membership, 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. Above 500 images, you will charge per image.

Few of previous articles on image optimization:

There are other packages as well, but they require us to install software like JpegOptim, Optipng, etc. on our 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 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 completely free service for optimizing images.

Though I wrote an article on 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

At 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 into 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 your images that are there 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();
    move_uploaded_file($_FILES['file']['tmp_name'], 'images/'.$_FILES['file']['name']);
    $img->optimize('images/'.$_FILES['file']['name']);
}
?>

<form method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <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 optimized version from reSmush.it, then as a fallback it uses PHP’s native methods like imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif, imagejpeg for optimization.

There are several new features in progress which I will add in the package and release it soon.

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.

3 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 *