How to Convert image png to jpg with Magento 2

How to Convert image png to jpg with Magento 2

Hello Magento Friends ?,

Hope you all are doing well. I am here with a very useful subject, How to Convert image png to jpg with Magento 2. Look back to our former published blog, How to Configure Price Scope in Magento 2. Let’s start with our today’s topic ?

Introduction:

Every E-commerce store is well equipped with different types of images. But it has an adverse impact on the performance of the store if the image is greater in size or not supported. The page load speed increases which in turn increases the bounce rates.

Same way, PNG is an image format that is bigger in size compared to the JPG image format. PNG is also not supported by all web browsers. On the other hand, JPG files are smaller in size in spite of maintaining the quality of the images.

Convert png to jpg is a way out for the above problem. PNG to JPG image convert will improve your store performance without compromising on the quality. Let’s convert png to jpg in Magento 2.

webpimage

Automatically convert image to webp image by installing Magento 2 WebP Image Converter.

How to Convert image ?️ png to jpg with Magento 2:

Follow the below code to convert png to jpg :

public function convert($ext, $path, $name, $newname = NULL)

    {

        $exploded = explode('.', $name);

        $extoriginal = $exploded[sizeof($exploded) - 1];

        switch ($extoriginal) {

            case 'jpg':

            case 'jpeg':

                $image = imagecreatefromjpeg($path . $name);

                break;

            case 'png':

                $image = imagecreatefrompng($path . $name);

                break;

        }

        $bg = imagecreatetruecolor(imagesx($image), imagesy($image));

        imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));

        imagealphablending($bg, TRUE);

        imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));

        imagedestroy($image);

        $quality = 100;

        $newname = ($newname == NULL) ? $exploded[0] : $newname;

        switch ($ext) {

            case 'jpg':

            case 'jpeg':

                $newimage = $path . $newname . ".jpg";

                imagejpeg($bg, $newimage, $quality);

                break;

            case 'png':

                $newimage = $path . $newname . ".png";

                imagepng($bg, $newimage, $quality);

                break;

        }

        imagedestroy($bg);

        return $newimage;

    }

That’s it.

Final Say:

Therefore, with the afore-mentioned code, you can successfully convert png to jpg image in Magento 2. Enrich your store with WebP Image Extension for Magento 2 to provide better quality images with reduced page loading. The module supports  WebP images format in product pages, category pages, and CMS pages.

Have questions? Mention in the comment section and I will be right back to you. Share with your Developer friends and stay connected!

Happy Converting ?

Previous Article

How to add Stock Status column in Admin Product Grid Magento 2

Next Article

Accelerate Your Move With Magento 2 Migration Loan by LoanBuilder

Write a Comment

Leave a Comment

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

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