0

Hello I'm trying to rotate an image with php as follows, I get the image in base64 and rotaciono, but when I try to turn it again into a base64 error as I do that emerges?

$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

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 />
Vanessa
  • 27
  • 6

3 Answers3

0

You're trying to pass an image object to base64_encode, which only takes a string as a parameter.

Take a look at this question regarding converting an image object to a string for base64 encoding.

Community
  • 1
  • 1
Lucas Penney
  • 2,624
  • 4
  • 27
  • 36
0

$rotate isn't a string; it's an image resource. Unfortunately there's no easy way to turn it into a string - you have to use output buffering. Have a look at this answer.

Community
  • 1
  • 1
George Brighton
  • 5,131
  • 9
  • 27
  • 36
0

Grateful for help, my code looked like this. problem solved

$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);?>
Vanessa
  • 27
  • 6