laravel - How to add text to an image in php and return the image to browser
one text
I'm using following code to create captcha image:
create function code:
public function create(){
$this->image=imagecreatetruecolor($this->width,$this->height);
for($i=0,$x=5;$i<$this->length;$i++,$x+=20){
$angel=[mt_rand(3,35),mt_rand(350,360)];
$y=mt_rand(30,35);
$textColor=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imagettftext($this->image, $this->fontSize, $angel[mt_rand(0,1)], $x, $y, $textColor, $font, $characters[$i]);
}
return response(imagepng($this->image), 200, ["Content-type" => "image/png"]);
}
controller file:
public function getCaptcha(){
if (ob_get_contents()) {
ob_clean();
}
return $captcha->create();
}
sometimes it shows error :
SourceThe byte stream was erroneous according to the character encoding that was declared. The character encoding declaration may be incorrect. error image