diff --git a/README.md b/README.md index acbba88..598ef41 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Poster Image +# Poster Editor This class is an alternative to the package [Intervention Image](https://github.com/Intervention/image) for more flexible work with text on the image. Use it if you need to **fit text** in a given area, automatically **calculate font size** and change **line height**. @@ -12,10 +12,10 @@ Note that this class supports PHP-GD driver only. You can also easily inherit your class — all methods of the parent can be overridden. ## Installation -The best way to install Poster Image is quickly and easily with [Composer](http://getcomposer.org/). +The best way to install Poster Editor is quickly and easily with [Composer](http://getcomposer.org/). However, you can require the class directly without using a loader - this will not affect performance in any way. -`php composer.phar require antonlukin/poster-image` +`php composer.phar require antonlukin/poster-editor` ## Usage In case of an error, the class methods return an exception. @@ -27,10 +27,10 @@ Therefore, it is best to call them inside a block `try..catch`. require_once __DIR__ . '/vendor/autoload.php'; // Or directly -// require_once __DIR__ . '/PosterImage.php'; +// require_once __DIR__ . '/PosterEditor.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); $image->make('images/bridge.jpg')->fit(600, 600); $image->show(); @@ -41,4 +41,4 @@ try { ## Contribution All project code is stored on Github. The best way to help the project is to report a bug or add some new functionality. -You can also open here an [issue](https://github.com/antonlukin/poster-image/issues) or send a [pull reguest](https://github.com/antonlukin/poster-image/pulls). \ No newline at end of file +You can also open here an [issue](https://github.com/antonlukin/poster-editor/issues) or send a [pull reguest](https://github.com/antonlukin/poster-editor/pulls). \ No newline at end of file diff --git a/composer.json b/composer.json index d6cd5de..e8020ee 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { - "name": "antonlukin/poster-image", - "version": "2.3", + "name": "antonlukin/poster-editor", + "version": "3.0", "description": "Wrapper for PHP's GD Library for easy image manipulation", "keywords": ["php", "image", "text", "gd"], - "homepage": "https://github.com/antonlukin/poster-image", + "homepage": "https://github.com/antonlukin/poster-editor", "type": "library", "license": "MIT", "authors": [ @@ -24,7 +24,7 @@ }, "autoload": { "files": [ - "src/PosterImage.php" + "src/PosterEditor.php" ] } } diff --git a/composer.lock b/composer.lock index 4101bbb..bdcfa56 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "645adead03c97e5c45328bed9f996fbb", + "content-hash": "1bb592b5be2ee14d709213eb31ada482", "packages": [], "packages-dev": [ { diff --git a/example/append.php b/example/append.php index ec9f830..6a6e87f 100644 --- a/example/append.php +++ b/example/append.php @@ -4,23 +4,23 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); $image->make('images/bridge.jpg')->fit(600, 300, 'top'); - $logo = new PosterImage\PosterImage(); + $logo = new PosterEditor\PosterEditor(); $logo->make('images/logo.png')->resize(300, 50)->grayscale(); $image->append($logo)->show(); } catch(Exception $e) { echo $e->getMessage(); -} \ No newline at end of file +} diff --git a/example/blackout.php b/example/blackout.php index a9d67fc..9a45088 100644 --- a/example/blackout.php +++ b/example/blackout.php @@ -4,16 +4,16 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); // Create from image and fit to 1200x630 area. $image->make('images/bridge.jpg')->fit(1200, 630); @@ -40,4 +40,4 @@ } catch(Exception $e) { echo $e->getMessage(); -} \ No newline at end of file +} diff --git a/example/blur.php b/example/blur.php index 5af327b..539d21a 100644 --- a/example/blur.php +++ b/example/blur.php @@ -4,20 +4,20 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); $image->make('images/bridge.jpg')->blur(); $image->show(); } catch(Exception $e) { echo $e->getMessage(); -} \ No newline at end of file +} diff --git a/example/canvas.php b/example/canvas.php index 2d8be24..4d55744 100644 --- a/example/canvas.php +++ b/example/canvas.php @@ -4,16 +4,16 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); // Create canvas. $image->canvas(500, 500); @@ -30,4 +30,4 @@ } catch(Exception $e) { echo $e->getMessage(); -} \ No newline at end of file +} diff --git a/example/filters.php b/example/filters.php index 0f6278a..94cdccc 100644 --- a/example/filters.php +++ b/example/filters.php @@ -4,26 +4,26 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); // Create from image. $image->make('images/bridge.jpg'); // Set filters. - $image->blur(2)->contrast(25)->brightness(-10); + $image->contrast(25)->brightness(-10); // Show it. $image->show(); } catch(Exception $e) { echo $e->getMessage(); -} \ No newline at end of file +} diff --git a/example/insert.php b/example/insert.php index bf23a6b..50853c2 100644 --- a/example/insert.php +++ b/example/insert.php @@ -4,16 +4,16 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); // Create from image and crop 600x600 area. $image->make('images/bridge.jpg')->fit(600, 600, 'bottom-right'); @@ -24,9 +24,7 @@ $image->insert( 'images/logo.png', array( - 'x' => '10%', - 'y' => '10%', - 'width' => 200, + 'width' => 500, ) ); @@ -35,4 +33,4 @@ } catch(Exception $e) { echo $e->getMessage(); -} \ No newline at end of file +} diff --git a/example/resize.php b/example/resize.php index b43b05d..a902200 100644 --- a/example/resize.php +++ b/example/resize.php @@ -4,16 +4,16 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); // Create from image and resize it down. $image->make('images/bridge.jpg')->resize(300, 500, false); @@ -23,4 +23,4 @@ } catch(Exception $e) { echo $e->getMessage(); -} \ No newline at end of file +} diff --git a/example/resource.php b/example/resource.php index 7fb2ff6..acf1a20 100644 --- a/example/resource.php +++ b/example/resource.php @@ -4,16 +4,16 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); // Create from image and fit 600x600 area. $image->make('images/bridge.jpg')->fit(600, 600); @@ -32,4 +32,4 @@ } catch(Exception $e) { echo $e->getMessage(); -} \ No newline at end of file +} diff --git a/example/shapes.php b/example/shapes.php index 019b448..b1bd5a4 100644 --- a/example/shapes.php +++ b/example/shapes.php @@ -4,16 +4,16 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); // Create from image and fit it from bottom. $image->make('images/bridge.jpg')->fit(1000, 630, 'bottom'); diff --git a/example/text-boundary.php b/example/text-boundary.php index f359547..5104618 100644 --- a/example/text-boundary.php +++ b/example/text-boundary.php @@ -4,23 +4,23 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); // Create from image and crop 900x600 left-bottom area. $image->make('images/bridge.jpg')->crop( 900, 600, array( 'x' => '0', - 'y' => '100%' + 'y' => '100' ) ); @@ -62,4 +62,4 @@ } catch(Exception $e) { echo $e->getMessage(); -} \ No newline at end of file +} diff --git a/example/text-center.php b/example/text-center.php index a2770f8..7292110 100644 --- a/example/text-center.php +++ b/example/text-center.php @@ -4,16 +4,16 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ require_once __DIR__ . '/../vendor/autoload.php'; try { - $image = new PosterImage\PosterImage(); + $image = new PosterEditor\PosterEditor(); // Create from image and fit 1200x630 area. $image->make('images/bridge.jpg')->fit(1200, 630); @@ -42,4 +42,4 @@ } catch(Exception $e) { echo $e->getMessage(); -} \ No newline at end of file +} diff --git a/src/PosterImage.php b/src/PosterEditor.php similarity index 94% rename from src/PosterImage.php rename to src/PosterEditor.php index 29836cd..d4042b3 100644 --- a/src/PosterImage.php +++ b/src/PosterEditor.php @@ -6,13 +6,13 @@ * php version 7.1 * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ -namespace PosterImage; +namespace PosterEditor; use Exception; @@ -20,13 +20,13 @@ * Draw images, text and shapes using php-gd. * * @category PHP - * @package PosterImage + * @package PosterEditor * @author Anton Lukin * @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @version Release: 1.0.0 - * @link https://github.com/antonlukin/poster-image + * @link https://github.com/antonlukin/poster-editor */ -class PosterImage +class PosterEditor { /** * Canvas resource @@ -382,18 +382,18 @@ public function resize($width, $height, $scale = true, $upsize = true) public function crop($width, $height, $options = array()) { $defaults = array( - 'x' => '50%', - 'y' => '50%', + 'x' => null, + 'y' => null, ); $options = array_merge($defaults, $options); - if (substr($options['x'], -1) === '%') { - $options['x'] = $this->calcPercents($this->width, $width, $options['x']); + if (null === $options['x']) { + $options['x'] = $this->findCenter($this->width, $width); } - if (substr($options['y'], -1) === '%') { - $options['y'] = $this->calcPercents($this->height, $height, $options['y']); + if (null === $options['y']) { + $options['y'] = $this->findCenter($this->height, $height); } $temp = $this->resource; @@ -705,25 +705,25 @@ public function blackout($level = 50) public function append($image, $options = array()) { $defaults = array( - 'x' => '50%', - 'y' => '50%', + 'x' => null, + 'y' => null, ); $options = array_merge($defaults, $options); - if (!($image instanceof PosterImage)) { + if (!($image instanceof PosterEditor)) { return $this->handleError('Image is not valid instance of this class.'); } $width = $image->width; $height = $image->height; - if (substr($options['x'], -1) === '%') { - $options['x'] = $this->calcPercents($this->width, $width, $options['x']); + if (null === $options['x']) { + $options['x'] = $this->findCenter($this->width, $width); } - if (substr($options['y'], -1) === '%') { - $options['y'] = $this->calcPercents($this->height, $height, $options['y']); + if (null === $options['y']) { + $options['y'] = $this->findCenter($this->height, $height); } imagecopyresampled($this->resource, $image->resource, $options['x'], $options['y'], 0, 0, $width, $height, $width, $height); @@ -745,8 +745,8 @@ public function append($image, $options = array()) public function insert($file, $options = array()) { $defaults = array( - 'x' => '50%', - 'y' => '50%', + 'x' => null, + 'y' => null, 'width' => null, 'height' => null, ); @@ -779,12 +779,12 @@ public function insert($file, $options = array()) } } - if (substr($options['x'], -1) === '%') { - $options['x'] = ceil(($this->width - $options['width']) * (trim($options['x'], '%') / 100)); + if (null === $options['x']) { + $options['x'] = $this->findCenter($this->width, $options['width']); } - if (substr($options['y'], -1) === '%') { - $options['y'] = ceil(($this->height - $options['height']) * (trim($options['y'], '%') / 100)); + if (null === $options['y']) { + $options['y'] = $this->findCenter($this->height, $options['height']); } $temp = $this->createResource($file, $type); @@ -1066,17 +1066,16 @@ protected function setType($format) } /** - * Calculate pixles from percents. + * Find image center usin from and to values. * - * @param integer $from Source image size. - * @param integer $to Destination image size. - * @param string $value Value to parse. + * @param integer $from Source size. + * @param integer $to Destination size. * * @return integer */ - protected function calcPercents($from, $to, $value) + protected function findCenter($from, $to) { - return ceil(($from - $to) * (trim($value, '%') / 100)); + return ceil(($from - $to) * 0.5); } /**