Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > PHP: Графика > Тумбы с альфа-каналом


Автор: set36 13.6.2006, 16:36
Сабж.
Когда я в галерею помещаю PNG с альфа каналом, то вся прозрачность в тумбе закрашивается чёрным. хотя сохраняет как PNG.
Может кто-то даст пример как бороться с этим smile 

Код "стандартный", могу кинуть, но он большой. 

Автор: skyboy 13.6.2006, 17:15
http://forum.vingrad.ru/index.php?showtopic=74476&hl=%D0%BF%D1%80%D0%BE%D0%B7%D1%80%D0%B0%D1%87%D0%BD%D0%BE%D1%81%D1%82%D1%8C
http://forum.vingrad.ru/index.php?showtopic=89901&hl=%D0%BF%D1%80%D0%BE%D0%B7%D1%80%D0%B0%D1%87%D0%BD%D0%BE%D1%81%D1%82%D1%8C
Скрипт по созданию картинки(файл transparent_image.php):
Код

<?php
  $im= imagecreatetruecolor(640,480);
  imagesavealpha($im,TRUE);
  imagefill($im,1,1,imagecolorallocatealpha($im,0,255,0,127));
  imageline($im,1,1,50,50,imagecolorallocate($im,255,0,0));
  header("Content-type: image/png");
  imagepng($im);
  ImageDestroy($im);
?>

Страница:
Код

<html>
<head>
  <title></title>
</head>
<body bgcolor="#0000000">
<img src="transparent_image.php">
</body>
</html>

Как это выглядит - в аттаче. Видимо,главное - при генерации вызывать "imagesavealpha" smile 

Автор: skyboy 13.6.2006, 17:50
или ещё вот:
Цитата

imagealphablending -- Set the blending mode for an image
Description
bool imagealphablending ( resource image, bool blendmode ) 

Т.е. для картинки надо установить режим работы с прозрачностью. Одно "но"  - надо использовать или imagealphablending, или imagesavealpha, но не одновременно. 

Автор: set36 13.6.2006, 19:58
В принципе, там отдельная функция для PNG и там написано CreateFromPNG;
сейчас попробую прилепить туда SaveAlpha.

________________________________________

Код

//GD lib
///resize class
class thumbnail
{
 var $img;

function thumbnail($imgfile)
{
 $this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile);
 $this->img["format"]=strtoupper($this->img["format"]);
 if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
 //JPEG
  $this->img["format"]="JPEG";
  $this->img["src"] = ImageCreateFromJPEG ($imgfile);
 } 
 elseif ($this->img["format"]=="PNG") {
 //PNG
  $this->img["format"]="PNG";
  $this->img["src"] = ImageCreateFromPNG ($imgfile);
 } 
 elseif ($this->img["format"]=="GIF") {
//GIF
  $this->img["format"]="GIF";
  $this->img["src"] = ImageCreateFromGIF ($imgfile);
 } 
 elseif ($this->img["format"]=="WBMP") {
 //WBMP
  $this->img["format"]="WBMP";
  $this->img["src"] = ImageCreateFromWBMP ($imgfile);
 }
@$this->img["lebar"] = imagesx($this->img["src"]);
@$this->img["tinggi"] = imagesy($this->img["src"]);
$this->img["quality"]=100;
}

function size_auto($size=100)
{
//size
 if ($this->img["lebar"]>=$this->img["tinggi"]) 
 {
    $this->img["lebar_thumb"]=$size;
    @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
 } 
 else 
 {
    $this->img["tinggi_thumb"]=$size;
    @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
 }
}

function jpeg_quality($quality=100)
{
 $this->img["quality"]=$quality;
}

function show()
{
//show thumb
@Header("Content-Type: image/".$this->img["format"]);

/* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
$this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
    @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
//JPEG
imageJPEG($this->img["des"],"",$this->img["quality"]);
} elseif ($this->img["format"]=="PNG") {
//PNG
imagePNG($this->img["des"]);
} elseif ($this->img["format"]=="GIF") {
//GIF
imageGIF($this->img["des"]);
} elseif ($this->img["format"]=="WBMP") {
//WBMP
imageWBMP($this->img["des"]);
}
}

function save($save="")
{
//save thumb
if (empty($save)) $save=strtolower("./thumb.".$this->img["format"]);
/* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
$this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
    @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
//JPEG
imageJPEG($this->img["des"],"$save",$this->img["quality"]);
} elseif ($this->img["format"]=="PNG") {
//PNG
imagePNG($this->img["des"],"$save");
} elseif ($this->img["format"]=="GIF") {
//GIF
imageGIF($this->img["des"],"$save");
} elseif ($this->img["format"]=="WBMP") {
//WBMP
imageWBMP($this->img["des"],"$save");
}
}
}
///end class

Всёравно не понимаю, куда приткнуть... 

Автор: skyboy 13.6.2006, 23:06
а вот так:
Код

imagesavealpha($this->img["des"],true);

для вариантов формата gif и png - не катит? 

Автор: set36 14.6.2006, 00:17
Я уже везде повписывал, всёравно сохраняет с чёрным фоном... 

Автор: skyboy 14.6.2006, 07:59
ну... тут могу только развести руками и сказать: "У меня же всё работает...." Может, с PHP чего не то? приведённый мною пример пробовал? сохраняет чёрный фон? 

Автор: set36 14.6.2006, 14:34
Я думаю, альфа-канал теряется до его сохранения.
Значит эту штуковину надо засунуть по-выше.

Спасибо за подсказку! Попробую, подумаю. 

Автор: skyboy 14.6.2006, 15:23
вставляй savealpha или imagealphablending сразу после создания ресурса. тогда сомнений не будет. 

Автор: 7910 14.6.2007, 18:53
Такая же фигня. Любой пример, где используется imagecreatetruecolor нормально не работает, теряется прозрачность. Но заработал такой прием без  imagecreatetruecolor:

$im = imagecreatefromgif('u/'.$entry);
$src_trans = imagecolortransparent($im);
 $dst = imagecreate($new_width,$new_height);
 imagepalettecopy($dst,$im);
 imagefill($dst,0,0,$src_trans);
 imagecolortransparent($dst,$src_trans);
 imagecopyresampled($dst,$im,0,0,0,0,$new_width,$new_height,$width,$height);

imagepng($dst, 'k/'.$i.'.png');

Может и Вам поможет

Автор: skyboy 14.6.2007, 19:26
надо же! ровно год прошел!

Автор: 7910 14.6.2007, 19:46
Sorry, я не заметила. Думала, это сегодня 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)