【问题标题】:PHP ImageCreateFromString and file_get_contentsPHP ImageCreateFromString 和 file_get_contents
【发布时间】:2010-01-07 02:24:19
【问题描述】:

我正在尝试在 PHP 中创建一个函数,它允许我输入基本上任何 URL,然后在其上运行一些函数,就像用户在我的服务器上上传一样。因此,我将调整大小并制作一些缩略图,但我需要帮助才能使图像处于可以在其上运行其他代码的状态。该站点上的另一位用户帮助我开始使用 ImageCreateFromString() 和 file_get_contents()

请注意,这段代码缺少很多我知道的东西,我只是想让基本功能正常工作,然后我会添加所有安全措施

我使用这样的 URL 尝试了下面的代码,并将照片 URL 添加到我的脚本 URL:

http://example.com/friendproject2/testing/photos/fromurl/?url=http://a0.twimg.com/a/1262802780/images/twitter_logo_header.png

但它什么也没显示,甚至没有错误

function getphotoURL($url){
    if(isset($url) && $url != 'bad') {
        $image = ImageCreateFromString(file_get_contents($url));
        if (is_resource($image) === true){
            echo 'The URL of the image we fetch is :' .$url. '<BR><BR>';
            //show image
            header('Content-Type: image/jpeg');
            imagejpeg($image, null, 100);
            imagedestroy($image); 
            imagedestroy($image); 
            // image is valid, do your magic here
        }else{
            // not a valid image, show error
            echo 'error getting URL photo from ' .$url;
        }
    }else{
        //url was empty
        echo 'The URL was not passed into our function';
    }
}
?>

###### 更新#####

这似乎是我的一个简单错误,就像检查 POST 请求而不是 GET 请求一样简单,下面是我的新代码。

我有几个问题,

1) 我正在使用 imagejpeg($image, null, 100);我想知道,我应该使用其他东西吗?它是否要求源图像是 jpg 或它可以与任何图像一起使用?我需要允许主要类型(jpg、jpeg、gif、png)

2) 与上述问题相同,但在屏幕上显示图像时,我将标题设置为:header('Content-Type: image/jpeg');其他类型的图片不应该是jpg吗?

3) 有没有一种方法可以确保传入的源 URL 是实际图像,如果不是图像,我可以做任何我想做的事情,比如显示我自己的错误或做我自己的代码一旦检测到 URL 不是有效的图像 url

<?PHP
// run our function
if(isset($_GET['url']) && $_GET['url'] != "") {
    getphotoURL($_GET['url'],'no');
}


function getphotoURL($url, $saveimage = 'yes'){
    if(isset($url) && $url != '') {
        $image = imagecreatefromstring(file_get_contents($url));
        if (is_resource($image) === true){
            if($saveimage === 'yes'){
                // resize image and make the thumbs code would go here if we are saving image:
                // resize source image if it is wider then 800 pixels
                // make 1 thumbnail that is 150 pixels wide
            }else{
                // We are not saving the image show it in the user's browser
                header('Content-Type: image/png');
                imagejpeg($image, null, 100);
                imagedestroy($image); 
            }
        }else{
            // not a valid resource, show error
            echo 'error getting URL photo from ' .$url;
        }
    }else{
        // url of image was empty
        echo 'The URL was not passed into our function';
    }
}
?>

【问题讨论】:

  • 您确定没有禁用错误报告吗?这应该至少输出一些东西(假设函数实际上正在被调用)。
  • Content-type 之前的回声将会把事情搞砸。
  • 您好,我只是确保 eeror 报告已打开,但屏幕上仍然没有显示任何内容 = error_reporting(E_ALL); //全部显示

标签: php image-processing


【解决方案1】:

在为 PNG 或 GIF 调用 imagecreatefromstring() 之后(用于透明度)

对图像进行以下操作:

imagealphablending($image, true); // setting alpha blending on
imagesavealpha($image, true);

会将纯黑色背景转换为 Alpha 通道。

【讨论】:

    【解决方案2】:

    回答您的新问题:

    1) 我正在使用 imagejpeg($image, null, 100);我想知道,我应该是 用别的东西?是否需要 源图像为 jpg 或将 它适用于任何图像吗?我需要 允许主要类型(jpg、jpeg、gif、 png)

    嗯,php.net 说,“imagejpeg() 从给定的图像创建一个 JPEG 文件”。但重要的部分是“图像资源,由图像创建函数之一返回,例如 imagecreatetruecolor()。”。并且您使用“imagecreatefromstring() 返回表示从给定数据获得的图像的图像标识符。如果您的 PHP 构建支持它们,将自动检测这些类型:JPEG、PNG、GIF、WBMP 和 GD2。” 所以,应该没问题。

    2) 与上述问题相同,但适用于何时 在我的屏幕上显示图像 标头设置为: header('Content-Type: image/jpeg'); 其他类型的不应该是jpg吗 图片?

    标题应该是 jpg 类型 - 如果这是文件类型,那么你是正确的。

    3) 有没有办法可以确定 传入的源 URL 是 真实的形象,做任何我想做的事 这不是图像,就像展示我自己的一样 错误或一旦检测到就执行我自己的代码 该网址不是有效的图片网址

    是的 - 而不是这样做:

    $image = ImageCreateFromString(file_get_contents($url));
    

    你可以这样做:

    $image = imagecreatefromjpeg($url);
    if (!$image) echo "error"; 
    

    imagecreatefromjpeg() 返回一个图像 代表图像的标识符 从给定的文件名中获取。

    但实际上,你所拥有的一切都很好。


    是否显示错误信息

        echo 'The URL was not passed into our function';
    

    还是什么都没有?

    如果正在显示错误消息,则检查 === 可能失败:

    图像资源将返回 成功。如果返回 FALSE 图像类型不受支持,数据为 不是可识别的格式,或者 图片已损坏,无法加载。

    另外,您的开发服务器上的错误日志记录是否已满?这样你就可以看到任何可能的警告被抛出?

    【讨论】:

    • 我设置了 error_reporting(E_ALL)。我现在有点工作了,我已经更新了我的问题,但如果你能帮助任何其他问题的话?
    • 您好,感谢您的帖子,它按照我的方式工作,只是图像看起来不太好,背景显示为黑色,我认为这是我尝试过的透明 png 文件来自推特网址
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    相关资源
    最近更新 更多