print text over picture on the fly with php - php

I am trying to combine two pieces of code in one. The first part convert the given .jpg image to .png and put it to IMG folder with the same name just different extension.
The second part of the code should print "text" over the picture while taking the picture from the folder IMG (the .png image created by the FIRST PART).
I am doing something wrong obviously as what I get printed in the end is just the source of the picture instead of using the < img src= part in the end.
I have tried to search for some identical issues here, but with no luck.
//FIRST PART
$file = basename($picture, ".jpg");
$file_create = "img/$file.png";
$image = ImageCreateFromJPEG("$picture");
ImageJpeg($image, "img/$file.png");
ImageDestroy($image);
//SECOND PART
header("Content-type: image/png");
$imgPath = "$file_create";
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = "test";
$fontSize = 3;
$x = 115;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
$final=imagejpeg($image);
print "<img src='img/$final' width=150 height=150 valign=middle title='$item' alt='$item'>";
Any ideas?

Here is a good tutorial for same
http://www.phpforkids.com/php/php-gd-library-adding-text-writing.php

Related

Running imagecreatefrompng throught image array but array just returns 1 image

I'm trying to crop of the bottom part of an image, which i get from a remote site.
Got it also working with the following code:
$u = $xmlString->xpath('//*[contains(#u, "/fds/")]');
foreach($u as $result) {
$itemLinks = 'http://exampleurl/'.$result['u'].'.png';
$in_filename = $itemLinks;
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 264;
$new_width = $width;
$image = imagecreatefrompng($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$transparentindex = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefill($new_image, 0, 0, $transparentindex);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
header("Content-Type: image/png");
imagepng($new_image);
}
The only problem with this code is the following:
I'm getting the Image Path from a remote XML file, which i filtered with xpath. So all my finished Image url's are stored in an array. But my code is just generating 1 image which contains the perfect size which i need.
It happens because its just generating 1 img in the end. Maybe also happens because it just returns 1 image with the name img.
Question: Does anyone have a idea why it wouldnt return all images?
For example:
Array contains 15 image links.
Im running my foreach loop through the array.
Foreach loop returns only 1 image.
Your issue is caused by the last two lines:
header("Content-Type: image/png");
imagepng($new_image);
This has the same effect as opening a single image file (like a .PNG) in your browser. You can't view multiple image files at the same time unless they are embedded in a HTML page.
If you want to show all fifteen images at once in the browser you'll need to save each image as you process it and then output an HTML file, something like this:
$images = '';
foreach($u as $result) {
// your existing code...
imagepng($new_image, './'.$result['u'].'.png');
$images .= '<img src="'.$result['u'].'.png">';
}
// wrap this in valid HTML syntax (<head>, <body>, etc.)
echo $images;

Text to image in nice way - with automatic break of long lines and good font

I am converting text into image so that users can directly share quotes into Facebook and Twitter.
I want to make the images with php gd. I have this code which works fine but it has some issues like long lines that are not breaking automatically. Also the font is not working as I want to use cosmic font.
Here is my code
<?php
header("Content-type: image/png");
$string = ' <p>I think the whole under-eye-bag thing is hereditary, and I just got lucky.</p> <p class="bq_fq_a"> <a id="qut" title="julianna_margulies"> Julianna Margulies </a> </p> <br>';
$font = '12';
$fonttype = '/path/cosmic.TTF';
$width = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);
$image = imagecreatetruecolor ($width,$height);
$white = imagecolorallocate ($image,255,255,255);
$black = imagecolorallocate ($image,0,0,0);
imagefill($image,0,0,$white);
imagestring ($image,$font,0,0,$string,$black);
imagepng ($image);
imagedestroy($image);
?>

Adding Text with GD Lib

I have been ripping my hair out with this and have tried many many solutions on here to no avail.
I am trying to add some text to an image, but all it is doing is showing my background image, is there anything glaringly obvious that I'm doing wrong here?
Thanks in adcance
<?
header('Content-Type: image/jpeg');
$fbid = $_POST['fbid'];
$background_img = $_POST['background'];
$message = $_POST['text'];
$ts = $_POST['ts'];
$filename = $fbid . "-" . $ts . ".jpg";
$image_canvas = imagecreatetruecolor(640,400);
$background = imagecreatefromjpeg($background_img);
$overlay = imagecreatefrompng("../images/image-overlay.png");
imagecopyresampled($background, $overlay, 0, 0, 0, 0, imagesx($overlay), imagesy($overlay), imagesx($overlay), imagesy($overlay));
imagefilledrectangle($image_canvas, 0,0,150,30, $background);
$white = imagecolorallocate($background, 255, 255, 255);
imagettftext($image_canvas, 25, 0, 50, 50, $white, "arial.TTF", $message);
imagejpeg($background,"../created/" . $filename, 100);
imagedestroy($background);
You're missing the canvas. Start the build with imageCreateTrueColor.
$imageCanvas = imageCreateTrueColor($width, $height);
//your code
$background = imagecreatefromjpeg($background_img);
//more of your code
imagefilledrectangle($imageCanvas, 0, 0, 150, 30, $background);
//now do the same for the text only us imag
imagettftext($imageCanvas, 25, 0, 50, 50, $white, "arial.TTF", $message);
Your merging the jpeg and the text elements on the $imageCanvas.
Have a look at this. It works, and the link to the page is below. Based on your original post, I believe this is what your looking for.
/* first composite the canvas with the background */
$background_img="../img/adfuba_october.png";
$compositeString = "composite.png";
list($width,$height) = getimagesize($background_img);
$image_canvas = imagecreatetruecolor($width,$height);
$background = imagecreatefrompng($background_img);
imagecopyresampled($image_canvas,$background,0,0,0,0,$width,$height,$width,$height);
/* now add the text */
$fontPath = "path/to/your/fontFile/ARIAL.TTF";
$fontSize = 24;
$percent = 0.25;
$txt_x = abs($width*$percent);
$txt_y = abs($height*$percent);
$color = "008844";
$message = "This is User Text";
imageTTFtext($image_canvas, $fontSize, 0, $txt_y, $txt_y, $color, $fontPath, $message);
/* now generate the file */
imagepng($image_canvas, $compositeString, 0) or die("error saving png");
?>
<p>This is a composite image:<br><img src="<?php echo $compositeString;?>"></p>
You can see the composite image here.
Couple things to keep in mind. The path to your TrueType font face should be absolute even if the TrueType font file is located in the same directory as your script.
Also, the canvas is your background object, then you layer images or text over top the canvas.
Finally, (and you may have figured this out) your layered elements are order dependent form canvas to text. Meaning canvas -> background -> another graphic -> then the text. Otherwise, you could end up covering up an element you meant to render in front. Hope that helps.

