Reputation: 2041
I have this image:
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?
Upvotes: 2
Views: 2180
Reputation: 8103
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);
?>
Upvotes: 3