Created
December 5, 2011 08:35
-
-
Save ptz0n/1432870 to your computer and use it in GitHub Desktop.
Image manipulation
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 | |
/** | |
* Resize Image Class | |
* | |
* @author Erik Pettersson <mail@ptz0n.se> | |
* @copyright 2011 Erik Pettersson <mail@ptz0n.se> | |
* @license http://www.opensource.org/licenses/mit-license.php MIT | |
*/ | |
class Image { | |
/** | |
* Image | |
* | |
* @var resource | |
*/ | |
var $image; | |
/** | |
* Image width | |
* | |
* @var int | |
*/ | |
var $imageWidth; | |
/** | |
* Image height | |
* | |
* @var int | |
*/ | |
var $imageHeight; | |
/** | |
* Load image from file | |
* | |
* @param string $file | |
* @return void | |
*/ | |
function load($file) | |
{ | |
$size = getimagesize($file); | |
switch($size[2]) { | |
case IMAGETYPE_JPEG : | |
$this->image = imagecreatefromjpeg($file); | |
break; | |
case IMAGETYPE_GIF : | |
$this->image = imagecreatefromgif($file); | |
break; | |
case IMAGETYPE_PNG : | |
$this->image = imagecreatefrompng($file); | |
break; | |
} | |
$this->getWidth(); | |
$this->getHeight(); | |
} | |
/** | |
* Save image to file | |
* | |
* @param string $file | |
* @return void | |
*/ | |
function save($file) | |
{ | |
imagesavealpha($this->image, true); | |
imagepng($this->image, $file); | |
chmod($file, 0755); | |
} | |
/** | |
* Output image | |
* | |
* @return void | |
*/ | |
function output() | |
{ | |
header('Content-type: image/png'); | |
imagesavealpha($this->image, true); | |
imagepng($this->image); | |
} | |
/** | |
* Get image width | |
* | |
*/ | |
function getWidth() | |
{ | |
return $this->imageWidth = imagesx($this->image); | |
} | |
/** | |
* Get image height | |
* | |
* @return int | |
*/ | |
function getHeight() | |
{ | |
return $this->imageHeight = imagesy($this->image); | |
} | |
/** | |
* Resize image to height | |
* | |
* @param int $height | |
* @return void | |
*/ | |
function resizeToHeight($height) | |
{ | |
$ratio = $height / $this->getHeight(); | |
$width = $this->getWidth() * $ratio; | |
$this->resize($width, $height); | |
} | |
/** | |
* Resize image to width | |
* | |
* @param int $width | |
* @return void | |
*/ | |
function resizeToWidth($width) | |
{ | |
$ratio = $width / $this->getWidth(); | |
$height = $this->getheight() * $ratio; | |
$this->resize($width, $height); | |
} | |
/** | |
* Resize image | |
* | |
* @param int $width | |
* @param int $height | |
* @return void | |
*/ | |
function resize($width, $height) | |
{ | |
$image = imagecreatetruecolor($width, $height); | |
imagecopyresampled($image, $this->image, 0, 0, 0, 0, $width, $height, $this->imageWidth, $this->imageHeight); | |
$this->image = $image; | |
$this->getWidth(); | |
$this->getHeight(); | |
} | |
/** | |
* Add reflection to image | |
* | |
* @param int $reflectionOpacity 0-127 | |
* @param int $reflectionHeight | |
* @param int $reflectionGap | |
* @return void | |
*/ | |
function addReflection($reflectionOpacity = 60, $reflectionHeight = 80, $reflectionGap = 12) | |
{ | |
// Create new image canvas | |
$image = imagecreatetruecolor($this->imageWidth, $this->imageHeight+$reflectionHeight+$reflectionGap); | |
imagealphablending($image, false); | |
// Fill with transparency | |
imagefill($image, 0, 0, imagecolortransparent($image, imagecolorallocatealpha($image, 255, 255, 255, 127))); | |
// Copy original image leaving space for reflection & gap | |
imagecopyresampled($image, $this->image, 0, 0, 0, 0, $this->imageWidth, $this->imageHeight, $this->imageWidth, $this->imageHeight); | |
// Create new single-line image to act as buffer | |
$reflection = imagecreatetruecolor($this->imageWidth, 1); | |
imagealphablending($reflection, false); | |
imagefill($reflection, 0, 0, imagecolortransparent($reflection, imagecolorallocatealpha($reflection, 255, 255, 255, 127))); | |
for ($y = 0; $y<$reflectionHeight; $y++) { | |
// Copy each line, starting at the bottom | |
imagecopy($reflection, $image, 0, 0, 0, $this->imageHeight-$y, $this->imageWidth, 1); | |
// Set transparency to vary between reflectionOpacity and 127 | |
imagefilter($reflection, IMG_FILTER_COLORIZE, 0, 0, 0, ((127-$reflectionOpacity)+($reflectionOpacity*($y/$reflectionHeight)))); | |
// Copy line back to mirrored position in original | |
imagecopyresized($image, $reflection, $a, $this->imageHeight+$y+$reflectionGap-1, 0, 0, $this->imageWidth-(2*$a), 1, $this->imageWidth, 1); | |
} | |
$this->image = $image; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment