{"id":2024,"date":"2009-08-07T09:54:22","date_gmt":"2009-08-07T07:54:22","guid":{"rendered":"http:\/\/blog.ginchen.de\/?p=2024"},"modified":"2012-02-04T01:09:22","modified_gmt":"2012-02-04T00:09:22","slug":"png-transparenzen-erhalten-mit-gd-lib","status":"publish","type":"post","link":"http:\/\/blog.ginchen.de\/en\/2009\/08\/07\/png-transparenzen-erhalten-mit-gd-lib\/","title":{"rendered":"Preserve PNG transparency with GD Lib"},"content":{"rendered":"<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.ginchen.de\/wp-content\/uploads\/2009\/08\/rotverlauf.png\" alt=\"Semi-transparent PNG\" title=\"Semi-transparent PNG\" width=\"150\" height=\"150\" class=\"alignright size-full wp-image-2138\" \/>\r\n\r\n<p>Lately I had to write a PHP function that generates differently sized thumbnails from an image with <a rel=\"external\" href=\"http:\/\/www.libgd.org\/\">GD Lib<\/a>. Thereby I had lots of trouble with semi-transparent PNG graphics. Although I found many tips on the internet that pointed me in the right direction, I couldn&#8217;t find a really complete and neat solution. Many people emphasized the oh so big importance of the functions <code>imagealphablending()<\/code> and <code>imagesavealpha()<\/code>, which &#8211; as it later transpired &#8211; were completely meaningless for my purposes.<br \/>\r\nWell, I&#8217;ll write the solution down here as usual, in case somebody else is looking for it as desperately as I did &#8211; or in case I forget it. ;) The pretty red dot here on the right will serve as visualization.<\/p>\r\n\r\n\r\n<!--more-->\r\n\r\n\r\n\r\n\r\n<pre lang=\"php\" line=\"1\">\r\n$filename = \"rotverlauf.png\";\r\n\r\n$oldimg = imagecreatefrompng($filename);\r\n\r\n$oldwidth = imagesx($oldimg);\r\n$oldheight = imagesy($oldimg);\r\n$newwidth = $oldwidth\/2;\r\n$newheight = $oldheight\/2;\r\n\r\n$newimage = imagecreate($newwidth, $newheight);\r\n$color = imagecolorallocatealpha($newimage, 0, 0, 0, 127);\r\nimagefill($newimage, 0, 0, $color);\r\n\r\nimagecopyresampled($newimage, $oldimg, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);\r\n<\/pre>\r\n\r\n<img decoding=\"async\" class=\"alignleft\" src=\"http:\/\/blog.ginchen.de\/wp-content\/uploads\/2009\/08\/gdlib-png\/\" \/>\r\n\r\n<p>The trick is to first create a new image (<code>$newimage<\/code>) and fill it with a transparent background color (<code>$color<\/code>). If you now copy the original image into it with the help of <code>imagecopyresampled()<\/code>, the transparency will be preserved.<br \/>\r\nThe example script reduces the red dot to half its size. The result can be seen on the left.<\/p>\r\n\r\n<p>But I had yet another problem: I wanted to save the thumbnails of PNG graphics in the JPEG format, too, i.&nbsp;e. put them on a white or black background, while the color gradient should of course be preserved. After a few unsuccessful attempts, the solution turned out to be quite simple. ;) You just have to replace the functions <code>imagecreate()<\/code> by <code>imagecreatetruecolor()<\/code> and <code>imagecolorallocatealpha()<\/code> by <code>imagecolorallocate()<\/code>. Ergo:<\/p>\r\n\r\n<pre lang=\"php\" line=\"10\">\r\n$newimage = imagecreatetruecolor($newwidth, $newheight);\r\n$color = imagecolorallocate($newimage, 255, 255, 255);\r\n<\/pre>\r\n\r\n<img decoding=\"async\" class=\"alignright\" src=\"http:\/\/blog.ginchen.de\/wp-content\/uploads\/2009\/08\/gdlib-png\/?type=jpg\" \/>\r\n\r\n<p>As you can see, I chose white (255,&nbsp;255,&nbsp;255) as background color this time, because it looks better than black in most of the cases.<br \/>\r\nActually I could have used <code>imagecreatetruecolor()<\/code> in the first example, too, when creating a PNG. But some servers (like mine) seem to have problems with that, so you should rather stick with <code>imagecreate()<\/code> when making PNGs.<br \/>\r\nOn the right you can now see the fantastic red dot as JPEG.<\/p>\r\n\r\n<p>Now that the new image is ready, you can either save it to a file:<\/p>\r\n\r\n<pre lang=\"php\">\r\nimagepng($nm, \"new-filename.png\"); \/\/or imagejpeg();\r\n<\/pre>\r\n\r\n<p>or output it directly:<\/p>\r\n\r\n<pre lang=\"php\">\r\nheader(\"Content-type: image\/png\"); \/\/or image\/jpg\r\nimagepng($nm); \/\/or imagejpeg();\r\n<\/pre>\r\n\r\n<ins datetime=\"2009-10-12T16:14:54+00:00\"><h3>Update (12th October 2009)<\/h3>\r\n\r\n<p>It has been brought to my attention that my version doesn&#8217;t seem to work correctly on every server either. If that is the case, you can try to replace the lines 11 and 12 by the following:<\/p>\r\n\r\n<pre lang=\"php\" line=\"11\">\r\n$color = imagecolorallocate($nm, 255, 255, 255);\r\nimagefill($nm, 0, 0, $color);\r\nimagecolortransparent($nm, $color);\r\n<\/pre>\r\n<\/ins>","protected":false},"excerpt":{"rendered":"Lately I had to write a PHP function that generates differently sized thumbnails from an image with GD Lib. Thereby I had lots of trouble with semi-transparent PNG graphics. Although I found many tips on the internet that pointed me in the right direction, I couldn&#8217;t find a really complete and neat solution. Many people [&hellip;]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[98,99,392,122,393],"_links":{"self":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/2024"}],"collection":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/comments?post=2024"}],"version-history":[{"count":5,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/2024\/revisions"}],"predecessor-version":[{"id":5858,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/2024\/revisions\/5858"}],"wp:attachment":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/media?parent=2024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/categories?post=2024"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/tags?post=2024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}