Error in Writing to Image file from PHP

前端 未结 4 1217
伪装坚强ぢ
伪装坚强ぢ 2021-01-19 06:23

I am attempting to write to an image file from a blob.

 if($_POST[\'logoFilename\'] != \'undefined\'){
  $logoFile = fopen($_POST[\'logoFilename\'], \'w\') o         


        
相关标签:
4条回答
  • 2021-01-19 06:59

    Your "Blob" is really a Data URI:

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

    Since you only want the decoded data part, you have to do

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

    But since PHP natively supports data:// streams, you can also do (thanks @NikiC)

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

    If the above doesnt work, you can try with GDlib:

    imagejpg(
        imagecreatefromstring(
            base64_decode( 
                str_replace('data:image/jpeg;base64,', '', $blob)
            )
        ), 
        'image.jpg'
    );
    
    0 讨论(0)
  • 2021-01-19 07:03

    This function will save data uri to file:

    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;
    }
    

    Usage in your case:

    // some_validation($_POST);
    $filename = saveDataUri($_POST['logoImage']);
    echo '<img src="' . $filename . '" alt="' . $filename . '" />';
    
    0 讨论(0)
  • 2021-01-19 07:05

    If it's really a blob, you might want to trying using mode "wb" as the second parameter to your fopen() call.

    EDIT: You might also consider just using file_put_contents(), which is binary-safe.

    0 讨论(0)
  • 2021-01-19 07:07

    If it's a file upload control, $_POST won't contain the information. You're looking for handling file uploads with $_FILES. (And more specifically, move_uploaded_file)

    Given the new update, try the following:

      // 
      // 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
      }
    
    0 讨论(0)
提交回复
热议问题