Error in Writing to Image file from PHP

前端 未结 4 1261
伪装坚强ぢ
伪装坚强ぢ 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:[][;charset=][;base64],
    

    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'
    );
    

提交回复
热议问题