php保存canvas导出的base64图片
代码如下:
<?php $img='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAABxCAYAAABoUdWRAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAubSURBVHhe7Z17bBxHHcft8/kFbpsW2kILfVhUBDUIO36kpBHEVKIRQlCoMKWCJhFJjAkkqXgo0BQnTaQSQC1pHsZJaGOVV6OAaQUioaVJ/micsx07qoJCA7juC9qKVjZxTeL4bL7f2d8ut3u7d3v2zd6G7Ecaze83u7c3+92Z2Zmd2bviIhdOnDhRdebMmS/DvBPhfQjjTA+KqampWHFx8TDiUcSvIqm9vr7+d8bWYEgTpre3dwcy0ypuaIBIGxsaGr4nrnYsYRKJxI2xWGxlGEUxgTjtEOer4mpFCXPq1KnykZGRB11ESSKcRChFcK12OoAAZ5GXD4prIyhx1Mm6VJ9BBNbrHxlusCA/q5CfLeKmEYQ4sSNHjlyWKgq+9FmELxRKFCEusYJCiKlgfnkxDx48WCFJeSdWWlrqrD68Gj1ih4XdCJsN04DiVFVVPXD06NH3SFJeiSF80jAVSYjyE7FDA0rMFErwWphrjBQDilNSUnJfT09PgyTlDQpTbZgqA8+LGUogzhaIcQfMw0aKEmcpAsX5lCTlBQrDYBJoR2461NXVPTY5ObkJF/E3kkRxFqGrse7YsWNLJWnGsIfJW7ICdomYoaaxsfEpCNOGkNooN8B/uK+vj1VuxqSWlvMKiHMCJedehzjkftyxPG/1fgmlMCi5ZWKanJPYxrx5895gf8YpDj6/irdzcadFKIXBif5LTAVO9O1iuuIhjurriJszoRQGDekzYppwlJ+RTOIgPefzDKUwuPNwfMbHDSatuON8UWxPRJwfi6ugOGiQt4nrm2J8iBm40nCLnkNfYbbYBQX54iOGDYZngJP+BaJ+xK/jhHlH9RrYXo7AHv31yjP4MM7tqNhZCa0whNWAV1zcGYE72KdxJ3tC3KyEsiqZQJTvONuNGTAmsS9CLQxK7wjbDZgrEAZU4jRh1RPTF6GuSk4SicQ7cILVGDhejJKkkE2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HAxkr8aY7R6HOFx9tRXVW81xB3JrDiPsKaOdWucQZw/aoe0oyX0XrDBEFlB/N1UcoNYdX9DCENwFqyHMWoTlkkT2BTKIDDNocAdRhTj6tx5gwY5d8CXGBHfkmxGxSlVSKJUYERERERERERERERERERERERERERER8f+M1j9oWLFiRVttbe1If38/f7znvELL9ElLS8tCxlNTU1y1xP8mGaafTCaHS0pKZiH9o7Jd7UfGx8c/09nZObxs2bIa7iPJ2Rjq6Ojgr6HlHS3CsKQgug0nPgRRjhup6kX1F2Kx2LUwr0N6J04qdUm9Yvny5Q8isl5PJtiXAh7H8ZTAJvA7d+/e7blgaCZoEYZXHSfThVJQW1ZW1gUxNpgiiGhFO3futP2CfCbwmSkcr8lNSF3oEmYJTmQxbcS8+kO82rzCLDGIue6PVcB8r4DbXa8+RcZnBiBkoLOm2r8MVeMgxHkcgVVqCNXpEbME+Sk9InLbrl27Uv9jQDtahKEYYtpKDOLj8JdACPVzj05hRESrQfbJ+lyqZWhgY8rqQJtCIFivybA0MIjrCj7/fLZ9dKC1KqWWHC+82hYiInadPXv2Umcjfl6DE8u48FBKkKpObmDbAIJanE2Rzf5REGgtMRQGZLrC7M/scWsjRLA1KC3Xs+NHYZwlhvvg+LPQMOd9jbL2FVXIdJNXoCiymw1pU9Zju+oNG6muUFi/veSc0F5iEK03vHRwtRfixA6ZJWbx4sWzysvL2fNl27IU6b9lOkGJ6cK+wwid9FlSEPj/uVuwn/M3qGaM1hLDaoSMH84QDnGYILsXsYFFVIO0plRRCI61gWJgWxsD7NX8PKqahiFBUdF/ATtoXvuO091uAAAAAElFTkSuQmCC'; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $file = 'images/' . time() . '.png'; $success = file_put_contents($file, $data); print $success ? $file : '无法保存文件.'; ?>
其中$img为canvas导出的base64编码的图片,替换掉其中不需要的信息,再使用base64_decode还原二进制图片流,然后将还原的图片流写入文件。