{"id":880,"date":"2020-03-17T07:15:21","date_gmt":"2020-03-17T07:15:21","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/17\/programmatically-convert-png-to-jpg-in-magento-2\/"},"modified":"2025-05-22T14:43:40","modified_gmt":"2025-05-22T09:13:40","slug":"programmatically-convert-png-to-jpg-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-convert-png-to-jpg-in-magento-2\/","title":{"rendered":"How To Programmatically Convert PNG To JPG In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019ll come to that point later, first, let\u2019s understand the&nbsp;<a href=\"https:\/\/www.shutterstock.com\/blog\/jpg-vs-png-vs-pdf\" target=\"_blank\" rel=\"noreferrer noopener\">difference between JPG and PNG image format<\/a>&nbsp;and which is the better option.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JPG file format:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Smaller file size<\/li>\n\n\n\n<li>Improves the speed performance<\/li>\n\n\n\n<li>Better for website and sharing<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">PNG fIle format<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Larger file size<\/li>\n\n\n\n<li>Lossless image quality<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When we talk about Magento 2, the platform doesn\u2019t 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default Magento 2 does not allow the same. Hence, I have come up with a solution to&nbsp;<em><strong>programmatically convert PNG to JPG in Magento 2<\/strong><\/em>&nbsp;store.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to programmatically convert PNG to JPG in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/$ext ==  'jpg';\n\/\/$path == 'full path of image';\n\/\/$name == 'image name with extension';\n\/\/$newname == 'newimage';\n\n\n\npublic function convert($ext, $path, $name, $newname = NULL)\n    {\n        $exploded = explode('.', $name);\n\n        $extoriginal = $exploded[sizeof($exploded) - 1];\n\n        switch ($extoriginal) {\n            case 'jpg':\n            case 'jpeg':\n                $image = imagecreatefromjpeg($path . $name);\n                break;\n            case 'png':\n                $image = imagecreatefrompng($path . $name);\n                break;\n        }\n\n        $bg = imagecreatetruecolor(imagesx($image), imagesy($image));\n        imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));\n        imagealphablending($bg, TRUE);\n        imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));\n        imagedestroy($image);\n        $quality = 100;\n\n        $newname = ($newname == NULL) ? $exploded[0] : $newname;\n\n        switch ($ext) {\n            case 'jpg':\n            case 'jpeg':\n                $newimage = $path . $newname . \".jpg\";\n                imagejpeg($bg, $newimage, $quality);\n                break;\n            case 'png':\n                $newimage = $path . $newname . \".png\";\n                imagepng($bg, $newimage, $quality);\n                break;\n        }\n        imagedestroy($bg);\n        return $newimage;\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please share the solution with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-880","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=880"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/880\/revisions"}],"predecessor-version":[{"id":15025,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/880\/revisions\/15025"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}