PHP:需要帮助从包含参数的URL下载图像

时间:2017-01-23 11:19:22

标签: php image download

我的问题: 如何使用PHP如何从使用参数设置其大小的URL下载图像?

以下是我需要下载的图片示例: http://s7d4.scene7.com/is/image/TrekBicycleProducts/Asset_441005?wid=2000

我收到此错误: 警告:fopen(images / Asset_441005?wid = 2000):无法打开流:没有错误

如果删除wid参数,图像下载正常,但宽度为400px,太小而无法使用: http://s7d4.scene7.com/is/image/TrekBicycleProducts/Asset_441005

这是我的代码不起作用:

<?php

$url_to_image = 'http://s7d4.scene7.com/is/image/TrekBicycleProducts/Asset_441005?wid=2000';

$ch = curl_init($url_to_image);

$my_save_dir = 'images/';
$filename = basename($url_to_image);
$complete_save_loc = $my_save_dir . $filename;

$fp = fopen($complete_save_loc, 'wb');

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

?>

以下是可行的代码,但图片太小,我无法使用:

<?php

    $url_to_image = 'http://s7d4.scene7.com/is/image/TrekBicycleProducts/Asset_441005';

    $ch = curl_init($url_to_image);

    $my_save_dir = 'images/';
    $filename = basename($url_to_image);
    $complete_save_loc = $my_save_dir . $filename;

    $fp = fopen($complete_save_loc, 'wb');

    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);

    ?>

1 个答案:

答案 0 :(得分:0)

使用以下代码对其进行排序:

<?php

$file = file_get_contents('http://s7d4.scene7.com/is/image/TrekBicycleProducts/Asset_441005?wid=2000');
$myfile = fopen("test.jpg", "w") or die("Unable to open file!");
fwrite($myfile, $file);
fclose($myfile);


?>

现在很简单,必须获取文件名,而不是使用test.jpg