par
sfourre » 08 sept. 2006, 21:50
Je redimentionne une image au format PNG, mais malheureusement la transparence de fond de l'image originale est remplacée par un fond noir !!!
J'utilise cette classe récupérée sur classes.scriptsphp.org et un peu bricolée :
class clImg
{
var $bg;
var $bgx;
var $bgy;
var $color;
function clImg ($szFic="")
{
$this->fctAjtBackground($szFic);
}
function fctCreImgAvecType($szFic, $handler)
{
$tbSize = getimagesize($szFic);
$wType=$tbSize[2];
switch ($wType)
{
case 2 :
$hdImg = imagecreatefromjpeg($szFic);
break;
case 3 :
$hdImg = imagecreatefrompng($szFic);
break;
case 1 :
$hdImg = imagecreatefromgif($szFic);
break;
}
$this->bgx=$tbSize[0];
$this->bgy=$tbSize[1];
return $hdImg;
}
function fctImgCalibre($inHoLarg, $inVeLarg)
{
// Détection du format de l'image : horizontale ou verticale
if ($this->bgx > $this->bgy) { $inDestLarg = $inHoLarg; }
else { $inDestLarg = $inVeLarg; }
// Contrôle s'il est nécessaire de redimentionner l'image
if ($inDestLarg > $this->bgx)
return FALSE;
// Calcul des dimentions de destinations et redimentionne
$inCoef = $this->bgx / $inDestLarg;
$inDestHaut = (int)($this->bgy / $inCoef);
$this->fctImgResize($inDestLarg, $inDestHaut);
}
function fctImgResize ($inDestLarg , $inDestHaut)
{
$hdImgDest = imagecreatetruecolor($inDestLarg, $inDestHaut);
$hdImgSrc = $this->bg;
imagecopyresampled($hdImgDest, $hdImgSrc, 0, 0, 0, 0, $inDestLarg, $inDestHaut, $this->bgx, $this->bgy);
$this->bg=$hdImgDest;
imagedestroy($hdImgSrc);
$this->bgx=$inDestLarg;
$this->bgy=$inDestHaut;
return TRUE;
}
function fctAjtBackground($szFic)
{
if (!empty($szFic) && file_exists($szFic))
$this->bg = $this->fctCreImgAvecType($szFic,'bg');
imagealphablending($this->bg , TRUE);
}
}
Classe utilisée comme ceci :
$obImg = new clImg("MonImage.png");
$obImg->fctImgCalibre(150, 150);
Ce noircissement ne serait il pas dû au format PNG qui gèe mal la transparence ?
Merci d'avance
Amicalement
Stéphane
Je redimentionne une image au format PNG, mais malheureusement la transparence de fond de l'image originale est remplacée par un fond noir !!!
J'utilise cette classe récupérée sur classes.scriptsphp.org et un peu bricolée :
[php]
class clImg
{
var $bg;
var $bgx;
var $bgy;
var $color;
function clImg ($szFic="")
{
$this->fctAjtBackground($szFic);
}
function fctCreImgAvecType($szFic, $handler)
{
$tbSize = getimagesize($szFic);
$wType=$tbSize[2];
switch ($wType)
{
case 2 :
$hdImg = imagecreatefromjpeg($szFic);
break;
case 3 :
$hdImg = imagecreatefrompng($szFic);
break;
case 1 :
$hdImg = imagecreatefromgif($szFic);
break;
}
$this->bgx=$tbSize[0];
$this->bgy=$tbSize[1];
return $hdImg;
}
function fctImgCalibre($inHoLarg, $inVeLarg)
{
// Détection du format de l'image : horizontale ou verticale
if ($this->bgx > $this->bgy) { $inDestLarg = $inHoLarg; }
else { $inDestLarg = $inVeLarg; }
// Contrôle s'il est nécessaire de redimentionner l'image
if ($inDestLarg > $this->bgx)
return FALSE;
// Calcul des dimentions de destinations et redimentionne
$inCoef = $this->bgx / $inDestLarg;
$inDestHaut = (int)($this->bgy / $inCoef);
$this->fctImgResize($inDestLarg, $inDestHaut);
}
function fctImgResize ($inDestLarg , $inDestHaut)
{
$hdImgDest = imagecreatetruecolor($inDestLarg, $inDestHaut);
$hdImgSrc = $this->bg;
imagecopyresampled($hdImgDest, $hdImgSrc, 0, 0, 0, 0, $inDestLarg, $inDestHaut, $this->bgx, $this->bgy);
$this->bg=$hdImgDest;
imagedestroy($hdImgSrc);
$this->bgx=$inDestLarg;
$this->bgy=$inDestHaut;
return TRUE;
}
function fctAjtBackground($szFic)
{
if (!empty($szFic) && file_exists($szFic))
$this->bg = $this->fctCreImgAvecType($szFic,'bg');
imagealphablending($this->bg , TRUE);
}
}
[/php]
Classe utilisée comme ceci :
[php]
$obImg = new clImg("MonImage.png");
$obImg->fctImgCalibre(150, 150);
[/php]
Ce noircissement ne serait il pas dû au format PNG qui gèe mal la transparence ?
Merci d'avance
Amicalement
Stéphane