我有一个base64图像字符串,我使用base64_decode
来获取图像字节。之后,我使用fopen
,fwrite
和fclose
将文件保存在服务器中。但文件输出得到\ r \ n,图像被破坏。当我使用preg_replace("\r\n", "\n")
时没有任何事情发生。
Base64图片字符串:
/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhQUExQVFBUUFBcUGBQVFRcVFxgXFhcWFhcXFRUYHCggGBolHBQVITEhJSkrLi4uFx8zODMsNygtLisBCgoKDg0OGhAQGCwcHBwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCw3LCwsLCwsLCw3Kzc3K//AABEIAOEA4QMBIgACEQEDEQH/xAAcAAABBQEBAQAAAAAAAAAAAAAEAAIDBQYBBwj/xAA/EAABAwIDBQUHAgQDCQAAAAABAAIRAyEEMUEFElFhcQaBkaGxExQiwdHh8DJCI1Jy8RUkMwc0YmOCkqKywv/EABoBAAMBAQEBAAAAAAAAAAAAAAECAwQABQb/xAAkEQACAgICAQQDAQAAAAAAAAAAAQIRAyESMQQTIjJBI1Fhgf/aAAwDAQACEQMRAD8A9AqMgoXEEgE8ldPpiFS7VfFJ5HDNefBWzmeb7Yq71R5uRMj0VBVMucfyyusW6XPtkRJ8FS423ff6L38elR58uwRzrzy9VDVOikfw5plQqoEQFdJSK4UBzuiVOmSicHhy9w4StDhMA0CABmpzmojxg5Gfp7Oecgmvwjm5g+C13sonQBRVKYPPuUfWZb0VRkjTJ0Kie2NFo8Rhx/ZAPww4J1kTFeJopSFyFbOwihdgym5IFMAATgVM7CFMNEhEVnGpCycAkFwomrrm3nyU1LDEiw/JVhhsCBZxv0+a7kgFYKZgaxyhGYSW1GuG9Mg27uCsqOCiLHd7wfFabY3ZoVDvSWjhBjxICnPKkthjBtm/7O7RNak3eHxAQTx5q1c2VW7Bwgp0gIjQ31aSJ8lZrwZ1ydG5dEbGQpE0ldlIMJJc3kkTqOkKv25THu9TT4fmFYoHbg/y9X+n6JoP3IWXR5FjR+uNTn5Kj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es = base64_decode($base64Image);
$file = fopen($path, $imageBytes);
fwrite($file, $imageBytes);
fclose($file);
原始图片字节:
解码后的图片字节:
当我放入
localhost
服务器(WAMP)时,它可以工作!但是在Web服务器中,不是。
答案 0 :(得分:1)
您应该在binary mode中打开文件。这可以防止某些操作系统翻译您的换行符。
$img = base64_decode($the_string);
$f = fopen('img.jpg', 'wb');
fwrite($f, $img);
fclose($f);