简体   繁体   English

使用PHP将PNG数据转换为JPG数据

[英]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: 我目前正在使用以下代码调用动态URL并获取缩略图所需的图像数据:

$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. 一旦我在thumb_content中有数据,我就可以将它作为PNG写入文件系统。 This works great as-is, however, I need a way to convert this and save it as a JPG instead. 这很好用,但是,我需要一种方法来转换它并将其另存为JPG。

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? 我正在审查这个问题,但它似乎在进行转换时从文件系统读取和写入: 使用PHP通过压缩将PNG转换为JPG?

Unless absolutely necessary, I don't want to write anything to the file system until I'm ready to write the final JPG. 除非绝对必要,否则在准备编写最终的JPG之前,我不想写任何文件系统。 I'd like to just work with the stream data. 我想使用流数据。

You can use file_get_contents : 您可以使用file_get_contents

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM