[PHP-DEV] CVS update: php3/functions From: thies (php-dev <email protected>)
Date: 10/09/98

Date: Friday October 9, 1998 @ 13:57
Author: thies

Update of /repository/php3/functions
In directory asf:/u2/tmp/cvs-serv4588

Modified Files:
        image.c
Log Message:
initial support for reading IPTC info from JPEG files.
i've added an extra optional parameter to GetImageSize (do we want an extra function?)

- everything should be backward compatible!!

getimagesize("whatsoever.jpg"[,&$info]);

info will be an assoc array filled up with the IPTC data from the JPEG file

you can see a "demo" at www.digicol.de:7777/tc/dgs/test.php3

binary IPTC fields are not supported as of now, will add support later!
(i will also add some textual description to the fields, right now you get "IPTC-Record-Number#IPTC-Dataset")

PS: this is my 1st commit so please tell me what i did wrong.....

Index: php3/functions/image.c
diff -c php3/functions/image.c:1.36 php3/functions/image.c:1.37
*** php3/functions/image.c:1.36 Fri Oct 9 12:16:05 1998
--- php3/functions/image.c Fri Oct 9 13:57:26 1998
***************
*** 26,32 ****
     | Authors: Rasmus Lerdorf |
     +----------------------------------------------------------------------+
   */
! /* $Id: image.c,v 1.36 1998/10/09 16:16:05 ssb Exp $ */
  /*
   * Based on Daniel Schmitt's imageinfo.c which carried the following
   * Copyright notice.
--- 26,32 ----
     | Authors: Rasmus Lerdorf |
     +----------------------------------------------------------------------+
   */
! /* $Id: image.c,v 1.37 1998/10/09 17:57:26 thies Exp $ */
  /*
   * Based on Daniel Schmitt's imageinfo.c which carried the following
   * Copyright notice.
***************
*** 70,75 ****
--- 70,76 ----
   (char) 0x1a, (char) 0x0a};
  
  /* return info as a struct, to make expansion easier */
+
  struct gfxinfo {
          unsigned int width;
          unsigned int height;
***************
*** 134,139 ****
--- 135,156 ----
  #define M_SOF15 0xCF
  #define M_EOI 0xD9 /* End Of Image (end of datastream) */
  #define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
+ #define M_APP0 0xe0
+ #define M_APP1 0xe1
+ #define M_APP2 0xe2
+ #define M_APP3 0xe3
+ #define M_APP4 0xe4
+ #define M_APP5 0xe5
+ #define M_APP6 0xe6
+ #define M_APP7 0xe7
+ #define M_APP8 0xe8
+ #define M_APP9 0xe9
+ #define M_APP10 0xea
+ #define M_APP11 0xeb
+ #define M_APP12 0xec
+ #define M_APP13 0xed
+ #define M_APP14 0xee
+ #define M_APP15 0xef
  
  static unsigned short php3_read2(FILE *fp)
  {
***************
*** 168,173 ****
--- 185,257 ----
          return (unsigned int) c;
  }
  
+ static void php3_read_iptc(FILE *fp,pval *info)
+ /* read IPTC-info */
+ {
+ unsigned short length;
+ unsigned char *buffer;
+ unsigned char key[ 16 ];
+ pval values;
+ pval *element;
+
+ unsigned char recnum, dataset;
+
+ int inx, len, cnt;
+
+ length = php3_read2(fp);
+ length -= 2; /* length includes itself */
+
+ buffer = emalloc(length);
+
+ if (fread(buffer,length,1,fp) != 1) {
+ return;
+ }
+
+ pval_destructor(info _INLINE_TLS);
+ if (array_init(info) == FAILURE) {
+ return;
+ }
+
+ inx = 0;
+ cnt = 0;
+
+ while (inx < length) {
+ if (buffer[ inx++ ] != 0x1c) { /* skip all junk */
+ continue;
+ }
+
+ if ((inx + 4) >= length)
+ break;
+
+ dataset = buffer[ inx++ ];
+ recnum = buffer[ inx++ ];
+ len = (((unsigned short) buffer[ inx ])<<8) | (unsigned short)buffer[ inx+1 ];
+ inx += 2;
+
+ if (buffer[ inx ] == 0)
+ continue;
+
+ sprintf(key,"%d#%03d",(unsigned int) dataset,(unsigned int) recnum);
+
+ if ((inx + len) >= length)
+ break;
+
+ if (_php3_hash_find(info->value.ht,key,strlen(key) + 1,(void **) &element) == FAILURE) {
+ if (array_init(&values) == FAILURE) {
+ return;
+ }
+
+ _php3_hash_update(info->value.ht, key, strlen(key)+1, (void *) &values, sizeof(pval), (void **) &element);
+ }
+
+ add_next_index_stringl(element,buffer+inx,len,1);
+
+ inx += len;
+ }
+
+ efree(buffer);
+ }
+
  static void php3_skip_variable(FILE *fp)
           /* skip over a variable-length block; assumes proper length marker */
  {
***************
*** 178,187 ****
          fseek(fp, (long) length, SEEK_CUR); /* skip the header */
  }
  
! static struct gfxinfo *php3_handle_jpeg(FILE *fp)
           /* main loop to parse JPEG structure */
  {
          struct gfxinfo *result = NULL;
          unsigned int marker;
          unsigned short in_width, in_height;
  
--- 262,272 ----
          fseek(fp, (long) length, SEEK_CUR); /* skip the header */
  }
  
! static struct gfxinfo *php3_handle_jpeg(FILE *fp,pval *info)
           /* main loop to parse JPEG structure */
  {
          struct gfxinfo *result = NULL;
+ int info_found = 0;
          unsigned int marker;
          unsigned short in_width, in_height;
  
***************
*** 203,245 ****
                          case M_SOF13:
                          case M_SOF14:
                          case M_SOF15:
! /* handle SOFn block */
! fseek(fp, 3L, SEEK_CUR); /* skip length and precision bytes */
! in_height = php3_read2(fp);
! in_width = php3_read2(fp);
! /* fill a gfxinfo struct to return the data */
! result = (struct gfxinfo *) emalloc(sizeof(struct gfxinfo));
! result->width = (unsigned int) in_width;
! result->height = (unsigned int) in_height;
! return result; /* this is all we were looking for */
                                  break;
                          case M_SOS:
                          case M_EOI:
                                  return NULL; /* we're about to hit image data, or are at EOF. Error. */
                                  break;
                          default:
                                  php3_skip_variable(fp); /* anything else isn't interesting */
                                  break;
                  }
          }
  }
  
  /* main function */
  void php3_getimagesize(INTERNAL_FUNCTION_PARAMETERS)
  {
! pval *arg1;
          FILE *fp;
          int itype = 0;
          char filetype[3];
          char pngtype[8];
          char temp[64];
          struct gfxinfo *result = NULL;
  
! if (getParameters(ht, 1, &arg1) == FAILURE) {
                  WRONG_PARAM_COUNT;
          }
! convert_to_string(arg1);
!
          /* Check open_basedir */
          if (_php3_check_open_basedir(arg1->value.str.val)) return;
          
--- 288,367 ----
                          case M_SOF13:
                          case M_SOF14:
                          case M_SOF15:
! if (result == NULL) {
! /* handle SOFn block */
! fseek(fp, 3L, SEEK_CUR); /* skip length and precision bytes */
! in_height = php3_read2(fp);
! in_width = php3_read2(fp);
! /* fill a gfxinfo struct to return the data */
! result = (struct gfxinfo *) emalloc(sizeof(struct gfxinfo));
!
! result->width = (unsigned int) in_width;
! result->height = (unsigned int) in_height;
! }
! break;
!
! case M_APP13:
! if (info && ! info_found) {
! php3_read_iptc(fp,info);
! info_found = 1;
! } else {
! php3_skip_variable(fp);
! }
                                  break;
+
                          case M_SOS:
                          case M_EOI:
                                  return NULL; /* we're about to hit image data, or are at EOF. Error. */
                                  break;
+
                          default:
                                  php3_skip_variable(fp); /* anything else isn't interesting */
                                  break;
                  }
