Warnings to errors imageconvolution

This commit is contained in:
Mark 2019-09-04 01:38:41 +02:00 committed by Christoph M. Becker
parent 34865f54e7
commit d1f9ab11b3
3 changed files with 30 additions and 14 deletions

View file

@ -3698,23 +3698,23 @@ PHP_FUNCTION(imageconvolution)
nelem = zend_hash_num_elements(Z_ARRVAL_P(hash_matrix)); nelem = zend_hash_num_elements(Z_ARRVAL_P(hash_matrix));
if (nelem != 3) { if (nelem != 3) {
php_error_docref(NULL, E_WARNING, "You must have 3x3 array"); zend_throw_error(NULL, "Convolution matrix must be a 3x3 array");
RETURN_FALSE; return;
} }
for (i=0; i<3; i++) { for (i=0; i<3; i++) {
if ((var = zend_hash_index_find(Z_ARRVAL_P(hash_matrix), (i))) != NULL && Z_TYPE_P(var) == IS_ARRAY) { if ((var = zend_hash_index_find(Z_ARRVAL_P(hash_matrix), (i))) != NULL && Z_TYPE_P(var) == IS_ARRAY) {
if (zend_hash_num_elements(Z_ARRVAL_P(var)) != 3 ) { if (zend_hash_num_elements(Z_ARRVAL_P(var)) != 3 ) {
php_error_docref(NULL, E_WARNING, "You must have 3x3 array"); zend_throw_error(NULL, "Convolution matrix must be a 3x3 array, matrix[%d] only has %d elements", i, zend_hash_num_elements(Z_ARRVAL_P(var)));
RETURN_FALSE; return;
} }
for (j=0; j<3; j++) { for (j=0; j<3; j++) {
if ((var2 = zend_hash_index_find(Z_ARRVAL_P(var), j)) != NULL) { if ((var2 = zend_hash_index_find(Z_ARRVAL_P(var), j)) != NULL) {
matrix[i][j] = (float) zval_get_double(var2); matrix[i][j] = (float) zval_get_double(var2);
} else { } else {
php_error_docref(NULL, E_WARNING, "You must have a 3x3 matrix"); zend_throw_error(NULL, "Convolution matrix must be a 3x3 array, matrix[%d][%d] cannot be found (missing integer key)", i, j);
RETURN_FALSE; return;
} }
} }
} }

View file

@ -9,6 +9,8 @@ if (!extension_loaded("gd")) die("skip GD not present");
?> ?>
--FILE-- --FILE--
<?php <?php
require __DIR__ . '/func.inc';
$image = imagecreatetruecolor(180, 30); $image = imagecreatetruecolor(180, 30);
// Writes the text and apply a gaussian blur on the image // Writes the text and apply a gaussian blur on the image
@ -19,8 +21,10 @@ $gaussian = array(
array(2.0, 4.0, 2.0) array(2.0, 4.0, 2.0)
); );
var_dump(imageconvolution($image, $gaussian, 16, 0)); trycatch_dump(
fn() => imageconvolution($image, $gaussian, 16, 0)
);
?> ?>
--EXPECTF-- --EXPECT--
Warning: imageconvolution(): You must have 3x3 array in %s on line %d !! [Error] Convolution matrix must be a 3x3 array
bool(false)

View file

@ -9,6 +9,8 @@ if (!extension_loaded("gd")) die("skip GD not present");
?> ?>
--FILE-- --FILE--
<?php <?php
require __DIR__ . '/func.inc';
$image = imagecreatetruecolor(180, 30); $image = imagecreatetruecolor(180, 30);
// Writes the text and apply a gaussian blur on the image // Writes the text and apply a gaussian blur on the image
@ -20,8 +22,18 @@ $gaussian = array(
array(1.0, 2.0) array(1.0, 2.0)
); );
var_dump(imageconvolution($image, $gaussian, 16, 0)); $gaussian_bad_key = array(
array(1.0, 2.0, 1.0),
array(2.0, 4.0, 2.0),
array(1.0, 2.0, 'x' => 1.0)
);
trycatch_dump(
fn() => imageconvolution($image, $gaussian, 16, 0),
fn() => imageconvolution($image, $gaussian_bad_key, 16, 0)
);
?> ?>
--EXPECTF-- --EXPECT--
Warning: imageconvolution(): You must have 3x3 array in %s on line %d !! [Error] Convolution matrix must be a 3x3 array, matrix[2] only has 2 elements
bool(false) !! [Error] Convolution matrix must be a 3x3 array, matrix[2][2] cannot be found (missing integer key)