从Url创建图像任何文件类型 [英] Create Image From Url Any File Type

查看:127
本文介绍了从Url创建图像任何文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 imagecreatefromgif() imagecreatefromjpeg() imagecreatefrompng( ) 但有没有办法从任何类型的有效图像的网址创建图像资源(最好是png)?或者您是否必须确定文件类型然后使用相应的函数?

I know of imagecreatefromgif(), imagecreatefromjpeg(), and imagecreatefrompng() but is there a way to create an image resource (for png preferably) from a url of any type of valid image? Or do you have to determine the file type and then use the appropriate function?

当我说url时我的意思是像 http:// sample .com / image.png ,不是数据网址

When I say url I mean something like http://sample.com/image.png, not a data url

推荐答案

也许你想要这个:



Maybe you want this:

$jpeg_image = imagecreatefromfile( 'photo.jpeg' );
$gif_image = imagecreatefromfile( 'clipart.gif' );
$png_image = imagecreatefromfile( 'transparent_checkerboard.PnG' );
$another_jpeg = imagecreatefromfile( 'picture.JPG' );
// This requires you to remove or rewrite file_exists check:
$jpeg_image = imagecreatefromfile( 'http://example.net/photo.jpeg' );
// SEE BELOW HO TO DO IT WHEN http:// ARGS IS NEEDED:
$jpeg_image = imagecreatefromfile( 'http://example.net/photo.jpeg?foo=hello&bar=world' );



以下是它的完成方式:



Here's how it's done:

function imagecreatefromfile( $filename ) {
    if (!file_exists($filename)) {
        throw new InvalidArgumentException('File "'.$filename.'" not found.');
    }
    switch ( strtolower( pathinfo( $filename, PATHINFO_EXTENSION ))) {
        case 'jpeg':
        case 'jpg':
            return imagecreatefromjpeg($filename);
        break;

        case 'png':
            return imagecreatefrompng($filename);
        break;

        case 'gif':
            return imagecreatefromgif($filename);
        break;

        default:
            throw new InvalidArgumentException('File "'.$filename.'" is not valid jpg, png or gif image.');
        break;
    }
}



进行一些小修改切换相同的功能已准备好用于网址:



With some small modifications to switch same function is ready for web url's:

    /* if (!file_exists($filename)) {
        throw new InvalidArgumentException('File "'.$filename.'" not found.');
    } <== This needs addiotional checks if using non local picture */
    switch ( strtolower( array_pop( explode('.', substr($filename, 0, strpos($filename, '?'))))) ) {
        case 'jpeg':

之后你可以使用它与 http://www.tld/image.jpg

After that you can use it with http://www.tld/image.jpg:

$jpeg_image = imagecreatefromfile( 'http://example.net/photo.jpeg' );
$gif_image = imagecreatefromfile( 'http://www.example.com/art.gif?param=23&another=yes' );



一些证据:



尽可能从官方PHP手册中读取 function.imagecreatefromjpeg.php GD允许从URL加载图像 function.fopen.php 支持,所以无需先获取图像并将其保存到文件中,然后打开该文件。

Some proofs:

As you can read from official PHP manual function.imagecreatefromjpeg.php GD allows loading images from URLs that is supported by function.fopen.php, so there is no need to fetch image first and save it to file, and open that file.

这篇关于从Url创建图像任何文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