PHP ImageCreateFromString和文件获取内容

PHP ImageCreateFromString和文件获取内容,php,image-processing,Php,Image Processing,我试图在PHP中创建一个函数,它允许我输入基本上任何URL,然后在上面运行一些函数,就像用户在我的服务器上上传一样。因此,我将调整大小,并作出一些缩略图,但我需要帮助只是得到一个状态,我可以在它运行我的其他代码的图像。该站点上的另一位用户帮助我开始使用ImageCreateFromString()和file_get_contents() 请注意,这段代码缺少很多我知道的东西,我只是想让基本功能正常工作,然后我会添加所有的安全措施 我尝试了下面的代码,使用了这样的URL,并将照片URL添加到我的脚

我试图在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';
    }
}
?>
函数getphotoURL($url){ 如果(isset($url)&&$url!='bad'){ $image=ImageCreateFromString(文件获取内容($url)); if(is_资源($image)==true){ echo“我们获取的图像的URL为:”.$URL。“

”; //显示图像 标题(“内容类型:图像/jpeg”); imagejpeg($image,null,100); 图像销毁($图像); 图像销毁($图像); //图像是有效的,在这里施展你的魔法 }否则{ //不是有效的图像,显示错误 echo“从“.$URL”获取URL照片时出错; } }否则{ //url为空 echo“URL没有传递到我们的函数中”; } } ?>
更新

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

我有几个问题

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

2)与上述问题相同,但在屏幕上显示图像时,我将标题设置为:标题('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';
    }
}
?>

回答您的新问题:

1) 我使用的是imagejpeg($image,null, 100); 我在想,我应该吗 用别的东西?需要吗 源图像将是jpg或will 它能正常工作吗?我需要 允许主要类型(jpg、jpeg、gif、, (巴布亚新几内亚)

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

2) 与上述问题相同,但适用于何时 在屏幕上显示我所拥有的图像 标题设置为: 标题(“内容类型:图像/jpeg”); 是否应该是其他类型的jpg 图像

标题应该是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';
    }
}
?>
是的-而不是做:

$image = ImageCreateFromString(file_get_contents($url));
你可以做:

$image = imagecreatefromjpeg($url);
if (!$image) echo "error"; 
imagecreatefromjpeg()返回图像 表示图像的标识符 从给定的文件名中获取

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


它是否显示错误消息

    echo 'The URL was not passed into our function';
还是什么都没有

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

将在上返回图像资源 成功。如果 不支持图像类型,数据为 未采用公认的格式,或 映像已损坏,无法加载


另外,您是否在开发服务器上最大限度地记录错误?通过这种方式,您可以看到任何可能引发的警告?

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

对图像执行以下操作:

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

将把平坦的黑色背景变成alpha通道。

您确定没有禁用错误报告吗?这应该至少会输出一些东西(假设函数确实被调用)。例如,内容类型之前的回音会把事情搞得一团糟。您好,我刚刚确保eeror reporting处于打开状态,但仍然没有在屏幕上显示任何内容=error\u reporting(E\u all)//显示所有已设置错误报告(E_ALL)。我现在有点工作了,我已经更新了我的问题,尽管还有一些其他问题,如果你能帮忙的话?嗨,谢谢你的帖子,它是按照我的方式工作的,只是图像看起来不好,背景显示为黑色,我想这是一个透明的png文件,我尝试从twitter URL