/*---------------------------------------------------------------*/
|
/*
|
Titre : Fonctions imagecreatefrombmp et imagebmp
|
|
URL : https://phpsources.net/code_s.php?id=120
|
Auteur : kurt67
|
Date édition : 14 Avril 2005
|
*/
|
/*---------------------------------------------------------------*/
|
function imagecreatefrombmp($dir) {
|
$bmp = "";
|
if (file_exists($dir)) {
|
$file = fopen($dir,"r");
|
while(!feof($file)) $bmp .= fgets($file,filesize($dir));
|
if (substr($bmp,0,2) == "BM") {
|
// Lecture du header
|
$chaine = "vtype/Vlength/v2reserved/Vbegin/Vsize/Vwidth/Vheight/vplanes";
|
$chaine .= "/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant";
|
$header = unpack("$chaine", $bmp);
|
|
extract($header);
|
// Lecture de l'image
|
$im = imagecreatetruecolor($width,$height);
|
$i = 0;
|
$e = 0;
|
$diff = floor(($imagesize - ($width*$height*($bits/8)))/$height);
|
for($y=$height-1;$y>=0;$y--) {
|
for($x=0;$x<$width;$x++) {
|
if ($bits == 32) {
|
$b = ord(substr($bmp,$begin+$i,1));
|
$v = ord(substr($bmp,$begin+$i+1,1));
|
$r = ord(substr($bmp,$begin+$i+2,1));
|
$i += 4;
|
} else if ($bits == 24) {
|
$b = ord(substr($bmp,$begin+$i,1));
|
$v = ord(substr($bmp,$begin+$i+1,1));
|
$r = ord(substr($bmp,$begin+$i+2,1));
|
$i += 3;
|
} else if ($bits == 16) {
|
$tot1 = decbin(ord(substr($bmp,$begin+$i,1)));
|
while(strlen($tot1)<8) $tot1 = "0".$tot1;
|
$tot2 = decbin(ord(substr($bmp,$begin+$i+1,1)));
|
while(strlen($tot2)<8) $tot2 = "0".$tot2;
|
$tot = $tot2.$tot1;
|
$r = bindec(substr($tot,1,5))*8;
|
$v = bindec(substr($tot,6,5))*8;
|
$b = bindec(substr($tot,11,5))*8;
|
$i += 2;
|
} else if ($bits == 1) {
|
$tot = decbin(ord(substr($bmp,$begin+$i,1)));
|
while(strlen($tot)<8) $tot = "0".$tot;
|
$ind = substr($tot,$e,1);
|
if ($ind) { $r=255;$v=255;$b=255;
|
} else { $r=0;$v=0;$b=0; }
|
$e++;
|
if ($e == 8) {
|
$e = 0;
|
$i++;
|
}
|
}
|
$col = imagecolorexact($im,$r,$v,$b);
|
if ($col == -1) $col = imagecolorallocate($im,$r,$v,$b);
|
imagesetpixel($im,$x,$y,$col);
|
}
|
// $i += $diff;
|
$i += 1;
|
$e = 0;
|
echo $e;
|
}
|
// retourne l'image
|
return $im;
|
imagedestroy($im);
|
} else return false;
|
} else return false;
|
}
|
|
function imagebmp($im,$dir="") {
|
$pix = "";
|
for($y=imagesy($im)-1;$y>=0;$y--) {
|
for($x=0;$x<imagesx($im);$x++) {
|
$rgb = ImageColorAt($im, $x, $y);
|
$r = ($rgb >> 16) & 0xFF;
|
$g = ($rgb >> 8) & 0xFF;
|
$b = $rgb & 0xFF;
|
$pix .= pack("C3",$b,$g,$r);
|
}
|
}
|
$ix = strlen($pix);
|
$ix1 = imagesx($im);
|
|
$header=pack("v2",$ix)+54,0,0,54,40,$ix1,imagesy($im),1,24,0,$ix,0,0,0,0);
|
if ($dir != "") {
|
$inF = fopen($dir,"w");
|
fwrite($inF,"BM".$header.$pix);
|
fclose($inF);
|
} else echo "BM".$header.$pix;
|
}
|
|
function infosbmp($dir) {
|
$bmp = "";
|
$chaine="vtype/Vlength/v2reserved/Vbegin/Vsize/Vwidth/Vheight/vplanes/vbits";
|
$chaine .= "/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant";
|
if (file_exists($dir)) {
|
$file = fopen($dir,"r");
|
while(!feof($file)) $bmp .= fgets($file,filesize($dir));
|
if (substr($bmp,0,2) == "BM") return unpack("$chaine", $bmp);
|
else return "Fichier bitmap non valide";
|
} else return "Fichier non trouvé";
|
}
|
|
|
if (!empty($_POST["dir"])) $dir = $_POST["dir"];
|
else $dir = "test.bmp";
|
|
echo '<form method="post"><input type="text" name="dir" value="'.$dir.'">
|
<input type="submit" value="load"></form>';
|
|
$infos = infosbmp($dir);
|
if (is_array($infos)) {
|
extract($infos);
|
echo "Taille totale en octects : ".$length."<br>";
|
echo "Taille du header en octects : ".$begin."<br>";
|
echo "Taille de l'image en octects : ".$imagesize."<br>";
|
echo "Dimensions de l'image : ".$width."*".$height."<br>";
|
echo "Bits par pixel : ".$bits."<br>";
|
echo "compression : ".$compression."<br>";
|
echo "Résolution horizontale en pixels-per-meter : ".$xres."<br>";
|
echo "Résolution verticale en pixels-per-meter : ".$yres."<br>";
|
echo "Nombre de couleurs utilisées : ".$ncolor."<br>";
|
echo "Nombre de couleurs importantes (0 = toutes) : ".$important."<br>";
|
$im = imagecreatefrombmp($dir);
|
imagebmp($im,"test.bmp");
|
echo"<img src=\"test.bmp\">Nombre de couleurs: ".imagecolorstotal($im)."<br>";
|
imagedestroy($im);
|
echo "Image affichée au format bmp.<br>";
|
} else echo $infos;
|
| ?> |
Invité
09 Juil 2009 à 22:55Il faut $header=pack("v2",$ix+54,0,0,54,40,$ix1,imagesy($im),1,24,0,$ix,0,0,0,0);
et non $header=pack("v2",$ix+54),0,0,54,40,$ix1,imagesy($im),1,24,0,$ix,0,0,0,0);
Sinon j'ai un bug car l'utilisation de imagecreatefrombmp
me donne :
Warning: imagesx(): supplied argument is not a valid Image resource in /home/img/inc/fonctions_img.php on line 280
Invité
05 Mai 2009 à 19:57humm, ça me fait une erreur ligne 103.
Un problème de , semble t'il.
Quelqu'un à une idée ?
Invité
22 Mars 2009 à 23:56Salut mec !
Merci pour ta fonction, mais je crois qu'il y a une toute petite erreur :
à la ligne 78 à 81, il faudrait plutôt mettre :
$i += $diff;
$e = 0;
Je l'ai testé en 32 et 24 bits et ça marche nikel!
Voilà =)