Problem with

  • HHawk
  • Novice
  • Novice
  • HHawk
  • Posts: 22

Post 3+ Months Ago

Okay here is what I am trying to do.

I am trying to get PHP to create text to image. However I am running into a small problem. The background is killing me. Since I use it on an image (gradient colored), a 1 color background will make it look ugly.

Making the background transparant, which partially works, makes it still look ugly.

Now I was wondering if you could a pre-made png-background which would be used beneath the generated text. Is that even possible? If yes how.

This is how it currently looks and works. I want to add a background to it.

Code: [ Select ]
<?php 
setlocale(LC_TIME, "nl_NL");

// Set the content-type 
header('Content-type: image/png'); 
 
// Create the image 
$im = imagecreatetruecolor(260, 20); 
 
// Create some colors 
$white = imagecolorallocate($im, 255, 255, 255); 
$grey = imagecolorallocate($im, 128, 128, 128); 
$black = imagecolorallocate($im, 0, 0, 0);
$custom = imagecolorallocate($im, 35, 38, 41);
imagefilledrectangle($im, 0, 0, 300, 20, $white);
 
// The text to draw 
#$text = "23 september 2010";

$text = strftime("%e %B'%y\n");
$text = strtoupper($text);

// Replace path by your own font path 
$font = 'ras.otf'; 
 
// Add some shadow to the text 
imagettftext($im, 14, 0, 1, 17, $grey, $font, $text);
imagettftext($im, 14, 0, 1, 19, $grey, $font, $text);
imagettftext($im, 14, 0, 0, 18, $grey, $font, $text);
imagettftext($im, 14, 0, 2, 18, $grey, $font, $text);
 
// Add the text 
imagettftext($im, 14, 0, 1, 18, $black, $font, $text); 
 
// Using imagepng() results in clearer text compared with imagejpeg() 
imagepng($im); 
imagedestroy($im); 
?>
  1. <?php 
  2. setlocale(LC_TIME, "nl_NL");
  3. // Set the content-type 
  4. header('Content-type: image/png'); 
  5.  
  6. // Create the image 
  7. $im = imagecreatetruecolor(260, 20); 
  8.  
  9. // Create some colors 
  10. $white = imagecolorallocate($im, 255, 255, 255); 
  11. $grey = imagecolorallocate($im, 128, 128, 128); 
  12. $black = imagecolorallocate($im, 0, 0, 0);
  13. $custom = imagecolorallocate($im, 35, 38, 41);
  14. imagefilledrectangle($im, 0, 0, 300, 20, $white);
  15.  
  16. // The text to draw 
  17. #$text = "23 september 2010";
  18. $text = strftime("%e %B'%y\n");
  19. $text = strtoupper($text);
  20. // Replace path by your own font path 
  21. $font = 'ras.otf'; 
  22.  
  23. // Add some shadow to the text 
  24. imagettftext($im, 14, 0, 1, 17, $grey, $font, $text);
  25. imagettftext($im, 14, 0, 1, 19, $grey, $font, $text);
  26. imagettftext($im, 14, 0, 0, 18, $grey, $font, $text);
  27. imagettftext($im, 14, 0, 2, 18, $grey, $font, $text);
  28.  
  29. // Add the text 
  30. imagettftext($im, 14, 0, 1, 18, $black, $font, $text); 
  31.  
  32. // Using imagepng() results in clearer text compared with imagejpeg() 
  33. imagepng($im); 
  34. imagedestroy($im); 
  35. ?>


So I want to know if I could add a fixed background, for instance background.png, to this created file.

The reason I am using PHP to render the text, is that browsers do not support shadows fully (especially IE). If there is no solution for this, I will have to use plain text. Sigh...

Can someone please show me how I can add a background image to the generated text-image? If it's possible? I lack the PHP knowledge to do this myself.

Thanks in advance!!
  • PaulR
  • Newbie
  • Newbie
  • PaulR
  • Posts: 5

Post 3+ Months Ago

Look at the code below, it is a simple background image I have created then uploaded to my server.
Just replace the 'http://www.ozzu.com' with 'www{dot}pulse-box{dot}com' and the example will work prefectly.

PHP Code: [ Select ]
header("Content-type: image/png");                               // Set the correct header type, edit accordingly.
$imageURL = 'http://www.ozzu.com/downloads/background.png'; // Define the image URL, I have used a default one from my server.
$im = imagecreatefrompng($imageURL)              // Create the image from the pre-defined URL
    or die("Cannot Initialize new GD image stream");             // Throw error if unable to initialize image stream            
imagealphablending($im, false);                                  // Alpha image blending
imagesavealpha($im, true);                                       // Save the Alpha blending
             
list($iWidth, $iHeight, $iType) = getimagesize($imageURL);       // Get width, height and image type, can be used later on
 
$black = imagecolorallocate($im, 0, 0, 0);  
$grey = imagecolorallocate($im, 50, 50, 50);                     // Set the text color: Black
$white = imagecolorallocate($im, 255, 255, 255);                 // Set the text color: White
 
imagestring($im, 2, 5, 5, "Some Text Here",  $white);            // Create the text and write it to the image
imagestring($im, 2, 5, 20, "Some Text Here", $grey);             // Create the text and write it to the image
imagestring($im, 2, 5, 35, "Some Text Here", $black);            // Create the text and write it to the image
imagepng($im);
imagedestroy($im);
 
  1. header("Content-type: image/png");                               // Set the correct header type, edit accordingly.
  2. $imageURL = 'http://www.ozzu.com/downloads/background.png'; // Define the image URL, I have used a default one from my server.
  3. $im = imagecreatefrompng($imageURL)              // Create the image from the pre-defined URL
  4.     or die("Cannot Initialize new GD image stream");             // Throw error if unable to initialize image stream            
  5. imagealphablending($im, false);                                  // Alpha image blending
  6. imagesavealpha($im, true);                                       // Save the Alpha blending
  7.              
  8. list($iWidth, $iHeight, $iType) = getimagesize($imageURL);       // Get width, height and image type, can be used later on
  9.  
  10. $black = imagecolorallocate($im, 0, 0, 0);  
  11. $grey = imagecolorallocate($im, 50, 50, 50);                     // Set the text color: Black
  12. $white = imagecolorallocate($im, 255, 255, 255);                 // Set the text color: White
  13.  
  14. imagestring($im, 2, 5, 5, "Some Text Here",  $white);            // Create the text and write it to the image
  15. imagestring($im, 2, 5, 20, "Some Text Here", $grey);             // Create the text and write it to the image
  16. imagestring($im, 2, 5, 35, "Some Text Here", $black);            // Create the text and write it to the image
  17. imagepng($im);
  18. imagedestroy($im);
  19.  



All you really have to change with your script is change the following line:
PHP Code: [ Select ]
$im = imagecreatetruecolor(260, 20)

To the following:
PHP Code: [ Select ]
$im = imagecreatefrompng($imageURL)

and it should work, but I'm not 100% sure as I can't get your script functioning properly :roll:

Play around with both scripts see if you can get it working, if you can't give me a PM 8)
Thanks, PaulR.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 84 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.