[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

PHP - Text overlay on Image

I try to create same picture with php (picture attached),
what I need to do is to change NewYork Classified to something else,
so I took background, I took same font did that:
$image = imagecreatefromjpeg("logo.jpg");
$font_size = 14;
$color = imagecolorallocate($image, 255,255,255);
$black = imagecolorallocate($image, 0,0,0);
ImageTTFText ($image, $font_size, 0, 56, 36, $black, "AlteHaasGroteskBold.ttf","Test Text");
header("Content-Type: image/jpeg");
imagepng($image);


My Question is, How to create same color text with same style or something very similar, as you can see there are some light comes on letters from angle and border around every letter , dark border very thin around every letter.
How to create that with PHP ?
logo.jpg
0
deriklogov
Asked:
deriklogov
  • 2
  • 2
3 Solutions
 
Ray PaseurCommented:
I do not think you can do this with a True-Type font, so it may be impossible in PHP.  It looks like it was done in Illustrator or Photoshop, perhaps.  I know you can do "drop shadows" by placing a shadow-color image at a one or two pixel offset, but the reflective look and the bordered letters appear to be outside of PHP's zone.  Maybe someone else can offer something...
0
 
Ray PaseurCommented:
Here is a pixel-resize enlargement of a slice of that image.  You can see the JPG boogers, etc.

My only suggestion of how to do this would be to have a set of transparent PNG images that contained the words you wanted to overlay on top of the logo banner.  You could create a flat file background image, then overlay the image of the words.  Not as easy as just writing text with imagettftext() but it would be closer to preserving the look and feel.

HTH, ~Ray
pixel-resize.png
0
 
deriklogovAuthor Commented:
Could you please at least suggest how to center that text in a middle of that box , here is my script:


<?php
// specify the file name - you can use a full path, or "../../" type stuff here
// if the image is not in the same directory as this code file
$image = imagecreatefrompng("logo.png");
// specify the font size
$font_size = 17;

// in this case, the color is white, but you can replace the numbers with the RGB values
// of any color you want
$color = imagecolorallocate($image, 255,255,255);

// make our drop shadow color
$black = imagecolorallocate($image, 0,100,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
// 55 - pixels to the right from the leftmost part of the image
// 35 - pixels down from the top of the image
// $black - the color we defined above
// "../fonts/ARIALBD.TTF" - the location on the server that the font can be found
// "Test Text" - the text we're overlaying - you can also use a variable here
ImageTTFText ($image, $font_size, 0, 94, 66, $black, "AlteHaasGroteskBold.ttf","NEWYORK CLASSIFIED");
header("Content-Type: image/jpeg");
imagepng($image);

?>





logo.png
0
 
deriklogovAuthor Commented:
Addition to previous question:
script have to pick up the size of the font depends on length of word, for ex. New york looks good at size 17, but something like Kansas City World will go out that box, so need to set the size depend on length and centered that in the middle of that box.
 
0
 
innotionentCommented:
After looking at the php website this function.
imagettfbbox
will give you the bounding box of the text you wish to write.

array imagettfbbox ( float $size , float $angle , string $fontfile , string $text )

the return is an array with the following
0 lower left corner, X position
1 lower left corner, Y position
2 lower right corner, X position
3 lower right corner, Y position
4 upper right corner, X position
5 upper right corner, Y position
6 upper left corner, X position
7 upper left corner, Y position

using these postions you can then calculate the position of the image and center it.

Here's something I whipped up fast.
You can adjust the center position here
$x = $bbox[0] + ceil((imagesx($image) - $bbox[2]) / 2) - ($bbox[4] / 2) + 30;
I'd find tune the position by changing the value of 30 up or down to suit your needs.
It's not perfect but it's close.
<?php
$mytext = "NEW YORK CLASSIFIED";
$font = "Alte Haas Grotesk Bold.ttf";
// specify the file name - you can use a full path, or "../../" type stuff here
// if the image is not in the same directory as this code file
$image = imagecreatefrompng("logo.png");
// specify the font size
$font_size = 17;

// in this case, the color is white, but you can replace the numbers with the RGB values
// of any color you want
$color = imagecolorallocate($image, 255,255,255);

// make our drop shadow color
$black = imagecolorallocate($image, 0,100,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
// 55 - pixels to the right from the leftmost part of the image
// 35 - pixels down from the top of the image
// $black - the color we defined above
// "../fonts/ARIALBD.TTF" - the location on the server that the font can be found
// "Test Text" - the text we're overlaying - you can also use a variable here

// First we create our bounding box for the first text
$bbox = imagettfbbox(10, 0, $font, $mytext);

// This is our cordinates for X and Y
$x = $bbox[0] + ceil((imagesx($image) - $bbox[2]) / 2) - ($bbox[4] / 2) + 30;


ImageTTFText ($image, $font_size, 0, $x, 66, $black, $font,$mytext);
header("Content-Type: image/jpeg"); 
imagepng($image);

?>

Open in new window

0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now