private function _resize()
{
// Setting up the ratios needed for resizing. We will compare these below to determine how to
// resize the image (based on height or based on width)
$this->xRatio = $this->maxWidth / $this->width;
$this->yRatio = $this->maxHeight / $this->height;
if ($this->xRatio * $this->height < $this->maxHeight || $this->forceWidth === true) {
// Resize the image based on width
$this->tnHeight = ceil($this->xRatio * $this->height);
$this->tnWidth = $this->maxWidth;
} else {
$this->tnWidth = ceil($this->yRatio * $this->width);
$this->tnHeight = $this->maxHeight;
}
// We don't want to run out of memory
ini_set('memory_limit', $this->memory_limit);
// Set up a blank canvas for our resized image (destination)
$this->dst = imagecreatetruecolor($this->tnWidth, $this->tnHeight);
// Set up the appropriate image handling functions based on the original image's mime type
switch ($this->mime) {
case 'image/gif':
// We will be converting GIFs to PNGs to avoid transparency issues when resizing GIFs
// This is maybe not the ideal solution, but IE6 can suck it
$this->creationFunction = 'ImageCreateFromGif';
$this->outputFunction = 'ImagePng';
$this->mime = 'image/png';
// We need to convert GIFs to PNGs
$this->doSharpen = FALSE;
$this->quality = round(10 - $this->quality / 10);
// We are converting the GIF to a PNG and PNG needs a compression level of 0 (no compression) through 9
break;
case 'image/x-png':
case 'image/png':
$this->creationFunction = 'ImageCreateFromPng';
$this->outputFunction = 'ImagePng';
$this->doSharpen = FALSE;
$this->quality = round(10 - $this->quality / 10);
// PNG needs a compression level of 0 (no compression) through 9
break;
default:
$this->creationFunction = 'ImageCreateFromJpeg';
$this->outputFunction = 'ImageJpeg';
$this->doSharpen = FALSE;
break;
}
// Read in the original image
$function = $this->creationFunction;
$this->src = $function($this->server_path . $this->image);
if (in_array($this->mime, array('image/gif', 'image/png'))) {
if ($this->color == '') {
// If this is a GIF or a PNG, we need to set up transparency
imagealphablending($this->dst, false);
imagesavealpha($this->dst, true);
} else {
// Fill the background with the specified color for matting purposes
if ($this->color[0] == '#') {
$this->color = substr($this->color, 1);
}
$this->background = FALSE;
if (strlen($this->color) == 6) {
$this->background = imagecolorallocate($this->dst, hexdec($this->color[0] . $this->color[1]), hexdec($this->color[2] . $this->color[3]), hexdec($this->color[4] . $this->color[5]));
} else {
if (strlen($color) == 3) {
$this->background = imagecolorallocate($this->dst, hexdec($this->color[0] . $this->color[0]), hexdec($this->color[1] . $this->color[1]), hexdec($this->color[2] . $this->color[2]));
}
}
if ($this->background) {
imagefill($this->dst, 0, 0, $this->background);
}
}
}
// Resample the original image into the resized canvas we set up earlier
ImageCopyResampled($this->dst, $this->src, 0, 0, $this->offsetX, $this->offsetY, $this->tnWidth, $this->tnHeight, $this->width, $this->height);
// try to grayscale it
if ($this->grayscale == TRUE) {
$this->dst = $this->_makeGrayscale($this->dst);
}
$this->etag = $this->_saveFile();
}