Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 292 lines (245 sloc) 8.392 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
<?php
/**
* Create a resampled version of an image with diffent modes of changing its dimensions
*
* @Author Markus J Doetsch
*
* @Example
* $newImage = new ReSample();
* $newImage->setImage(PATH_TO_IMAGE, NAME_SUFFIX);
* $newImage->fit(WIDTH, HEIGHT);
* echo $newImage->getImage();
*/
class ReSample
{
  protected $_resource;
  protected $_width;
  protected $_height;
  protected $_filename;
  protected $_type;
  
  public function __construct(){
    $gd = gd_info();
    
    if(!$gd['PNG Support'] || !$gd['JPEG Support']){
      throw new Exception("No support for png and jpg found");
    }
  }
  
  public function __destruct(){
    if (is_resource($this->_resource)) {
      imagedestroy($this->_resource);
    }
  }
  
  public function setImage($resource, $versionName){
        
    // get image info, validate it's an image
    if(!list($width, $height, $type) = getimagesize($resource)) {
      throw new Exception("Must be an image");
    }
    
    // get file info
    $info = pathinfo($resource);
    
    // create image from resource
    switch($type){
      case IMAGETYPE_JPEG: $img = imagecreatefromjpeg($resource); break;
      case IMAGETYPE_PNG: $img = imagecreatefrompng($resource); break;
      case IMAGETYPE_GIF: $img = imagecreatefromgif($resource); break;
      case IMAGETYPE_BMP: $img = imagecreatefromwbmp($resource); break;
      default : throw new Exception("Supplied image must be bmp, gif, jpg or png");
    }
    
    // store image & data
    $this->_resource = $img;
    $this->_width = $width;
    $this->_height = $height;
    $this->_filename = $info['dirname'] . '/' . $info['filename'] . (!empty($versionName) ? '_' . $versionName : ''); // NOTE: without extension!
    $this->_type = $type;
  }
  
  public function getImage($type = null){
    if(!empty($type) && $type !== $this->_type){
      if($type == IMAGETYPE_JPEG || $type == IMAGETYPE_PNG){
        $this->_type = $type;
      }else{
        throw new Exception("Image type for output must be png or jpg");
      }
    }
    
    $this->_saveImage();
    
    return $this->_filename . $this->_getExtension($this->_type);
  }
  
  public function getImageInfo(){
    return array(
      'path' => $this->_filename . $this->_getExtension($this->_type),
      'width' => $this->_width,
      'height' => $this->_height,
      'type' => $this->_type,
      'mime' => image_type_to_mime_type($this->_type)
    );
  }
  
  public function getRawImage(){
    return $this->_resource;
  }
  
  /** @method resize
* Resize to given dimensions regardless of proportions */
  public function resize($width, $height){
    
    $sourceDims = array(
      'x' => 0,
      'y' => 0,
      'width' => $this->_width,
      'height' => $this->_height,
    );
    
    $destDims = array(
      'x' => 0,
      'y' => 0,
      'width' => $width,
      'height' => $height,
    );
    
    $this->_createImage($width, $height, $sourceDims, $destDims);
  }
  
  /** @method fit
* Proportionally scale to the longest side within the given dimensions */
  public function fit($width, $height){
    
    $srcRatio = $this->_width / $this->_height;
    $destRatio = $width / $height;
    
    // source image is wider than requested
    if($srcRatio > $destRatio){
      $tmpWidth = $width;
      $tmpHeight = round($width / $srcRatio);
      $destX = 0;
      $destY = 0;
    }else{
      $tmpWidth = round($height * $srcRatio);
      $tmpHeight = $height;
      $destX = 0;
      $destY = 0;
    }
    
    $sourceDims = array(
      'x' => 0,
      'y' => 0,
      'width' => $this->_width,
      'height' => $this->_height
    );
    
    $destDims = array(
      'x' => $destX,
      'y' => $destY,
      'width' => $tmpWidth,
      'height' => $tmpHeight
    );
    
    $this->_createImage((int)$tmpWidth, (int)$tmpHeight, $sourceDims, $destDims);
  }
  
