2

I have this image:

enter image description here

Between the text, I want to add some text. To do that I have the following code:

<?php
error_reporting(E_ALL);

$thumb_src              = 'copyright-symbol.png';
$explode                = explode('.', $thumb_src);                
$extension              = strtolower( end ( $explode ) );
$file_name              = basename( $thumb_src );

//image_resize_base_width( $relative_url, $relative_url, 350, $extension);
$jpg_image = imagecreatefrompng( $thumb_src );

// set font size
$font        = @imageloadfont($jpg_image);
$fontSize    = imagefontwidth($font);

$orig_width  = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);


// Create your canvas containing both image and text
$canvas = imagecreatetruecolor( $orig_width, ($orig_height + 40 ) );              
// Allocate A Color For The background
$bcolor = imagecolorallocate( $canvas, 255, 255, 255 );

// Add background colour into the canvas
imagefilledrectangle( $canvas, 0, 0, $orig_width, ($orig_height + 40), $bcolor );
// Save image to the new canvas
imagecopyresampled( $canvas, $jpg_image, 0, 0, 0, 0, $orig_width, $orig_height, $orig_width, $orig_height );

$font_path = 'font/arial.ttf';
$path = 'upload';
$text = 'cc-by-nd-';

// Allocate A Color For The Text
$color = imagecolorallocate($canvas, 0, 0, 0);


// Print Text On Image
imagettftext( $canvas, 13, 0, 0, $orig_height + 25, $color, $font_path, $text) ;
// Send Image to Browser

imagepng( $canvas, $path  . '/' . $file_name );

// Clear Memory
imagedestroy($canvas);

But the code is not adding text to the image. Is there anything wrong in my code?

5
  • 2
    Might want to remove @ from $font = @imageloadfont($jpg_image);. Looks like you're loading an image as font, is $jpg_image a bitmap font? Commented Dec 19, 2020 at 12:22
  • var_dump($jpg_image) is return resource(3) of type (gd). Commented Dec 19, 2020 at 12:25
  • More info here: php.net/manual/en/function.imageloadfont.php Commented Dec 19, 2020 at 12:27
  • See here: code-boxx.com/php-add-text-image Commented Dec 19, 2020 at 12:32
  • @AbsoluteBeginner can you find any issue on my code? Commented Dec 19, 2020 at 12:33

1 Answer 1

3

You may change the codes to

<?php
error_reporting(E_ALL);

header('Content-Type: image/png');

$thumb_src              = 'copyright-symbol.png';
$explode                = explode('.', $thumb_src);                
$extension              = strtolower( end ( $explode ) );
$file_name              = basename( $thumb_src );

//image_resize_base_width( $relative_url, $relative_url, 350, $extension);
$jpg_image = imagecreatefrompng( $thumb_src );

// set font size
$font        = @imageloadfont($jpg_image);
$fontSize    = imagefontwidth($font);

$orig_width  = imagesx($jpg_image);
$orig_height = imagesy($jpg_image);


// Create your canvas containing both image and text
$canvas = imagecreatetruecolor( $orig_width, ($orig_height + 40 ) );              
// Allocate A Color For The background
$bcolor = imagecolorallocate( $canvas, 255, 255, 255 );

// Add background colour into the canvas

imagefilledrectangle( $canvas, 0, 0, $orig_width, ($orig_height + 40), $bcolor );

// Save image to the new canvas
imagecopyresampled( $canvas, $jpg_image, 0, 0, 0, 0, $orig_width, $orig_height, $orig_width, $orig_height );

$font_path = './font/arial.ttf';
//$path = 'upload';


// Create some colors
$white = imagecolorallocate($canvas, 255, 255, 255);
$grey = imagecolorallocate($canvas, 128, 128, 128);
$black = imagecolorallocate($canvas, 0, 0, 0);
imagefilledrectangle($canvas, 0, 0, 399, 29, $white);

// The text to draw
// Replace path by your own font path
$font = 'font/arial.ttf';

$text = 'cc-by-nd-';

// Add some shadow to the text

imagettftext( $canvas, 100, 0, ($orig_width / 2) - 300, ($orig_height/2) , $color, $font_path, $text) ;


// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($canvas);
imagedestroy($canvas);



?>

Result

Sign up to request clarification or add additional context in comments.

4 Comments

Let me try it now
Showing me a black background
Please use the image I have uploaded.
Answer amended (please make sure arial.ttf file exists in font folder)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.