1

I am trying to get a image data from url using file_get_contents() and then crop the image and pass the the cropped image data as string to another function. My codes are,

 $imageData = file_get_contents($imageurl);
$imgcrop = imagecrop($x,$y,$width,$height);

gettype($imgcrop ) // a resource type, I want them to be string/bytes

uploadThisData(imgcrop) ;

So my problem is $imgcrop data is resource type, how can I get string/bytes value from this? Thank you.

  • Does this answer your question? [Convert input=file to byte array](https://stackoverflow.com/questions/37134433/convert-input-file-to-byte-array) – Molly Dec 12 '19 at 04:20

1 Answers1

1

You can save your image resource as a png by doing imagepng($imgcrop,'cropped.png')

You can then convert the png into a byte array like this

imagepng($imgcrop,'cropped.png');
$cropedpng = imagecreatefrompng("cropped.png");
$array = array(); 

foreach(str_split($cropedpng) as $char){ 
    array_push($array, ord($char)); 
}
var_dump(implode(' ', $array));

Or you could convert it into Base64 like this

imagepng($imgcrop,'cropped.png');
$cropedpng = imagecreatefrompng("cropped.png");
$base64 = ''. base64_encode($cropedpng).'';
DCQ
  • 111
  • 6