  /** @method fill
* Completely fill the given dimensions proportionally,
* overlap will be cropped from center */
  public function fill($width, $height){
    
    $srcRatio = $this->_width / $this->_height;
    $destRatio = $width / $height;
        
    // source image is wider than requested
    if($srcRatio > $destRatio){
      $tmpWidth = round($this->_height * $destRatio);
      $tmpHeight = $this->_height;
      $sourceX = round(($this->_width - $tmpWidth) / 2);
      $sourceY = 0;
    }else{
      $tmpWidth = $this->_width;
      $tmpHeight = round($this->_width / $destRatio);
      $sourceX = 0;
      $sourceY = round(($this->_height - $tmpHeight) / 2);
    }
    
    $sourceDims = array(
      'x' => $sourceX,
      'y' => $sourceY,
      'width' => $tmpWidth,
      'height' => $tmpHeight
    );
    
    $destDims = array(
      'x' => 0,
      'y' => 0,
      'width' => $width,
      'height' => $height
    );
    
    $this->_createImage($width, $height, $sourceDims, $destDims);
  }
  
  /** @method crop
* Crop the given dimensions
* if no offset coordinates are given, cropping will occur from center
*/
  public function crop($width, $height, $x = false, $y = false){
    $sourceDims = array(
      'x' => ($x ? $x : $this->_width / 2 - $width / 2 ),
      'y' => ($y ? $y : $this->_height / 2 - $height / 2),
      'width' => $width,
      'height' => $height
    );
    
    $destDims = array(
      'x' => 0,
      'y' => 0,
      'width' => $width,
      'height' => $height
    );
    
    $this->_createImage($width, $height, $sourceDims, $destDims);
  }
  
  /** @method letterbox
* Creates an image with the supplied dimensions into which
* the original image is proportionally fitted and centered.
* Missing pixels are filled with transparent (png) or white (jpg)
* */
  public function letterBox($width, $height){
    
    $srcRatio = $this->_width / $this->_height;
    $destRatio = $width / $height;
    
    // source image is wider than requested
    if($srcRatio > $destRatio){
      $tmpWidth = $width;
      $tmpHeight = round($width / $srcRatio);
      $destX = 0;
      $destY = round(($height - $tmpHeight) / 2);
    }else{
      $tmpWidth = round($height * $srcRatio);
      $tmpHeight = $height;
      $destX = round(($width - $tmpWidth) / 2);
      $destY = 0;
    }
    
    $sourceDims = array(
      'x' => 0,
      'y' => 0,
      'width' => $this->_width,
      'height' => $this->_height
    );
    
    $destDims = array(
      'x' => $destX,
      'y' => $destY,
      'width' => $tmpWidth,
      'height' => $tmpHeight
    );
    
    $this->_createImage($width, $height, $sourceDims, $destDims);
  }
  
  private function _createImage($width, $height, $sourceDims, $destDims){
    // TODO: transparency flattening of semi-transparent areas on background in jpg mode
    
    // create new image
    $newImage = imagecreatetruecolor($width, $height);
    
    // initial alpha properties
    imagealphablending($newImage, false);
    imagesavealpha($newImage, true);
    
    // fill image with transparent color
    $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
    imagefilledrectangle($newImage, 0, 0, $width, $height, $transparent);
    
    // change blending before copy
    imagealphablending($newImage, true);
    
    // copy the resampled original into the new image
    imagecopyresampled(
      $newImage, $this->_resource,
      $destDims['x'], $destDims['y'], $sourceDims['x'], $sourceDims['y'],
      $destDims['width'], $destDims['height'], $sourceDims['width'], $sourceDims['height']
    );
    
    // apply results
    $this->_resource = $newImage;
    $this->_width = $width;
    $this->_height = $height;
  }
  
  private function _saveImage(){
    // TODO: error handling if write failed
    switch($this->_type){
      case IMAGETYPE_JPEG: imagejpeg($this->_resource, $this->_filename . $this->_getExtension(IMAGETYPE_JPEG), 85); break;
      default: imagepng($this->_resource, $this->_filename . $this->_getExtension(IMAGETYPE_PNG)); break;
    }
  }
  
  private function _getExtension($type){
    switch($type){
      case IMAGETYPE_JPEG: return '.jpg'; break;
      default: return image_type_to_extension($type);
    }
  }
}
Something went wrong with that request. Please try again.