从服务器加载图片时出现问题。 PNG格式的图像背景变黑。
抱歉英语不好 提前谢谢。
<?php
//Database Settings
include('../../../../includes/bdconfig.ini.php');
$t = time();
//Database Required Settings
include('../../../../includes/adminrequired.php');
//If Not Logged In Redirect to Login Page
if(!$session){header("Location: $siteurl/admin/login/");}
// Image Re-size and Thumbnail Features Go Here
$h1 = "SELECT * FROM dlistings ORDER BY listed DESC LIMIT 0, 1"; $k1 = mysql_query($h1); $c1 = mysql_fetch_array($k1);
$productid = "$c1[did]";
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($c1[img1]){
$img1name = "../../../../imgs/1/$c1[img1]";
$thumb1name = "../../../../thumbs/imgs/$c1[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(100,100);
$image->save($thumb1name);
}
//Re-size if Image 2 Exist
if($c1[img2]){
$img2name = "../../../../imgs/2/$c1[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($c1[img3]){
$img3name = "../../../../imgs/3/$c1[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($c1[img4]){
$img4name = "../../../../imgs/4/$c1[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/?added=listing&productid=$productid");?>
你能告诉我这里有什么问题吗?我该怎么办?
答案 0 :(得分:1)
这是可怕的代码!仍然是代码
$this->image = imagecreatefrompng($filename);
是正确的,应该正确加载png。检查图像是否应该在哪里。 png是否包含透明度?如果是这样,您需要执行一些技巧来保持透明度。
请参阅:Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?