PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Sep 24, 2018 5:43 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: Wed Jul 07, 2004 1:10 pm 
Offline
Forum Commoner

Joined: Sat Jul 26, 2003 6:57 pm
Posts: 63
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


Last edited by like_duh44 on Wed Jul 07, 2004 4:05 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 1:14 pm 
Offline
Forum Contributor
User avatar

Joined: Tue May 11, 2004 7:32 pm
Posts: 401
Location: UK
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 1:21 pm 
Offline
Forum Commoner

Joined: Sat Jul 26, 2003 6:57 pm
Posts: 63
What would you mean by 'reading'?
Like...just open a binary connection? fread("test.bmp", "rb"); ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 1:24 pm 
Offline
Forum Contributor
User avatar

Joined: Tue May 11, 2004 7:32 pm
Posts: 401
Location: UK
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 1:39 pm 
Offline
Forum Commoner

Joined: Sat Jul 26, 2003 6:57 pm
Posts: 63
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?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 1:43 pm 
Offline
Forum Commoner

Joined: Sat Jul 26, 2003 6:57 pm
Posts: 63
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?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 1:44 pm 
Offline
Forum Contributor
User avatar

Joined: Tue May 11, 2004 7:32 pm
Posts: 401
Location: UK
Depends how the imagecreatefromstring function works, try it and see? It's not what I meant, but it would be worth a shot.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 3:41 pm 
Offline
Forum Commoner

Joined: Sat Jul 26, 2003 6:57 pm
Posts: 63
I tried it, but to no avail. What were you thinking of?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 4:05 pm 
Offline
Forum Commoner

Joined: Sat Jul 26, 2003 6:57 pm
Posts: 63
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 07, 2004 7:16 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2004 8:48 am 
Offline
Forum Commoner

Joined: Sat Jul 26, 2003 6:57 pm
Posts: 63
How would I go about plotting each pixel..?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2004 9:45 am 
Offline
Forum Regular

Joined: Thu Dec 18, 2003 4:58 pm
Posts: 836
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



?>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2004 2:01 pm 
Offline
Forum Commoner

Joined: Sat Jul 26, 2003 6:57 pm
Posts: 63
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...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2004 3:22 pm 
Offline
Forum Regular

Joined: Thu Dec 18, 2003 4:58 pm
Posts: 836
That error is a bug in the imagecreatefromgd function, unfortunately you would need to upgrade your PHP version to something more recent.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2004 4:55 pm 
Offline
Forum Commoner

Joined: Sat Jul 26, 2003 6:57 pm
Posts: 63
So basically there's nothing I can do to convert a bmp to jpeg?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group