ultima_tum Posted September 26, 2016 Share Posted September 26, 2016 Hi!I am desperately trying to figure out an issue I have with imagecolorallocate.I am trying to personalize an image (http://31.216.48.48/~ulftiede/email_tests/own/image.php?name=Charlie)The color of the text always comes in as the light red, even though it is set as 0, 0, 0. i also have tried other colors in RGB <?php //Set the Content Type header('Content-type: image/gif'); // Create Image From Existing File $jpg_image = imagecreatefromgif('heartbeat.gif'); // Allocate A Color For The Text $color = imagecolorallocate($jpg_image, 0, 0, 0); // Set Path to Font File $font_path = 'youmurderer.otf'; // Set Text to Be Printed On Image $text = $_GET['name']; // Print Text On Image Size, Angle, Position imagettftext($jpg_image, 140, 15, 370, 320, $color, $font_path, $text); // Send Image to Browser imagegif($jpg_image); // Clear Memory imagedestroy($jpg_image); ?> can anyone think of a reason for it?thanks! image.php Quote Link to comment Share on other sites More sharing options...
requinix Posted September 26, 2016 Share Posted September 26, 2016 GIF images have a maximum of 256 colors available. Your heartbeat.gif is using all 256 already, so imagecolorallocate will fail and return false. However false == 0 so when you try to write the text on the image it will use the color at palette index #0, which is light red. Either a) Use a different file format - I suggest PNG b) Save the image using fewer than 256 colors Quote Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2016 Share Posted September 26, 2016 c) if the image already uses black, get the index of that color ($black = imagecolorexact($jpg_image,0,0,0)) and use it for the text Quote Link to comment Share on other sites More sharing options...
ultima_tum Posted September 26, 2016 Author Share Posted September 26, 2016 Thanks so much for your quick reply!Ideally i would like to try option a) and I have amended everything to reflect it, however still can't seem to be able to get it. i have updated the files on the server, so the link above is the same <?php //Set the Content Type header('Content-type: image/png'); // Create Image From Existing File $jpg_image = imagecreatefrompng('heartbeat.png'); // Allocate A Color For The Text $color = imagecolorallocate($jpg_image, 0, 0, 0); // Set Path to Font File $font_path = 'youmurderer.otf'; // Set Text to Be Printed On Image $text = $_GET['name']; // Print Text On Image Size, Angle, Position imagettftext($jpg_image, 140, 15, 370, 320, $color, $font_path, $text); // Send Image to Browser imagepng($jpg_image); // Clear Memory imagedestroy($jpg_image); ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted September 26, 2016 Share Posted September 26, 2016 (edited) heartbeat.png is a real PNG, however it's set up using a palette. So I need to clarify what I said earlier: a) Use a different file format - I suggest PNG, making sure you save in RGB or truecolor format (or whatever your image editor calls it) Attached is a PNG in RGB format. However you've moved away from GIF format, and that means you could improve the quality of the image. Do you still have the raw source available? You should recreate your base image using that as a source, rather than convert the inferior-quality GIF to a superior-quality PNG. Edited September 26, 2016 by requinix Quote Link to comment Share on other sites More sharing options...
ultima_tum Posted September 26, 2016 Author Share Posted September 26, 2016 This makes a lot of sense now and does work!Really appreciate your help and also explaining it, so I know why and what!THANK YOU!! Ulf Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.