Ver Mensaje Individual
  #7 (permalink)  
Antiguo 14/01/2007, 11:04
Avatar de blakeyed
blakeyed
 
Fecha de Ingreso: marzo-2004
Ubicación: Ahora mismo, Málaga
Mensajes: 78
Antigüedad: 22 años, 6 meses
Puntos: 0
Re: Reducir foto proporcionalmente sin GD

Bueno, alla va, no quieres GD, porque en GD hay una funcion getimagesize() que te dice al menos el tamaño de la imagen. Como las imagenes no son muy grandes puedes utilizar el <IMG SRC="imagen" WIDTH=xx HEIGHT=xx> pero deberás calcular antes, ancho y alto de la imagen. Aquí te paso una función datos_img() le pasas el archivo y te da en un array [0] = ancho; [1] = alto;

A ver si te funciona... aunque getimagesize() es más completa...

Código PHP:
function network_safe_fread( $file_handle, $length )
{
  
// Create blank string to receive data
  
$data = "";

  
// Keep reading data from the file until either EOF occurs or we have
  // retrieved the requested number of bytes

  
while ( ( !feof( $file_handle ) ) && ( strlen($data) < $length ) )
    {
      
$data .= fread( $file_handle, $length-strlen($data) );
    }

  
// return the data read
  
return $data;
}

function 
abreimagen($filename) 
{
  
ignore_user_abort(true);
  
$filehnd = @fopen($filename, 'rb');
  return 
$filehnd;
}

function 
tipo_imagen($mnjador) 
{
  
$data = network_safe_fread( $mnjador, 2 );  // Lee los primeros dos bytes
  
if ( $data == "\xFF\xD8" ) return "jpeg"; else     // FF D8 = Inicio de Imagen JPEG
    
if ( $data == "GI" ) {                                 // Un GIF casi seguro ;) pero leemos otro caracter más 
      
$data = network_safe_fread( $mnjador, 1 );  // Lee los primeros dos bytes
      
if ($data=="F") return "gif"; else return false; // Es un gif ¿? lo damos por bueno
    
} else
      if ( 
$data == "\x89P" ) {                             // Los dos primeros bits del PNG... 
    
$data = network_safe_fread( $mnjador, 6 );  // La cabecera entera del PNG
    
if ($data=="NG\x0D\x0A\x1A\x0A") return "png"; else return false; // Es un gif ¿? lo damos por bueno
      
} else
    return 
false;  
}

function 
LittleEndian2Int($byteword) 
{
  
$intvalue = 0;
  
$byteword = strrev($byteword);
  
$bytewordlen = strlen($byteword);
  for (
$i = 0; $i < $bytewordlen; $i++) {
    
$intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
  }
  return 
$intvalue;
}
    
function 
BigEndian2Int($byteword) 
{
  return 
LittleEndian2Int(strrev($byteword));
}
    
function 
saca_cabecera_gif ($mnjador) 
{
  
$data = network_safe_fread( $mnjador, 3 );  // Leemos la version
  
$anch = network_safe_fread( $mnjador, 2 );  // Leemos la version
  
$alto = network_safe_fread( $mnjador, 2 );  // Leemos la version
  
$salida[0] = LittleEndian2Int($anch);
  
$salida[1] = LittleEndian2Int($alto);
  return 
$salida;
}

function 
saca_cabecera_png ($mnjador) 
{
  
$data = network_safe_fread( $mnjador, 4 );  // Chunksize
  
$data = network_safe_fread( $mnjador, 4 );  // HIDR
  
$anch = network_safe_fread( $mnjador, 4 ); 
  
$alto = network_safe_fread( $mnjador, 4 ); 
  
$salida[0] = BigEndian2Int($anch);
  
$salida[1] = BigEndian2Int($alto);
  return 
$salida;
}

function 
get_jpeg_header_data( $filehnd )
{
  
// Read the third character
  
$data = network_safe_fread( $filehnd, 2 );

  
// Check that the third character is 0xFF (Start of first segment header)
  
if ( $data{0} != "\xFF" )
    {
      
// NO FF found - close file and return - JPEG is probably corrupted
      
fclose($filehnd);
      return 
FALSE;
    }

  
// Flag that we havent yet hit the compressed image data
  
$hit_compressed_image_data = FALSE;
  while ( ( 
$data{1} != "\xD9" ) && (! $hit_compressed_image_data) && ( ! feof( $filehnd ) )&& (!isset($salida)))
    {
      
// Found a segment to look at.
      // Check that the segment marker is not a Restart marker - restart markers don't have size or data after them
      
if (  ( ord($data{1}) < 0xD0 ) || ( ord($data{1}) > 0xD7 ) )
    {
      
$sizestr = network_safe_fread( $filehnd, 2 ); // Read the next two bytes (size)
      
$decodedsize = unpack ("nsize", $sizestr); // convert the size bytes to an integer
      
$segdatastart = ftell( $filehnd ); // Save the start position of the data
      
$segdata = network_safe_fread( $filehnd, $decodedsize['size'] - 2 ); // Read the segment data with length indicated by the previously read size
      
if (  ( ord($data{1}) >= 0xC0 ) && ( ord($data{1}) <= 0xCF ) && ( ord($data{1}) != 0xC4 )
        && ( 
ord($data{1}) != 0xCC )  && ( ord($data{1}) != 0xC8 )) {
        
$salida[0] = ord( $segdata{ 3 } ) * 256 + ord( $segdata{ 4 } );
        
$salida[1] = ord( $segdata{ 1 } ) * 256 + ord( $segdata{ 2 } );
      }
    }

      
// If this is a SOS (Start Of Scan) segment, then there is no more header data - the compressed image data follows
      
if ( $data{1} == "\xDA" )
    {
      
// Flag that we have hit the compressed image data - exit loop as no more headers available.
      
$hit_compressed_image_data = TRUE;
    }
      else
    {
      
// Not an SOS - Read the next two bytes - should be the segment marker for the next segment
      
$data = network_safe_fread( $filehnd, 2 );

      
// Check that the first byte of the two is 0xFF as it should be for a marker
      
if ( $data{0} != "\xFF" )
        {
          
// NO FF found - close file and return - JPEG is probably corrupted
          
fclose($filehnd);
          return 
FALSE;
        }
    }
    }

  
// Return the header data retrieved
  
return $salida;
}

function 
cierra_img($mnja) {
  
fclose($mnja);
  
ignore_user_abort(false);
}

function 
ancho_img($archivo) {
  
$dt = datos_img($archivo);
  return (
$dt)?$dt[0]:false;
}

function 
alto_img($archivo) {
  
$dt = datos_img($archivo);
  return (
$dt)?$dt[1]:false;
}

function 
datos_img($archivo) {
  
$mnj = abreimagen($archivo);
  if (
$mnj) {
    
$tipo = tipo_imagen($mnj);
    if (
$tipo=="jpeg") 
      
$res = get_jpeg_header_data($mnj); 
    else
      if (
$tipo=="gif") 
    
$res = saca_cabecera_gif ($mnj); 
      else
    if (
$tipo=="png") 
      
$res = saca_cabecera_png ($mnj); 
    else 
      
$res = false;    
  } else 
$res = false;
  
cierra_img($mnj);
  return 
$res;
} 

Suerte!
__________________
Saludos

Gaspar Fernández
Poesía Binaria