【问题标题】:How to get File-Type of an Image in a HTML page?如何在 HTML 页面中获取图像的文件类型?
【发布时间】:2012-09-23 22:18:16
【问题描述】:

我编写了一个简短的脚本来从 URL 上传图像,以免热链接到它们。如果图像扩展名不是 .jpeg,则上传的图像会损坏。我不知道如何保留文件扩展名或文件名,所以我不得不为它们添加一个时间戳和一个静态扩展名。

<?php

    ini_set('user_agent', 'TEST/1.0 +http://127.0.0.1');

    require_once('simple_html_dom.php');

    // Create DOM from URL
    $html = file_get_html('http://www.discogs.com/viewimages?release='.$_POST["album_id"]);

    // Grab the coverart
    $img = $html->find('.image_frame', 0);

    $url = $img->src;

    $file = file_get_contents($url);

    $image = 'discogs_'.time().'_image.jpeg';

    file_put_contents('/path/to/file/'.$image,$file);

    echo $image;

?>

在 Baba 的帮助下更新了代码:http://codepad.org/3zH3B882

【问题讨论】:

标签: php


【解决方案1】:

您可以尝试将getimagesize 与image_type_to_extension 一起使用

require_once('simple_html_dom.php');
$url = "http://www.discogs.com/viewimages?artist=Test+%282%29";
$html = file_get_html($url);
$img = $html->find('.image_frame', 0);
$info = getimagesize($img->src);
$extention = image_type_to_extension($info[2]);
$image = 'discogs_'.time().'_image' . $extention;
echo $image ;

输出

discogs_1348438953_image.gif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 2020-07-10
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2014-10-23
    相关资源
    最近更新 更多