Jump to content

Convert PNG to JPG in PHP


markduce

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.