{"id":245,"date":"2012-05-14T17:36:23","date_gmt":"2012-05-14T16:36:23","guid":{"rendered":"http:\/\/blog.invoiceberry.com\/?p=245"},"modified":"2016-08-09T08:36:43","modified_gmt":"2016-08-09T06:36:43","slug":"convert-png-and-gif-pictures-to-jpeg-pictures-with-php","status":"publish","type":"post","link":"https:\/\/www.invoiceberry.com\/blog\/convert-png-and-gif-pictures-to-jpeg-pictures-with-php\/","title":{"rendered":"Convert PNG and GIF pictures to JPEG pictures with PHP"},"content":{"rendered":"<p>A lot of websites offer users to upload avatars, company logos or maybe just a picture that\u00a0the user has taken. The most common picture formats are normally JPEG, PNG and GIF.<\/p>\n<p>But how can you convert a GIF or PNG image from one of your website visitors into a JPEG image to store it on your webserver?<\/p>\n<p>Below is a short and easy guide to convert PNG and GIF images into the JPEG format with the help of PHP.<\/p>\n<p>First, set a directory where the image should be stored and find out the file extension:<\/p>\n<pre> \/\/directory where your uploaded files are stored\r\n $uploads_directory = \"\/your\/directory\/where\/you\/want\/to\/save\/your\/images\/\"\r\n\r\n\/\/check extension of the file\r\n $str = $_FILES['file']['name'];\r\n $i = strrpos($str,\".\");\r\n if (!$i) { $BAD .= \"An error occured.\"; }\r\n $l = strlen($str) - $i;\r\n $ext = substr($str,$i+1,$l);\r\n\r\n\/\/directory + file name of the image\r\n $upload_filename = $uploads_directory. \"new_image.\". $ext \u00a0;<\/pre>\n<p>&nbsp;<\/p>\n<p>Then run the following code if the file extension is PNG or GIF:<\/p>\n<pre>\/\/check file extension\r\n if ($ext == \"png\" || $ext == \"gif\"){\r\n\r\n\/\/new file name once the picture is converted\r\n $converted_filename = $uploads_directory. \"new_converted_image.jpg\";\r\n\r\nif ($ext==\"png\") $new_pic = imagecreatefrompng($upload_filename);\r\n if ($ext==\"gif\") $new_pic = imagecreatefromgif($upload_filename);\r\n\r\n\/\/ Create a new true color image with the same size\r\n $w = imagesx($new_pic);\r\n $h = imagesy($new_pic);\r\n $white = imagecreatetruecolor($w, $h);\r\n\r\n\/\/ Fill the new image with white background\r\n $bg = imagecolorallocate($white, 255, 255, 255);\r\n imagefill($white, 0, 0, $bg);\r\n\r\n\/\/ Copy original transparent image onto the new image\r\n imagecopy($white, $new_pic, 0, 0, 0, 0, $w, $h);\r\n\r\n$new_pic = $white;\r\n\r\nimagejpeg($new_pic, $converted_filename);\r\n imagedestroy($new_pic);\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Let us know if you need any help implementing this feature on your website. One of our web developers will be happy to help you!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A lot of websites offer users to upload avatars, company logos or maybe just a picture that\u00a0the user has taken. The most common picture formats are normally JPEG, PNG and GIF. But how can you convert a GIF or PNG image from one of your website visitors into a JPEG image to store it on [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":4564,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":""},"categories":[27],"tags":[46,28],"jetpack_featured_media_url":"https:\/\/i2.wp.com\/www.invoiceberry.com\/blog\/wp-content\/uploads\/2012\/05\/convert_gif_png_into_jpeg_with_php.png?fit=1380%2C580&ssl=1","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5m6q6-3X","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/posts\/245"}],"collection":[{"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":10,"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":5274,"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/posts\/245\/revisions\/5274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/media\/4564"}],"wp:attachment":[{"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.invoiceberry.com\/blog\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}