用cURL输出外部图像,但是如何改变图像的宽度呢?
header('Content-type: image/jpeg');
$url = $_GET['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch);
echo $file;
谁能告诉我如何将 $file
的宽度更改为 300px?
header('Content-type: image/jpeg');
$url = $_GET['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch);
echo $file;
Can anyone please tell me how I can change the width of $file
to 300px?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果图像在
标签内发送给浏览器,那么您可以通过更改
width
属性让浏览器重新缩放它。否则,您需要抓取该文件并在输出之前在 PHP 代码中重新缩放它。
您可以使用 imagecopyresampled() 来执行此操作,计算高度。然后输出结果。
例如,从上面的代码将图像获取到
$file
中。If the image is heading for a browser inside an
<img>
tag, then you can get the browser to rescale it by changing thewidth
attribute.Otherwise you need to grab the file and rescale it in your PHP code before outputting it.
You can use imagecopyresampled() to do that, calculating the height. Then outputting the result.
Example, following on from obtaining the image in to
$file
from the code above.