Foros del Web » Programando para Internet » PHP »

como convertir un bmp a jpg

Estas en el tema de como convertir un bmp a jpg en el foro de PHP en Foros del Web. Hola a todos , estoy realizando una base de datos en el cual puedo subir imagenes y lo estoy desplegando en el navegador de forma ...
  #1 (permalink)  
Antiguo 01/07/2009, 11:36
Avatar de asassa  
Fecha de Ingreso: julio-2008
Ubicación: En el DF ectuoso
Mensajes: 240
Antigüedad: 15 años, 9 meses
Puntos: 0
como convertir un bmp a jpg

Hola a todos , estoy realizando una base de datos en el cual puedo subir imagenes y lo estoy desplegando en el navegador de forma correcta, pero el haber usuarios que utilizan archivos bmp, los suben en ese formato y pues claro que pesan mucho, y al mostrarlos en pantalla tardan mucho o no cargan. a diferencia de los jpg que si los muestra corectamente..

Alguien sabe como podria hacer para que al momento de subirlos a la base de datos se convierta a un archivo jpg y obiamente cambie su peso .?

gracias
  #2 (permalink)  
Antiguo 01/07/2009, 12:12
 
Fecha de Ingreso: mayo-2009
Mensajes: 62
Antigüedad: 14 años, 11 meses
Puntos: 2
Respuesta: como convertir un bmp a jpg

Te pongo un código que me encontré en la web hace tiempo, haciendo una galería de fotos... Sorry por no pasarte la liga, pero todavía no ajusto la cuota de 30 mensajes... Vas a necesitar que tu servidor soporte GD library si mal no recuerdo...

