Skip to content

Commit

Permalink
Merge pull request #7 from joseluisq/v2-dev
Browse files Browse the repository at this point in the history
v2 release
  • Loading branch information
joseluisq authored Sep 29, 2016
2 parents dc38aa9 + 4747c21 commit 98073dc
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 276 deletions.
28 changes: 15 additions & 13 deletions examples/card.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@

// Creating an avatar image
$avatar_image = new Image();
$avatar_image->load('http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=100.jpg');
$avatar_image->setTop(60);
$avatar_image->setLeft(70);
$avatar_image
->load('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
->centerCrop(100, 100)
->setTop(60)
->setLeft(70);

$about_text = new Text("MY AWESOME PRESENTATION CARD GENERATED WITH GIMAGE");
$about_text->setSize(16);
$about_text->setWidth(300);
$about_text->setLeft(210);
$about_text->setLineHeight(1.2);
$about_text->setTop(75);
$about_text->setColor(204, 164, 116);
$about_text->setFontface(BASE_PATH . '/fonts/Lato-Lig.ttf');
$about_text = new Text("MY AWESOME PRESENTATION CARD\n~ BY GIMAGE ~");
$about_text->setSize(16)
->setWidth(300)
->setLeft(210)
->setLineHeight(1.5)
->setTop(75)
->setColor(204, 164, 116)
->setFontface(BASE_PATH . '/fonts/Lato-Lig.ttf');

$twitter_text = new Text('@username');
$twitter_text = new Text('@joseluisq/gimage');
$twitter_text->setSize(11);
$twitter_text->setWidth(70);
$twitter_text->setLeft(450);
$twitter_text->setLeft(410);
$twitter_text->setTop(210);
$twitter_text->setColor(130, 127, 125);
$twitter_text->setFontface(BASE_PATH . '/fonts/Lato-Reg.ttf');
Expand Down
7 changes: 4 additions & 3 deletions examples/crop.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

$image = new Image();
// Load an image (300px x 300px)
$image->load('http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=300.jpg');
$image
->load('http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=300.jpg')
// Resize and crop in the middle (100px x 60px)
$image->centerCrop(100, 60);
$image->save(__DIR__ . '/crop.jpg');
->centerCrop(100, 60)
->save(__DIR__ . '/crop.jpg');
7 changes: 4 additions & 3 deletions examples/rotate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

// Rotate an image to 90º
$image = new Image();
$image->load('http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=100.jpg');
$image->rotate(90);
$image->save(__DIR__ . '/rotate.jpg');
$image
->load('http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=100.jpg')
->rotate(90)
->save(__DIR__ . '/rotate.jpg');
32 changes: 17 additions & 15 deletions examples/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@
require __DIR__ . '/../tests/bootstrap.php';

$figure = new Figure(400, 250);
$figure->setBackgroundColor(47, 42, 39);
$figure->create();
$figure
->setBackgroundColor(47, 42, 39)
->create();

$text = new Text('Output PNG file to browser o save into file.');
$text->setWidth(400);
$text->setHeight(250);
$text->setLineHeight(1.2);
$text->setAlign('center');
$text->setValign('center');
$text->setSize(22);
$text->setColor(255, 255, 255);
$text->setFontface(BASE_PATH . '/fonts/Lato-Bol.ttf');
$text = new Text('Output PNG .');
$text->setWidth(400)
->setHeight(250)
->setLineHeight(1.2)
->setAlign('center')
->setValign('center')
->setSize(22)
->setColor(255, 255, 255)
->setFontface(BASE_PATH . '/fonts/Lato-Bol.ttf');

$canvas = new Canvas($figure);
$canvas->append($text);
$canvas->toPNG();
$canvas->draw();
$canvas->save(__DIR__ . '/text.png');
$canvas
->append($text)
->toPNG()
->draw()
->save(__DIR__ . '/text.png');
35 changes: 17 additions & 18 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

> A simple PHP library for easy image handling.
GImage is a simple extended library based on [PHP Image Processing and GD](http://php.net/manual/en/book.image.php) for easy image handling. With GImage you can read, create, crop, resize, rotate, embed text, merge and save your JPG or PNG images easy. GImage require **PHP 5.3** or higher and [GD Extension](http://php.net/manual/en/book.image.php).
GImage is a simple extended library based on [PHP Image Processing and GD](http://php.net/manual/en/book.image.php) for easy image handling. With GImage you can read, create, crop, resize, rotate, embed text, merge and save your JPG or PNG images easy. GImage require **PHP 5.5** or later and [GD Extension](http://php.net/manual/en/book.image.php).

![A simple presentation card with GImage](https://cloud.githubusercontent.com/assets/1700322/11167177/21e9f6ca-8b25-11e5-8737-c50a48506f17.png)
![A simple presentation card with GImage](https://cloud.githubusercontent.com/assets/1700322/18941713/eed7fa34-85d8-11e6-8033-bf787e4aa236.png)

[*View sample code*](#creating-a-simple-presentation-card)

Expand All @@ -24,38 +24,37 @@ Working with external JPG image and output on browser as PNG format.

```php
<?php
require 'src/gutils.php';
require 'src/gimage.php';

use GImage\Image;

// Loading an image (200x200) from an URL (or local path)
$avatar = new GImage();
$avatar->load('http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=200.jpg');
$avatar = new Image();
$avatar->load('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png');
// Scaling to 50% (100x100)
$avatar->scale(50);
// Changing to PNG format
$avatar->toPNG();
// Preserving the image before saving or outputting
$avatar->preserve();
// Saving to local path
$avatar->save('my-avatar.png');
$avatar->save('octocat.png');
// Preparing for destroy the resource after outputting
$avatar->preserve(false);
// Outputting on the browser and destroy the resource.
$avatar->output();
```

### Using GFigure
### Using a Figure

Creating a simple green rectangle.

```php
<?php
require 'src/gutils.php';
require 'src/gfigure.php';
require 'src/gimage.php';

use GImage\Figure;

// Setting rectangle sizes
$rectangle = new GFigure(500, 300);
$rectangle = new Figure(500, 300);
// Setting a green background color
$rectangle->setBackgroundColor(170, 188, 147);
// Creating the figure
Expand All @@ -65,15 +64,15 @@ $rectangle->output();
```

### Creating a simple presentation card
Creating a simple presentation card with GImage, GCanvas, GFigure and GText.
Creating a simple presentation card with Image, Canvas, Figure and GText.

```php
<?php
require 'src/gutils.php';
require 'src/gimage.php';
require 'src/gfigure.php';
require 'src/gtext.php';
require 'src/gcanvas.php';

use GImage\Image;
use GImage\Text;
use GImage\Figure;
use GImage\Canvas;

$avatar_image = new GImage();
$avatar_image->load('http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=100.jpg');
Expand Down
41 changes: 23 additions & 18 deletions src/Canvas.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,43 @@
* @package GImage
* @access public
* @version 2.0.0
* @author José Luis Quintana <quintana.io>
* @author José Luis Quintana <https://git.io/joseluisq>
* @license https://github.com/joseluisq/gimage/blob/master/license.md
* @property array $_list An array of elements classes (GImage, GFigure or GText classes)
* @property array $list An array of elements (Image, Figure or Text classes)
* @link Github https://github.com/joseluisq/gimage
*/
class Canvas extends Image {

private $_list = array();
private $list = [];

/**
* Constructs a new Canvas.
* @param mixed $element Only GImage or GFigure class
* @param mixed $element Only Image or Figure class
* @access public
* @return void
*/
function __construct($element = NULL) {
function __construct($element = null) {
parent::__construct($element);
}

/**
* Adds one or more elements to canvas.
* @param mixed $elements Single or array of GImage, GFigure, GText classes.
* @param mixed $elements Single or array of Image, Figure, Text classes.
* @access public
* @return void
*/
public function append($elements) {
if (!empty($elements)) {
$elements = is_array($elements) ? $elements : array($elements);
$elements = is_array($elements) ? $elements : [$elements];

foreach ($elements as $element) {
if ($element instanceof Image || $element instanceof Text) {
$this->_list[] = $element;
$this->list[] = $element;
}
}
}

return $this;
}

/**
Expand All @@ -53,21 +55,21 @@ public function append($elements) {
* @return void
*/
public function draw() {
$canvas = $this->_resource;
$canvas = $this->resource;

if ($canvas) {
$list = $this->_list;
$list = $this->list;

foreach ($list as $element) {
if ($element instanceof Image) {
$simage = $element->getResource();
imagecopyresampled($canvas, $simage, $element->getLeft(), $element->getTop(), $element->getBoxLeft(), $element->getBoxTop(), $element->getBoxWidth(), $element->getBoxHeight(), $element->getWidth(), $element->getHeight());
} else {
if ($element instanceof Text) {
$rgb_color = $element->getColor();
$color = imagecolorallocatealpha($canvas, $rgb_color[0], $rgb_color[1], $rgb_color[2], $element->getOpacity());
$rgbColor = $element->getColor();
$color = imagecolorallocatealpha($canvas, $rgbColor[0], $rgbColor[1], $rgbColor[2], $element->getOpacity());

$lines_str = $element->wrappText();
$linesStr = $element->wrappText();
$cords = $element->calculateTextBox($element->getSize(), $element->getAngle(), $element->getFontface(), $element->getString());

// Alignment
Expand All @@ -88,23 +90,26 @@ public function draw() {
$angle = $element->getAngle();
$font = $element->getFontface();

foreach ($lines_str as $i => $line_str) {
foreach ($linesStr as $i => $lineStr) {
$ny = $y + ($line_height * $i);

imagettftext(
$canvas, $size, $angle, $x, $ny, $color, $font, $line_str
$canvas, $size, $angle, $x, $ny, $color, $font, $lineStr
);
}
}
}
}

$this->_resource = $canvas;
$this->resource = $canvas;
} else {
throw new \Exception(''
. 'GImage or GFigure class is not assigned. '
. 'You can do it using the "Canvas->from($element)" method.');
. 'Image or Figure class is not assigned. '
. 'You can do it using the "Canvas->from($element)" method.'
. '');
}

return $this;
}

}
Loading

0 comments on commit 98073dc

Please sign in to comment.