简体   繁体   English

如何在cakePHP中将图像数据从二进制解码为png格式?

[英]How to Decode image data from binary to png format in cakePHP.?

I'm writing a webservices for an IphoneAPP using cakephp as the backend for the Iphone App. 我正在使用Cakephp作为Iphone App的后端为IphoneAPP编写Web服务。 Everthing works fine with updating / deleting /editing data from the database, in other words iphone app is running perfectly with the database interactivity. Everthing可以很好地与数据库中的数据进行更新/删除/编辑,换句话说,iphone应用程序可与数据库交互完美运行。

Now after some modifications in the App , I had to save image when the post from the iphone. 现在,在App中进行了一些修改之后,我不得不保存来自iPhone的帖子时的图像。 The URL that is generated when we post is something like this http://somedomainname.com/customer/add/?name=abc&number=165adf&image=89504e470d0a1a0a0000000d494844520000004e0000005f08060000004620f1230000001974455874536f6674 我们发布时生成的URL如下所示: http://somedomainname.com/customer/add/?name=abc&number=165adf&image=89504e470d0a1a0a0000000d494844520000004e0000005f08060000004620f1230000001974455874536f6674

Above URL contains image value in binary , which is apng image. 上方的URL包含二进制形式的图像值,即apng图像。 I would like to decode the binary code and upload it in the folder. 我想解码二进制代码并将其上传到文件夹中。 I have used the code to upload the file 我已使用代码上传文件

$folder= "img/Invoicecustomer";
        $formdata = $data; 
        $itemId = null;
        // setup dir names absolute and relative
        $folder_url = WWW_ROOT.$folder;
        $rel_url = $folder;

        // create the folder if it does not exist
        if(!is_dir($folder_url)) {
            mkdir($folder_url, 0777, true);
            chmod($folder_url, 0777);
        }

        // if itemId is set create an item folder
        if($itemId) {
            // set new absolute folder
            $folder_url = WWW_ROOT.$folder.'/'.$itemId; 
            // set new relative folder
            $rel_url = $folder.'/'.$itemId;
            // create directory
            if(!is_dir($folder_url)) {
                mkdir($folder_url);
            }
        }

        // list of permitted file types, this is only images but documents can be added
        $permitted = array('image/gif','image/jpeg','image/pjpeg','image/png');

        // loop through and deal with the files
        foreach($formdata as $file) {
            // replace spaces with underscores
            $filename = str_replace(' ', '_', $file['signature']);
            // assume filetype is false
            $typeOK = false;
            // check filetype is ok
            foreach($permitted as $type) {
                if($type == $file['type']) {
                    $typeOK = true;
                    break;
                }
            }

            // if file type ok upload the file
            if($typeOK) {
                // switch based on error code
                switch($file['error']) {
                    case 0:
                        // check filename already exists
                        if(!file_exists($folder_url.'/'.$filename)) {
                            // create full filename
                            $full_url = $folder_url.'/'.$filename;
                            $url = $rel_url.'/'.$filename;
                            // upload the file
                            $success = move_uploaded_file($file['tmp_name'], $url);
                        } else {
                            // create unique filename and upload file
                            ini_set('date.timezone', 'Europe/London');
                            $now = date('Y-m-d-His');
                            $full_url = $folder_url.'/'.$now.$filename;
                            $url = $rel_url.'/'.$now.$filename;
                            $success = move_uploaded_file($file['tmp_name'], $url);
                        }
                        // if upload was successful
                        if($success) {
                            // save the url of the file
                            $result['urls'][] = $url;
                        } else {
                            $result['errors'][] = "Error uploaded $filename. Please try again.";
                        }
                        break;
                    case 3:
                        // an error occured
                        $result['errors'][] = "Error uploading $filename. Please try again.";
                        break;
                    default:
                        // an error occured
                        $result['errors'][] = "System error uploading $filename. Contact webmaster.";
                        break;
                }
            } elseif($file['error'] == 4) {
                // no file was selected for upload
                $result['nofiles'][] = "No file Selected";
            } else {
                // unacceptable file type
                $result['errors'][] = "$filename cannot be uploaded. Acceptable file types: gif, jpg, png.";
            }
        }

i'm getting an error as it is not recognizing image from the url, which is in binary form. 我收到一个错误,因为它无法识别来自URL的图像,URL为二进制格式。 How can i decode the image and upload it in folder.? 如何解码图像并将其上传到文件夹中? Also the image sent from the iphone is encoded, i have check in Xcode there is no code to decode the image. 从iPhone发送的图像也被编码,我已经在Xcode中签入,没有代码可以解码图像。

The problem might be caused by truncation of the URL (data) due to a max length? 问题可能是由于最大长度而导致URL(数据)被截断了? See 1 and 2 . 参见12

I think it can be done by using simple PHP functions. 我认为可以通过使用简单的PHP函数来完成。 Generally i use base64 encoding/decoding functions to upload images using webservices. 通常,我使用base64编码/解码功能通过网络服务上传图像。

Below is a code snipt. 下面是一个代码片段。

$upload_file = $path_to_folder . '/' . $file_name;
file_put_contents($upload_file, base64_decode($image, true));
//$filesize = filesize($upload_file);
chmod($upload_file, 0777);

Here $path_to_folder will be Your relative path to destination. 这里$ path_to_folder将是您到目的地的相对路径。 $file_name will be any dynamic name and can have any extension(jpg,png), for example :- $file_name = 'xyz.png'. $ file_name将是任何动态名称,并且可以具有任何扩展名(jpg,png),例如:-$ file_name ='xyz.png'。

"$image" will be a base64 encoded string that we get from mobile device. “ $ image”将是我们从移动设备获得的base64编码的字符串。 Last one line is to set 777 permission of that uploaded file. 最后一行是设置该上载文件的777权限。

I hope this will help you in your task.. 希望这对您的工作有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM