Skip to content

Instantly share code, notes, and snippets.

@rafa-acioly
Last active June 10, 2016 18:41
Show Gist options
  • Save rafa-acioly/442de6aca152543fc279d96981581131 to your computer and use it in GitHub Desktop.
Save rafa-acioly/442de6aca152543fc279d96981581131 to your computer and use it in GitHub Desktop.
<?php
/**
* Upload
*
* @author Rafael Acioly <aciolyr@gmail.com>
* @copyright 2016 Rafael Acioly
* @link http://www.github.com/rafa-acioly
* @version 1.0.0
*
* MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace App\Controller;
/**
* Watermark
*
* This class provides the implementation for an uploaded file. It exposes
* common attributes for the uploaded file (e.g. name, extension, media type)
* and allows you to attach validations to the file that must pass for the
* upload to succeed.
*
* @author Rafael Acioly <aciolyr@gmail.com>
* @since 1.0.0
* @package Image
*/
class WaterMark {
public $backgroundImage;
public $imageOver;
public $path;
public function __construct($backgroundImage = NULL, $overImage = NULL)
{
if ($backgroundImage != NULL) {
$this->backgroundImage = new \Imagick();
$this->backgroundImage->readImage($backgroundImage);
}
if ($overImage != NULL) {
$this->imageOver = new \Imagick();
$this->imageOver->readImage($overImage);
}
}
/**
* return the path of the image with complete treatment
*/
public function getTemplate()
{
return 'src/templates/'.$this->imageOver->getImageFilename();
}
/**
* Define the storage path of image
* @param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Set image to use in background
* @param string $image
*/
public function setBackgroundImage($image)
{
$this->backgroundImage = new \Imagick();
$this->backgroundImage->readImage($image);
}
/**
* Set over image os template
* @param $overImage string
*/
public function setOverImage($overImage)
{
$this->imageOver = new \Imagick();
$this->imageOver->readImage($overImage);
$this->imageOver->setImageFilename(end(explode('/', $overImage)));
}
/**
* Get the param $_FILES sended by post and create a image
* @param $key string
* @throws \Exception Cannot find uploaded file identified by key on $_FILES
*/
public function setBackgroundImageFromUpload($key)
{
if (!isset($_FILES[$key])) {
throw new \Exception("Cannot find uploaded file identified by key: $key");
}
$this->backgroundImage = new \Imagick();
$this->backgroundImage->readImage($_FILES[$key]["name"]);
}
/**
* Define the background image based on upload sended by upload from user
* @param string $key
*/
public function setOverImageFromUpload($key)
{
if (!isset($_FILES[$key])) {
throw new \Exception("Cannot find uploaded file identified by key: $key");
}
$this->imageOver = $_FILES[$key]["name"];
}
/**
* Define the over image based on upload sended by upload from user
* @param string $key
*/
public function setOverImageSize(Array $size = NULL)
{
if ($size != NULL) {
$this->imageOver->scaleImage($size[0], $size[1]);
}
}
/**
* return the dimensions values from images
*/
public function getImageSizes()
{
$size = array(
'background' => array(
'width' => $this->backgroundImage->getImageWidth(),
'height' => $this->backgroundImage->getImageHeight()
),
'OverImage' => array(
'width' => $this->imageOver->getImageWidth(),
'height' => $this->imageOver->getImageHeight()
)
);
return $size;
}
/**
* Insert the watermark on main image
* @param array $position
*/
public function insert(Array $position)
{
$size = $this->getImageSizes();
if ($size['background']['height'] < $size['OverImage']['height'] || $size['background']['width'] < $size['OverImage']['width']) {
throw new \Exception("The background image is smaller than the Over imagem", 1);
}
$this->backgroundImage->compositeImage($this->imageOver, \Imagick::COMPOSITE_OVER, $position[0], $position[1]);
$this->backgroundImage->writeImage('src/templates/'.$this->imageOver->getImageFilename());
}
public function setImageOverOnCenter()
{
/**
TODO:
- create a function that place the overImage always on center of background image.
the idea:
$position = array(
'x' => ($size['background']['width'] - $size['imageOver']['width']) / 2;
'y' => ($size['background']['height'] - $size['imageOver']['height']) / 2;
);
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment