Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background color for text #302

Open
urtvs opened this issue Jun 29, 2022 · 7 comments
Open

Background color for text #302

urtvs opened this issue Jun 29, 2022 · 7 comments
Labels

Comments

@urtvs
Copy link

urtvs commented Jun 29, 2022

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

@claviska
Copy link
Owner

claviska commented Jul 8, 2022

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.

@claviska claviska changed the title background color for text Background color for text Jul 8, 2022
@scharf-roie
Copy link

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
Copy link

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
Copy link

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
Copy link
Owner

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
Copy link

scharf-roie commented Sep 29, 2022

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
Copy link
Owner

claviska commented Oct 3, 2022

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants