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 layers support #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 70 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# PHP-add-text-to-image
Add text to an existing or new image. PHP >= 7.0

```shell
composer require ghostff/php-text-to-image
```

### Usage
```php
$text1 = Text::from('Text One')->color(231, 81, 0);
Expand Down Expand Up @@ -41,6 +37,76 @@ Writing to a new image:
// Or save to a file
(new TextToImage())->setDimension(350, 350)->setBackgroundColor(0, 0, 0)->addTexts($text1, $text2, $text3, $text)->render(__DIR__ . '/tmp.png');
```
Writing with background layer:
```php
$text = Text::from('Text example')->position(10, 25);
$bg_layer = BackgroundLayer::create()->color(0,0,0, 85)->position(0,0,null, 40);
$text_to_image = new TextToImage(__DIR__ . '/default.png');
$text_to_image->addTexts($text)->addBackgroundLayer($bg_layer)->render(__DIR__ . '/tmp.png');
```
Example function:
```php
/**
* Print text on image with background layer. Supports multiline.
* @param $path string Image path
* @param $text string Text that splitted with "\r\n" will be wrapped on next line.
* @param $text_position string 'top' - text will be on the top of image, 'bottom' - on the bottom
* @return bool
*/
public static function setText($path, $text, $text_position = 'top')
{
$bg_layer_y2 = 0;
$bg_layer_y_increment = 29;
$text_font_size = 14;
$font_path = 'arial.ttf'; //path to your font
$pos_x = 10;
$pos_y_increment = 25;
$pos_y = 0;
$bg_layer_x = 0;
$bg_layer_y = 0;
$text_image = new TextToImage($path);
$max_length = number_format(0.075 * $text_image->getWidth()); // 75 letter / 1000px
if ($text_position == 'bottom') {
$pos_y = $text_image->getHeight() + $pos_y_increment / 1.5;
$bg_layer_y = $text_image->getHeight();
$bg_layer_y2 = null;
}
$text_array_tmp = explode("\r\n", $text);
$text_array = [];
foreach ($text_array_tmp as $tmp_value) { //generating array of rows based on the letters count
while (mb_strlen($tmp_value) > $max_length) {
$offset = mb_strpos($tmp_value, ' ', $max_length);
if ($offset === false) {
break;
}
$sub_text = mb_substr($tmp_value, 0, $offset);
$text_array[] = $sub_text;
$tmp_value = mb_substr($tmp_value, $offset);
}
$text_array[] = $tmp_value;
}
unset($text_array_tmp);
if ($text_position == 'bottom') {
$text_array = array_reverse($text_array);
}
foreach ($text_array as $value) { //printing text
if ($text_position == 'bottom') {
$pos_y -= $pos_y_increment;
$bg_layer_y -= $bg_layer_y_increment;
} else {
$pos_y += $pos_y_increment;
$bg_layer_y2 += $bg_layer_y_increment;
}

$txt1 = Text::from($value)->position($pos_x, $pos_y)->font( $text_font_size, $font_path, true);
$text_image->addTexts($txt1);
}
$bg_layer = BackgroundLayer::create()->color(0, 0, 0, 85)->position($bg_layer_x, $bg_layer_y, null, $bg_layer_y2); //printing bg layer
$text_image->addBackgroundLayer($bg_layer);

$text_image->render($path);
}
```


----
Expand Down
90 changes: 90 additions & 0 deletions src/BackgroundLayer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php declare(strict_types=1);

namespace Ghostff\TextToImage;

class BackgroundLayer
{
private $position_x1 = 0;
private $position_y1 = 0;
private $position_x2 = null;
private $position_y2 = null;
private $color = [0xff, 0xff, 0xff, 0xff];

public function getPosition(): array {
return [$this->position_x1, $this->position_y1, $this->position_x2, $this->position_y2];
}

public function getColor(): array {
return $this->color;
}

public static function create(): self
{
return new self();
}

/**
* Generic Set.
*
* @param int $position_x1 Left X position.
* @param int $position_y1 Left Y position.
* @param int $position_x2 Right X position. If value is null than it will equals the width of image.
* @param int $position_y2 Right Y position. If value is null than it will equals the height of image.
* @param array $color Text color [r, g, b]
* @return $this
*/
public function set(
int $position_x1 = 0,
int $position_y1 = 0,
int $position_x2 = null,
int $position_y2 = null,
array $color = [255, 255, 255, 255]
): self
{
$this->position_x1 = $position_x1;
$this->position_y1 = $position_y1;
$this->position_x2 = $position_x2;
$this->position_y2 = $position_y2;
$this->color = $color + [0, 0, 0, 255];

return $this;
}

/**
* Sets the position of added background layer.
*
* @param int $x1 Left X position.
* @param int $y1 Left Y position.
* @param int $x2 Right X position. If value is null than it will equals the width of image.
* @param int $y2 Right Y position. If value is null than it will equals the height of image.
* @return $this
*/
public function position(int $x1, int $y1 = 0, int $x2=null, int $y2=null): self
{
$this->position_x1 = $x1;
$this->position_y1 = $y1;
$this->position_x2 = $x2;
$this->position_y2 = $y2;

return $this;
}

/**
* Sets a color for background layer.
* The red, green and blue parameters are integers between 0 and 255 or hexadecimals between 0x00 and 0xFF.
*
* @param int $r Value of red component .
* @param int $g Value of green component.
* @param int $b Value of blue component.
* @param int $a A value between 0 and 255. 255 indicates completely opaque while 0 indicates completely transparent.
*
* @return $this
*/
public function color(int $r = 255, int $g = 255, int $b = 255, int $a = 255): self
{
$this->color = [$r, $g, $b, $a];

return $this;
}

}
Loading