做图片裁切时,得到的图片是base 64格式的,因此就遇到了如何将base 64格式的数据转换为图片的问题。
代码如下:
$img = I("post.coverimg");
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img, $result)) {
$type = $result[2];
$name = time() . '_' . mt_rand() .".{$type}";
$new_file = "./Uploads/" . $name;//文件夹位置
file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img)));
}
然后顺利的将图片存到了文件夹中。