PHP 从应用程序/八位字节流创建图像

发布于 2024-11-06 05:15:30 字数 892 浏览 1 评论 0 原文

我的应用程序正在从移动设备发送内容类型为“application/octet-stream”的图像。

我需要使用 GD 库处理这些图像,这意味着我需要能够从数据创建图像对象。

通常,我一直在使用 imagecreatefromjpeg、imagecreatefrompng、imagecreatefromgif 等来处理从 Web 表单上传的文件,但这些作为应用程序/八位字节流来找我时似乎不起作用。

关于如何实现我的目标有什么想法吗?

编辑

这是我用来创建图像标识符的代码...我的处理程序在我的网站中完美运行,我可以告诉我的网站和 iOS 数据之间的唯一区别是内容类型

public function open_image($path) {
        # JPEG:
        $im = @imagecreatefromjpeg($path);
        if ($im !== false) { $this->image = $im; return $im; }

        # GIF:
        $im = @imagecreatefromgif($path);
        if ($im !== false) { $this->image = $im; return $im; }

        # PNG:
        $im = @imagecreatefrompng($path);
        if ($im !== false) { $this->image = $im; return $im; }

        $this->error_messages[] = "Please make sure the image is a jpeg, a png, or a gif.";
        return false;
    }

My application is having images sent to it from mobile devices with the content-type "application/octet-stream".

I need to process these images using the GD library, which means I need to be able to create an image object from the data.

Typically, I have been using imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif, etc, to handle files uploaded from web forms, but these don't seem to work when coming to me as application/octet-stream.

Any ideas on how I can achieve my goal?

EDIT

Here is the code I use to create the image identifier...my handler works perfectly throughout my site, the only difference I can tell between my site and the data from the iOS is the content-type

public function open_image($path) {
        # JPEG:
        $im = @imagecreatefromjpeg($path);
        if ($im !== false) { $this->image = $im; return $im; }

        # GIF:
        $im = @imagecreatefromgif($path);
        if ($im !== false) { $this->image = $im; return $im; }

        # PNG:
        $im = @imagecreatefrompng($path);
        if ($im !== false) { $this->image = $im; return $im; }

        $this->error_messages[] = "Please make sure the image is a jpeg, a png, or a gif.";
        return false;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赢得她心 2024-11-13 05:15:30

简单:)

$image = imagecreatefromstring( $data );

具体来说:

$data = file_get_contents($_FILES['myphoto']['tmp_name']);
$image = imagecreatefromstring( $data );

Easy :)

$image = imagecreatefromstring( $data );

Specifically:

$data = file_get_contents($_FILES['myphoto']['tmp_name']);
$image = imagecreatefromstring( $data );
π浅易 2024-11-13 05:15:30

我在 codeigniter 论坛中发现了一种更改 mime 的方法并且它有效,我想您也可以用于其他框架,这是 链接 和代码:

//if mime type is application/octet-stream (psp gives jpegs that type) try to find a   more specific mime type

$mimetype = strtolower(preg_replace("/^(.+?);.*$/", "\\1", $_FILES['form_field']  ['type'])); //reg exp copied from CIs Upload.php

if($mimetype == 'application/octet-stream'){
    $finfo = finfo_open(FILEINFO_MIME, '/usr/share/file/magic');
    if($finfo){
    $_FILES['form_field']['type'] = finfo_file($finfo, $_FILES['form_field']['tmp_name']);
    finfo_close($finfo);
    }
    else echo "finfo_open() returned false");
}  

Fileinfo 扩展需要安装在服务器上。

这对我有用。

I found this in the codeigniter forum a way to change the mime and it works, I suppose you can use for other frameworks as well, this is the link and this the code:

//if mime type is application/octet-stream (psp gives jpegs that type) try to find a   more specific mime type

$mimetype = strtolower(preg_replace("/^(.+?);.*$/", "\\1", $_FILES['form_field']  ['type'])); //reg exp copied from CIs Upload.php

if($mimetype == 'application/octet-stream'){
    $finfo = finfo_open(FILEINFO_MIME, '/usr/share/file/magic');
    if($finfo){
    $_FILES['form_field']['type'] = finfo_file($finfo, $_FILES['form_field']['tmp_name']);
    finfo_close($finfo);
    }
    else echo "finfo_open() returned false");
}  

Fileinfo extension needs to be installed on the server.

It worked for me.

可是我不能没有你 2024-11-13 05:15:30

你也可以使用这个功能。它不需要任何其他依赖项。并且也适用于其他类型。

function _getmime($file){
    if($info = @getimagesize($file)) {
        return image_type_to_mime_type($info[2]);
    } else {
        return mime_content_type($file);
    }
} 

you can use this function too. it not require any other dependency. and work perfectly for other types also.

function _getmime($file){
    if($info = @getimagesize($file)) {
        return image_type_to_mime_type($info[2]);
    } else {
        return mime_content_type($file);
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文