public function crop($width, $height, $startX = null, $startY = null)
    {
        if ($width === null) {
            $width = $this->width;
        }

        if ($height === null) {
            $height = $this->height;
        }

        if ($startX === null) {
            $startX = floor(($this->width - $width) / 2);
        }

        if ($startY === null) {
            $startY = floor(($this->height - $height) / 2);
        }

        // Calculate dimensions
        $startX = max(0, min($this->width, $startX));
        $startY = max(0, min($this->height, $startY));
        $width = min($width, $this->width - $startX);
        $height = min($height, $this->height - $startY);

        $image = imagecreatetruecolor($width, $height);
        $this->preserveTransparency($image);

        imagecopyresampled($image, $this->image, 0, 0, $startX, $startY, $width, $height, $width, $height);
        $this->setImage($image);
        $this->width = $width;
        $this->height = $height;

        return $this;
    }