Write product name on image

Hi,

I want to write name of product on image using php script.

I did search on Google and try some script but no result.

I have attached blank image and want to write name on this image.

Any idea?

-Thanks

You can write on an image using GD or Imagemagick. Imagemagick has more options and I think simpler to use but it is not installed with php and some people are paranoid about using it as it’s an external program.

What code did you use?

Hi Rubble,

I try many example code from Google but no result.

If could find good working example for me ,I will appreciate it.

-Thanks

What errors did you get and what did not work?

GD example:


<?php  
$canvas = imagecreate( 200, 100 );  

$black = imagecolorallocate( $canvas, 0, 0, 0 );  
$white = imagecolorallocate( $canvas, 255, 255, 255 );  

imagefilledrectangle( $canvas, 0, 0, 200, 100, $white );  

$font = "../fonts/verdana.ttf";  
$text = "Sunflower";  
$size = "30";  
$degrees = "30";  
$photo= "../original_images/sunflower.jpg"; 

$box = imagettfbbox( $size, 0, $font, $text );  
$x = (200 - ($box[2] - $box[0])) / 2;  
$y = (100 - ($box[1] - $box[7])) / 2;  
$y -= $box[7];  

imageTTFText( $canvas, $size, 0, $x, $y, $black, $font, $text );  

imagecolortransparent ( $canvas, $white );  

imagerotate( $canvas, $degrees, 0);  

imagepng( $canvas, "temp.png" );  

ImageDestroy( $canvas );  

$watermark = imagecreatefrompng('temp.png');    
$watermark_width = imagesx($watermark);    
$watermark_height = imagesy($watermark);    
$image = imagecreatetruecolor($watermark_width, $watermark_height);    
$image = imagecreatefromjpeg($photo);    
$size = getimagesize($photo);    
$dest_x = $size[0] - $watermark_width - 100;    
$dest_y = $size[1] - $watermark_height - 200;    
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50);    
imagejpeg($image, 'watermark_GD.jpg');    
imagedestroy($image);    
imagedestroy($watermark);    

unlink( 'temp.png');  
?>  

Imagemagick example:


<?php 

$font = "../fonts/verdana.ttf";  
$text = "Sunflower";  
$size = "40";  
$degrees = "30";  
$photo= "../original_images/sunflower.jpg"; 

exec("convert $photo -font $font -pointsize $size -fill rgba(0,0,0,0.4) // 
-gravity north -annotate +0+25 $text watermark_IM.jpg");  
?> 

Hi Rubble,

I try the example code and check website http://www.rubblewebs.co.uk/imagemagick/examples/example2.php

<?php
// Create the canvas
$canvas = imagecreate( 150, 80 );
// First colour - this will be the default colour for the canvas
$light_blue = imagecolorallocate( $canvas, 176, 226, 255 );
// The second colour - to be used for the text
$black = imagecolorallocate( $canvas, 0, 0, 0 );
// Path to the font you are going to use
$font = "verdana.ttf";
// Text to write
$text = "Text";
// Font size
$size = "40";
// Add the text to the canvas
imageTTFText( $canvas, $size, 0, 15, 60, $black, $font, $text );
// Save as Text.jpg
imagejpeg( $canvas, "Text.jpg" );
// Clear the memory of the tempory image 
ImageDestroy( $canvas );
?>

<?php  
$canvas = imagecreate( 200, 100 );  

$black = imagecolorallocate( $canvas, 0, 0, 0 );  
$white = imagecolorallocate( $canvas, 255, 255, 255 );  

imagefilledrectangle( $canvas, 0, 0, 200, 100, $white );  

$font = "verdana.ttf";  
$text = "Sunflower";  
$size = "30";  
$degrees = "30";  
$photo= "productimage.jpg"; 

$box = imagettfbbox( $size, 0, $font, $text );  
$x = (200 - ($box[2] - $box[0])) / 2;  
$y = (100 - ($box[1] - $box[7])) / 2;  
$y -= $box[7];  

imageTTFText( $canvas, $size, 0, $x, $y, $black, $font, $text );  

imagecolortransparent ( $canvas, $white );  

imagerotate( $canvas, $degrees, 0);  

imagepng( $canvas, "temp.png" );  

ImageDestroy( $canvas );  

$watermark = imagecreatefrompng('temp.png');    
$watermark_width = imagesx($watermark);    
$watermark_height = imagesy($watermark);    
$image = imagecreatetruecolor($watermark_width, $watermark_height);    
$image = imagecreatefromjpeg($photo);    
$size = getimagesize($photo);    
$dest_x = $size[0] - $watermark_width - 100;    
$dest_y = $size[1] - $watermark_height - 200;    
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50);    
imagejpeg($image, 'watermark_GD.jpg');    
imagedestroy($image);    
imagedestroy($watermark);    

unlink( 'temp.png');  
?>

When I run from my WAMP PHP Version 5.4.3 ,it shows watermark_GD.jpg image is created on my folder but text is not written

Could you please attached all files in one folder.

I have attached verdana.ttf,productimage.jpg already attached to run exmaple.

I think the co-ordinates on image are missing and I writing on wrong position therefore I can’t see the text.

Please advise.

-Thanks

Hi,

Following example working but when text is big then going out of image.

<?php
  //Set the Content Type
  header('Content-type: image/jpeg');

  // Create Image From Existing File
  $jpg_image = imagecreatefromjpeg('sunset.jpg');

  // Allocate A Color For The Text
  $white = imagecolorallocate($jpg_image, 255, 255, 255);

  // Set Path to Font File
  $font_path = 'verdana.TTF';

  // Set Text to Be Printed On Image
  $text = "Experience is a hard teacher because she gives the test first, the lesson afterwards. - Vernon Law";

  // Print Text On Image
  imagettftext($jpg_image, 25, 0, 30, 55, $white, $font_path, $text);

  // Send Image to Browser
  imagejpeg($jpg_image);

  // Clear Memory
  imagedestroy($jpg_image);
?>

I got this example from following URL

http://www.phpforkids.com/php/php-gd-library-adding-text-writing.php

Any idea?

-Thanks

You will need to modify the values in imagettftext as the current values are hard coded in.

You will need to adjust the font size to make sure it fits and the position is a bit hit and miss unless you calculate the size of the text “bounding box” and do some other calculations to get the correct position on the image.

It all gets very complicated in GD but hopefuly you will only need to write the code once.

Hi,

I have change font size ,color etc.

<?php
  //Set the Content Type
  header('Content-type: image/jpeg');

  // Create Image From Existing File
  $jpg_image = imagecreatefromjpeg('product.jpg');

  // Allocate A Color For The Text
  $white = imagecolorallocate($jpg_image, 288, 288, 288);

  // Set Path to Font File
  $font_path = 'verdana.TTF';

  // Set Text to Be Printed On Image
  $text = "Experience is a hard teacher because she gives the test first, the lesson afterwards. - Vernon Law";

  // Print Text On Image
  imagettftext($jpg_image, 10, 0, 30, 55, $white, $font_path, $text);

  // Send Image to Browser
  imagejpeg($jpg_image);

  // Clear Memory
  imagedestroy($jpg_image);
?>

The problem is when length of text is big then it is going out of the box.

How can I make it fit on image.

I have attached expected image updated using photoshop.

Any idea?

Word wrapping might work but I would try these first:


// Set Text to Be Printed On Image
  $text = "Experience is a hard teacher
because she gives the test
first, the lesson afterwards.
- Vernon Law";

or

// Set Text to Be Printed On Image
  $text = "Experience is a hard teache\
because she gives the\
test first, the lesson afterwards.\
- Vernon Law";

Hi Rubble,

Many thanks for update.It works fine now.

The next problem is how to split words so that they will fit the image on new lines.

I have attached example images.

<?php
  //Set the Content Type
  header('Content-type: image/jpeg');

  // Create Image From Existing File
  $jpg_image = imagecreatefromjpeg('product2.jpg');

  // Allocate A Color For The Text
  $white = imagecolorallocate($jpg_image, 288, 288, 288);

  // Set Path to Font File
  $font_path = 'Roboto-Regular.ttf';

// Confirmation of Lost Stolen or Destroyed Stock Certificate

$text  ="\
Confirmation of ";
$text .="\
Lost Stolen or ";
$text .="\
Destroyed Stock ";
$text .="\
Certificate";

/*
$text  ="\
Board Resolution";
$text .="\
Appointing an";
$text .="\
Auditor";
*/

//$text = "Board Resolution \
 Appointing an \
 Auditor";

  // Set Text to Be Printed On Image
//  $text = "Experience is a hard teacher because she gives the test first, the lesson afterwards. - Vernon Law";

  // Print Text On Image
  imagettftext($jpg_image, 11, 0, 20, 35, $white, $font_path, $text);

  // Send Image to Browser
  imagejpeg($jpg_image);

  // Clear Memory
  imagedestroy($jpg_image);
?>

Any idea?

-Thanks

This works for me:


$text  = wordwrap('Confirmation of Lost Stolen or Destroyed Stock Certificate', 15, "\
");

Many thanks Rubble,

It works :slight_smile:

Hi Rubble,

Any idea how to save new image in folder .

-Thanks

All the GD functions along with examples can be found on the php website.

// Save Image
  imagejpeg($jpg_image, 'output.jpg');