Skip to content

Commit

Permalink
Merge pull request #3 from bencagri/master
Browse files Browse the repository at this point in the history
Enable adding multiple lines
  • Loading branch information
ikidnapmyself authored Jun 19, 2018
2 parents 038c7ab + ca586eb commit f504e96
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 50 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require ("vendor/autoload.php");

use DngoIO\CoverCreator\Generator;

$selectors = [
$text1 = [
'font-size' => 18, //px
'font-type' => __DIR__ . '../assets/Roboto-Regular.ttf', //path of ttf file on server
'text-color' => [61,183,228],
Expand All @@ -27,9 +27,26 @@ $selectors = [
'background-url' => __DIR__ . '../assets/background.jpg' //path of the png
];

$text2 = [
'font-size' => 12, //px
'font-type' => __DIR__ . '../assets/Roboto-Italic.ttf', //path of ttf file on server
'text-color' => [61,183,228],
'left' => 50,
'top' => 30,
];


$config = [
'auto-center' => true,
'angle' => 0,
'header' => 'Content-type: image/jpeg',
];

try {
$generator = new Generator("My Text On Image", $selectors);
$generator = new Generator();
$generator->setConfig($config); //or new Generator($config)
$generator->addLine('My First Text', $text1);
$generator->addLine('Second ine Text', $text2);
$generator->generate();
}catch (\Exception $e) {
echo $e->getMessage();
Expand Down
108 changes: 80 additions & 28 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,118 @@
namespace DngoIO\CoverCreator;


use DngoIO\CoverCreator\Exception\InvalidTypeException;
use DngoIO\CoverCreator\Selectors;
use DngoIO\CoverCreator\Selectors\SelectorValidator;

class Generator
{

/** @var array */
protected $selectors;

/** @var array */
protected $config;

protected $text;
/** @var array */
protected $texts = [];

public function __construct($text, array $selectors = [], array $configVars = [])
/**
* Generator constructor.
* @param array $configVars
*/
public function __construct(array $configVars = [])
{
//merge selectors with default values
$selectors = array_merge((new Config)->getSelectors(), $selectors);
$this->selectors = new Selectors($selectors);

//merge config with default values
$this->config = array_merge((new Config)->getConfig(), $configVars);

$this->text = $text;
$this->setConfig($configVars);
}

/**
* Generate the image
*/
public function generate()
{

//Set the Content Type
header($this->config['header']);

// Create Image From Existing File
$image = null;

$image = imagecreatefrompng($this->selectors['background-url']);
foreach ($this->texts as $text => $selector) {

// Allocate A Color For The Text
$text_color = imagecolorallocate($image, $this->selectors['text-color'][0],$this->selectors['text-color'][1], $this->selectors['text-color'][2]);
$font_size = $this->selectors['font-size'];
$angle = $this->config['angle'];
// Create Image From Existing File
$image = imagecreatefrompng($selector['background-url']);

$text = $this->text;
$font_path = $this->selectors['font-type'];
// Allocate A Color For The Text
$text_color = imagecolorallocate($image, $selector['text-color'][0], $selector['text-color'][1], $selector['text-color'][2]);
$font_size = $selector['font-size'];
$angle = $this->config['angle'];

$x = $this->selectors['left'];
$y = $this->selectors['top'];
$font_path = $selector['font-type'];

if($this->config['auto-center']) {
$center = $this->autoCenter($image,$font_size,$angle,$font_path,$text);
$x = $x + $center['x'];
$y = $y + $center['y'];
}
$x = $selector['left'];
$y = $selector['top'];

imagettftext($image, $font_size, $angle, $x, $y, $text_color, $font_path, $text);
if ($this->config['auto-center']) {
$center = $this->autoCenter($image, $font_size, $angle, $font_path, $text);
$x = $x + $center['x'];
$y = $y + $center['y'];
}

imagettftext($image, $font_size, $angle, $x, $y, $text_color, $font_path, $text);

}

// Send Image to Browser
imagejpeg($image, null,95);
imagejpeg($image, null, 95);

// Clear Memory
imagedestroy($image);
}


/**
* @param $text
* @param array $selectors
* @return $this
* @throws InvalidTypeException
*/
public function addLine($text, array $selectors = [])
{
//merge selectors with default values
$selectors = array_merge((new Config)->getSelectors(), $selectors);

//validate selectors
$validator = SelectorValidator::validate($selectors,\DngoIO\CoverCreator\Selectors::$selectorRules);
if ($validator->isSuccess() == false) {
throw new InvalidTypeException(implode(',',$validator->getErrors()));
}

$this->texts[$text] = $selectors;

return $this;

}

/**
* @param array $config
* @return $this
*/
public function setConfig(array $config)
{
//merge config with default values
$this->config = array_merge((new Config)->getConfig(), $config);

return $this;
}

/**
*
* @param $image
* @param $font_size
* @param $angle
* @param $font_path
* @param $text
* @return array
*/
public function autoCenter($image, $font_size, $angle, $font_path, $text)
{
// Get image dimensions
Expand Down
21 changes: 1 addition & 20 deletions src/Selectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace DngoIO\CoverCreator;

use DngoIO\CoverCreator\Exception\InvalidTypeException;
use DngoIO\CoverCreator\Selectors\SelectorValidator;

class Selectors
{

/**
* @var array
*/
public $selectorRules= [
public static $selectorRules= [
'font-size' => ['integer'], //px
'font-type' => ['file'], //path of ttf file on server
'text-color' => ['rgb'], // RGB as array
Expand All @@ -20,20 +17,4 @@ class Selectors
'background-url' => ['file'] //path of the png
];

/**
* Selectors constructor.
* @param array $selectors
* @throws InvalidTypeException
* @throws \SimpleValidator\SimpleValidatorException
*/
public function __construct(array $selectors)
{
$validator = SelectorValidator::validate($selectors,$this->selectorRules);
if ($validator->isSuccess() == false) {
throw new InvalidTypeException(implode(',',$validator->getErrors()));
}

return $selectors;
}

}

0 comments on commit f504e96

Please sign in to comment.