[NOT SOLVED]Creating a thumbnail of a BMP?
Moderator: General Moderators
-
like_duh44
- Forum Commoner
- Posts: 63
- Joined: Sat Jul 26, 2003 6:57 pm
[NOT SOLVED]Creating a thumbnail of a BMP?
I am designing part of a website where the user can upload an image (99% will be bmp's), and I want it to automatically create a thumbnail etc. Unfortunately, I have GD 1.6.3 so bmp support is gone. Is there another way to possibly convert the bmp to jpeg and work from there?
Note: I do not want to have the user change it themselfs, just upload
Note: I do not want to have the user change it themselfs, just upload
Last edited by like_duh44 on Wed Jul 07, 2004 4:05 pm, edited 1 time in total.
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
-
like_duh44
- Forum Commoner
- Posts: 63
- Joined: Sat Jul 26, 2003 6:57 pm
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
Yes.. read it in and then go through the data byte by byte. A BMP is a straight raw byte dump of an image (most the time, unless it is an 16 or 256 colour run-length encoded one (very rare!)).
This should help: http://www.fastgraph.com/help/bmp_header_format.html
This should help: http://www.fastgraph.com/help/bmp_header_format.html
-
like_duh44
- Forum Commoner
- Posts: 63
- Joined: Sat Jul 26, 2003 6:57 pm
so...something like this?
Is that right?
Code: Select all
<?php
$filename = "test.bmp";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
$im = imagecreatefromstring($contents);
$tim = imagecreate(100, 100);
imagecopyresampled ($tim,$im,0,0,0,0,100,100,800,600);
imagejpeg($tim, '', 90);
imagedestroy($tim);
imagedestroy($im);
?>-
like_duh44
- Forum Commoner
- Posts: 63
- Joined: Sat Jul 26, 2003 6:57 pm
Also, if I wanted to conver the bmp to a jpeg, could I just do this:
Would that work?
Code: Select all
<?php
$filename = "test.bmp";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
$im = imagecreatefromstring($contents);
imagejpeg($im, 'test.jpg', 90);
?>- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
-
like_duh44
- Forum Commoner
- Posts: 63
- Joined: Sat Jul 26, 2003 6:57 pm
-
like_duh44
- Forum Commoner
- Posts: 63
- Joined: Sat Jul 26, 2003 6:57 pm
I can read the string no problem, and output it no problem:
But I cant get the raw data into another file in JPEG format
Code: Select all
<?php
Header("Content-type: image/jpeg");
$filename = $_GET['name'];
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Once or while you load the BMP into memory, you can plot the pixels into an image resource and then have GD produce the image in jpeg format.. this isn't hard.. Here's some more details on the bitmap file format: http://msdn.microsoft.com/library/defau ... s_7ltf.asp
-
like_duh44
- Forum Commoner
- Posts: 63
- Joined: Sat Jul 26, 2003 6:57 pm
Code: Select all
<?php
function bmp2gd($src, $dest = false)
{
if(!($src_img = fopen($src, "rb")))
{
return false;
}
if(!($dest_img = fopen($dest, "wb")))
{
return false;
}
$header = unpack("vtype/Vsize/v2reserved/Voffset", fread($src_img, 14));
$info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread($src_img, 40));
extract($header);
extract($info);
if($type != 0x4D42)
{
return false;
}
$p_size = $offset - 54;
$ncolor = $p_size / 4;
$gdh = '';
$gdh .= ($p_size == 0) ? "\xff\xfe" : "\xff\xff";
$gdh .= pack("n2", $width, $height);
$gdh .= ($p_size == 0) ? "\x01" : "\x00";
if($p_size)
{
$gdh .= pack("n", $ncolor);
}
$gdh .= "\xff\xff\xff\xff";
fwrite($dest_img, $gdh);
if($p_size)
{
$palette = fread($src_img, $p_size);
$gdp = '';
$j = 0;
while($j < $p_size)
{
$b = $palette{$j++};
$g = $palette{$j++};
$r = $palette{$j++};
$a = $palette{$j++};
$gdp .= $r . $g . $b . $a;
}
$gdp .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);
fwrite($dest_img, $gdp);
}
$scan_line_size = (($bits * $width) + 7) >> 3;
$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0;
for($i = 0, $l = $height - 1; $i < $height; $i++, $l--)
{
fseek($src_img, $offset + (($scan_line_size + $scan_line_align) * $l));
$scan_line = fread($src_img, $scan_line_size);
if($bits == 24)
{
$gd_scan_line = '';
$j = 0;
while($j < $scan_line_size)
{
$b = $scan_line{$j++};
$g = $scan_line{$j++};
$r = $scan_line{$j++};
$gd_scan_line .= "\x00" . $r . $g . $b;
}
}
else if($bits ==
{
$gd_scan_line = $scan_line;
}
else if($bits == 4)
{
$gd_scan_line = '';
$j = 0;
while($j < $scan_line_size)
{
$byte = ord($scan_line{$j++});
$p1 = chr($byte >> 4);
$p2 = chr($byte & 0x0F);
$gd_scan_line .= $p1 . $p2;
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
else if($bits == 1)
{
$gd_scan_line = '';
$j = 0;
while($j < $scan_line_size)
{
$byte = ord($scan_line{$j++});
$p1 = chr((int) (($byte & 0x80) != 0));
$p2 = chr((int) (($byte & 0x40) != 0));
$p3 = chr((int) (($byte & 0x20) != 0));
$p4 = chr((int) (($byte & 0x10) != 0));
$p5 = chr((int) (($byte & 0x08) != 0));
$p6 = chr((int) (($byte & 0x04) != 0));
$p7 = chr((int) (($byte & 0x02) != 0));
$p8 = chr((int) (($byte & 0x01) != 0));
$gd_scan_line .= $p1 . $p2 . $p3 . $p4 .
$p5 . $p6 . $p7 . $p8;
}
$gd_scan_line = substr($gd_scan_line, 0, $width);
}
fwrite($dest_img, $gd_scan_line);
}
fclose($src_img);
fclose($dest_img);
return true;
}
function imagecreatefrombmp($filename)
{
$tmp_name = tempnam('/tmp', 'GD');
if(bmp2gd($filename, $tmp_name))
{
$img = imagecreatefromgd($tmp_name);
unlink($tmp_name);
return $img;
}
return false;
}
// usage......
$img = imagecreatefrombmp('setup.bmp'); // location of source windows bmp file
imagejpeg($img, 'setup.jpg'); // creates the setup.jpg file
?>-
like_duh44
- Forum Commoner
- Posts: 63
- Joined: Sat Jul 26, 2003 6:57 pm
-
like_duh44
- Forum Commoner
- Posts: 63
- Joined: Sat Jul 26, 2003 6:57 pm