I'd like to get the content of the file from imagePng()
, on my code, it returns empty.
Just for a test, if I remove the ob_end_clean()
and it's printing the content but I don't know how can I getting it. I guess I'm missing something in my code.
public function create_thumb($content_file, $percent=5){
$result = array(
'success' => true,
);
list($width, $height) = @getimagesize($content_file);
$n_width = $width * $percent;
$n_height = $height * $percent;
$image_src = @imageCreateFromPng($content_file);
if($image_src){
ob_start();
$image = @imagecreatetruecolor($n_width, $n_height);
@imagecopyresampled($image, $image_src, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
@imagepng($image, null, 100);
$result['thumb'] = ob_get_contents();
ob_end_clean();
}else{
$result['success'] = false;
$result['image_src'] = $image_src;
}
return $result;
}
My Return:
{
"success": true,
"thumb": {
"success": true,
"thumb": ""
}
}
Solution
It was missing base64_encode(ob_get_contents())
on my code.
ob_start();
@imagepng($image, null, 100);
$result['thumb'] = 'data:image/png;base64,' . base64_encode(ob_get_contents())
ob_end_clean();