2013-04-18 152 views
1

我正在嘗試重新設計我的網站,以便我的原始方形,基於圖像的圖像渲染可以更多地是圖像的切口......以擺脫網格模式。PNG - 保留透明度

下面是它原來的樣子......

enter image description here

這裏就是我要去爲我的粗略實物模型:

enter image description here

所以我重新保存圖像的縮略圖具有透明背景......我只想讓狗展示,而正方形是透明的,它會顯示下面的網站背景。

enter image description here

然而,當我呈現在網頁上,它有這個黑色的背景。

enter image description here

我檢查了我的CSS,看看是否有某種IMG類或類爲渲染的漫畫...甚至引導,看看那裏有可能是背景色感分配給黑色(也搜索到了十六進制代碼000000),但沒有找到一個...

後來我發現,它與我的縮略腳本重新採樣PNG的方式做......因爲我m爲所謂的透明圖像獲得黑色背景,我責怪imagecreatetruecolor()其中returns an image identifier representing a black image of the specified size.

我想在這裏以下Cheekysoft's question關於重新取樣後保持透明......但它沒有工作......他的兩個要點是:

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); 

?> 

誰能幫助?

謝謝!

回答

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); 
// ... 
+0

哎呀我經過在$ IMAGE_FILE到imagealphablending(),而不是$ tmp_img ...爲什麼它需要的溫度圖像,而不是原來的形象呢? – Growler 2013-04-18 05:35:39

+0

您必須使用未創建的臨時圖像創建圖像。您創建的圖像將使用黑色背景默認值 – mukund 2013-04-18 05:40:37

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);