Generating preview images for DOC, TXT and RTF files - php

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.

Related

Using Malayalam language word in imagettftext()

I am trying to convert Malayalam(Indian language) words to image using php imagettftext() function. I am using Malayalam font as well. Here is a block of code I am using.
// malayalam word
$utf8str = "കറുപ്പ്";
// buffer output in case there are errors
ob_start();
// create blank image
$im = imagecreatetruecolor(400,40);
$white = imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,imagesx($im),imagesy($im),$white);
// write the text to image
$font = "anjali.ttf";
$color = imagecolorallocatealpha($im, 50, 50, 50, 0); // dark gray
$size = 20;
$angle = 0;
$x = 5;
$y = 25;
imagettftext($im, $size, $angle, $x, $y , $color, $font, $utf8str);
// display the image, if no errors
$err = ob_get_clean();
if( !$err ) {
header("Content-type: image/png");
imagepng($im);
}
With this code, I get the following output
I have tried many Malayalam fonts and codes from stackoverflow. Can anybody help me with this?
After a long research, I came to know that existing malayalam fonts does not support all the characters. We will have to wait for new fonts to come up that support all the characters.

How to generate a text into an png without spacing (top and bottom)

I am trying to write a php function to convert text (via imagettftext) into an image (png) cropped from the top to the bottom of the letters.
I've tried with GD (php) and html2canvas (javascript)
But the generated image has always a spacing around the text. The text is contained in a for the height and line-height...
Try this,
<?php
header ("Content-type: image/png");
$text='CONVERT ME TO AN IMAGE';
$string = $text;
$font = 3;
$width = ImageFontWidth($font) * strlen($string); // minimum width required to hold the font
$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);
?>

How to convert UTF-8 URDU text to image on the fly in Php?

اردو کے فونٹ کے مسائل
میرے ویب پیچ کے فونٹ ، براوزر کی وجہ سے بدل جاتے ہیں، میں اپنے صفحے کو امیج
بنا کر براوزر کو دینا چایتا ہوں۔
I have tried a number of tutorials / examples and searches. I could not find solution.
A Tip: ( Hope it will make someone's life easier.)
A big trouble , I resolved in image creation was use of Windows Share-Point to save my .php files. All files i was saving , had something UTF BOM .
When I saved same file using simple Notepad in UTF-8 encoding , my image creation in PHP started.
My Problem.
I have a sentence in URDU ( a language Spoken by a billion in South Asia ), say a string - saved in UTF-8 , perfectly being saved in database and on shown at web browsers.
Unlike English, Urdu letters join to make words, While joining , their apparent shape is cropped - size or style.
I am working on this code
<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
//$SourceFile is source of the image file to be watermarked
//$WaterMarkText is the text of the watermark
//$DestinationFile is the destination location where the watermarked images will be placed
//Delete if destinaton file already exists
#unlink($DestinationFile);
//This is the vertical center of the image
$top = getimagesize($SourceFile);
$top = $top[1]/2;
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
//Path to the font file on the server. Do not miss to upload the font file
$font = 'arial.ttf';
//Font sie
$font_size = 36;
//Give a white shadow
$white = imagecolorallocate($image_p, 255, 255, 155);
imagettftext($image_p, $font_size, 0, 10, $top, $white, $font, $WaterMarkText);
//Print in black color
$black = imagecolorallocate($image_p, 0, 0, 0);
imagettftext($image_p, $font_size, 0, 8, $top-1, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
?>
<?php
/*
// The text to draw
require('../../../I18N/Arabic.php'); // It converts the left side language to right side
$Arabic = new I18N_Arabic('Glyphs'); //
$font = './DroidNaskh-Bold.ttf';
$text = "جب درخشاں ہوں ستاروں کے چراغ";
$text = $Arabic->utf8Glyphs('جب درخشاں ہوں ستاروں کے چراغ ');
It gave me ﻍﺍﺭچ ےک ںﻭﺭﺎﺘﺳںﻭہ ںﺎﺸﺧﺭﺩ ﺐﺟ , reversing the string ,
*/
$text = "ﻍﺍﺭچ ےک ںﻭﺭﺎﺘﺳںﻭہ ںﺎﺸﺧﺭﺩ ﺐﺟ"; // Problem in joining of Urdu -
// Text
// Sequence of characters reversed in string using utf8Glyphs( ) ;
$SourceFile = 'nature (28).jpg';//Source image
$DestinationFile = 'watermarked/sky.jpg';//Destination path
//Call the function to watermark the image
watermarkImage ($SourceFile, $text, $DestinationFile);
//Display watermarked image if desired
if(file_exists($DestinationFile)){
echo "<img src=\"watermarked/sky.jpg\">";
echo "<p>The image has been watermarked at '".$DestinationFile."'</p>";
}
?>
My image creation on the FLY for other then UTF-8 is working fine.
Kind regards.
You'll need an additional library to perform Arabic glyph joining. Check out AR-PHP.

print text over picture on the fly with 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

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.

Categories