Page 1 of 1

[NOT SOLVED]Creating a thumbnail of a BMP?

Posted: Wed Jul 07, 2004 1:10 pm
by like_duh44
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

Posted: Wed Jul 07, 2004 1:14 pm
by launchcode
GD won't support it directly - but the BMP format is an *extremely* simple one, so there is nothing to stop you reading the format yourself and then creating a JPG/PNG version of it - once done you could them convert your new JPG/PNG into a thumbnail size.

Posted: Wed Jul 07, 2004 1:21 pm
by like_duh44
What would you mean by 'reading'?
Like...just open a binary connection? fread("test.bmp", "rb"); ?

Posted: Wed Jul 07, 2004 1:24 pm
by launchcode
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

Posted: Wed Jul 07, 2004 1:39 pm
by like_duh44
so...something like this?

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);

?>
Is that right?

Posted: Wed Jul 07, 2004 1:43 pm
by like_duh44
Also, if I wanted to conver the bmp to a jpeg, could I just do this:

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);

?>
Would that work?

Posted: Wed Jul 07, 2004 1:44 pm
by launchcode
Depends how the imagecreatefromstring function works, try it and see? It's not what I meant, but it would be worth a shot.

Posted: Wed Jul 07, 2004 3:41 pm
by like_duh44
I tried it, but to no avail. What were you thinking of?

Posted: Wed Jul 07, 2004 4:05 pm
by like_duh44
I can read the string no problem, and output it no problem:

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;

?>
But I cant get the raw data into another file in JPEG format

Posted: Wed Jul 07, 2004 7:16 pm
by feyd
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

Posted: Thu Jul 08, 2004 8:48 am
by like_duh44
How would I go about plotting each pixel..?

Posted: Thu Jul 08, 2004 9:45 am
by redmonkey

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

?>

Posted: Thu Jul 08, 2004 2:01 pm
by like_duh44
THank you very much for the help, but it's saying that, online 127, where you have imagecreatefromgd, it's saying the file is not a valid GD file...

Posted: Thu Jul 08, 2004 3:22 pm
by redmonkey
That error is a bug in the imagecreatefromgd function, unfortunately you would need to upgrade your PHP version to something more recent.

Posted: Thu Jul 08, 2004 4:55 pm
by like_duh44
So basically there's nothing I can do to convert a bmp to jpeg?