class.manipicture.php
<?php /** * @access public * @author Jakub Kubera <jakubkubera1992@gmail.com> * @copyright (c) 2013 http://jakubkubera.pl * @description Class for manipulating the picture * @version 1.0 * @link http://jakubkubera.pl * @license http://creativecommons.org/licenses/by/3.0/ Creative Commons 3.0 * @param string $fileName Path to image (JPG file) * @param integer $widthThumb Width of the thumbnail * @param integer $heightThumb Height of the thumbnail * @param string $fileNameWatermark Path to watermark image (PNG file) (for creating watermark) * @param integer $positionWatermark Position for watermark image (* 0: Centered, * 1: Top Left, * 2: Top Right, * 3: Footer Right, * 4: Footer left, * 5: Top Centered, * 6: Center Right, * 7: Footer Centered, * 8: Center Left) * @param string $textWatermark Text for watermark text * @param integer $sizeWatermark Font size for watermark text * @param integer $positionWatermarkText Position for watermark text (* 0: Centered, * 1: Top Left, * 2: Top Right, * 3: Footer Right, * 4: Footer left, * 5: Top Centered, * 6: Center Right, * 7: Footer Centered, * 8: Center Left) */ class ManiPicture { public $name; private $img; private $width; private $height; private $imgNew; private $widthThumb; private $heightThumb; private $watermark; private $watermarkPosition = 0; private $watermarkText = 'Default text'; private $watermarkTextSize = 15; private $watermarkPositionTextWidth; private $watermarkPositionTextHeight; public function __construct($name) { $this->name = $name; } /* * Creating the thumbnail */ public function createThumb($widthThumb, $heightThumb) { $this->widthThumb = $widthThumb; $this->heightThumb = $heightThumb; $this->img = imagecreatefromjpeg($this->name); $this->width = imagesx($this->img); $this->height = imagesy($this->img); $this->imgNew = imagecreatetruecolor($this->widthThumb, $this->heightThumb); imagecopyresampled($this->imgNew, $this->img, 0, 0, 0, 0, $this->widthThumb, $this->heightThumb, $this->width, $this->height); imagejpeg($this->imgNew, 'thumb_'.$this->name, 100); imagedestroy($this->imgNew); imagedestroy($this->img); } /* * Creating the effect of smooth */ public function createSmooth() { $this->img = imagecreatefromjpeg($this->name); imagefilter($this->img, IMG_FILTER_SMOOTH, -5); imagejpeg($this->img, 'smooth_'.$this->name, 100); imagedestroy($this->img); } /* * Creating the effect of brightness */ public function createBrightness() { $this->img = imagecreatefromjpeg($this->name); imagefilter($this->img, IMG_FILTER_BRIGHTNESS, 100); imagejpeg($this->img, 'brightness_'.$this->name, 100); imagedestroy($this->img); } /* * Positions for watermark (from 0 to 8) */ public function getPositionsForWatermark() { switch ($this->watermarkPosition) { case 0: $positionX = (imagesx($this->img) / 2) - (imagesx($this->watermark) / 2); $positionY = (imagesy($this->img) / 2) - (imagesy($this->watermark) / 2); break; case 1: $positionX = 0; $positionY = 0; break; case 2: $positionX = imagesx($this->img) - imagesx($this->watermark); $positionY = 0; break; case 3: $positionX = (imagesx($this->img) - imagesx($this->watermark)) - 5; $positionY = (imagesy($this->img) - imagesy($this->watermark)) - 5; break; case 4: $positionX = 0; $positionY = imagesy($this->img) - imagesy($this->watermark); break; case 5: $positionX = ((imagesy($this->img) - imagesx($this->watermark)) / 2); $positionY = 0; break; case 6: $positionX = imagesx($this->img) - imagesx($this->watermark); $positionY = (imagesy($this->img) / 2) - (imagesy($this->watermark) / 2); break; case 7: $positionX = ((imagesx($this->img) - imagesx($this->watermark)) / 2); $positionY = imagesy($this->img) - imagesy($this->watermark); break; case 8: $positionX = 0; $positionY = (imagesy($this->img) / 2) - (imagesy($this->watermark) / 2); break; default: $positionX = (imagesx($this->img) / 2) - (imagesx($this->watermark) / 2); $positionY = (imagesy($this->img) / 2) - (imagesy($this->watermark) / 2); break; } } /* * Creating the watermark with image */ public function createWatermarkImage($fileNameWatermark, $position = 0) { $this->img = imagecreatefromjpeg($this->name); $this->watermark = imagecreatefrompng($fileNameWatermark); $this->watermarkPosition = $position; $positions = $this->getPositionsForWatermark(); imagecopy($this->img, $this->watermark, $positions['x'], $positions['y'], 0, 0, imagesx($this->watermark), imagesy($this->watermark)); imagejpeg($this->img, 'watermark_'.$this->name, 100); imagedestroy($this->img); } /* * Positions for text (from 0 to 8) */ public function getPositionsForText() { switch ($this->watermarkPosition) { case 0: $positionX = (imagesx($this->img) / 2) - ($this->watermarkPositionTextWidth / 2) - $this->watermarkPositionTextWidth * strlen($this->watermarkText) / 2; $positionY = imagesy($this->img) / 2; break; case 1: $positionX = 0; $positionY = $this->watermarkPositionTextHeight; break; case 2: $positionX = imagesx($this->img) - $this->watermarkPositionTextWidth * strlen($this->watermarkText); $positionY = $this->watermarkPositionTextHeight; break; case 3: $positionX = imagesx($this->img) - $this->watermarkPositionTextWidth * strlen($this->watermarkText); $positionY = imagesy($this->img) - $this->watermarkPositionTextHeight; break; case 4: $positionX = 0; $positionY = imagesy($this->img) - $this->watermarkPositionTextHeight; break; case 5: $positionX = (imagesx($this->img) / 2) - ($this->watermarkPositionTextWidth / 2) - $this->watermarkPositionTextWidth * strlen($this->watermarkText) / 2; $positionY = $this->watermarkPositionTextHeight; break; case 6: $positionX = imagesx($this->img) - $this->watermarkPositionTextWidth * strlen($this->watermarkText); $positionY = imagesy($this->img) / 2; break; case 7: $positionX = (imagesx($this->img) / 2) - ($this->watermarkPositionTextWidth / 2) - $this->watermarkPositionTextWidth * strlen($this->watermarkText) / 2; $positionY = imagesy($this->img) - $this->watermarkPositionTextHeight; break; case 8: $positionX = 0; $positionY = imagesy($this->img) / 2; break; default: $positionX = (imagesx($this->img) / 2) - ($this->watermarkPositionTextWidth / 2) - $this->watermarkPositionTextWidth * strlen($this->watermarkText) / 2; $positionY = imagesy($this->img) / 2; break; } } /* * Creating the watermark with text */ public function createWatermarkText($text, $size = 15, $position = 0) { $this->img = imagecreatefromjpeg($this->name); $this->watermarkText = $text; $this->watermarkTextSize = $size; $this->watermarkPosition = $position; $this->watermarkPositionTextWidth = imagefontwidth(40); $this->watermarkPositionTextHeight = imagefontheight(40); $positions = $this->getPositionsForText(); $image = imagecreate(100, 100); $white = imagecolorallocate($image, 255, 255, 255); imagettftext($this->img, $this->watermarkTextSize, 0, $positions['x'], $positions['y'], $white, 'arial.ttf', $this->watermarkText); imagejpeg($this->img, 'text_'.$this->name, 100); imagedestroy($this->img); } } /* * Example usage */ $fileName = '20120229028.jpg'; $maniPicture = new ManiPicture($fileName); $widthThumb = 150; $heightThumb = 150; $maniPicture->createThumb($widthThumb, $heightThumb); $maniPicture->createSmooth(); $maniPicture->createBrightness(); $fileNameWatermark = 'stamp009.png'; $positionWatermark = 4; $maniPicture->createWatermarkImage($fileNameWatermark, $positionWatermark); $textWatermark = 'przykladowy tekst'; $sizeWatermark = 15; $positionWatermark = 6; $maniPicture->createWatermarkText($textWatermark, $sizeWatermark, $positionWatermark);