From da4f3edac4b99ce681bebb75da81a8d0046311bb Mon Sep 17 00:00:00 2001 From: Rasmus Lerdorf Date: Mon, 13 Mar 2000 05:58:50 +0000 Subject: [PATCH] gd-jpeg support @Add GD-JPEG Support (Rasmus) --- ext/gd/config.m4 | 20 ++++++++ ext/gd/gd.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++ ext/gd/php_gd.h | 2 + ext/pdf/config.m4 | 2 +- 4 files changed, 137 insertions(+), 1 deletion(-) diff --git a/ext/gd/config.m4 b/ext/gd/config.m4 index 68ebe14e21..c0eb469bab 100644 --- a/ext/gd/config.m4 +++ b/ext/gd/config.m4 @@ -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=) + ]) + AC_CHECK_LIB(gd, gdImageCreateFromJpeg, [AC_DEFINE(HAVE_GD_JPG, 1, [ ])]) + ac_cv_lib_gd_gdImageLine=yes ;; *) diff --git a/ext/gd/gd.c b/ext/gd/gd.c index aacbb2e651..a14b8981f2 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -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) diff --git a/ext/gd/php_gd.h b/ext/gd/php_gd.h index 3d5e37f742..f99544f101 100644 --- a/ext/gd/php_gd.h +++ b/ext/gd/php_gd.h @@ -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); diff --git a/ext/pdf/config.m4 b/ext/pdf/config.m4 index bdcde9910a..49e6e88dc5 100644 --- a/ext/pdf/config.m4 +++ b/ext/pdf/config.m4 @@ -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= and --with-jpeg-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= and --with-jpeg-dir=)]) LIBS=$old_LIBS LDFLAGS=$old_LDFLAGS AC_ADD_LIBRARY(pdf) -- 2.50.1