2013-03-06 164 views
0

從服務器加載圖像時出現問題。 PNG格式的圖像背景變黑。imagecopyresampled PNG黑色背景

對不起,英語先謝謝了。

<?php 

    //Database Settings 
    include('../../../../../includes/bdconfig.ini.php'); 

    //Database Required Settings 
    include('../../../../../includes/adminrequired.php'); 

    //If Not Logged In Redirect to Login Page 
    if(!$session){header("Location: $siteurl/admin/login/");} 

    //Listing Id 
    $productid = "$_GET[productid]"; 

    //Select Listing Information From Database Table 
    $q2 = "SELECT * from dlistings where did = '$productid'"; $r2 = mysql_query($q2); $a2 = mysql_fetch_array($r2); 

    class SimpleImage {var $image; var $image_type; 

    function load($filename) {$image_info = getimagesize($filename); $this->image_type = $image_info[2]; 
    if($this->image_type == IMAGETYPE_JPEG) {$this->image = imagecreatefromjpeg($filename);} 
    elseif($this->image_type == IMAGETYPE_GIF) {$this->image = imagecreatefromgif($filename);} 
    elseif($this->image_type == IMAGETYPE_PNG) {$this->image = imagecreatefrompng($filename);}} 

    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {if($image_type == IMAGETYPE_JPEG) {imagejpeg($this->image,$filename,$compression);} 
    elseif($image_type == IMAGETYPE_GIF) {imagegif($this->image,$filename);} 
    elseif($image_type == IMAGETYPE_PNG) {imagepng($this->image,$filename);} 
    if($permissions != null) {chmod($filename,$permissions);}} 

    function output($image_type=IMAGETYPE_JPEG) {if($image_type == IMAGETYPE_JPEG) {imagejpeg($this->image);} 
    elseif($image_type == IMAGETYPE_GIF) {imagegif($this->image);} 
    elseif($image_type == IMAGETYPE_PNG) {imagepng($this->image);}} 

    function getWidth() {return imagesx($this->image);} 
    function getHeight() {return imagesy($this->image);} 

    function resizeToHeight($height) {$ratio = $height/$this->getHeight(); 
    $width = $this->getWidth() * $ratio; 
    $this->resize($width,$height);} 

    function resizeToWidth($width) {$ratio = $width/$this->getWidth(); 
    $height = $this->getheight() * $ratio; 
    $this->resize($width,$height);} 

    function scale($scale) {$width = $this->getWidth() * $scale/100; 
    $height = $this->getheight() * $scale/100; 
    $this->resize($width,$height);} 

    function resize($width,$height) {$new_image = imagecreatetruecolor($width, $height); 
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
    $this->image = $new_image;}} 

    $image = new SimpleImage(); 

    //Re-size and Create Thumbnail if Image 1 Exist 
    if($a2[img1] && $_GET[which]==1){ 

    $img1name = "../../../../../imgs/1/$a2[img1]"; 
    $thumb1name = "../../../../../thumbs/imgs/$a2[img1]"; 

    $img1_dimensions = getimagesize($img1name); 
    $awidth = $img1_dimensions[0]; 
    $aheight = $img1_dimensions[1]; 

    $arpercent = '100'; 
    if($awidth > 350){$arpercent = '95';} 
    if($awidth > 500){$arpercent = '90';} 
    if($awidth > 700){$arpercent = '50';} 
    if($awidth > 900){$arpercent = '40';} 
    if($awidth > 1100){$arpercent = '30';} 
    if($awidth > 1400){$arpercent = '25';} 
    if($awidth > 1600){$arpercent = '20';} 
    if($awidth > 1800){$arpercent = '15';} 

    $anewwidth = round((($arpercent/100)*$awidth)); 
    $anewheight = round((($arpercent/100)*$aheight)); 

    if($anewwidth > 450){$anewwidth = '400'; $anewheight = '400';} 

    $image->load($img1name); 
    $image->resize($anewwidth,$anewheight); 
    $image->save($img1name); 

    $image->load($img1name); 
    $image->resize(150,150); 
    $image->save($thumb1name); 

    } 

    //Re-size if Image 2 Exist 
    if($a2[img2] && $_GET[which]==2){ 

    $img2name = "../../../../../imgs/2/$a2[img2]"; 

    $img2_dimensions = getimagesize($img2name); 
    $bwidth = $img2_dimensions[0]; 
    $bheight = $img2_dimensions[1]; 

    $brpercent = '100'; 
    if($bwidth > 350){$brpercent = '90';} 
    if($bwidth > 500){$brpercent = '90';} 
    if($bwidth > 700){$brpercent = '50';} 
    if($bwidth > 900){$brpercent = '40';} 
    if($bwidth > 1100){$brpercent = '30';} 
    if($bwidth > 1400){$brpercent = '25';} 
    if($bwidth > 1600){$brpercent = '20';} 
    if($bwidth > 1800){$brpercent = '15';} 

    $bnewwidth = round((($brpercent/100)*$bwidth)); 
    $bnewheight = round((($brpercent/100)*$bheight)); 

    if($bnewwidth > 450){$bnewwidth = '400'; $bnewheight = '400';} 

    $image->load($img2name); 
    $image->resize($bnewwidth,$bnewheight); 
    $image->save($img2name); 

    } 

    //Re-size if Image 3 Exist 
    if($a2[img3] && $_GET[which]==3){ 

    $img3name = "../../../../../imgs/3/$a2[img3]"; 

    $img3_dimensions = getimagesize($img3name); 
    $cwidth = $img3_dimensions[0]; 
    $cheight = $img3_dimensions[1]; 

    $crpercent = '100'; 
    if($cwidth > 350){$crpercent = '90';} 
    if($cwidth > 500){$crpercent = '90';} 
    if($cwidth > 700){$crpercent = '50';} 
    if($cwidth > 900){$crpercent = '40';} 
    if($cwidth > 1100){$crpercent = '30';} 
    if($cwidth > 1400){$crpercent = '25';} 
    if($cwidth > 1600){$crpercent = '20';} 
    if($cwidth > 1800){$crpercent = '15';} 

    $cnewwidth = round((($crpercent/100)*$cwidth)); 
    $cnewheight = round((($crpercent/100)*$cheight)); 

    if($cnewwidth > 450){$cnewwidth = '400'; $cnewheight = '400';} 

    $image->load($img3name); 
    $image->resize($cnewwidth,$cnewheight); 
    $image->save($img3name); 

    } 

    //Re-size if Image 4 Exist 
    if($a2[img4] && $_GET[which]==4){ 

    $img4name = "../../../../../imgs/4/$a2[img4]"; 

    $img4_dimensions = getimagesize($img4name); 
    $dwidth = $img4_dimensions[0]; 
    $dheight = $img4_dimensions[1]; 

    $drpercent = '100'; 
    if($dwidth > 350){$drpercent = '90';} 
    if($dwidth > 500){$drpercent = '90';} 
    if($dwidth > 700){$drpercent = '50';} 
    if($dwidth > 900){$drpercent = '40';} 
    if($dwidth > 1100){$drpercent = '30';} 
    if($dwidth > 1400){$drpercent = '25';} 
    if($dwidth > 1600){$drpercent = '20';} 
    if($dwidth > 1800){$drpercent = '15';} 

    $dnewwidth = round((($drpercent/100)*$dwidth)); 
    $dnewheight = round((($drpercent/100)*$dheight)); 

    if($dnewwidth > 450){$dnewwidth = '400'; $dnewheight = '400';} 

    $image->load($img4name); 
    $image->resize($dnewwidth,$dnewheight); 
    $image->save($img4name); 

    } 






    header("Location: $siteurl/admin/manage/listings/edit/?did=$productid"); 

    ?> 

