claviska / SimpleImage

A PHP class that makes working with images and GD as simple as possible.
MIT License
1.38k stars 382 forks source link

Background color for text #302

Open urtvs opened 2 years ago

urtvs commented 2 years ago

Hi is it possible to set backgroud color for text adding to image?

claviska commented 2 years ago

This isn't currently possible, but you can call text() with the third &$boundary argument to get the text's position, then draw a rectangle() with the desired fill color, then call text() again.

That's not super efficient, of course. I'd accept a PR that adds this capability to text() though.

scharf-roie commented 1 year ago

Hey, I would like to tackle this issue! Would you be able to explain how you test your application since it is not provided in the explanation? Do I just fork the repo into VSCode and start working from there, as I already followed your installation guide?

scharf-roie commented 1 year ago

Hey, I would like to tackle this issue! Would you be able to explain how you test your application since it is not provided in the explanation? Do I just fork the repo into VSCode and start working from there, as I already followed your installation guide?

@claviska

scharf-roie commented 1 year ago

public function text($text, $options, $backgroundColor, &$boundary = null) {

// Pass the boundary back by reference
$boundary = [
  'x1' => $x + $boxText[0],
  'y1' => $y + $boxText[1] - $boxHeight, // $y is the baseline, not the top!
  'x2' => $x + $boxWidth + $boxText[0],
  'y2' => $y + $boxText[1],
  'width' => $boxWidth,
  'height' => $boxHeight
];

//add background color
if ($backgroundColor != "white") {
  rectangle($boundary['x1'], $boundary['y1'], $boundary['x2'], $boundary['y2'], $backgroundColor, $thickness = 1);
}

I have never used PHP before so please let me know if my approach is poor, but I have attempted to minimize the calling of text by creating a new backroundColor parameter for text(). Then, I added code after the boundaries are set which generates a rectangle that will be written over by the text after being called. I made it an if statement because I was unsure if it is possible to set a default value for backgroundColor in PHP. If so it would only generate the rectangle if the color isn't white, which I assumed was default, and potentially it would allow users to not have to add an additional input and use the function the same why they did before I added the extra parameter. Please let me know if I have any fault in my logic otherwise I will create a pull request and push the code.

claviska commented 1 year ago

I don't know of a more efficient way to do this using GD, aside from the suggestion I made above. I would like to make sure text() remains backwards compatible, so the arguments shouldn't change. In this case, you'd need to do:

text($text, $options, &$boundary, $backgroundColor) {
  // ...
}

At this time, there are no tests in the library. You can test things out locally using the example folder.

scharf-roie commented 1 year ago

Hey @claviska, I made a pull request with a function that specifically has a solution for this feature. Would you be able to explain how I can test this code using the example folder, as I am not familiar with testing in PHP?

claviska commented 1 year ago

Whoops, I completely overlooked the existing $options argument in text(), but I left a review comment on the PR that will carry this feature request home.

Let me know if you have any questions!