]> granicus.if.org Git - php/commitdiff
gd-jpeg support
authorRasmus Lerdorf <rasmus@php.net>
Mon, 13 Mar 2000 05:58:50 +0000 (05:58 +0000)
committerRasmus Lerdorf <rasmus@php.net>
Mon, 13 Mar 2000 05:58:50 +0000 (05:58 +0000)
@Add GD-JPEG Support (Rasmus)

ext/gd/config.m4
ext/gd/gd.c
ext/gd/php_gd.h
ext/pdf/config.m4

index 68ebe14e2193b9212fe82444604605a6a5107b94..c0eb469babe82b611382df2e157dde8beeb83968 100644 (file)
@@ -36,6 +36,26 @@ dnl Some versions of GD support both PNG and GIF. Check for both.
           AC_ADD_LIBRARY(png)
           AC_ADD_LIBRARY(z)
         fi
+
+        AC_MSG_CHECKING([for libjpeg (needed by gd-1.8+)])
+        AC_ARG_WITH(jpeg-dir,
+        [  --with-jpeg-dir[=DIR]   jpeg dir for gd-1.8+],[
+          AC_MSG_RESULT(yes)
+          if test -z $withval; then
+            withval="/usr/local"
+          fi
+          old_LIBS=$LIBS
+          LIBS="$LIBS -L$withval/lib"
+          AC_CHECK_LIB(jpeg,jpeg_read_header, [LIBS="$LIBS -L$withval/lib -ljpeg"],[AC_MSG_RESULT(no)],)
+          LIBS=$old_LIBS
+          AC_ADD_LIBRARY_WITH_PATH(jpeg, $withval/lib)
+          LIBS="$LIBS -L$withval/lib -ljpeg"
+        ],[
+          AC_MSG_RESULT(no)
+          AC_MSG_WARN(If configure fails try --with-jpeg-dir=<DIR>)
+        ]) 
+        AC_CHECK_LIB(gd, gdImageCreateFromJpeg, [AC_DEFINE(HAVE_GD_JPG, 1, [ ])])
+
         ac_cv_lib_gd_gdImageLine=yes
       ;;
     *)
