4

我正在尝试从 blob 写入图像文件。

 if($_POST['logoFilename'] != 'undefined'){
  $logoFile = fopen($_POST['logoFilename'], 'w') or die ("Cannot create ".$_POST['logoFilename']);

  fwrite($logoFile, $_POST['logoImage']);

  fclose($logoFile);
}

在前面的代码片段中,$_POST['logoImage']是一个 BLOB。该文件已正确写入根目录,但无法打开该文件。在 ubuntu 11.04 中,我收到以下错误:

Error interpreting JPEG image file (Not a JPEG file: starts with 0x64 0x61).

如果我创建一个 img 并设置它的 src=blob,BLOB 会正确显示

下面是 BLOB 的第一个片段:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/2wBDAAgGBgcGBQgHBwcJCQ
4

4 回答 4

10

您的“Blob”实际上是一个Data URI

data:[<MIME-type>][;charset=<encoding>][;base64],<data>

由于您只想要解码的数据部分,因此您必须这样做

file_put_contents(
    'image.jpg',
    base64_decode( 
        str_replace('data:image/jpeg;base64,', '', $blob)
    )
);

但由于 PHP 本身支持 data:// 流,你也可以这样做(感谢@NikiC)

file_put_contents('image.jpg', file_get_contents($blob));

如果上述方法不起作用,您可以尝试使用 GDlib:

imagejpg(
    imagecreatefromstring(
        base64_decode( 
            str_replace('data:image/jpeg;base64,', '', $blob)
        )
    ), 
    'image.jpg'
);
于 2011-07-15T16:46:26.123 回答
0

如果是文件上传控件,$_POST则不包含信息。您正在寻找使用$_FILES. (更具体地说,move_uploaded_file

鉴于新的更新,请尝试以下操作:

  // 
  // Export a image blob to a file using either the specific image name
  // @blob     : The actual image blob
  // @fileName : Can be the explicit name (with an extension) or this can be
  //             just a generic file name and the extension (based on data
  //             type) will be appended automatically. This can also include
  //             path information.
  // Exmaples:
  //   storeBlob('data:image/png;base64,...', 'myimage');      ::  saves as myimage.png
  //   storeBlob('data:image/jpg;base64,...', 'img/new.jpg');  ::  saves as img/new.jpg
  function storeBlob($blob, $fileName)
  {
    $blobRE = '/^data:((\w+)\/(\w+));base64,(.*)$/';
    if (preg_match($blobRE, $blob, $m))
    {
      $imageName = (preg_match('/\.\w{2,4}$/', $fileName) ? $fileName : sprintf('%s.%s', $fileName, $m[3]));

      return file_put_contents($imageName,base64_decode($m[4]));
    }
    return false; // error
  }
于 2011-07-15T16:39:37.260 回答
0

如果它真的是一个 blob,您可能想尝试使用模式“wb”作为 fopen() 调用的第二个参数。

编辑:您也可以考虑只使用file_put_contents(),这是二进制安全的。

于 2011-07-15T16:42:36.790 回答
0

此函数会将数据 uri 保存到文件中:

function saveDataUri($blob, $filename = null) 
{

    // generate unique name basing on content
    if (empty($filename)) {
        $filename = md5($blob);
    }

    // parse data URI
    $semiPos = strpos($blob, ';', 5);
    $comaPos = strpos($blob, ',', 5);
    $mime = substr($blob, 5, $semiPos - 5);
    $data = substr($blob, $comaPos + 1);

    $isEncoded = strpos(substr($blob, $semiPos, $comaPos), 'base64');

    if ($isEncoded) {
        $data = base64_decode($data);
    }


    // save image data to file
    switch ($mime) {
           case 'image/png':
                $ext = 'png';
            break;
           case 'image/gif':
                $ext = 'gif';
                break;
           case 'image/jpg':
           case 'image/jpeg':
           default:
                $ext = 'jpg';
                break;  
    }

    $outFile = $filename . '.' . $ext;
    $funcName = 'image' . $ext;
    $result = $funcName(imagecreatefromstring($data), $outFile);

    if ($result) {

        return $outFile;
    }

    return $result;
}

在您的情况下使用:

// some_validation($_POST);
$filename = saveDataUri($_POST['logoImage']);
echo '<img src="' . $filename . '" alt="' . $filename . '" />';
于 2011-07-15T18:30:37.653 回答