+
+ if ((result && (! info)) || (result && info && info_found))
+ return result;
          }
+
+ return NULL;
  }
  
  /* main function */
  void php3_getimagesize(INTERNAL_FUNCTION_PARAMETERS)
  {
! pval *arg1,*info = 0;
          FILE *fp;
          int itype = 0;
          char filetype[3];
          char pngtype[8];
          char temp[64];
          struct gfxinfo *result = NULL;
+
+ switch(ARG_COUNT(ht)){
+ case 1:
+ if (getParameters(ht, 1, &arg1) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_string(arg1);
+ break;
+
+ case 2:
+ if (getParameters(ht, 2, &arg1, &info) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ if (!ParameterPassedByReference(ht, 2)) {
+ php3_error(E_WARNING, "Array to be filled with values must be passed by reference.");
+ RETURN_FALSE;
+ }
+ convert_to_string(arg1);
+ break;
  
! default:
                  WRONG_PARAM_COUNT;
+ break;
          }
!
          /* Check open_basedir */
          if (_php3_check_open_basedir(arg1->value.str.val)) return;
          
***************
*** 252,258 ****
                  result = php3_handle_gif (fp);
                  itype = 1;
          } else if (!memcmp(filetype, php3_sig_jpg, 3)) {
! result = php3_handle_jpeg(fp);
                  itype = 2;
          } else if (!memcmp(filetype, php3_sig_png, 3)) {
                  fseek(fp, 0L, SEEK_SET);
--- 374,380 ----
                  result = php3_handle_gif (fp);
                  itype = 1;
          } else if (!memcmp(filetype, php3_sig_jpg, 3)) {
! result = php3_handle_jpeg(fp,info);
                  itype = 2;
          } else if (!memcmp(filetype, php3_sig_png, 3)) {
                  fseek(fp, 0L, SEEK_SET);
***************
*** 276,281 ****
--- 398,404 ----
                  add_index_long(return_value, 2, itype);
                  sprintf(temp, "width=\"%d\" height=\"%d\"", result->width, result->height); /* safe */
                  add_index_string(return_value, 3, temp, 1);
+
                  efree(result);
          }
  }

--
PHP Development Mailing List   http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe <email protected>
For help: php-dev-help <email protected>