com php-src: Add support for webp in imagecreatefromstring: ext/gd/gd.c ext/gd/php_gd.h ext/gd/tests/imagecreatefromstring.bmp
ext/gd/tests/imagecreatefromstring.gif ext/gd/tests/imag ecreatefromstring.webp ext/gd/tests/imagecreatefromstring _gif.phpt
ext/gd/tests/imagecreatefromstring_png.phpt ext/gd/tests/imagecreatefromstring_webp.phpt
| From: | Christoph Michael Becker | Date: | Sun, 30 Jul 2017 00:50:33 +0000 |
| Subject: | com php-src: Add support for webp in imagecreatefromstring: ext/gd/gd.c ext/gd/php_gd.h ext/gd/tests/imagecreatefromstring.bmp ext/gd/tests/imagecreatefromstring.gif ext/gd/tests/imag ecreatefromstring.webp ext/gd/tests/imagecreatefromstring _gif.phpt ext/gd/tests/imagecreatefromstring_png.phpt ext/gd/tests/imagecreatefromstring_webp.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-98965@lists.php.net to get a copy of this message | ||
Commit: 47fcd13055595e063c6b3580bad6f04346c0de34
Author: Andreas Treichel <gmblar+github@gmail.com> Sun, 30 Jul 2017 02:50:33 +0200
Parents: f6a44916623f9d8dc24d35d52f2998700f0567a6
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=47fcd13055595e063c6b3580bad6f04346c0de34
Log:
Add support for webp in imagecreatefromstring
Changed paths:
M ext/gd/gd.c
M ext/gd/php_gd.h
A ext/gd/tests/imagecreatefromstring.bmp
A ext/gd/tests/imagecreatefromstring.gif
A ext/gd/tests/imagecreatefromstring.webp
A ext/gd/tests/imagecreatefromstring_gif.phpt
A ext/gd/tests/imagecreatefromstring_png.phpt
A ext/gd/tests/imagecreatefromstring_webp.phpt
Diff:
diff --git a/ext/gd/gd.c b/ext/gd/gd.c
index 3e7d778..2de1a0a 100644
--- a/ext/gd/gd.c
+++ b/ext/gd/gd.c
@@ -133,7 +133,7 @@ static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS);
static gdImagePtr _php_image_create_from_string (zval *Data, char *tn, gdImagePtr
(*ioctx_func_p)());
static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn,
gdImagePtr (*func_p)(), gdImagePtr (*ioctx_func_p)());
static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void
(*func_p)());
-static int _php_image_type(char data[8]);
+static int _php_image_type(char data[12]);
static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type);
static void _php_image_bw_convert(gdImagePtr im_org, gdIOCtx *out, int threshold);
@@ -2224,7 +2224,7 @@ static int _php_ctx_getmbi(gdIOCtx *ctx)
*/
static const char php_sig_gd2[3] = {'g', 'd', '2'};
-static int _php_image_type (char data[8])
+static int _php_image_type (char data[12])
{
/* Based on ext/standard/image.c */
@@ -2232,18 +2232,18 @@ static int _php_image_type (char data[8])
return -1;
}
- if (!memcmp(data, php_sig_gd2, 3)) {
+ if (!memcmp(data, php_sig_gd2, sizeof(php_sig_gd2))) {
return PHP_GDIMG_TYPE_GD2;
- } else if (!memcmp(data, php_sig_jpg, 3)) {
+ } else if (!memcmp(data, php_sig_jpg, sizeof(php_sig_jpg))) {
return PHP_GDIMG_TYPE_JPG;
- } else if (!memcmp(data, php_sig_png, 3)) {
- if (!memcmp(data, php_sig_png, 8)) {
- return PHP_GDIMG_TYPE_PNG;
- }
- } else if (!memcmp(data, php_sig_gif, 3)) {
+ } else if (!memcmp(data, php_sig_png, sizeof(php_sig_png))) {
+ return PHP_GDIMG_TYPE_PNG;
+ } else if (!memcmp(data, php_sig_gif, sizeof(php_sig_gif))) {
return PHP_GDIMG_TYPE_GIF;
} else if (!memcmp(data, php_sig_bmp, sizeof(php_sig_bmp))) {
return PHP_GDIMG_TYPE_BMP;
+ } else if(!memcmp(data, php_sig_riff, sizeof(php_sig_riff)) && !memcmp(data +
sizeof(php_sig_riff) + sizeof(uint32_t), php_sig_webp, sizeof(php_sig_webp))) {
+ return PHP_GDIMG_TYPE_WEBP;
}
else {
gdIOCtx *io_ctx;
@@ -2294,19 +2294,19 @@ PHP_FUNCTION(imagecreatefromstring)
zval *data;
gdImagePtr im;
int imtype;
- char sig[8];
+ char sig[12];
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &data) == FAILURE) {
return;
}
convert_to_string_ex(data);
- if (Z_STRLEN_P(data) < 8) {
+ if (Z_STRLEN_P(data) < sizeof(sig)) {
php_error_docref(NULL, E_WARNING, "Empty string or invalid image");
RETURN_FALSE;
}
- memcpy(sig, Z_STRVAL_P(data), 8);
+ memcpy(sig, Z_STRVAL_P(data), sizeof(sig));
imtype = _php_image_type(sig);
@@ -2345,6 +2345,15 @@ PHP_FUNCTION(imagecreatefromstring)
im = _php_image_create_from_string(data, "BMP", gdImageCreateFromBmpCtx);
break;
+ case PHP_GDIMG_TYPE_WEBP:
+#ifdef HAVE_GD_WEBP
+ im = _php_image_create_from_string(data, "WEBP", gdImageCreateFromWebpCtx);
+ break;
+#else
+ php_error_docref(NULL, E_WARNING, "No WEBP support in this PHP build");
+ RETURN_FALSE;
+#endif
+
default:
php_error_docref(NULL, E_WARNING, "Data is not in a recognized format");
RETURN_FALSE;
diff --git a/ext/gd/php_gd.h b/ext/gd/php_gd.h
index a99f2e8..7f94b25 100644
--- a/ext/gd/php_gd.h
+++ b/ext/gd/php_gd.h
@@ -62,6 +62,8 @@ PHPAPI extern const char php_sig_gif[3];
PHPAPI extern const char php_sig_jpg[3];
PHPAPI extern const char php_sig_png[8];
PHPAPI extern const char php_sig_bmp[2];
+PHPAPI extern const char php_sig_riff[4];
+PHPAPI extern const char php_sig_webp[4];
extern zend_module_entry gd_module_entry;
#define phpext_gd_ptr &gd_module_entry
diff --git a/ext/gd/tests/imagecreatefromstring.bmp b/ext/gd/tests/imagecreatefromstring.bmp
new file mode 100644
index 0000000..22b5c66
Binary files /dev/null and b/ext/gd/tests/imagecreatefromstring.bmp differ
diff --git a/ext/gd/tests/imagecreatefromstring.gif b/ext/gd/tests/imagecreatefromstring.gif
new file mode 100644
index 0000000..a17060a
Binary files /dev/null and b/ext/gd/tests/imagecreatefromstring.gif differ
diff --git a/ext/gd/tests/imagecreatefromstring.webp b/ext/gd/tests/imagecreatefromstring.webp
new file mode 100644
index 0000000..e3c3c17
Binary files /dev/null and b/ext/gd/tests/imagecreatefromstring.webp differ
diff --git a/ext/gd/tests/imagecreatefromstring_gif.phpt
b/ext/gd/tests/imagecreatefromstring_gif.phpt
new file mode 100644
index 0000000..c913f4e
--- /dev/null
+++ b/ext/gd/tests/imagecreatefromstring_gif.phpt
@@ -0,0 +1,20 @@
+--TEST--
+imagecreatefromstring() - GIF format
+--SKIPIF--
+<?php
+if (!extension_loaded('gd')) die('skip ext/gd required');
+if (!(imagetypes() & IMG_GIF)) die('skip GIF support required');
+?>
+--FILE--
+<?php
+// create an image from a GIF string representation
+$im = imagecreatefromstring(file_get_contents(__DIR__ . '/imagecreatefromstring.gif'));
+var_dump(imagesx($im));
+var_dump(imagesy($im));
+
+?>
+===DONE===
+--EXPECT--
+int(10)
+int(10)
+===DONE===
diff --git a/ext/gd/tests/imagecreatefromstring_png.phpt
b/ext/gd/tests/imagecreatefromstring_png.phpt
new file mode 100644
index 0000000..efb8cc3
--- /dev/null
+++ b/ext/gd/tests/imagecreatefromstring_png.phpt
@@ -0,0 +1,20 @@
+--TEST--
+imagecreatefromstring() - PNG format
+--SKIPIF--
+<?php
+if (!extension_loaded('gd')) die('skip ext/gd required');
+if (!(imagetypes() & IMG_PNG)) die('skip PNG support required');
+?>
+--FILE--
+<?php
+// create an image from a PNG string representation
+$im = imagecreatefromstring(file_get_contents(__DIR__ . '/imagecreatefromstring.gif'));
+var_dump(imagesx($im));
+var_dump(imagesy($im));
+
+?>
+===DONE===
+--EXPECT--
+int(10)
+int(10)
+===DONE===
diff --git a/ext/gd/tests/imagecreatefromstring_webp.phpt
b/ext/gd/tests/imagecreatefromstring_webp.phpt
new file mode 100644
index 0000000..fbae919
--- /dev/null
+++ b/ext/gd/tests/imagecreatefromstring_webp.phpt
@@ -0,0 +1,20 @@
+--TEST--
+imagecreatefromstring() - WEBP format
+--SKIPIF--
+<?php
+if (!extension_loaded('gd')) die('skip ext/gd required');
+if (!(imagetypes() & IMG_WEBP)) die('skip WEBP support required');
+?>
+--FILE--
+<?php
+// create an image from a WEBP string representation
+$im = imagecreatefromstring(file_get_contents(__DIR__ . '/imagecreatefromstring.webp'));
+var_dump(imagesx($im));
+var_dump(imagesy($im));
+
+?>
+===DONE===
+--EXPECT--
+int(10)
+int(10)
+===DONE===