简体   繁体   English

imagecopyresampled png黑色背景

[英]imagecopyresampled png black background

I have a problem when an image is loading from the server. 从服务器加载图像时出现问题。 The background of images in PNG format becomes black. PNG格式的图像背景变为黑色。

Sorry for bad English thanks in advance. 对不起,英语不好,谢谢。

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

    ?>

Can you please tell me what is wrong here? 你能告诉我这里有什么问题吗? What am i supposed to do? 我应该做些什么?

You are transforming a PNG file with transparancy into a JPEG, that has no support for transparancy. 您正在将具有透明性的PNG文件转换为不支持透明性的JPEG。

Even though your save function had an option for PNG files, you are not using it. 即使您的保存功能可以选择PNG文件,也不会使用它。

$image->save($img1name);

Runs the following function. 运行以下功能。

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

But you never give the second parameter. 但是,您永远不会给出第二个参数。 Therefor it defaults to a JPEG file. 因此,它默认为JPEG文件。 If you would use $image->save($img1name, IMAGETYPE_PNG); 如果使用$image->save($img1name, IMAGETYPE_PNG); it is likely to work. 它可能会起作用。

If you want all images in PNG, you could just change your save function as follows: 如果要使用PNG中的所有图像,则可以如下更改保存功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM