Chr*_*ris 6 php curl gd image-manipulation
我希望能够从Web服务器检索远程图像,重新采样,然后将其提供给浏览器并将其保存到文件中.这是我到目前为止:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "$rURL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$out = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
$imgRes = imagecreatefromstring($out);
imagejpeg($imgRes, $filename, 70);
header("Content-Type: image/jpg");
imagejpeg($imgRes, NULL, 70);
exit();
Run Code Online (Sandbox Code Playgroud)
更新
根据以下讨论更新以反映正确答案
您可以使用以下命令将文件提取到 GD 资源中imagecreatefromstring():
imagecreatefromstring() 返回一个图像标识符,表示从给定数据获得的图像。如果您的 PHP 版本支持以下类型,将自动检测这些类型:JPEG、PNG、GIF、WBMP 和 GD2。
从那里开始,使用imagecopyresampled()就非常简单了。
使用imagejpeg()或最适合您的输出格式输出它。
使用文件名生成一个输出:
imagejpeg($image_resource, "/path/to/image.jpg");
Run Code Online (Sandbox Code Playgroud)
然后将相同的资源发送到浏览器:
header("Content-type: image/jpeg");
imagejpeg($image_resource);
Run Code Online (Sandbox Code Playgroud)
根据图像的格式,您可能需要使用 imagepng() 或 imagegif() 函数。
您可能希望根据输入文件类型使用不同的输出格式。您可以使用getimagesize()获取输入文件类型。
请记住,您可以使用该参数调整生成的 JPEG 图像的质量$quality。默认值为 75%,这看起来很糟糕。