【问题标题】:How to save an image from website using PHP如何使用 PHP 从网站保存图像
【发布时间】:2017-05-02 01:12:54
【问题描述】:

所以我找到了一个我想从中下载图片的页面的完美示例,这恰好是http://www.habbo.com/habbo-imaging/avatarimage?figure=ch-215-110.hd-180-7.lg-275-110.hr-893-61&direction=3&head_direction=3&headonly=1&gesture=sml&size=1

现在,如果您要在桌面上保存图像,它会读取为 PNG 文件,虽然我正在尝试使用 PHP 保存它,但我希望它保存为 GIF。

到目前为止我所做的是:

$ch = curl_init('https://www.habbo.com/habbo-imaging/avatarimage?figure=hr-125&direction=3&head_direction=3&headonly=1&gesture=sml&size=1');
$fp = fopen('game/c_images/badges/' . $badge_id . '.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

我设法使权限正常工作并保存了图像,但它只保存了文件名并且照片不存在。所以我猜这与我将 PNG 文件保存为 GIF 有关,肯定有我遗漏的东西。

【问题讨论】:

标签: php image curl png gif


【解决方案1】:

是的,您可以使用 html dom 保存图像。 喜欢使用

   `$new_image_name = '2018_'.mt_rand();
    $base_url = 'https://www.habbo.com/habbo-imaging/avatarimage?figure=hr-125&direction=3&head_direction=3&headonly=1&gesture=sml&size=1';
    $img = "./image_path/$new_image_name.jpg";
    file_put_contents($img,file($base_url));
    $local_image_url = "http://example.org/test/path_to_image/$new_image_name.jpg";`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    相关资源
    最近更新 更多