Skip to content

Commit

Permalink
add rotate and build methods
Browse files Browse the repository at this point in the history
  • Loading branch information
antonlukin committed Jun 14, 2021
1 parent db19c74 commit 6738b6c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 35 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "antonlukin/poster-editor",
"version": "4.0",
"version": "4.1",
"description": "Wrapper for PHP's GD Library for easy image manipulation",
"keywords": ["php", "image", "text", "gd"],
"homepage": "https://github.com/antonlukin/poster-editor",
Expand Down
138 changes: 104 additions & 34 deletions src/PosterEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,60 +94,51 @@ public function setResource($resource)
}

/**
* Create resource using file path.
* Make new image instance from file.
*
* @param string $file Path to image file.
* @param integer $type Optional. File image type.
* @param string $file Path to file.
*
* @return instance
* @return $this
*/
public function createResource($file, $type = null)
public function make($file)
{
if (null === $type) {
list($width, $height, $type) = getimagesize($file);
if (!is_readable($file)) {
return $this->handleError('Image is not a valid file');
}

switch ($type) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($file);
break;
// Get image dimensions.
list($width, $height, $type) = getimagesize($file);

case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($file);
break;
$this->canvas($width, $height);

case IMAGETYPE_PNG:
$image = imagecreatefrompng($file);
break;
$temp = $this->createFromFile($file, $type);

case IMAGETYPE_WEBP:
$image = imagecreatefromwebp($file);
imagecopyresampled($this->resource, $temp, 0, 0, 0, 0, $width, $height, $width, $height);
imagedestroy($temp);

default:
return $this->handleError('Unsupported image type');
}
$this->type = $type;

return $image;
return $this;
}

/**
* Make new image instance from file.
* Build new image instance from binary data.
*
* @param string $file Path to file.
* @param string $data Binary image data.
*
* @return $this
*/
public function make($file)
public function build($data)
{
if (!is_readable($file)) {
return $this->handleError('Image is not a valid file');
}

// Get image dimensions.
list($width, $height, $type) = getimagesize($file);
list($width, $height, $type) = getimagesizefromstring($data);

$this->canvas($width, $height);
$this->insert($file);

$temp = $this->createFromString($data);

imagecopyresampled($this->resource, $temp, 0, 0, 0, 0, $width, $height, $width, $height);
imagedestroy($temp);

$this->type = $type;

Expand All @@ -168,7 +159,7 @@ public function canvas($width, $height, $options = array())
{
$defaults = array(
'color' => array(0, 0, 0),
'opacity' => 0,
'opacity' => 100,
);

$options = array_merge($defaults, $options);
Expand All @@ -183,6 +174,7 @@ public function canvas($width, $height, $options = array())
// Turn off transparency blending (temporarily)
imagealphablending($this->resource, false);

// Get color from options.
$color = $this->getColor($options);

// Completely fill the background with transparent color
Expand Down Expand Up @@ -694,6 +686,34 @@ public function blackout($level = 0)
return $this;
}

/**
* Rotate image.
*
* @param float $angle Rotation angle.
* @param int $options Optional. Optional. List of rotation options.
*
* @return $this
*/
public function rotate($angle, $options = array())
{
$defaults = array(
'color' => array(0, 0, 0),
'opacity' => 100,
);

$options = array_merge($defaults, $options);

// Get color from options.
$color = $this->getColor($options);

$this->resource = imagerotate($this->resource, $angle, $color);

$this->width = imagesx($this->resource);
$this->height = imagesy($this->resource);

return $this;
}

/**
* Append another image instance.
*
Expand Down Expand Up @@ -787,7 +807,7 @@ public function insert($file, $options = array())
$options['y'] = $this->findCenter($this->height, $options['height']);
}

$temp = $this->createResource($file, $type);
$temp = $this->createFromFile($file, $type);

imagecopyresampled($this->resource, $temp, $options['x'], $options['y'], 0, 0, $options['width'], $options['height'], $width, $height);
imagedestroy($temp);
Expand Down Expand Up @@ -842,6 +862,7 @@ public function text($text, $options = array(), &$boundary = array())
$this->drawDebug($options);
}

// Get color from options.
$color = $this->getColor($options);

// Get wrapped text and updated font-size.
Expand Down Expand Up @@ -1075,6 +1096,55 @@ protected function setType($format)
}
}

/**
* Create image using file path.
*
* @param string $file Path to image file.
* @param integer $type Optional. File image type.
*
* @return instance
*/
protected function createFromFile($file, $type = null)
{
if (null === $type) {
list($width, $height, $type) = getimagesize($file);
}

switch ($type) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($file);
break;

case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($file);
break;

case IMAGETYPE_PNG:
$image = imagecreatefrompng($file);
break;

case IMAGETYPE_WEBP:
$image = imagecreatefromwebp($file);

default:
return $this->handleError('Unsupported image type');
}

return $image;
}

/**
* Create a new image from the image stream in the string.
*
* @param string $data A string containing the image data.
*
* @return instance
*/
protected function createFromString($data)
{
return imagecreatefromstring($data);
}

/**
* Find image center usin from and to values.
*
Expand Down

0 comments on commit 6738b6c

Please sign in to comment.