How To Programmatically Convert PNG To JPG In Magento 2
Magento 2 is an insanely flexible platform. However, at some point, you can face issues like the PNG image format is not supported in the pdf files in Magento 2 store.
I’ll come to that point later, first, let’s understand the difference between JPG and PNG image format and which is the better option.
JPG file format:
- Smaller file size
- Improves the speed performance
- Better for website and sharing
PNG fIle format
- Larger file size
- Lossless image quality
When we talk about Magento 2, the platform doesn’t support the PNG file formats in the PDF files. For example, you want to add the barcode image in the catalog PDF file or invoice PDF file.
The default Magento 2 does not allow the same. Hence, I have come up with a solution to programmatically convert PNG to JPG in Magento 2 store.
Method to programmatically convert PNG to JPG in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
//$ext == 'jpg'; //$path == 'full path of image'; //$name == 'image name with extension'; //$newname == 'newimage'; 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; } |
Any doubts? Do mention them in the Comments section below. I’ll help you out.
Please share the solution with fellow developers via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Thank you for sharing it Sanjay!
Hello Rafael,
Thank you for your appreciation.
Thank You
Hi, Thank you for the code to convert images ext.
Can you please explain how to use the above code for convertation?
Hello Faizan,
You need to place this function where you want to convert and call the function.
Thank You