public function save($filename, $image_type = null, $quality = null, $permissions = null)
{
$image_type = $image_type ?: $this->source_type;
$dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight());
imageinterlace($dest_image, $this->interlace);
switch ($image_type) {
case IMAGETYPE_GIF:
$background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1);
imagecolortransparent($dest_image, $background);
imagefill($dest_image, 0, 0, $background);
imagesavealpha($dest_image, true);
break;
case IMAGETYPE_JPEG:
$background = imagecolorallocate($dest_image, 255, 255, 255);
imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background);
break;
case IMAGETYPE_PNG:
imagealphablending($dest_image, false);
imagesavealpha($dest_image, true);
break;
}
imagecopyresampled($dest_image, $this->source_image, $this->dest_x, $this->dest_y, $this->source_x, $this->source_y, $this->getDestWidth(), $this->getDestHeight(), $this->source_w, $this->source_h);
switch ($image_type) {
case IMAGETYPE_GIF:
imagegif($dest_image, $filename);
break;
case IMAGETYPE_JPEG:
if ($quality === null) {
$quality = $this->quality_jpg;
}
imagejpeg($dest_image, $filename, $quality);
break;
case IMAGETYPE_PNG:
if ($quality === null) {
$quality = $this->quality_png;
}
imagepng($dest_image, $filename, $quality);
break;
}
if ($permissions) {
chmod($filename, $permissions);
}
imagedestroy($dest_image);
return $this;
}