Creating image and loading it in the page - php

I want to create/generate an image with PHP, adding text into it, but without saving it into the FTP, I'd like to load it on the site by changing the content type to image/png, the same way I did it with ASP.NET:
Response.ContentType = "image/png";
rImage.Save(Response.OutputStream, ImageFormat.Png);
I found a function called file_put_contents, but I'm not so sure this is what I'm looking for. If you know how to do something like this, please tell me.
I'm trying this code, but it fails to load the image, and shows the browser's default error image.
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

Below example will regenerate an image with text over it:
<?php
header("Content-type: image/png"); // Set the header so that browser can output the image
$string = 'Print this text on Image'; // Text to be added on the image
$im = imagecreatefrompng("images/button1.png"); // Take base image
$orange = imagecolorallocate($im, 220, 210, 60); // Allocate a color for an image
imagestring($im, 3, 0, 0, $string, $orange); // Write the string at the top left
imagepng($im); // Create new image
imagedestroy($im); // destroy object
?>
Reference: http://php.net/manual/en/image.examples-png.php

Related

PHP imagejpeg - convert output to base64

I am generating a simple text in an image like this...
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($im);
// Free up memory
imagedestroy($im);
I need the output as a base64 string, I have tried base64_encode($im) but it is not working correctly for me.
Does anybody have an example I can see?
Try using the ob_get_clean function to get the image from the output buffer and then encode it to base64:
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
ob_start();
// Output the image
imagejpeg($im);
$img = ob_get_clean();
ob_end_clean();
// Free up memory
imagedestroy($im);
echo base64_encode($img);

PHP imagettftext() breaks the image if I use imagecreatefrompng, imagecreatefromjpeg and imagecreatefromgif

The image get loaded to the browser fine but when I try writing some text onto it, the image breaks (like this: http://www.tradenepal.com.np/test.php ). When I comment out imagettftext(), the image does not load again. This happening on my localhost and I use WampServer Version 2.5. I have gone through so many comments on the inetrnet but I can't seem to know what the problem is. Any help would much be appreciated. Thank you. My code:
<?php
//Set content type
header('Content-type: image/jpeg');
// Create image from existing image
$jpgImage = imagecreatefromjpeg('file.jpg');
// Allocate color for text
$white = imagecolorallocate($jpgImage, 255, 255, 255);
// Set Path to Font File
$font = 'arialbd.ttf';
// Text to print to image
$text = 'Testing text output';
// Print Text On Image
imagettftext($jpgImage, 75, 0, 50, 400, $white, $font, $text);
// Send Image to Browser
imagejpeg($jpgImage);
// Clear Memory
imagedestroy($jpgImage);
?>
// Send Image to Browser
imagepng($jpg_image); <------ remove image type png
// Output the image
imagejpeg($jpg_image);
I've tested, it works.
<?php
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('file.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'arialbd.ttf';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>

Wamp Server not displaying image on page inside html tags?

why this works fine on wamp server:
<?php
//Let's generate a totally random string using md5
$md5_hash = md5(rand(0,999));
//We don't need a 32 character long string so we trim it down to 5
$security_code = substr($md5_hash, 15, 5);
//Set the session to store the security code
$_SESSION['security_code'] = $security_code;
//Set the image width and height
// $im = imagecreate(97, 18);
$im= #imagecreate(97, 18)
or die("Cannot Initialize new GD image stream");
//white background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 167, 167, 167);
//Add randomly generated string in white to the image
imagestring($im, 5, 27, 1, $security_code, $textcolor);
//Tell the browser what kind of file is come in
header("Content-Type: image/png");
//Output the newly created image in jpeg format
imagepng($im);
//Free up resources
imagedestroy($im);
?>
but this, doesn't work (just add html tags) ?
<html>
<body>
<?php
//Let's generate a totally random string using md5
$md5_hash = md5(rand(0,999));
//We don't need a 32 character long string so we trim it down to 5
$security_code = substr($md5_hash, 15, 5);
//Set the session to store the security code
$_SESSION['security_code'] = $security_code;
//Set the image width and height
// $im = imagecreate(97, 18);
$im= #imagecreate(97, 18)
or die("Cannot Initialize new GD image stream");
//white background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 167, 167, 167);
//Add randomly generated string in white to the image
imagestring($im, 5, 27, 1, $security_code, $textcolor);
//Tell the browser what kind of file is come in
header("Content-Type: image/png");
//Output the newly created image in jpeg format
imagepng($im);
//Free up resources
imagedestroy($im);
?>
</body>
</html>
Can you tell me why inside html tags php code doesn't work on WAMP Server, but only php code works fine?

From text to images in PHP

Please take look the demo site at
http://jsfiddle.net/Alidad/fexGj/
That demo is called "Image title", my next step is to convert both text and images as one group to images so that way i can click right mouse to copy and paste over.
However, there is PHP code that allow me to convert into the image from text but I can't hardly figure out how can I convert from "image title" (demo) into the HTML!
Here is sample PHP code but I can't figure out how to combine it!
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Have any help me please how to convert this to images from jQuery code!
AM
Easiest way would probably be to just do:
// index.html
<img src="render_image.php?text=Hello World" />
and then:
// render_image.php
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, $_GET['text'], $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Using this with slight modifications to your javascript code should work.
More info on using the $_GET variable: http://php.net/manual/en/reserved.variables.get.php

How to draw a png image in php?

$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
I have already draw a black image i want draw a file suppose 3.png on it ..
How to do that ?
You have to load an image you want to draw and then use imagecopy() to draw it:
// the part you already have; creates 538x616 px black image
$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
// load image from file and draw it onto black image;
// for loading PNG, use imagecreatefrompng()
$overlayImage = imagecreatefromjpeg('macro_photo_1.jpg');
imagecopy($image, $overlayImage, 10, 10, 0, 0, imagesx($overlayImage), imagesy($overlayImage));
// send image to the browser
header("Content-Type: image/png");
imagepng($image);
exit;
I would also advise to go through the list of GD and Image functions to see what (and how) can be done with images in PHP.
imagepng($image);
And if you haven't already you need:
header('Content-Type: image/png');
Before you call imagepng to let the browser know it's looking at an image not an HTML page.
Saroj, http://www.php.net/manual/en/function.imagecreate.php and from same page, here is the example snippet
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
HTH!

Categories