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