Magento Tutorials

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.

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 ?

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

18 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 weeks ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago