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


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

我正在为IphoneAPP编写一个Web服务,使用cakepp作为IphoneAPP的后端。Everthing可以很好地更新/删除/编辑数据库中的数据,换言之,iphone应用程序可以完美地与数据库交互运行。

现在,在应用程序中进行了一些修改后,我不得不在iphone上保存帖子时的图像。当我们发布时生成的URL是这样的http://somedomainname.com/customer/add/?name=abc&数字=165adf&图像=89504e470d0a1a00000000d494844520000004e0000005f08060000004620f123000001974455874536f6674

上面的URL包含二进制的图像值,即apng图像。我想解码二进制代码并将其上传到文件夹中。我已经用代码上传文件

$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.";
            }
        }

我得到了一个错误,因为它无法从二进制形式的url中识别图像。如何解码图像并将其上传到文件夹中。?此外,从iphone发送的图像是编码的,我在Xcode中检查了没有解码图像的代码。

问题可能是由于URL(数据)的最大长度而被截断引起的?参见1和2。

我认为这可以通过使用简单的PHP函数来完成。通常,我使用base64编码/解码功能来使用Web服务上传图像。

下面是一个代码剪辑。

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

此处$path_to_folder将是您到目的地的相对路径$file_name将是任何动态名称,并且可以有任何扩展名(jpg,png),例如:-$file_name='xyz.png'.

"$image"将是我们从移动设备中获得的base64编码字符串。最后一行是设置上传文件的777权限。

我希望这能帮助你完成任务。。