Skip to content

Instantly share code, notes, and snippets.

@delboy1978uk
Created December 1, 2014 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delboy1978uk/e7883706473e0eeca315 to your computer and use it in GitHub Desktop.
Save delboy1978uk/e7883706473e0eeca315 to your computer and use it in GitHub Desktop.
<?php
class Image {
protected $_image;
protected $_image_type;
/**
* @param string $filename
*/
public function load($filename)
{
$image_info = getimagesize($filename);
$this->_image_type = $image_info[2];
if( $this->_image_type == IMAGETYPE_JPEG )
{
$this->_image = imagecreatefromjpeg($filename);
}
elseif( $this->_image_type == IMAGETYPE_GIF )
{
$this->_image = imagecreatefromgif($filename);
}
elseif( $this->_image_type == IMAGETYPE_PNG )
{
$this->_image = imagecreatefrompng($filename);
}
}
/**
* @param string $filename
* @param string $image_type
* @param int $compression
* @param string $permissions
*/
public function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->_image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->_image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->_image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
/**
* @param string
*/
public function output()
{
switch($this->getImageType())
{
case IMAGETYPE_JPEG:
imagejpeg($this->_image);
break;
case IMAGETYPE_GIF:
imagegif($this->_image);
break;
case IMAGETYPE_PNG:
imagepng($this->_image);
break;
}
}
public function getWidth() {
return imagesx($this->_image);
}
public function getHeight() {
return imagesy($this->_image);
}
public function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
public function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
public function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
/**
* Now with added Transparency resizing feature
* @param int $width
* @param int $height
*/
public function resize($width,$height)
{
$new_image = imagecreatetruecolor($width, $height);
if ( ($this->getImageType() == IMAGETYPE_GIF) || ($this->getImageType() == IMAGETYPE_PNG) )
{
$transparency = imagecolortransparent($this->_image);die($transparency.'.');
if ($transparency >= 0)
{
$transparent_color = imagecolorsforindex($this->_image, $transparency);
$transparency = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($new_image, 0, 0, $transparency);
imagecolortransparent($new_image, $transparency);
}
elseif ($this->getImageType() == IMAGETYPE_PNG)
{
imagealphablending($new_image, false);
$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
imagefill($new_image, 0, 0, $color);
imagesavealpha($new_image, true);
}
}
imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->_image = $new_image;
}
public function getImageType()
{
return $this->_image_type;
}
public function getHeader() {
if( $this->_image_type == IMAGETYPE_JPEG ) {
return 'image/jpeg';
} elseif( $this->_image_type == IMAGETYPE_GIF ) {
return 'image/gif';
} elseif( $this->_image_type == IMAGETYPE_PNG ) {
return 'image/png';
}
}
/**
* Free's up memory
*/
public function destroy()
{
imagedestroy($this->_image);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment