Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created May 3, 2012 12:34
Show Gist options
  • Save eminetto/2585353 to your computer and use it in GitHub Desktop.
Save eminetto/2585353 to your computer and use it in GitHub Desktop.
Coderockr Image
<?php
/**
* Image
*
* @author Rafael Armenio - <[email protected]>
* @author Elton Minetto - <[email protected]>
* @author Andre Espeiorin - <[email protected]>
* Based in http://www.codesphp.com/php-category/gd-graphiques/php-function-proportional-image-resizing.html
*/
namespace Coderockr;
class Image {
/**
* image
*
* @access public
* @var resource
*/
public $image;
/**
* source
*
* @access public
* @var string
*/
public $source;
/**
* origWidth
*
* @access public
* @var integer
*/
public $origWidth;
/**
* origHeight
*
* @access public
* @var integer
*/
public $origHeight;
/**
* type
*
* @access public
* @var string
*/
public $type;
/**
* destType
*
* @access public
* @var string
*/
public $destType;
/**
* quality
*
* @access public
* @var float
*/
public $quality;
/**
* __construct
*
* @access public
* @param string $source
* @return void
*/
public function __construct($source, $destType = 'jpg') {
$this->source = $source;
$this->destType = $destType;
/*if( ! file_exists($this->source) ){
$this->image = null;
return false;
}*/
$size = getimagesize($this->source);
$this->origWidth = $size[0];
$this->origHeight = $size[1];
$this->type = $size[2];
$this->quality = 100;
switch ($this->type) {
case 1:
$this->image = imagecreatefromgif($this->source);
break;
case 2:
$this->image = imagecreatefromjpeg($this->source);
break;
case 3:
$this->image = imagecreatefrompng($this->source);
break;
case 6:
$this->image = self::_imageCreateFromBMP($this->source);
break;
default:
$this->image = null;
return false;
break;
}
}
public function resize( $width = 0, $height = 0, $file, $dest, $proportional = true, $output = 'file', $delete_original = false )
{
if ( $height <= 0 && $width <= 0 ) return false;
$info = getimagesize($file);
$image = '';
$final_width = 0;
$final_height = 0;
list($width_old, $height_old) = $info;
if ($proportional) {
if ($width == 0) $factor = $height/$height_old;
elseif ($height == 0) $factor = $width/$width_old;
else $factor = max ( $width / $width_old, $height / $height_old);
$final_width = round ($width_old * $factor);
$final_height = round ($height_old * $factor);
} else {
$final_width = ( $width <= 0 ) ? $width_old : $width;
$final_height = ( $height <= 0 ) ? $height_old : $height;
}
switch ($info[2]) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($file);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($file);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($file);
break;
default:
return false;
}
//$image_resized = imagecreatetruecolor( $final_width, $final_height );
$image_resized = imagecreatetruecolor( $width, $height );
if(($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG)) {
$trnprt_indx = imagecolortransparent($image);
// If we have a specific transparent color
if($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($image, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($image_resized, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($image_resized, $trnprt_indx);
}
// Always make a transparent background color for PNGs that don't have one allocated already
elseif ($info[2] == IMAGETYPE_PNG) {
// Turn off transparency blending (temporarily)
imagealphablending($image_resized, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($image_resized, 0, 0, $color);
// Restore transparency blending
imagesavealpha($image_resized, true);
}
}
// faz o calculo para jogar a imagem no centro do tamanho maximo
$x = 0;
$y = 0;
if( $width != $final_width ){
$x = ($width - $final_width) / 2;
}
if( $height != $final_height ){
$y = ($height - $final_height) / 2;
}
imagecopyresampled($image_resized, $image, $x, $y, 0, 0, $final_width, $final_height, $width_old, $height_old);
//imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
if($delete_original) @unlink($file);
switch (strtolower($output)) {
case 'browser':
$mime = image_type_to_mime_type($info[2]);
header("Content-type: $mime");
$output = NULL;
break;
case 'file':
$output = $dest;
break;
case 'return':
return $image_resized;
break;
default:
break;
}
switch ($info[2]) {
case IMAGETYPE_GIF:
imagegif($image_resized, $output);
break;
case IMAGETYPE_JPEG:
imagejpeg($image_resized, $output);
break;
case IMAGETYPE_PNG:
imagepng($image_resized, $output);
break;
default:
return false;
}
return true;
}
/**
* Cria uma imagem no formato BMP.
* http://www.php.net/manual/pt_BR/function.imagecreate.php#53879
*/
private function _imageCreateFromBMP($filename)
{
//Ouverture du fichier en mode binaire
if (! $f1 = fopen($filename,"rb")) return FALSE;
//1 : Chargement des ent�tes FICHIER
$FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
if ($FILE['file_type'] != 19778) return FALSE;
//2 : Chargement des ent�tes BMP
$BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
'/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
'/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
$BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
$BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
$BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
$BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
$BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
$BMP['decal'] = 4-(4*$BMP['decal']);
if ($BMP['decal'] == 4) $BMP['decal'] = 0;
//3 : Chargement des couleurs de la palette
$PALETTE = array();
if ($BMP['colors'] < 16777216)
{
$PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
}
//4 : Cr�ation de l'image
$IMG = fread($f1,$BMP['size_bitmap']);
$VIDE = chr(0);
$res = imagecreatetruecolor($BMP['width'],$BMP['height']);
$P = 0;
$Y = $BMP['height']-1;
while ($Y >= 0)
{
$X=0;
while ($X < $BMP['width'])
{
if ($BMP['bits_per_pixel'] == 24)
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
elseif ($BMP['bits_per_pixel'] == 16)
{
$COLOR = unpack("n",substr($IMG,$P,2));
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 8)
{
$COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 4)
{
$COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
elseif ($BMP['bits_per_pixel'] == 1)
{
$COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}
else
return FALSE;
imagesetpixel($res,$X,$Y,$COLOR[1]);
$X++;
$P += $BMP['bytes_per_pixel'];
}
$Y--;
$P+=$BMP['decal'];
}
//Fermeture du fichier
fclose($f1);
return $res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment