您好我正在尝试使用php旋转图像,如下所示,我在base64和rotaciono中获取图像,但是当我尝试将其再次转换为base64错误时,我会这样做出现?
$img64=$_POST['IMG'];
$img = str_replace('data:image/jpeg;base64,', '', $img64);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$source = imagecreatefromstring($data);
$rotate = imagerotate($source, 90, 0); // if want to rotate the image
$data = base64_encode($rotate);//ERROR
错误:
<b>Warning</b>: base64_encode() expects parameter 1 to be string, resource given in <b>C:\EasyPHP-DevServer-13.1VC9\data\localweb\projects\STEP\php\rotateLand.php</b> on line <b>9</b><br />
答案 0 :(得分:0)
您正在尝试将图像对象传递给base64_encode,它只接受一个字符串作为参数。
查看有关将图像对象转换为字符串进行base64编码的this question。
答案 1 :(得分:0)
$rotate
不是字符串;这是一个图像资源。不幸的是,没有简单的方法将它变成一个字符串 - 你必须使用输出缓冲。看看this answer。
答案 2 :(得分:0)
感谢您的帮助,我的代码看起来像这样。问题解决了
$img64 = $_POST['IMG'];
$img = str_replace('data:image/jpeg;base64,', '', $img64);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$source = imagecreatefromstring($data);
$rotate = imagerotate($source, $_POST['rot'], 0);
ob_start();
imagejpeg($rotate);
$contents = ob_get_contents();
ob_end_clean();
echo $rotate = base64_encode($contents);?>