Created
September 14, 2015 08:55
-
-
Save mockiemockiz/dba8f30626916f980999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mockie | |
* Date: 13/09/15 | |
* Time: 22:13 | |
*/ | |
namespace ImageManipulator\Utilities; | |
class ImageFrameCreator { | |
protected $expandingValues = ['left' => 0, 'top' => 0, 'right' => 0, 'bottom' => 0]; | |
protected $path; | |
protected $baseImageManipulator; | |
protected $baseImage; | |
protected $newImagePosition = ['dst_x' => 0, 'dst_y' => 0, 'src_x' => 0, 'src_y' => 0]; | |
protected $imageAttr; | |
protected $rgb; | |
protected $newImage; | |
protected $center; | |
public function __construct( | |
BaseImageManipulator $baseImageManipulator, | |
$expandingValues = ['left' => 0, 'top' => 0, 'right' => 0, 'bottom' => 0], | |
$center = true, | |
$rgb = [255, 255, 255] | |
) | |
{ | |
$this->baseImageManipulator = $baseImageManipulator; | |
$this->imageAttr = $this->baseImageManipulator->getImageAttributes(); | |
$this->newImage = $this->baseImageManipulator->imageCreateFromAny(); | |
$this->expandingValues = array_merge($this->expandingValues, $expandingValues); | |
$this->newImagePosition($center); | |
} | |
/** | |
* Create a base image | |
* | |
* @return resource | |
*/ | |
protected function createBaseImage() | |
{ | |
$expandingHeight = (int) $this->expandingValues['top'] + (int) $this->expandingValues['bottom']; | |
$expandingWidth = (int) $this->expandingValues['left'] + (int) $this->expandingValues['right']; | |
if (!$this->baseImage) { | |
$this->baseImage = imagecreatetruecolor( | |
$this->imageAttr['width'] + $expandingWidth, | |
$this->imageAttr['height'] + $expandingHeight | |
); | |
} | |
return $this->baseImage; | |
} | |
/** | |
* Define the new image positions. | |
* | |
* @param $center | |
*/ | |
protected function newImagePosition($center) | |
{ | |
$this->newImagePosition['dst_x'] = $this->expandingValues['left']; | |
$this->newImagePosition['dst_y'] = $this->expandingValues['bottom']; | |
if ($center) { | |
$horizontalCenter = ($this->expandingValues['left'] + $this->expandingValues['right']) / 2; | |
$verticalCenter = ($this->expandingValues['top'] + $this->expandingValues['bottom']) / 2; | |
$this->newImagePosition['dst_x'] = $horizontalCenter; | |
$this->newImagePosition['dst_y'] = $verticalCenter; | |
} | |
} | |
/** | |
* Fill the color | |
* | |
* @param $baseImage | |
*/ | |
protected function fillTheColor($baseImage) | |
{ | |
$bg = imagecolorallocate ( $baseImage, 255, 255, 255 ); | |
imagefill ( $baseImage, 0, 0, $bg ); | |
} | |
/** | |
* Generate the new image and the frame. | |
* | |
*/ | |
public function createFrame() | |
{ | |
$baseImage = $this->createBaseImage(); | |
$this->fillTheColor($baseImage); | |
imagecopyresampled( | |
$baseImage, //dst_image | |
$this->newImage, //src_image | |
$this->newImagePosition['dst_x'], | |
$this->newImagePosition['dst_y'], | |
0, | |
0, | |
$this->imageAttr['width'], | |
$this->imageAttr['height'], | |
$this->imageAttr['width'], | |
$this->imageAttr['height'] | |
); | |
imagejpeg($baseImage,__DIR__.'/test/b.jpg',100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment