markduce Posted December 9, 2011 Share Posted December 9, 2011 Hello, I have the following image on my site, using the Google QR Code api. For reasons that I won't bore you with, I need to get this image in to a JPG format, so need to do this through PHP. The HTML output to the broswer is below <img src="http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=http://www.menuanalyser.co.uk/nutritional-report/11599" alt="QR Code" width="150" height="150"/> The actual source of the image doesn't lead to anywhere though, it only works when it's in the code on the site, you can see it working at: http://www.menuanalyser.co.uk/nutritional-report/11599 I'm at my wits end trying to find how to do this and would really appreciate any help you can offer. I thought it would be something like the following: <?php $image = imagecreatefrompng("http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=".$pathhold2."); $qrcode2=imagejpeg($image); imagedestroy($image); ?> However, this throws out an error message because it can't find a PNG at that address. Any advice much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/252815-convert-png-to-jpg-in-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 9, 2011 Share Posted December 9, 2011 The & in the url are causing a HTTP 400 error back from google. Give the following a try - <?php $url = "http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=http://www.menuanalyser.co.uk/nutritional-report/11599"; $im = imagecreatefrompng($url); // Set the content type header - in this case image/jpeg header('Content-Type: image/jpeg'); // Output the image imagejpeg($im); You only use & encoding when outputting a link on a page to a browser, not in the actual URL that is requested. Quote Link to comment https://forums.phpfreaks.com/topic/252815-convert-png-to-jpg-in-php/#findComment-1296144 Share on other sites More sharing options...
markduce Posted December 9, 2011 Author Share Posted December 9, 2011 Thanks for your very prompt reply, fantastic knowledge about the amp thing. It's almost there now, just one last slight problem. My page is built up of a string which is then added to throughout the script and then returned at the end. So when this is outputted straight to the browser, I think it's causing the following to happen, all sorts of horrible characters at : http://www.menuanalyser.co.uk/nutritional-report/11247 My string is simply called $markup, so I tried the following: $markup.=imagejpeg($im); Any ideas how to add it on to my markup string? Quote Link to comment https://forums.phpfreaks.com/topic/252815-convert-png-to-jpg-in-php/#findComment-1296152 Share on other sites More sharing options...
PFMaBiSmAd Posted December 9, 2011 Share Posted December 9, 2011 Each image on a web page requires an <img src= .... tag... ummm.., just read this post - http://www.phpfreaks.com/forums/index.php?topic=349307.msg1648276#msg1648276 You cannot output the image data in your markup on the page. The browser requests the image (all media files) separately and renders it (or not, depending on the visitor's browser settings.) Quote Link to comment https://forums.phpfreaks.com/topic/252815-convert-png-to-jpg-in-php/#findComment-1296158 Share on other sites More sharing options...
markduce Posted December 9, 2011 Author Share Posted December 9, 2011 Ah fair enough, I will save it to a file then just pop the file in the source for the img tags then. Thanks again for all your help, much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/252815-convert-png-to-jpg-in-php/#findComment-1296161 Share on other sites More sharing options...
scootstah Posted December 9, 2011 Share Posted December 9, 2011 You could use headers to change the content type to an image, then just use the php page in your img src. But in the long run saving the file will drastically reduce server load. Quote Link to comment https://forums.phpfreaks.com/topic/252815-convert-png-to-jpg-in-php/#findComment-1296274 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.