1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
function bmp2jpg($nombmp,$nomjpg)
{
$p=imagecreatefrombmp($nombmp);
if ($p)
{
imagejpeg($p, $nomjpg, 75);
imagedestroy($p);
return 1;
}
else return 0;
}
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
$header = unpack("vtype/Vlength/v2reserved/Vbegin/Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", $bmp);
extract($header);
// Lecture de l'image
$im = imagecreatetruecolor($width,$height);
$i = 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;
}
$col = imagecolorexact($im,$r,$v,$b);
if ($col == -1) $col = imagecolorallocate($im,$r,$v,$b);
imagesetpixel($im,$x,$y,$col);
}
$i += $diff;
}
// retourne l'image
return $im;
imagedestroy($im);
} else return false;
} else return false;
} |
Partager