function resize($width, $height)
{
// Generate new GD image
$new = imagecreatetruecolor($width, $height);
if ($this->original_info['format'] === 'gif') {
// Preserve transparency in GIFs
$transparent_index = imagecolortransparent($this->image);
if ($transparent_index >= 0) {
$transparent_color = imagecolorsforindex($this->image, $transparent_index);
$transparent_index = imagecolorallocate($new, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($new, 0, 0, $transparent_index);
imagecolortransparent($new, $transparent_index);
}
} else {
// Preserve transparency in PNGs (benign for JPEGs)
imagealphablending($new, false);
imagesavealpha($new, true);
}
// Resize
imagecopyresampled($new, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
// Update meta data
$this->width = $width;
$this->height = $height;
$this->image = $new;
return $this;
}