4

PHP を使用して円形の画像をトリミングしたいのですが、新しい画像には透明なピクセルが含まれているようです。もちろん、楕円の外側の領域のみを背景を透明にしたい

私のコードは以下のとおりです。

        $image = imagecreatetruecolor($this->dst_w, $this->dst_h);
        imagealphablending($image,true);
        imagecopy ( $image , $image_s , 0, 0, $this->src_x, $this->src_y, $this->dst_w, $this->dst_h );
        $mask = imagecreatetruecolor($this->src_x, $this->src_y);
        $mask = imagecreatetruecolor($this->dst_w, $this->dst_h);
        $transparent = imagecolorallocate($mask, 255, 0, 0);
        imagecolortransparent($mask, $transparent);
        imagefilledellipse($mask, $this->dst_w/2, $this->dst_h/2, $this->dst_w, $this->dst_h, $transparent);
        $red = imagecolorallocate($mask, 0, 0, 0);
        imagecopymerge($image, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h,100);
        imagecolortransparent($image, $red);
        imagefill($image,0,0, $red);

        if ($ext=="jpg" || $ext=="jpeg") {
            imagejpeg($image, $this->croppedImage);
        } else if ($ext=="png") {
            imagepng($image, $this->croppedImage);
        }           
        imagedestroy($image);
        imagedestroy($mask);
        // <------- END generate cropped Image ------->

        // <------- START generate transparent Image ------->               
        $this->generateTransparentImage('circle');

……

実際に生成された画像の例は次のとおりです。 ここに画像の説明を入力

編集: generateTransparentImage 関数は、上記のコードとは関係ありません。この関数は、この画像を生成します: http://s7.postimage.org/byybq9163/Koala7_500x375_c_transparent.png

4

3 に答える 3

8

注意すべき点がいくつかあります。

@DainisAbolsが示唆したように、透明性のために珍しい色を使用することをお勧めします。ここでは、黒を使用しています:

    $red = imagecolorallocate($mask, 0, 0, 0);
    imagecopymerge($image, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h,100);
    imagecolortransparent($image, $red);

変数が赤と呼ばれていても、RGB 値は 0-0-0 です。珍しい色には、派手な青 (0-0-255)、派手な緑 (0-255-0)、派手な黄色 (255-255-0)、派手なシアン (0-255-255)、派手なピンク (255-0-) があります。 255)。赤はどこにでもありふれた色で、あまり派手ではないので特別色から外しています。

次に、ここにある画像が両方ともトゥルー カラーのものであっても、各画像に色を割り当てることをお勧めします。$red上の例では、 の黒を含む変数を作成して$maskいますが、 の透過色として使用しています$image

最後に、画像サイズと同じ半径の楕円を描いているので、imagefill左上の角だけでなく、画像の各角も必要です。あなたの例では機能しますが、これは黒を透明色に選択したためです。

これが完全な実装です。

<?php

class CircleCrop
{

    private $src_img;
    private $src_w;
    private $src_h;
    private $dst_img;
    private $dst_w;
    private $dst_h;

    public function __construct($img)
    {
        $this->src_img = $img;
        $this->src_w = imagesx($img);
        $this->src_h = imagesy($img);
        $this->dst_w = imagesx($img);
        $this->dst_h = imagesy($img);
    }

    public function __destruct()
    {
        if (is_resource($this->dst_img))
        {
            imagedestroy($this->dst_img);
        }
    }

    public function display()
    {
        header("Content-type: image/png");
        imagepng($this->dst_img);
        return $this;
    }

    public function reset()
    {
        if (is_resource(($this->dst_img)))
        {
            imagedestroy($this->dst_img);
        }
        $this->dst_img = imagecreatetruecolor($this->dst_w, $this->dst_h);
        imagecopy($this->dst_img, $this->src_img, 0, 0, 0, 0, $this->dst_w, $this->dst_h);
        return $this;
    }

    public function size($dstWidth, $dstHeight)
    {
        $this->dst_w = $dstWidth;
        $this->dst_h = $dstHeight;
        return $this->reset();
    }

    public function crop()
    {
        // Intializes destination image
        $this->reset();

        // Create a black image with a transparent ellipse, and merge with destination
        $mask = imagecreatetruecolor($this->dst_w, $this->dst_h);
        $maskTransparent = imagecolorallocate($mask, 255, 0, 255);
        imagecolortransparent($mask, $maskTransparent);
        imagefilledellipse($mask, $this->dst_w / 2, $this->dst_h / 2, $this->dst_w, $this->dst_h, $maskTransparent);
        imagecopymerge($this->dst_img, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h, 100);

        // Fill each corners of destination image with transparency
        $dstTransparent = imagecolorallocate($this->dst_img, 255, 0, 255);
        imagefill($this->dst_img, 0, 0, $dstTransparent);
        imagefill($this->dst_img, $this->dst_w - 1, 0, $dstTransparent);
        imagefill($this->dst_img, 0, $this->dst_h - 1, $dstTransparent);
        imagefill($this->dst_img, $this->dst_w - 1, $this->dst_h - 1, $dstTransparent);
        imagecolortransparent($this->dst_img, $dstTransparent);

        return $this;
    }

}

デモ :

$img = imagecreatefromjpeg("test4.jpg");
$crop = new CircleCrop($img);
$crop->crop()->display();

結果 :

ここに画像の説明を入力

于 2012-10-19T07:51:53.790 に答える
1

黒をトリミングして削除しています(または黒を透明に設定しています)。画像に黒色が含まれているため、画像も削除されます。

色を削除する代わりに、外側のレイヤーの色をピンクなどに置き換えてから、透明に設定してみてください。

于 2012-10-18T11:14:14.870 に答える