Código PHP:
Ver original
  1. <?php
  2.  
  3. /**
  4.  *
  5.  * @convert BMP to GD
  6.  *
  7.  * @param string $src
  8.  *
  9.  * @param string|bool $dest
  10.  *
  11.  * @return bool
  12.  *
  13.  */
  14. function bmp2gd($src, $dest = false)
  15. {
  16.     /*** try to open the file for reading ***/
  17.     if(!($src_f = fopen($src, "rb")))
  18.     {
  19.         return false;
  20.     }
  21.  
  22. /*** try to open the destination file for writing ***/
  23. if(!($dest_f = fopen($dest, "wb")))
  24.     {
  25.         return false;
  26.     }
  27.  
  28. /*** grab the header ***/
  29. $header = unpack("vtype/Vsize/v2reserved/Voffset", fread( $src_f, 14));
  30.  
  31. /*** grab the rest of the image ***/
  32. $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant",
  33. fread($src_f, 40));
  34.  
  35. /*** extract the header and info into varibles ***/
  36. extract($info);
  37. extract($header);
  38.  
  39. /*** check for BMP signature ***/
  40. if($type != 0x4D42)
  41. {
  42.     return false;
  43. }
  44.  
  45. /*** set the pallete ***/
  46. $palette_size = $offset - 54;
  47. $ncolor = $palette_size / 4;
  48. $gd_header = "";
  49.  
  50. /*** true-color vs. palette ***/
  51. $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF";
  52. $gd_header .= pack("n2", $width, $height);
  53. $gd_header .= ($palette_size == 0) ? "\x01" : "\x00";
  54. if($palette_size) {
  55. $gd_header .= pack("n", $ncolor);
  56. }
  57. /*** we do not allow transparency ***/
  58. $gd_header .= "\xFF\xFF\xFF\xFF";
  59.  
  60. /*** write the destination headers ***/
  61. fwrite($dest_f, $gd_header);
  62.  
  63. /*** if we have a valid palette ***/
  64. if($palette_size)
  65. {
  66.     /*** read the palette ***/
  67.     $palette = fread($src_f, $palette_size);
  68.     /*** begin the gd palette ***/
  69.     $gd_palette = "";
  70.     $j = 0;
  71.     /*** loop of the palette ***/
  72.     while($j < $palette_size)
  73.     {
  74.         $b = $palette{$j++};
  75.         $g = $palette{$j++};
  76.         $r = $palette{$j++};
  77.         $a = $palette{$j++};
  78.         /*** assemble the gd palette ***/
  79.         $gd_palette .= "$r$g$b$a";
  80.     }
  81.     /*** finish the palette ***/
  82.     $gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor);
  83.     /*** write the gd palette ***/
  84.     fwrite($dest_f, $gd_palette);
  85. }
  86.  
  87. /*** scan line size and alignment ***/
  88. $scan_line_size = (($bits * $width) + 7) >> 3;
  89. $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0;
  90.  
  91. /*** this is where the work is done ***/
  92. for($i = 0, $l = $height - 1; $i < $height; $i++, $l--)
  93. {
  94.     /*** create scan lines starting from bottom ***/
  95.     fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l));
  96.     $scan_line = fread($src_f, $scan_line_size);
  97.     if($bits == 24)
  98.     {
  99.         $gd_scan_line = "";
  100.         $j = 0;
  101.         while($j < $scan_line_size)
  102.         {
  103.             $b = $scan_line{$j++};
  104.             $g = $scan_line{$j++};
  105.             $r = $scan_line{$j++};
  106.             $gd_scan_line .= "\x00$r$g$b";
  107.         }
  108.     }
  109.     elseif($bits == 8)
  110.     {
  111.         $gd_scan_line = $scan_line;
  112.     }
  113.     elseif($bits == 4)
  114.     {
  115.         $gd_scan_line = "";
  116.         $j = 0;
  117.         while($j < $scan_line_size)
  118.         {
  119.             $byte = ord($scan_line{$j++});
  120.             $p1 = chr($byte >> 4);
  121.             $p2 = chr($byte & 0x0F);
  122.             $gd_scan_line .= "$p1$p2";
  123.         }
  124.         $gd_scan_line = substr($gd_scan_line, 0, $width);
  125.     }
  126.     elseif($bits == 1)
  127.     {
  128.         $gd_scan_line = "";
  129.         $j = 0;
  130.         while($j < $scan_line_size)
  131.         {
  132.             $byte = ord($scan_line{$j++});
  133.             $p1 = chr((int) (($byte & 0x80) != 0));
  134.             $p2 = chr((int) (($byte & 0x40) != 0));
  135.             $p3 = chr((int) (($byte & 0x20) != 0));
  136.             $p4 = chr((int) (($byte & 0x10) != 0));
  137.             $p5 = chr((int) (($byte & 0x08) != 0));
  138.             $p6 = chr((int) (($byte & 0x04) != 0));
  139.             $p7 = chr((int) (($byte & 0x02) != 0));
  140.             $p8 = chr((int) (($byte & 0x01) != 0));
  141.             $gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8";
  142.         }
  143.     /*** put the gd scan lines together ***/
  144.     $gd_scan_line = substr($gd_scan_line, 0, $width);
  145.     }
  146.     /*** write the gd scan lines ***/
  147.     fwrite($dest_f, $gd_scan_line);
  148. }
  149. /*** close the source file ***/
  150. fclose($src_f);
  151. /*** close the destination file ***/
  152. fclose($dest_f);
  153.  
  154. return true;
  155. }
  156.  
  157. /**
  158.  *
  159.  * @ceate a BMP image
  160.  *
  161.  * @param string $filename
  162.  *
  163.  * @return bin string on success
  164.  *
  165.  * @return bool false on failure
  166.  *
  167.  */
  168. function ImageCreateFromBmp($filename)
  169. {
  170.     /*** create a temp file ***/
  171.     $tmp_name = tempnam("/tmp", "GD");
  172.     /*** convert to gd ***/
  173.     if(bmp2gd($filename, $tmp_name))
  174.     {
  175.         /*** create new image ***/
  176.         $img = imagecreatefromgd($tmp_name);
  177.         /*** remove temp file ***/
  178.         unlink($tmp_name);
  179.         /*** return the image ***/
  180.         return $img;
  181.     }
  182.     return false;
  183. }
  184.  
  185. /*** example usage ***/
  186.  
  187. /*** read in the BMP image ***/
  188. $img = ImageCreateFromBmp("test.bmp");
  189. /*** write the new jpeg image ***/
  190. imagejpeg($img, "test/test.jpg");
  191.  
  192. ?>
  193.  
  194. <!-- display the image -->
  195. <img src="test/test.jpg" alt="new jpg image" />

Saludos!
  #3 (permalink)  
Antiguo 02/07/2009, 08:23
Avatar de asassa  
Fecha de Ingreso: julio-2008
Ubicación: En el DF ectuoso
Mensajes: 240
Antigüedad: 15 años, 9 meses
Puntos: 0
Respuesta: como convertir un bmp a jpg

muchas gracias lo revisare y te dire como me fue..gracias nuevamente
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Tema Cerrado




La zona horaria es GMT -6. Ahora son las 01:29.