你能告訴我這裏有什麼問題嗎?我應該做些什麼?

+0

是您的原始PNG圖片的透明背景?將其保存爲JPEG後,不支持透明度,因此它默認爲黑色。 – Neograph734 2013-03-06 16:16:34

+0

so black我無法修復嘗試不同的圖片很不一樣 – 2013-03-06 16:19:10

+0

PNG文件中是否有透明度? – Neograph734 2013-03-06 16:21:38

回答

0

您正在將透明度高的PNG文件轉換爲JPEG格式,但不支持透明度。

即使您的保存功能有PNG文件的選項,您沒有使用它。

$image->save($img1name); 

運行以下功能。

function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { 
    if($image_type == IMAGETYPE_JPEG) { 
    imagejpeg($this->image,$filename,$compression); 
    } 
    elseif($image_type == IMAGETYPE_GIF) { 
    imagegif($this->image,$filename); 
    } 
    elseif($image_type == IMAGETYPE_PNG) { 
    imagepng($this->image,$filename); 
    } 
    if($permissions != null) { 
    chmod($filename,$permissions); 
    } 
} 

但是,你永遠不會給第二個參數。因此,它默認爲JPEG文件。如果您使用$image->save($img1name, IMAGETYPE_PNG);它很可能會工作。

如果你想在PNG的所有圖像,你可以只改變你的保存功能如下:

function save($filename, $image_type=IMAGETYPE_PNG, $compression=75, $permissions=null) 
+0

這是否解決了您的問題?然後,您可以點擊我答案旁邊的綠色勾號符號,以便將來有類似問題的人員知道該怎麼做。 – Neograph734 2013-03-06 16:42:20

+0

再次出現同樣的問題我沒有透明 – 2013-03-06 17:00:36

+0

但生成的文件現在是一個PNG文件? – Neograph734 2013-03-06 17:13:31