Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/302241-imagecolorallocate-colors-dont-work/
Share on other sites

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

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);
?> 

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.

heartbeat.png

Edited by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.