문제

I have an image encoded in a base64 string and i need it to save on a folder as a image. This is the code:

$img = Input::get('myField');
        define('UPLOAD_DIR', 'C:/wamp/www/laravel/dentalRoyale/public/assets/profile/');
        $img = str_replace('data:image/png;base64,', '', $img);
        $img = str_replace(' ', '+', $img);
        $data = base64_decode($img);
        $file = UPLOAD_DIR . uniqid() . '.png';
        $success = file_put_contents($file, $data);

Yeah, it saves on the profile folder. But i can't open the image which i badly need. How can i fix this? Thanks

도움이 되었습니까?

해결책 2

Problem fixed. The image is now viewable. I forgot to change this str_replace('data:image/png;base64,', '', $img) to this str_replace('data:image/jpeg;base64,', '', $img), because the type of my image is jpeg.

다른 팁

Firstly, are you absolutely certain that it's a png?

Secondly, $img = str_replace(' ', '+', $img); isn't needed. If anything, you'd need to do that in reverse if the string is uri encoded.

Try commenting out that line and checking that it's actually a PNG.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top