cgicorner.ch ¦ CGI Hilfe ¦ Knowledge Base ¦ Bildgrösse (Breite und Höhe) bestimmen    
   http://www.cgicorner.ch

Main Download CGI Hilfe Knowledge Base Links Gaestebuch Witze-Bereich Sitemap Impressum

Bildgrösse (Breite und Höhe) bestimmen

Ist es möglich, ohne irgendwelche Module, die Breite und Höhe eines GIF-, JPG- oder PNG-Bildes zu bestimmen?

Natürlich ist es möglich. Nachfolgend nun der relativ umfangreiche Quellcode. Das Bildformat kann dann mittels ($width,$height)=&getImageSize("myfile.gif"); bestimmt werden. Die Funktion funktioniert mit GIF-, JPG, und PNG-Dateien.
###########################################################################
sub getImageSize
  {
  my ($width,$height);
  if ($_[0] =~ m/\.gif\Z/i)
    { ($width,$height)=getImageSizeGIF($_[0]); }
  elsif ($_[0] =~ m/\.jpe?g\Z/i)
    { ($width,$height)=getImageSizeJPG($_[0]); }
  elsif ($_[0] =~ m/\.PNG\Z/i)
    { ($width,$height)=getImageSizePNG($_[0]); }
  return ($width,$height);
  }
###########################################################################
sub getImageSizeGIF
  {
  my ($lf,$image,$width,$height,$s,@size);
  $lf=$/;
  undef $/;
  open (IMAGE, $_[0]) ;    
  binmode(IMAGE);
  $image=<IMAGE> ;
  close(IMAGE);
  $/=$lf;
  if(!(substr($image,0,6) =~ m/GIF8[7,9]a/) || (length($s=substr($image, 6, 4))!=4))
    { return; }
  (@size)=unpack("C"x4,$s);
  $width= $size[1]<<8|$size[0];
  $height= $size[3]<<8|$size[2];
  return ($width,$height);
  }
###########################################################################
sub getImageSizeJPG
  {
  my ($lf,$image,$width,$height,@size);
  $lf=$/;
  undef $/;
  open (IMAGE, $_[0]);    
  binmode(IMAGE);
  $image=<IMAGE> ;
  close(IMAGE);
  $/=$lf;
  my $count=2 ;
  my $length=length($image);
  my $ch="" ;

  while (($ch ne "\xda") && ($count<$length))
    {
    while (($ch ne "\xff") && ($count<$length))
      {
      $ch=substr($image,$count,1); 
      $count++;
      }
    while (($ch eq "\xff") && ($count<$length))
      {
      $ch=substr($image,$count,1);
      $count++;
      }
    if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3))
      {
      $count+=3;
      (@size)=unpack("C"x4,substr($image,$count,4));
      $width=$size[2]<<8|$size[3];
      $height=$size[0]<<8|$size[1];
      return ($width,$height);
      }
    else
      {
      (@size)= unpack("C"x2,substr($image,$count,2));
      $count += $size[0]<<8|$size[1];
      }
    }
  }
###########################################################################
sub getImageSizePNG
  {
  my ($lf,$image,$width,$height,@size);
  $lf=$/;
  undef $/;
  open (IMAGE, $_[0]);    
  binmode(IMAGE);
  $image=<IMAGE> ;
  close(IMAGE);
  $/=$lf;
  if ($image =~ /IHDR(.{8})/)
    {
    $image = $1;
    ($width,$height) = unpack( "NN", $image);
    }
  else
    { print "Datei ist keine gueltige PNG Datei\n"; };
  return ($width,$height);
  }

Dieser Artikel wurde zugesandt von: Helmut Walter, inspire-world.de

Weiterführende Links:


 
Druckfreundliche Version zum Anfang der Seite
Copyright (c) 2011 by cgicorner.ch
Diese Seite ist ein Teil von http://www.cgicorner.ch
URL dieser Seite: http://www.cgicorner.ch/cgi-bin/kb/kb.cgi?file=imgsize.kb
Letzte Änderung: 23. Oktober 2003