0

从服务器加载图像时出现问题。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");

    ?>

你能告诉我这里有什么问题吗?我应该做些什么?

4

1 回答 1

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)
于 2013-03-06T16:27:41.397 回答