public function resize($newWidth, $newHeight, $option = 'auto', $backgroundColor = '#000000', $sourceX = 0, $sourceY = 0)
{
if (empty($this->image)) {
return false;
}
if (!in_array($option, $this->availableOptions)) {
$option = 'auto';
}
if ($option == 'crop') {
$optimalwidth = $newWidth;
$optimalheight = $newHeight;
if ($this->width < $newWidth) {
$optimalwidth = $this->width;
}
if ($this->height < $newHeight) {
$optimalheight = $this->height;
}
$this->crop($optimalwidth, $optimalheight, $sourceX, $sourceY);
} else {
$optimalheight = $this->getSizeByFixedWidth($newWidth);
$optimalwidth = $this->getSizeByFixedHeight($newHeight);
if ($optimalheight > $newHeight) {
$optimalheight = $this->getSizeByFixedWidth($optimalwidth);
} elseif ($optimalwidth > $newWidth) {
$optimalwidth = $this->getSizeByFixedHeight($optimalheight);
}
$this->imageResized = imagecreatetruecolor($optimalwidth, $optimalheight);
imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalwidth, $optimalheight, $this->width, $this->height);
}
$tmpimage = @imagecreatetruecolor($newWidth, $newHeight);
$rgbArray = $this->hex2rgb($backgroundColor);
if (empty($rgbArray)) {
$backgroundColor = imagecolorallocate($tmpimage, 0, 0, 0);
} else {
$backgroundColor = imagecolorallocate($tmpimage, $rgbArray['red'], $rgbArray['green'], $rgbArray['blue']);
}
imagefill($tmpimage, 0, 0, $backgroundColor);
$dstX = (int) (0.5 * ($newWidth - $optimalwidth));
$dstY = (int) (0.5 * ($newHeight - $optimalheight));
imagecopyresampled($tmpimage, $this->imageResized, $dstX, $dstY, 0, 0, $optimalwidth, $optimalheight, $optimalwidth, $optimalheight);
$this->imageResized = $tmpimage;
return $this;
}