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.
Related
I would like to create a transparent png of some given text. I don't want to specify the width and height of the image, but have it automatically size to the size of the text. I've experimented with both imagemagick and PHP, however, haven't quite got it. How would I do so using either of these technologies, or any other technology? Also, why is one technology better than the other?
imagemagick solution
Works, except requires size of image to be specified instead of automatically sizing to text size.
convert -size 560x85 xc:transparent -font Palatino-Bold -pointsize 72 -fill black -stroke red -draw "text 20,55 'Linux and Life'" linuxandlife.png
PHP Solution
Works except chops a bit off the right side. Also, if I make multiple images with text of the same font size and font type, and they all have capital letters in them, the height of the images are not all the same, yet I would have expected them to have been the same. Also, just played with image functions for the first time today, and please let me know if I am doing anything else incorrect.
<?php
$font_size = 11;
$angle=0;
//$fonttype="/usr/share/fonts/liberation/LiberationMono-Regular.ttf";
$fonttype="/usr/share/fonts/dejavu/DejaVuSans.ttf";
$text='Some text';
$file='MyFile.png';
$bbox = imagettfbbox($font_size, $angle, $fonttype, $text);
$x1 = $bbox[2] - $bbox[0];
$y1 = $bbox[1] - $bbox[7];
$im = #imagecreatetruecolor($x1, $y1);
imagesavealpha($im, true);
imagealphablending($im, false);
$color_background = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $color_background);
$color_text = imagecolorallocate($im, 255, 0, 0);
imagettftext($im, $font_size, $angle, 0, $font_size, $color_text, $fonttype, $text);
imagepng($im,$file);
imagedestroy($im);
?>
I would choose the pure php solution, so you are not depended on external libs like imagemagick.
Maybe it wohl be a good idea if you would use browser caching, so you don`t ne to generate the images on every request.
Yust add the following code before your 'imagettfbbox' and put your image generation code in the ELSE.
$string = $text . filemtime($file);
$eTag = md5($string);
header("ETag: ".$eTag);
$httpModEtag = !empty($_SERVER['HTTP_IF_NONE_MATCH'])? $_SERVER['HTTP_IF_NONE_MATCH']:"";
if($httpModEtag===$eTag)
{
// tells the browser to use his cached image
header("HTTP/1.1 304 Not Modified", true, 304);
}
else
{
// tells the browser to refresh the cache
header("Cache-Controll: must-revalidate");
// ------ Place your Image Generation code here -------
}
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.
So I'm trying to make a Captcha form for my registration form and I'm not sure what's going wrong because whenever I open up the page I see the image shown when the url of an image is not found (I would post a picture, but I can't since this is my first question).
I'm not sure what I'm doing wrong because I was following a tutorial as I was making it and it seemed to work for them. Also, no errors are being displayed so I have no idea what could be wrong. My code is here:
<?php
session_start();
header('Content-type: image/jpeg');
$text = 4539;
$font_size = 4;
$image_height = 40;
$image_width = 200;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$font_colour = imagecolorallocate($image, 0, 0, 0);
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
$font = 'font';
imagettftext($image, 0, 15, 30, $font_colour, $font, $text);
?>
Also the $text variable will be set to random when I know the code works, the value is static for testing purposes. I would appreciate any help I can get. Thanks in advance
You need to output the image to the browser after you add the text to it:
imagettftext($image, 0, 15, 30, $font_colour, $font, $text);
imagepng($image);
imagedestroy($image);
The destroy may not be strictly needed as the php process exits after the script is done, but the examples have it so I included it.
Also, you can choose between png, gif and jpeg image formats by using either imagepng(), imagegif() or imagejpeg(). The png format should be good here.
I would like to change the color of an image with php.
if I wanted to make it appear redder applicherei an image on a higher level across an image with a transparent red and more or less high can indicate how the original photo should be red.
I can say gd php functions to create an image of a color (RGBA) and apply it to another image?
thanks :)
You can try using GD's imagecopymerge function, which copies one image to another and supports alpha transparency. Something like this should work:
<?php
$redimg = imagecreatetruecolor(100, 100);
$image = imagecreatefrompng('image.png');
// sets background to red
$red = imagecolorallocate($redimg, 255, 0, 0);
imagefill($redimg, 0, 0, $red);
// Merge the red image onto the PNG image
imagecopymerge($image, $redimg, 0, 0, 0, 0, 100, 100, 75);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
imagedestroy($redimg);
?>
There's more information here.
This one is empty image before writing
Here is my code
<?php
function LoadJpeg($imgname)
{
$im = #imagecreatefromjpeg($imgname);
$grey = imagecolorallocate($im, 255, 255, 0);
// The text to draw
$text = 'http://www.stackoverflow.com';
// Replace path by your own font path
$font = 'CONSOLA.TTF';
list($width, $height) = getimagesize($imgname);
// wants to know how to use this width/height dynamically //
imagettftext($im, 20, 45, 200, 450, $grey, $font, $text);
return $im;
}
header('Content-Type: image/jpeg');
$img = LoadJpeg('Blue_hills.jpg');
imagejpeg($img);
imagedestroy($img);
?>
Image after writing text on it
What I want is vertically and horizontally center the text on 45 degree. Please help me on this. Thanks for you all.
Use the imagettfbbox function to retrieve the dimensions the text rendering would require. Then use that information to calculate the x,y coordinate you should target within the destination image for the text to be centered respective to the width/height.
I know very little about PHP (I'm making this up as I go along).
I'd recommend making a new square image, setting it to have a transparent background with imagecolortransparent(). Then write the text to the transparent image.
Next I'd try using imagecopyresized() to copy and scale the text to the new window. Use the minimum of the original's height and width for the destination size. The offset would be something like (max($width, $height)-min($width, $height))/2. Apply the offset to whichever dimension is greater.