【问题标题】:PHP cURL How to get image width?PHP cURL 如何获取图像宽度?
【发布时间】:2016-08-17 06:36:48
【问题描述】:

这是我迄今为止尝试过的:

$h = curl_init();
$headers = array(
"Range: bytes=0-700"
);
curl_setopt($h, CURLOPT_URL, $url);
curl_setopt($h, CURLOPT_HTTPHEADER, $headers);
curl_setopt($h, CURLOPT_RETURNTRANSFER, 1); //return the image value

$mh = curl_multi_init();
curl_multi_add_handle($mh, $h);

$running = null;
do {
   curl_multi_exec($mh, $running);
} while ($running > 0);

$raw = curl_multi_getcontent($h);
$im = imagecreatefromstring($raw);



 $width = imagesx($im);
    $height = imagesy($im);


var_dump($width);

每当我运行代码时都会收到此错误:

> ( ! ) Warning: imagecreatefromstring(): Data is not in a recognized
> format in
> C:\wamp\www\......funcs.php on
> line 399 Call Stack
> # Time    Memory  Function    Location 1  0.0022  294120  {main}( )   ..\...

如果你 var_dump( $raw ) 它说:

string '<html>

<head><title>302 Found</title></head>

<body bgcolor="white">

<center><h1>302 Found</h1></center>

<hr><center>BSWS/2.1</center>

</body>

</html>

' (length=157)

可能是什么问题...我已经花了 6 个多小时试图解决这个问题...请帮助。谢谢

【问题讨论】:

    标签: php image curl width


    【解决方案1】:

    看起来您正在接收来自服务器的重定向,并且 cURL 将其作为内容而不是图像本身提供给您。尝试使用CURLOPT_FOLLOWLOCATION 选项。

    所以:

    curl_setopt($h, CURLOPT_URL, $url);
    curl_setopt($h, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($h, CURLOPT_RETURNTRANSFER, 1); //return the image value
    

    变成:

    curl_setopt($h, CURLOPT_URL, $url);
    curl_setopt($h, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($h, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($h, CURLOPT_RETURNTRANSFER, 1); //return the image value
    

    此处为 cURL 选项的完整列表:http://php.net/manual/en/function.curl-setopt.php

    HTH

    【讨论】:

    • 谢谢!!...就是这样!
    猜你喜欢
    • 2012-10-17
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多