1

元の正方形のタイルベースの画像レンダリングが画像の切り抜きになるように、サイトを再設計しようとしています...そのグリッドパターンを取り除くために.

元の姿はこんな感じ…

ここに画像の説明を入力

これが私がやろうとしていることの大まかなモックアップです:

ここに画像の説明を入力

そこで、背景が透明な画像のサムネイルを再保存しました...犬を見せたいだけで、正方形は透明で、その下にサイトの背景が表示されます。

ここに画像の説明を入力

しかし、ページにレンダリングすると、この黒い背景になります。

ここに画像の説明を入力

私はCSSをチェックして、何らかのimgクラス、またはレンダリングされたコミックのクラスがあるかどうかを確認しました...またはブートストラップでさえ、背景色が黒に割り当てられている場所があるかどうかを確認しました(また、hexも検索しました)コード 000000) が見つかりませんでした...

次に、サムネイル作成スクリプトがpngを再サンプリングする方法に関係していることがわかりました...おそらく透明な画像の背景が黒くなっているので、imagecreatetruecolor()どちらを責めますかreturns an image identifier representing a black image of the specified size.

リサンプリング後の透明度の保持に関する Cheekysoft の質問hereに従ってみましたが、うまくいきませんでした。

imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );

サイズ変更/リサンプルする前に配置すると、透明に表示されることがわかりまし$img = imagecreatefrompng($image_file);た...これは私が望むものです...しかし、リサンプルした後ではありません。

Thumbnailer.php コード:

    <?php
#Appreciation goes to digifuzz (http://www.digifuzz.net) for help on this

$image_file = $_GET['img']; //takes in full path of image
$MAX_WIDTH = $_GET['mw'];
$MAX_HEIGHT = $_GET['mh'];
global $img;

//Check for image
if(!$image_file || $image_file == "") { 
    die("NO FILE."); 
}

//If no max width, set one
if(!$MAX_WIDTH || $MAX_WIDTH == "") { 
    $MAX_WIDTH="100"; 
}

//if no max height, set one
if(!$MAX_HEIGHT || $MAX_HEIGHT == "") { 
    $MAX_HEIGHT = "100"; 
}

$img = null;
//create image file from 'img' parameter string
if( preg_match('/\.jpg$/',$image_file) || preg_match('/\.jpeg$/',$image_file) ){ 
    $img = imagecreatefromjpeg($image_file); 
} else { 
    $img = imagecreatefrompng($image_file); 
} 

//if image successfully loaded...
if($img) {
    //get image size and scale ratio
    $width = imagesx($img);
    $height = imagesy($img);

    //takes min value of these two
    $scale = min($MAX_WIDTH/$width, $MAX_HEIGHT/$height);

    //if desired new image size is less than original, output new image
    if($scale < 1) {
        $new_width = floor($scale * $width);

        $new_height = floor($scale * $height);


        $tmp_img = imagecreatetruecolor($new_width, $new_height);

        //copy and resize old image to new image
        imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

        //replace actual image with new image
        $img = $tmp_img;
    }
}
//set the content type header
header('Content-Type: image/png', true); 

imagealphablending( $img, false );
imagesavealpha( $img, true );
imagepng($img); 


imagedestroy($img);

?>

誰でも助けることができますか?

ありがとう!

4

2 に答える 2

1

画像をリサンプリング/サイズ変更する前に、宛先画像で imagealphablending() を呼び出す必要があります。

//copy and resize old image to new image
imagealphablending($tmp_img, false);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// ...
于 2013-04-18T05:25:24.227 に答える
1

画像を作成した後、alphablending と traparent コードをかなり下に配置しました。createresample 行と、imagecreatefrompng によって作成された画像を含む透明な画像の前に配置するだけです。

imagealphablending( $tmp_img, false );
imagesavealpha( $tmp_img, true );
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

または、次のことを試すことができます

$img = ImageCreateFromPNG($image_file);
$tmp_img = imagecreatetruecolor($new_width,$new_height);
imagecolortransparent($tmp_img, imagecolorallocate($tmp_img, 0, 0, 0));
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
于 2013-04-18T05:25:29.317 に答える