Created
February 24, 2015 10:33
-
-
Save bob6664569/43e30cbda53ca847ad5f to your computer and use it in GitHub Desktop.
Simple Image Resize script
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 | |
/** | |
* Convert and resize image JPG, GIF, PNG | |
* | |
* Example : | |
* function resizeAction () | |
* { | |
* $image = new Image(); | |
* | |
* $im = $image->readImage($_GET['filename']); | |
* $new = $image->resize($im, $_GET['width'], $_GET['height'], true); | |
* $image->writeImage($new); | |
* | |
* exit; | |
* } | |
* | |
*/ | |
Class Image | |
{ | |
const TYPE_JPG = 'jpg'; | |
const TYPE_GIF = 'gif'; | |
const TYPE_PNG = 'png'; | |
const TYPE_TXT = 'txt'; | |
private $_img; | |
public function __construct($img, $type = NULL) | |
{ | |
$res = $this->_readImage($img, $type); | |
if($res) | |
$this->_img = $res; | |
else | |
throw new Exception("No image found !"); | |
} | |
private function _filenameToMime ($filename) | |
{ | |
$types = array( | |
'jpg' => self::TYPE_JPG, | |
'gif' => self::TYPE_GIF, | |
'png' => self::TYPE_PNG, | |
'txt' => self::TYPE_TXT | |
); | |
foreach ($types as $extension => $mime) | |
if (preg_match('/\.'.$extension.'$/', $filename)) return $mime; | |
} | |
/** | |
* Returns a PHP image resource | |
*/ | |
private function _readImage ($filename, $type = NULL) | |
{ | |
if ($type === NULL) | |
$type = $this->_filenameToMime($filename); | |
switch ($type) | |
{ | |
case self::TYPE_JPG: | |
return @imagecreatefromjpeg($filename); | |
break; | |
case self::TYPE_GIF: | |
return @imagecreatefromgif($filename); | |
break; | |
case self::TYPE_PNG: | |
return @imagecreatefrompng($filename); | |
break; | |
case self::TYPE_TXT: | |
return @imagecreatefromstring($filename); | |
break; | |
default: | |
break; | |
} | |
return false; | |
} | |
private function _imagealphamask($mask) { | |
// Get sizes and set up new picture | |
$xSize = imagesx($this->_img); | |
$ySize = imagesy($this->_img); | |
$newPicture = imagecreatetruecolor( $xSize, $ySize ); | |
imagesavealpha( $newPicture, true ); | |
imagefill( $newPicture, 0, 0, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) ); | |
// Resize mask if necessary | |
if( $xSize != imagesx( $mask ) || $ySize != imagesy( $mask ) ) { | |
$tempPic = imagecreatetruecolor( $xSize, $ySize ); | |
imagecopyresampled( $tempPic, $mask, 0, 0, 0, 0, $xSize, $ySize, imagesx( $mask ), imagesy( $mask ) ); | |
imagedestroy( $mask ); | |
$mask = $tempPic; | |
} | |
// Perform pixel-based alpha map application | |
for( $x = 0; $x < $xSize; $x++ ) { | |
for( $y = 0; $y < $ySize; $y++ ) { | |
$alpha = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) ); | |
$alpha = 127 - floor( $alpha[ 'red' ] / 2 ); | |
$color = imagecolorsforindex( $this->_img, imagecolorat( $this->_img, $x, $y ) ); | |
imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) ); | |
} | |
} | |
imagedestroy($this->_img); | |
$this->_img = $newPicture; | |
} | |
public function getImage() | |
{ | |
return $this->_img; | |
} | |
public function writeImage ($filename, $forceType = false) | |
{ | |
$type = $this->_filenameToMime($filename); | |
$finalType = ($forceType) ? $forceType : $type; | |
$filename = ($finalType != $type) ? substr($filename, 0, strlen($filename) - 4).'.'.$finalType : $filename; | |
switch ($finalType) | |
{ | |
case self::TYPE_JPG: | |
imagejpeg($this->_img, $filename); | |
break; | |
case self::TYPE_GIF: | |
imagegif($this->_img, $filename); | |
break; | |
case self::TYPE_PNG: | |
imagepng($this->_img, $filename); | |
break; | |
default: | |
break; | |
} | |
} | |
public function resize ($width = null, $height = null, $crop = true, $fetch = false, $zoom = false) | |
{ | |
$origW = imagesx($this->_img); | |
$origH = imagesy($this->_img); | |
$newRatioW = $origW / $width; | |
$newRatioH = $origH / $height; | |
if($fetch) | |
{ | |
$newImgW = $width; | |
$newImgH = $height; | |
if($crop) | |
{ | |
if($newRatioW > $newRatioH) | |
{ | |
$newRatioW = $newRatioH; | |
$newX = 0 - ((($origW / $newRatioW) - $width) / 2); | |
$newY = 0; | |
}else{ | |
$newRatioH = $newRatioW; | |
$newX = 0; | |
$newY = 0 - ((($origH / $newRatioH) - $height) / 2); | |
} | |
}else{ | |
$newX = $newY = 0; | |
$newW = $width; | |
$newH = $height; | |
} | |
}else{ | |
if($crop) | |
{ | |
$newImgH = $height; | |
$newImgW = $width; | |
if($newRatioW > $newRatioH) | |
{ | |
if($newRatioH < 1) | |
{ | |
if($newRatioW < 1) return true; // No resize needed | |
$newRatioH = 1; | |
$newImgH = $origH; | |
}else | |
if($newRatioW < 1) $newImgW = $origW; | |
$newRatioW = $newRatioH; | |
$newX = 0 - ((($origW / $newRatioW) - $width) / 2); | |
$newY = 0; | |
}else{ | |
if($newRatioW < 1) | |
{ | |
if($newRatioH < 1) return true; // No resize needed | |
$newRatioW = 1; | |
$newImgW = $origW; | |
}else | |
if($newRatioH < 1) $newImgH = $origH; | |
$newRatioH = $newRatioW; | |
$newX = 0; | |
$newY = 0 - ((($origH / $newRatioH) - $height) / 2); | |
} | |
}else{ | |
$newX = $newY = 0; | |
if($newRatioW < $newRatioH) | |
{ | |
$newRatioW = $newRatioH; | |
$newImgW = $origW / $newRatioH; | |
$newImgH = $origH / $newRatioH; | |
}else{ | |
$newRatioH = $newRatioW; | |
$newImgW = $origW / $newRatioW; | |
$newImgH = $origH / $newRatioW; | |
} | |
} | |
} | |
// now we know the new image's dimensions | |
$newW = $origW / $newRatioW; | |
$newH = $origH / $newRatioH; | |
$newImg = imagecreatetruecolor($newImgW, $newImgH); | |
imagealphablending( $newImg, false ); | |
imagesavealpha( $newImg, true ); | |
if($zoom) | |
{ | |
$perc = 1 + ($zoom / 100); | |
$newW = $newW * $perc; | |
$newH = $newH * $perc; | |
$newX -= ($newW - ($newW / $perc)) / 2; | |
$newY -= ($newY - ($newY / $perc)) / 2; | |
} | |
imagecopyresampled($newImg, $this->_img, $newX, $newY, 0, 0, $newW, $newH, $origW, $origH); | |
$this->_img = $newImg; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment