Dexter
Dexter

Reputation: 1178

Use PHP to convert PNG data into JPG data

I'm currently using the following code to call a dynamic URL and get the image data I need for a thumbnail:

$thumb_url = $thumbUrl."?key=".$key."&document=".$document."&width=148&height=148";
$crl = curl_init();
$timeout = 120;
curl_setopt ($crl, CURLOPT_URL, $thumb_url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$thumb_content = curl_exec($crl);
@curl_close($crl);

Once I have the data inside thumb_content, I can write it as a PNG to the file system. This works great as-is, however, I need a way to convert this and save it as a JPG instead.

I was reviewing this question, but it seems to read and write from the file system while doing the conversion: Use PHP to convert PNG to JPG with compression?

Unless absolutely necessary, I don't want to write anything to the file system until I'm ready to write the final JPG. I'd like to just work with the stream data.

Upvotes: 0

Views: 395

Answers (1)

ProGM
ProGM

Reputation: 7108

You can use file_get_contents:

$img = imagecreatefromstring(file_get_contents($thumb_url));
if ($img !== false)
    imagejpeg($img, "/path/to/save/file.jpg");

Upvotes: 2

Related Questions