1

I am creating image in Core PHP from text with this code

header ("Content-type: image/png");
$text='test@example.com';
$string = $text;                                            
$font   = 3;
$width  = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);

$im = @imagecreate ($width,$height);
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, 0, 0,  $string, $text_color);
echo imagepng ($im);

The code above works fine In core PHP file but now i am working on project in laravel framework .I am totally new to laravel . When i try the same code in laravel it doesn't work . I tried changing response methods but still failed here's what i have tried.

public function imgCreate(){
    header ("Content-type: image/png");
    $text='test@example.com';
    $string = $text;                                            
    $font   = 3;
    $width  = ImageFontWidth($font) * strlen($string);
    $height = ImageFontHeight($font);
    $im = @imagecreate ($width,$height);
    $background_color = imagecolorallocate ($im, 255, 255, 255); //white background
    $text_color = imagecolorallocate ($im, 0, 0,0);//black text
    imagestring ($im, $font, 0, 0,  $string, $text_color);

    echo imagepng($im);
    //return response($im)->withHeaders(['Content-type'=>'image/png']); 
}

So basically,i pasted the same code in controller function (imgCreate) . I tried different response methods all seem to have an error When i return like this

 //echo imagepng($im);
return response($im)->withHeaders(['Content-type'=>'image/png']);

It throws an error "The Response content must be a string or object implementing __toString(), "resource" given."

and when i tried simple method by

echo imagepng($im);
//return response($im)->withHeaders(['Content-type'=>'image/png']);  

i get an unreadable string like this :-

"�PNG IHDRp K�xPLTE���U��~�IDAT�c0S �� ��q��������67?>�/�Ð8ۘ��f��3�%Hn�cH���}����H� �riggc�0Ncȝm�qss��ǒ%7�1X||x����l����ripW400#; �^10���IEND�B`�1"

Is it possible to create image from text in laravel or not ? or am i doing something wrong ? According to me the problem seems to be header "Content-type" which is responsible for returning result in the form of image.

Thanks in Advance !!!!!

Prince Arora
  • 354
  • 1
  • 6
  • 20

2 Answers2

5

You can achieve this by encoding image to base_64

    $text='test@example.com';
    $string = $text;                                            
    $font   = 3;
    $width  = ImageFontWidth($font) * strlen($string);
    $height = ImageFontHeight($font);
    $im = @imagecreate ($width,$height);
    $background_color = imagecolorallocate ($im, 255, 255, 255); //white background
    $text_color = imagecolorallocate ($im, 0, 0,0);//black text
    imagestring ($im, $font, 0, 0, $string, $text_color);
    ob_start();
    imagepng($im);
    $imstr = base64_encode(ob_get_clean());
    imagedestroy($im);
    return view('index',array('data'=>$imstr));

And to view image in your view

    <img src="data:image/png;base64,{{ $data }}"/>

Inspired by https://stackoverflow.com/a/3386050/8317643

DEarTh
  • 975
  • 1
  • 7
  • 18
0

Your code seems to work when run isolated. I assume laravel sends custom headers before your output or adds something to your output. Both can fail your image.

To solve your problem: find out what the content type is at the browser side and/or find out if laravel outputs something else. Eg html code.

Zim84
  • 3,404
  • 2
  • 35
  • 40
  • yeah exactly i thought this but laravel still returning Content-Type: text/html; so i tried changing header types but seem to failed – Prince Arora Apr 07 '18 at 05:43