I am converting text to image using PHP's imagettftext function and then saving it to the server. The problem I'm having is getting it to save in the directory that I want, it has no problems saving to the current directory where the script runs.
Here is my code:
function txt2img($text, $fSize)
{
// Path to our font file
$fType = 'verdana.ttf';
// First we create our bounding box for the text
$bbox = imagettfbbox($fSize, 0, $fType, $text);
// We calculate the width and height using the x and y points.
// Width is: upper right corner, X position - upper left corner, X position
$width = $bbox[4] - $bbox[6] + 8;
// Height is: lower left corner, Y position - upper left corner, Y position
$height = $bbox[1] - $bbox[7] + 4;
// Determine the x position for the text to start
$x = 2;
// Determine the y position for the text to start
$y = $height - 4;
// Create the image
$im = imagecreatetruecolor($width, $height);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
// Create the background color to match the website
$default = imagecolorallocate($im, 255, 238, 198);
// Set the background color
imagefilledrectangle($im, 0, 0, $width, $height, $default);
// Write it
imagettftext($im, $fSize, 0, $x, $y, $black, $fType, $text);
// Set the location to save the image.
//$save = "email_address.png";
$save = "../images/contact/email_address.png";
// Set the folder permissions to allow us to write to the folder.
chmod("../images/contact/",0755);
// .png tends to look better than a .jpg
imagepng($im,$save);
imagedestroy($im);
// Return the image dimensions so they can be stored in the database. This is used to properly diplay the image on the front end.
return ($width."+".$height);
}
If I use the $save location that is commented out, it works perfectly fine, it just isn't in the directory that I want it to be in. I have tried other paths, such as "/images/contact/email_address.png" but no joy. This also worked without the "chmod" permission change, I just added it as another option for troubleshooting. I have also gone directly to the folder and set the permissions for the user, again, no joy.
I'm running this on a local WAMP server. Also, $text has been checked and $fSize is the font size, which has also been checked. Because this works so well in the current directory, I feel this is not a coding issue as much as it is a directory issue, but that's just my view.
So it does save in the folder you're working in as per the commented out save. Have you tried images/contact/email_address.png"; and /images/contact/email_address.png"; without the dots ? The dots are going up a level and if that's above the root it will be unaccesable to php
Related
I tried to overlay some text on a youtube thumbnail using php GD, but that does'nt seem to work.
The code I have tried is:
<?php
$im = file_get_contents('http://i.ytimg.com/vi/6E9wBFl5o-c/mqdefault.jpg');
$image = imagecreatefromjpeg($im);
$font_size = 14;
$color = imagecolorallocate($image, 255,255,255);
$black = imagecolorallocate($image, 0,0,0);
// and now we do the overlay - the layers of text start top to bottom, so
// the drop shadow comes first
// $image - the base image file we specified above
// $font_size - Well duh. Its the size of the font
// 0 - the angle of the text - we don't want an angle, so we leave it at 0
// 56 - pixels to the right from the leftmost part of the image
// 36 - pixels down from the top of the image
// $black - the color we defined above
// "Test Text" - the text we're overlaying - you can also use a variable here
ImageTTFText ($image, $font_size, 0, 56, 36, $black, "font.ttf","Test Text");
// Now add the actual white text "on top"
ImageTTFText ($image, $font_size, 0, 55, 35, $color, "font.ttf","Test Text");
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
So how can I get the image returned with text written on it.
imagecreatefromjpeg needs a filename as parameter, see http://php.net/manual/function.imagecreatefromjpeg.php
this should work:
$image = imagecreatefromjpeg('http://i.ytimg.com/vi/6E9wBFl5o-c/mqdefault.jpg');
To debug the script you can access the url in the browser. Any warnings or error messages will be readible.
A simple error is that you declare a JPG file in the header, but return a PNG file.
header("Content-type: image/jpg");
imagepng($image);
Also make sure that the font file is really accessible from the PHP file.
I have a very frustrating situation. I am using PHP GD for the first time, and it's been a bit of a rollercoaster relationship. Basically, I am trying to merge 2 images, a square one (with a height/width of x) onto a rectangle (with a width of x and a height of y).
The square needs to be centered vertically. But this isn't the issue - I've managed to position it correctly.
Whats happening is, my rectangle is white. My square has a white background, so when the images are merged, it should just look like my asset on a white rectangluar background.
When I merge the image though, GD is for some reason changing my background white rectangle to black - so you can see the white square in the middle, with black "bars" on top and bottom. Can anyone help?
Code is:
//create copy of original image to correct size
imagecopyresized($dst_image, $src_image, 0,0,0,0,$x_width,$x_height,$orig_img_x_width,$orig_img_x_height);
imagejpeg($dst_image, "resized_copy.jpg", 100);
$img = imagecreatetruecolor(1333, 2000);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill ( $img, 0, 0, $white );
imagefilledrectangle($img,0,0,1333,2000, $white);
imagejpeg($img, "rectangle.jpg", 100);
//merge images
$dest2 = imagecreatefromjpeg("rectangle.jpg");
$src2 = imagecreatefromjpeg('resized_copy.jpg');
imagecopymerge($dest2, $src2, 0, 0, 0, -333.5, $x_width, $x_height, 100);
imagejpeg($dest2, "final_image.jpg", 100);
I've tried using imagecopy instead of imagecopymerge, but I get the same result. I'm sure there is a simple explanation, but I cant seem to find it trawling through the php manual.
I've read your question a few times but I'm not convinced I understand exactly what you are trying to achieve so I've made a few assumptions in producing the below code.
For the sake of simplicity I've created a 'square.jpg' test image file like so:
(Note that I've used small image sizes here so I can show them inline.)
// read in the square test image.
$square = imagecreatefromjpeg('square.jpg');
$square_x = imagesx($square); // 100px
$square_y = imagesy($square); // 100px
// create the rectangular image to merge with.
$rectangle = imagecreatetruecolor(100, 200);
$rectangle_x = imagesx($rectangle); // 100px
$rectangle_y = imagesy($rectangle); // 200px
// note that this isn't white, but rather a lovely shade of blue to better
// show the image on the white SO background!
$white = imagecolorallocate($rectangle, 128, 128, 255);
imagefill($rectangle, 0, 0, $white);
// merge the images.
imagecopymerge(
$rectangle,
$square,
0,
($rectangle_y / 2) - ($square_y / 2), // to vertically centre the square.
0,
0,
$square_x,
$square_y,
75 // Just to show the merge clearly; change back to 100 for your usage.
);
imagejpeg($rectangle, 'final_image.jpg', 100);
imagedestroy($rectangle);
imagedestroy($square);
This gives me the following image in final_image.jpg:
Hope you are doing great.
I’m still a newbie with php so after making some reading and while checking some posts here I was able to put some text over an image with the imagecreatefrompng() function using the PHP GD, users will come to a form and they will be able to enter their name and the name will be written over the image , unfortunately I have been unable to align the text center horizontally, I tried all ways possible (my ways obviously and must be wrong) with imagettfbbox but I failed in all my attempts, could you please guys help me out a little bit to align the string center horizontally? Also since I’m using a kind of alternative big font I need that the size decrease if the name entered is kind of long so this way it will not surpass the image limits and will stay at the center. I’m getting the value of the text from a form as you may check at the beginning of my code:
<?php
$nombre=$_POST['nombre'];
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('fabian.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'fabian.TTF';
// Set Text to Be Printed On Image , I set it to uppercase
$text =strtoupper($nombre);
// Print Text On Image
imagettftext($jpg_image, 75, 0, 50, 400, $white, $font_path, $text);
// Send Image to Browser
imagepng($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
?>
Your help will be highly appreciated, later on I will break my head trying to save the image by clicking a submit button since I do not want the users to save the image by right clicking on it.
Thanks pals!
You need the width of the image and the width of the text to relate both.
// get image dimensions
list($img_width, $img_height,,) = getimagesize("fabian.jpg");
// find font-size for $txt_width = 80% of $img_width...
$font_size = 1;
$txt_max_width = intval(0.8 * $img_width);
do {
$font_size++;
$p = imagettfbbox($font_size, 0, $font_path, $text);
$txt_width = $p[2] - $p[0];
// $txt_height=$p[1]-$p[7]; // just in case you need it
} while ($txt_width <= $txt_max_width);
// now center the text
$y = $img_height * 0.9; // baseline of text at 90% of $img_height
$x = ($img_width - $txt_width) / 2;
imagettftext($jpg_image, $font_size, 0, $x, $y, $white, $font_path, $text);
You can use stil/gd-text class to align the text. Disclaimer: I am the author.
<?php
use GDText\Box;
use GDText\Color;
$jpg_image = imagecreatefromjpeg('fabian.jpg');
$textbox = new Box($jpg_image);
$textbox->setFontSize(75);
$textbox->setFontFace('fabian.TTF');
$textbox->setFontColor(new Color(255, 255, 255));
$textbox->setBox(
50, // distance from left edge
50, // distance from top edge
200, // textbox width
100 // textbox height
);
// text will be aligned inside textbox to center horizontally and to top vertically
$textbox->setTextAlign('center', 'top');
$textbox->draw(strtoupper($nombre));
However it is not a complete answer, because it can't decrease font size automatically.
I am writing to print text to an image using PHP. However, the function imagettftext() uses the baseline, whereas I need the text vertically centered.
So, I either need a method to print text with y not the distance from top to baseline, but from top to top of bounding box OR I need a method using which I could determine the distance between top of bounding box and baseline.
Apparently, I am confusing you. So, to make it clear: I am aware of the function imagettfbbox(). Using that function I can determine height and width of resulting text box. Its height, however, is utterly useless for vertical alignment when printing with imagettftext(), because the Y parameter is not the distance to the top of the box (or even the bottom, but at least something I could have used having the height) but the distance to the baseline of the text within.
EDIT: Why am I not accepting the latest answer?
See my latest comment below the answer, and use this image as a reference.
I do not know if the answer still interested.However, the imagettfbbox() function give you more information than simply the height and the width of the bounding box. It's designed exactly to return information needed by the imagettftext() to manage the text as you want.
The trick lies in the fact that the coordinates returned from imagettfbbox() are not related to the absolute top left corner, but to the baseline of the font for the particular text. This is the reason because the box is specified in point coordinates, and these are often negative.
In short:
$dims = imagettfbbox($fontsize, 0, $font, $text);
$ascent = abs($dims[7]);
$descent = abs($dims[1]);
$width = abs($dims[0])+abs($dims[2]);
$height = $ascent+$descent;
...
// In the example code, for the vertical centering of the text, consider
// the simple following formula
$y = (($imageHeight/2) - ($height/2)) + $ascent;
This works perfectly for my projects.
Hope this help.
Sorry for english.
Marco.
Not entirely sure what your asking...can you give an example? Perhaps imagettfbbox is what you need?
// get bounding box dims
$dims = imagettfbbox($fontsize, 0, $font, $quote);
// do some math to find out the actual width and height
$width = $dims[4] - $dims[6]; // upper-right x minus upper-left x
$height = $dims[3] - $dims[5]; // lower-right y minus upper-right y
edit: Here is an example of vertically centered text
<?php
$font = 'arial.ttf';
$fontsize = 100;
$imageX = 500;
$imageY = 500;
// text
$text = "FOOBAR";
// create a bounding box for the text
$dims = imagettfbbox($fontsize, 0, $font, $text);
// height of bounding box (your text)
$bbox_height = $dims[3] - $dims[5]; // lower-right y minus upper-right y
// Create image
$image = imagecreatetruecolor($imageX,$imageY);
// background color
$bgcolor = imagecolorallocate($image, 0, 0, 0);
// text color
$fontcolor = imagecolorallocate($image, 255, 255, 255);
// fill in the background with the background color
imagefilledrectangle($image, 0, 0, $imageX, $imageY, $bgcolor);
$x = 0;
$y = (($imageY/2) - ($bbox_height/2)) + $fontsize;
imagettftext($image, $fontsize, 0, $x, $y , $fontcolor, $font, $text);
// tell the browser that the content is an image
header('Content-type: image/png');
// output image to the browser
imagepng($image);
// delete the image resource
imagedestroy($image);
?>
Ok, now it's officialy 10 hours that I'm trying to make this work, but by every minute I'm farther and farther from the final goal...
The thing is, I need to create a function, class or whatever that by the selected TTF or truetype font create, properly resize, and fill the image with a text given by the url. The image shouldn't be longer than that text.
I've given up on all my codes, and the best thing I've got so far is taken from the net ...
<?php
if(!isset($_GET['size'])) $_GET['size'] = 20;
if(!isset($_GET['text'])) $_GET['text'] = "Moj tekst";
$get_font = ( isset( $_GET['font'] ) ) ? $_GET['font'] : 'arial';
$size = imagettfbbox($_GET['size'], 0, "font/".$get_font.".ttf", $_GET['text']);
$xsize = abs($size[0]) + abs($size[2]);
$ysize = abs($size[5]) + abs($size[1]);
$image = imagecreate($xsize, $ysize);
imageSaveAlpha($image, true);
ImageAlphaBlending($image, false);
$transparentColor = imagecolorallocatealpha($image, 200, 200, 200, 127);
Imagefill($image, 0, 0, $transparentColor);
$blue = imagecolorallocate($image, 0, 0, 255);
$white = ImageColorAllocate($image, 255,255,255);
$black = ImageColorAllocate($image, 0,0,0); //-($_GET['size']/20)
imagettftext($image, $_GET['size'], 0, abs($size[0]), abs($size[5]), $black, "font/".$get_font.".ttf", $_GET['text']);
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
This code is ok, although the edge of the last or first latter is sometimes partly hidden or just it doesn't fit into the created image (depending on font size), which isn't the problem with smaller font size. And the function i really need is by changing image's rotation to change the entire images proportions so the text fits in.
Example : http://img199.imageshack.us/img199/9368/32739521.jpg
Also, if there is finished function or class that does this, it would be great.. :'( :)
If you are always missing one letter of the end of the beginning, why not just add the width of a single letter to your final size?
In my experience, imagettfbbox doesn't do a very good job of deciding what does and doesn't fit within a bounding box - you need to fudge it a little yourself :)
Hope this helps.
Luke Peterson