Php code help regarding images

So I have this php script which generates random text on an image.
The text file is a different php file and the image file is a seperate php file.
The image.php file calls this text.php to select a random text.
This version works fine, but is it possible to generate a random image on my existing image file?
I have included the current versions of my code.
This is text.php:
<?php
$t[] = 'Sample text 1 ';
$t[] = 'Sample text 2';
shuffle($t);
?>
This is image.php:
<?php
require_once 'text.php';
$text = wordwrap($t[0], 31, "\n", true); //text
$image = imagecreatefromjpeg('img_empty.jpg'); //background image
?>
Any suggestions are welcome.
Your question is bit unclear.. You don't want text.php then you can directly use it's code in image.php as shown below.
image.php
<?php
$t[] = 'Sample text 1 ';
$t[] = 'Sample text 2';
shuffle($t);
$text = wordwrap($t[0], 31, "\n", true); //text
$image = imagecreatefromjpeg('img_empty.jpg'); //background image
Several options here
Have several background-images saved and select one randomly among them.
Use GD and Image Functions to draw on your existing image before outputting it (look at imageline() for drawing lines for example).
Use GD's imagecreatetruecolor() to create the image, to even get random width/height.
if you want to create an image with random text this will do it
image.php
require_once 'text.php';
header("Content-type: image/png");
$string = wordwrap($t[0], 31, "\n", true); //text
$font = 2;
$width = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);
$image = imagecreatetruecolor ($width,$height);
$white = imagecolorallocate ($image,255,255,255);
$black = imagecolorallocate ($image,0,0,0);
imagefill($image,0,0,$white);
imagestring ($image,$font,0,0,$string,$black);
imagepng ($image);
imagedestroy($image);
UPDATED CODE TO YOUR RESPONSE:
if you want to get your random text over a specific image
require_once 'text.php';
header("Content-type: image/png");
$string = wordwrap($t[0], 31, "\n", true); //text
$image = ImageCreateFromJPEG("img_empty.jpg");
//Defining color. Making the color of text as red (#FFF) as 255,000,000
$color = imagecolorallocate($image , 255, 000, 000); //
//put string over image with $color as color
imagestring($image,5,126,22,$string,$color);
imagejpeg($image,NULL,100);
The above code will put a red color random text over your specified image.this works for me. Also try changing the colors defined in imagecolorallocate() function.
reference: http://blog.doh.ms/2008/02/12/adding-text-to-images-in-real-time-with-php/

Generating preview images for DOC, TXT and RTF files

I've been trying to find a method of achieving this for awhile now with no luck.
Unfortunately none of these formats are supported by ImageMagicK
Thanks
Customize the code to fit into your requirements.
I've attached a sample image generated from a text file that contains "Generating preview images for TXT files" sentence by the following code:
<?php
Header ("Content-type: image/gif");
$txtfile = "test.txt";
$testarr = array();
if(!file_exists($txtfile)){
$string = "File not found.";
}
else{
$testarr = file($txtfile);
srand ((float) microtime() * 10000000);
$string = '-'.$testarr[array_rand($testarr)];
$string = substr($string,0,strlen($string)-2);
}
$font = 4;
$width = ImageFontWidth($font)* strlen($string);
$height = ImageFontHeight($font);
$im = ImageCreate($width,$height);
$x=imagesx($im)-$width ;
$y=imagesy($im)-$height;
$background_color = imagecolorallocate ($im, 242, 242, 242); // white colored background
$text_color = imagecolorallocate ($im, 0, 0,0); // black colored text
$trans_color = $background_color; // transparent
imagecolortransparent($im, $trans_color);
imagestring ($im, $font, $x, $y, $string, $text_color);
imagegif($im);
ImageDestroy($im);
?>
Links would be useful :
http://visionmasterdesigns.com/tutorial-convert-text-into-transparent-png-image-using-php/
http://www.phpro.org/examples/Text-to-Image-with-GD.html
I think you are going to need to do this on a machine with a GUI/Window environment by opening the files and then taking screenshots of them like http://litmus.com/ or https://browserlab.adobe.com/en-us/index.html
There is a program written do this for PDFs from HTML using WebKits rendering engine: http://code.google.com/p/wkhtmltopdf/
My suggestion is to convert all those documents to pdf and then print that pdf to image.
For Doc to PDF, you could try LiveDocx.

Categories