PHP Developers Network
http://forums.devnetwork.net/

[NOT SOLVED]Creating a thumbnail of a BMP?
http://forums.devnetwork.net/viewtopic.php?f=32&t=23557
Page 1 of 1

Author:  like_duh44 [ Wed Jul 07, 2004 1:10 pm ]
Post subject:  [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

Author:  launchcode [ Wed Jul 07, 2004 1:14 pm ]
Post subject: 

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.

Author:  like_duh44 [ Wed Jul 07, 2004 1:21 pm ]
Post subject: 


Author:  launchcode [ Wed Jul 07, 2004 1:24 pm ]
Post subject: 

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

Author:  like_duh44 [ Wed Jul 07, 2004 1:39 pm ]
Post subject: 

so...something like this?

Syntax: [ Download ] [ Hide ]
<?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?

Author:  like_duh44 [ Wed Jul 07, 2004 1:43 pm ]
Post subject: 

Also, if I wanted to conver the bmp to a jpeg, could I just do this:
Syntax: [ Download ] [ Hide ]
<?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?

Author:  launchcode [ Wed Jul 07, 2004 1:44 pm ]
Post subject: 

Depends how the imagecreatefromstring function works, try it and see? It's not what I meant, but it would be worth a shot.

Author:  like_duh44 [ Wed Jul 07, 2004 3:41 pm ]
Post subject: 


Author:  like_duh44 [ Wed Jul 07, 2004 4:05 pm ]
Post subject: 

I can read the string no problem, and output it no problem:

Syntax: [ Download ] [ Hide ]
<?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

Author:  feyd [ Wed Jul 07, 2004 7:16 pm ]
Post subject: 


Author:  like_duh44 [ Thu Jul 08, 2004 8:48 am ]
Post subject: 


Author:  redmonkey [ Thu Jul 08, 2004 9:45 am ]
Post subject: 

Syntax: [ Download ] [ Hide ]
<?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



?>

Author:  like_duh44 [ Thu Jul 08, 2004 2:01 pm ]
Post subject: 

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...

Author:  redmonkey [ Thu Jul 08, 2004 3:22 pm ]
Post subject: 


Author:  like_duh44 [ Thu Jul 08, 2004 4:55 pm ]
Post subject: 

So basically there's nothing I can do to convert a bmp to jpeg?

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/