public function flip($type)
    {
        // Initial positioning
        $x = 0;
        $y = 0;

        // Save original dimensions
        $width = $this->width;
        $height = $this->height;

        switch ($type) {
            case self::IMG_FLIP_BOTH:
                $x = $this->width - 1;
                $y = $this->height - 1;

                $width = -$this->width;
                $height = -$this->height;
            break;

            case self::IMG_FLIP_HORIZONTAL:
                $x = $this->width - 1;
                $width = -$this->width;
            break;

            case self::IMG_FLIP_VERTICAL:
                $y = $this->height - 1;
                $height = -$this->height;
            break;

            default:
                throw new UnexpectedValueException("Invalid flip type's value supplied");
        }

        // Done. Now create a new image
        $image = imagecreatetruecolor($this->width, $this->height);
        $this->preserveTransparency($image);

        imagecopyresampled($image, $this->image, 0, 0, $x, $y, $this->width, $this->height, $width, $height);

        // Override with new one
        $this->setImage($image);

        return $this;
    }