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 ?
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.
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.
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 ?
Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…
In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…
October was an exciting month for MageComp! From significant updates across our Magento 2 extension…
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…