index aacbb2e6515bfeca71b7134809317e16c3787294..a14b8981f29e5b16df960049b4759962dee0d680 100644 (file)
@@ -115,6 +115,10 @@ function_entry gd_functions[] = {
 #ifdef HAVE_GD_GIF
        PHP_FE(imagecreatefromgif,                                              NULL)
        PHP_FE(imagegif,                                                                NULL)
+#endif
+#ifdef HAVE_GD_JPG
+       PHP_FE(imagecreatefromjpeg,                                             NULL)
+       PHP_FE(imagejpeg,                                                               NULL)
 #endif
        PHP_FE(imagedestroy,                                                    NULL)
        PHP_FE(imagefill,                                                               NULL)
@@ -635,6 +639,116 @@ PHP_FUNCTION(imagegif)
 
 #endif /* HAVE_GD_GIF */
 
+#ifdef HAVE_GD_JPG
+
+/* {{{ proto int imagecreatefromjpeg(string filename)
+   Create a new image from JPEG file or URL */
+PHP_FUNCTION(imagecreatefromjpeg)
+{
+       zval **file;
+       gdImagePtr im;
+       char *fn=NULL;
+       FILE *fp;
+       int issock=0, socketd=0;
+       GDLS_FETCH();
+
+       if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &file) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+       convert_to_string_ex(file);
+       fn = (*file)->value.str.val;
+#if WIN32|WINNT
+       fp = fopen((*file)->value.str.val, "rb");
+#else
+       fp = php_fopen_wrapper((*file)->value.str.val, "r", IGNORE_PATH|IGNORE_URL_WIN, &issock, &socketd, NULL);
+#endif
+       if (!fp) {
+               php_strip_url_passwd(fn);
+               php_error(E_WARNING,
+                                 "ImageCreateFromJpeg: Unable to open %s for reading", fn);
+               RETURN_FALSE;
+       }
+       im = gdImageCreateFromJpeg(fp);
+       fflush(fp);
+       fclose(fp);
+
+       ZEND_REGISTER_RESOURCE(return_value, im, GDG(le_gd));
+}
+/* }}} */
+
+/* {{{ proto int imagejpeg(int im [, string filename [, int quality]])
+   Output image to browser or file */
+PHP_FUNCTION(imagejpeg)
+{
+       zval **imgind, **file, **qual;
+       gdImagePtr im;
+       char *fn=NULL;
+       FILE *fp;
+       int argc;
+       int output=1, q=-1;
+       GDLS_FETCH();
+
+       argc = ARG_COUNT(ht);
+       if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &imgind, &file, &qual) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+       ZEND_FETCH_RESOURCE(im, gdImagePtr, imgind, -1, "Image", GDG(le_gd));
+
+       if (argc == 3) {
+               convert_to_long_ex(qual);
+               q = (*qual)->value.lval;
+       }
+
+       if (argc > 1) {
+               convert_to_string_ex(file);
+               fn = (*file)->value.str.val;
+               if (fn && strlen(fn) && php_check_open_basedir(fn)) {
+                       php_error(E_WARNING, "ImageJpeg: Invalid filename");
+                       RETURN_FALSE;
+               }
+       }
+
+       if (argc > 1 && strlen(fn)) {
+               fp = fopen(fn, "wb");
+               if (!fp) {
+                       php_error(E_WARNING, "ImageJpeg: unable to open %s for writing", fn);
+                       RETURN_FALSE;
+               }
+               gdImageJpeg(im,fp,q);
+               fflush(fp);
+               fclose(fp);
+       }
+       else {
+               int   b;
+               FILE *tmp;
+               char  buf[4096];
+               tmp = tmpfile();
+               if (tmp == NULL) {
+                       php_error(E_WARNING, "Unable to open temporary file");
+                       RETURN_FALSE;
+               }
+               output = php_header();
+               if (output) {
+                       gdImageJpeg(im, tmp, q);
+            fseek(tmp, 0, SEEK_SET);
+#if APACHE && defined(CHARSET_EBCDIC)
+                       SLS_FETCH();
+            /* This is a binary file already: avoid EBCDIC->ASCII conversion */
+            ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
+#endif
+            while ((b = fread(buf, 1, sizeof(buf), tmp)) > 0) {
+                php_write(buf, b);
+            }
+        }
+        fclose(tmp);
+        /* the temporary file is automatically deleted */
+    }
+    RETURN_TRUE;
+}
+/* }}} */
+
+#endif
+
 /* {{{ proto int imagedestroy(int im)
    Destroy an image */
 PHP_FUNCTION(imagedestroy)
index 3d5e37f7428da80169861de22496bf0d97406b79..f99544f101c2c5e44451918b2458cfc1caf1267a 100644 (file)
@@ -90,6 +90,7 @@ PHP_FUNCTION(imagecopy);
 PHP_FUNCTION(imagecopyresized);
 PHP_FUNCTION(imagecreate);
 PHP_FUNCTION(imagecreatefromgif );
+PHP_FUNCTION(imagecreatefromjpeg );
 PHP_FUNCTION(imagedestroy);
 PHP_FUNCTION(imagefill);
 PHP_FUNCTION(imagefilledpolygon);
@@ -98,6 +99,7 @@ PHP_FUNCTION(imagefilltoborder);
 PHP_FUNCTION(imagefontwidth);
 PHP_FUNCTION(imagefontheight);
 PHP_FUNCTION(imagegif );
+PHP_FUNCTION(imagejpeg );
 PHP_FUNCTION(imageinterlace);
 PHP_FUNCTION(imageline);
 PHP_FUNCTION(imageloadfont);
index bdcde9910a260524aa4271c191a6e4f54c9d4f18..49e6e88dc55f3caa66eab5abec0cf99e212ec02b 100644 (file)
@@ -17,7 +17,7 @@ echo $withval
                  old_LIBS=$LIBS
                  LIBS="$LIBS -ltiff -ljpeg -lpng -lz"
       AC_CHECK_LIB(pdf, PDF_show_boxed, [AC_DEFINE(HAVE_PDFLIB,1,[ ])],
-        [AC_MSG_ERROR(pdflib extension requires at least pdflib 3.x. You may as well need libtiff and libjpeg. In such a case use the options --with-tiff-dir=<DIR> and --with-jpeg-dir=<DIR>)])
+        [AC_MSG_ERROR(pdflib extension requires at least pdflib 3.x. You may also need libtiff and libjpeg. If so, use the options --with-tiff-dir=<DIR> and --with-jpeg-dir=<DIR>)])
       LIBS=$old_LIBS
       LDFLAGS=$old_LDFLAGS
       AC_ADD_LIBRARY(pdf)