要獲得正確的畫面無黑的背景,你必須遵循的策略:
首先創建新的圖像項目 設置它的alphablending假 設置它的savealpha真 FIL它與透明矩形 複製你變灰的圖片在它
您的代碼將與此類似:
switch ($handle->file_src_name_ext) {
case 'gif':
$sourceIm = imagecreatefromgif($savepath.$handle->file_dst_name);
break;
case 'jpg':
$sourceIm = imagecreatefromjpeg($savepath.$handle->file_dst_name);
break;
case 'png':
default:
$sourceIm = imagecreatefrompng($savepath.$handle->file_dst_name);
break;
}
$iw = imagesx($sourceIm);
$ih = imagesy($sourceIm);
$im = imagecreatetruecolor($iw, $ih);
if (function_exists('imagecolorallocatealpha')) {
imagealphablending($im, false);
imagesavealpha($im, true);
$transparent = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefilledrectangle($im, 0, 0, $iw, $ih, $transparent);
}
if ($sourceIm) {
imagefilter($sourceIm, IMG_FILTER_GRAYSCALE);
}
imagecopyresampled($im, $sourceIm, 0, 0, 0, 0, $iw, $ih, $iw, $ih);
switch ($handle->file_src_name_ext) {
case 'gif':
imagepng($im, $savepath.'grd'.$row->id.'.gif');
break;
case 'jpg':
imagejpeg($im, $savepath.'grd'.$row->id.'.jpg');
break;
case 'png':
default:
imagepng($im, $savepath.'grd'.$row->id.'.png');
break;
}
goodluck!
看看我balda pomoshnik