09-27-2008, 03:05 AM
|
#1 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
imagecreatefrom() function
This simple CVS Diff (against PHP 5.3 Alpha 2) will add a new function to PHP's gd extension called "imagecreatefrom", it sort of works like getimagesize(), but it just creates an image instance without specificing the type.
DIFF:
Code:
Index: gd.c
===================================================================
RCS file: /repository/php-src/ext/gd/gd.c,v
retrieving revision 1.312.2.20.2.32.2.17
diff -u -r1.312.2.20.2.32.2.17 gd.c
--- gd.c 29 Aug 2008 11:09:28 -0000 1.312.2.20.2.32.2.17
+++ gd.c 25 Sep 2008 22:25:43 -0000
@@ -167,6 +167,7 @@
static int _php_image_type(char data[8]);
static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type);
static void _php_image_bw_convert(gdImagePtr im_org, gdIOCtx *out, int threshold);
+static int _php_image_get_type(php_stream *stream, char *filetype TSRMLS_DC);
/* {{{ arginfo */
static
@@ -977,6 +978,12 @@
ZEND_END_ARG_INFO()
#endif
+static
+ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefrom, 0)
+ ZEND_ARG_INFO(0, im)
+ ZEND_ARG_INFO(0, on)
+ZEND_END_ARG_INFO()
+
/* }}} */
/* {{{ gd_functions[]
@@ -1148,6 +1155,8 @@
PHP_FE(imageconvolution, arginfo_imageconvolution)
#endif
+ PHP_FE(imagecreatefrom, arginfo_imagecreatefrom)
+
{NULL, NULL, NULL}
};
/* }}} */
@@ -5121,6 +5130,105 @@
/* }}} */
#endif
+/* {{{ php_gd_getimagetype
+*/
+/* Return values
+ * . Anything above 0 means one of the PHP_GDIMG_TYPE_* consts
+ * . 0: unknown format
+ * . -1: read error
+ * . -2: support for format not found
+ */
+/* Based heavily on php_getimagetype in ext/standard/image.c */
+static int _php_image_get_type(php_stream *stream, char *filetype TSRMLS_DC)
+{
+ char tmp[12];
+
+ if(!filetype) {
+ filetype = tmp;
+ }
+
+ if((php_stream_read(stream, tmp, 3)) != 3) {
+ return -1;
+ }
+
+ if (!memcmp(filetype, php_sig_gif, 3)) {
+#ifdef HAVE_GD_GIF_READ
+ return PHP_GDIMG_TYPE_GIF;
+#else
+ return -2;
+#endif
+ } else if (!memcmp(filetype, php_sig_jpg, 3)) {
+#ifdef HAVE_GD_JPG
+ return PHP_GDIMG_TYPE_JPG;
+#else
+ return -2;
+#endif
+ } else if(!memcmp(filetype, php_sig_png, 3)) {
+ if (php_stream_read(stream, filetype+3, 5) != 5) {
+ return -1;
+ } else if (!memcmp(filetype, php_sig_png, 8)) {
+#ifdef HAVE_GD_PNG
+ return PHP_GDIMG_TYPE_PNG;
+#else
+ return -2;
+#endif
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "PNG file corrupted by ASCII conversion");
+ return -1;
+ }
+ }
+
+ return 0;
+}
+/* }}} */
+
+/* {{{ proto resource imagecreatefrom(string filename)
+ Creates an image resource based on a filename */
+PHP_FUNCTION(imagecreatefrom)
+{
+ char *filename = NULL;
+ int filename_len, imagetype;
+ php_stream *stream = NULL;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
+ return;
+ }
+
+ stream = php_stream_open_wrapper(filename, "rb", STREAM_MUST_SEEK | REPORT_ERRORS | IGNORE_PATH | ENFORCE_SAFE_MODE, NULL);
+
+ if (!stream) {
+ RETURN_FALSE;
+ }
+
+ imagetype = _php_image_get_type(stream, NULL TSRMLS_CC);
+ php_stream_close(stream);
+
+ switch (imagetype) {
+ case PHP_GDIMG_TYPE_GIF:
+ _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageCreateFromGif, gdImageCreateFromGifCtx);
+ break;
+ case PHP_GDIMG_TYPE_JPG:
+ _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageCreateFromJpeg, gdImageCreateFromJpegCtx);
+ break;
+ case PHP_GDIMG_TYPE_PNG:
+ _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImageCreateFromPng, gdImageCreateFromPngCtx);
+ break;
+ case 0:
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown/unsupported image format");
+ RETURN_FALSE;
+ break;
+ case -1:
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Read error!");
+ RETURN_FALSE;
+ break;
+ case -2:
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "GD is not compiled with support for image format");
+ RETURN_FALSE;
+ break;
+ }
+}
+/* }}} */
+
/*
* Local variables:
* tab-width: 4
Index: php_gd.h
===================================================================
RCS file: /repository/php-src/ext/gd/php_gd.h,v
retrieving revision 1.59.2.3.2.5.2.4
diff -u -r1.59.2.3.2.5.2.4 php_gd.h
--- php_gd.h 18 Jul 2008 01:51:49 -0000 1.59.2.3.2.5.2.4
+++ php_gd.h 25 Sep 2008 22:25:16 -0000
@@ -195,6 +195,8 @@
PHP_FUNCTION(imagexbm);
#endif
+PHP_FUNCTION(imagecreatefrom);
+
PHP_GD_API int phpi_get_le_gd(void);
#else
Compile php like:
Code:
./configure --with-gd=shared
nmake
(or make for Unix)
And test it like:
Code:
php -d extension_dir=./ -d extension=php_gd2.dll -r "var_dump(imagecreatefrom('sample.jpg'));"
(or gd2.so for Unix)
Also note that this will only work for GIF/PNG/JPEG images and you must compile php with support for each format to make it available for imagecreatefrom() to pick up.
Hope this could be any useful for someone ;)
__________________
|
|
|