I want to create an image with text and store in temp without output to browser
I have used the following code:
header("Content-type: image/jpeg");
$imgPath = 'images/certificate.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = $userid;
$fontSize = 3;
$x = 115;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image,'images/certi.jpg');
imagedestroy($image);
When using imagejpeg to save a file, you do not need the header header("Content-type: image/jpeg"); line
If you are not returning image data, then you should not be setting the header.
Remove the header("Content-type: image/jpeg");
I'm not clear on exactly what you're trying to do, but if the work has been done, there is no need to return any output, if output is not desired.
Related
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 !!!!!
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
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.
I have a function set up to create multiple images with unique serial numbers on each of them. The problem is that its creating a small square on the browser and not redirecting the browser after saving back to the dashboard
header("Content-type: image/png");
$imgPath = 'background1.png';
$string = $serialNumber;
$directory = "serial_image/".$string.".png";
$image = imagecreatefrompng($imgPath);
$color = imagecolorallocate($image, 0, 0, 0);
$font = 'arial.ttf';
$fontSize = 13;
$x = 20;
$y = 212;
imagettftext($image, $fontSize, 0, $x, $y, $color, $font, $string);
imagepng($image, $directory);
I call the function to create 10 images each with the unique serial number. They are created but then the browser stops and just shows this square.
I have tried ob_start() and ob_flush() with no luck
I think its a header issue.
Can someone please help with this one.
I have a captcha Code which works perfectly but I really dont know why it displays funny.
Can anyone help me figure out what might be the cause of this? Here is the code:
<?php
session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
?>
This is what is displays:
kX’OýÓx£Ç> ñ=¨µÖ/b{ct÷¯½¬6É-ÀiH¡ä csdòyäæcÆ^"Ö|7¥è¦ss¤i™û%³‘¶>02q–Ú2q;A!p+Ÿ¢€:¿øX>'þÂþÈþÓÿDûöo™öx¾Ñö]Û¼?o›ågø7mÇËŒqF‡ñÄú•Ÿ¦j~M½·öWkx¤šÓÎ]²ù²™!Ü:ìeä“Ô“\¥QEQEÿÙ
You're missing the header. Add the following after session_start():
header('Content-Type: image/jpeg');
The output will be something like:
When you're creating an image like this, you need to tell the browser what type of content you're wanting to display. Hence the reason:
header("Content-type: image/jpeg");
is needed. Read the Wiki article on MIME if this confuses you. You'll also find that you'll need to specify the content type whenever you're generating other file types as well (for example: PDF files generated on the fly with PHP will require header('Content-type: application/pdf'); )
I have an image on my server and I want to write text to it. Like a watermark. I am able to write text to the image, but I want to add a background to the text so it's easy to read. Here is what I have so far.
header("Content-type: image/jpeg");
$imgPath = 'pic.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 224, 73, 87);
$string = "Please type the word in the circle.";
$fontSize = 8;
$x = 25;
$y = 200;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
in this class you need to have a background base.png and font arial.ttf you can have a diffident font but must be a ttf .if you want to have diffident font format you must make change on code
class SecurityImg{
static function Image_Create($basename){//Create image
$im =imagecreatefrompng ($basename);
//only replace imagecreatefrompng with imagecreatefromjpeg for open jpg instead of png
return($im);
}
static function PutTextOnImage($text,$baseimage,$angel,$xi,$yi){
// Create some colors
$text_color= imagecolorallocate($baseimage, 255, 50, 150);
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($baseimage, 15, $angel, $xi, $yi, $text_color, $font, $text);
return($baseimage);
}
static function Create($imgbase,$TEXT){
$ifp=self::Image_Create($imgbase);
$im=self::PutTextOnImage($TEXT,$ifp,0,10,20);
return($im);
}
}
$Securityimg=new SecurityImg();
$im=$Securityimg->Create("base.png","test");
// Output the image
// Set the content-type
header('Content-Type: image/jpeg');
imagejpeg($im);
// Using imagepng() results in clearer text compared with imagejpeg()
imagedestroy($im);
Check out this :
<?php
$stamp = imagecreatefrompng('stampimg.png');
$im = imagecreatefrompng('mainimage.png');
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$imgx = imagesx($im);
$imgy = imagesy($im);
$centerX=round($imgx/2);
$centerY=round($imgy/2);
imagecopy($im, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
I wrote this function to create png file from text :
function pngfromtext($text){
$fontsize = 5;
$width = imagefontwidth($fontsize)* strlen($text);
$height = imagefontheight($fontsize);
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 255, 255);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
}
I put codes to functions.php file, When use this function in another page I get this error:
Warning: Cannot modify header information - headers already sent by (output started at ..\functions.php on line 58
�PNG IHDRZ^%JPLTE����ٟ�tRNS#��f�IDAT�c` Hȱ�7�H��'��`c��s�����i��$���Hl`8��Ɛ�� ��#�c��p�� q�3f�òm�� �g�ـ�6fF ���h�bc�sXd4c�A4����?|�¦����r+���!IEND�B`�
What is wrong?
Set the header at the point where you know that your output is going to be an image. This means set this statement
header('Content-type: image/png');
at the beginning of your php script.
There is also a possibility that the header-command was already executed before this point here.
You can't use multiple header in on page.
before header('Content-type: image/png'); put ob_clean();
that cleans the response object so you can add the headers again