Skip to content

Commit 0a55c4b

Browse files
committed
- (s)rgb distance works way better for now, re enable threshold
1 parent 7698bc5 commit 0a55c4b

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

ext/gd/gd.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,10 +1224,7 @@ PHP_MINIT_FUNCTION(gd)
12241224
REGISTER_LONG_CONSTANT("IMG_CROP_BLACK", GD_CROP_BLACK, CONST_CS | CONST_PERSISTENT);
12251225
REGISTER_LONG_CONSTANT("IMG_CROP_WHITE", GD_CROP_WHITE, CONST_CS | CONST_PERSISTENT);
12261226
REGISTER_LONG_CONSTANT("IMG_CROP_SIDES", GD_CROP_SIDES, CONST_CS | CONST_PERSISTENT);
1227-
#ifdef GD_ENABLE_CROP_THRESHOLD
12281227
REGISTER_LONG_CONSTANT("IMG_CROP_THRESHOLD", GD_CROP_THRESHOLD, CONST_CS | CONST_PERSISTENT);
1229-
#endif
1230-
12311228
#else
12321229
REGISTER_LONG_CONSTANT("GD_BUNDLED", 0, CONST_CS | CONST_PERSISTENT);
12331230
#endif
@@ -5160,49 +5157,46 @@ PHP_FUNCTION(imagecrop)
51605157
double threshold = 0.5f;
51615158
gdImagePtr im;
51625159
gdImagePtr im_crop;
5163-
HashTable rect_hash;
51645160
gdRect rect;
5161+
zval *z_rect;
51655162
zval **tmp;
51665163

5167-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|h", &IM, &rect_hash) == FAILURE) {
5164+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|a", &IM, &z_rect) == FAILURE) {
51685165
return;
51695166
}
51705167

51715168
ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
51725169

5173-
if (zend_hash_find(&rect_hash, "x", strlen("x"), (void **)&tmp) != FAILURE) {
5170+
if (zend_hash_find(HASH_OF(z_rect), "x", sizeof("x"), (void **)&tmp) != FAILURE) {
51745171
rect.x = Z_LVAL_PP(tmp);
51755172
} else {
51765173
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing x position");
51775174
RETURN_FALSE;
51785175
}
51795176

5180-
if (zend_hash_find(&rect_hash, "y", strlen("x"), (void **)&tmp) != FAILURE) {
5177+
if (zend_hash_find(HASH_OF(z_rect), "y", sizeof("x"), (void **)&tmp) != FAILURE) {
51815178
rect.y = Z_LVAL_PP(tmp);
51825179
} else {
51835180
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing y position");
51845181
RETURN_FALSE;
51855182
}
51865183

5187-
if (zend_hash_find(&rect_hash, "width", strlen("x"), (void **)&tmp) != FAILURE) {
5184+
if (zend_hash_find(HASH_OF(z_rect), "width", sizeof("width"), (void **)&tmp) != FAILURE) {
51885185
rect.width = Z_LVAL_PP(tmp);
51895186
} else {
51905187
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing width");
51915188
RETURN_FALSE;
51925189
}
51935190

5194-
if (zend_hash_find(&rect_hash, "height", strlen("x"), (void **)&tmp) != FAILURE) {
5195-
rect.width = Z_LVAL_PP(tmp);
5191+
if (zend_hash_find(HASH_OF(z_rect), "height", sizeof("height"), (void **)&tmp) != FAILURE) {
5192+
rect.height = Z_LVAL_PP(tmp);
51965193
} else {
51975194
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing height");
51985195
RETURN_FALSE;
51995196
}
52005197

52015198
im_crop = gdImageCrop(im, &rect);
52025199

5203-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown flip mode");
5204-
RETURN_FALSE;
5205-
52065200
if (im_crop == NULL) {
52075201
RETURN_FALSE;
52085202
} else {
@@ -5238,15 +5232,15 @@ PHP_FUNCTION(imagecropauto)
52385232
case GD_CROP_SIDES:
52395233
im_crop = gdImageCropAuto(im, mode);
52405234
break;
5241-
#ifdef GD_ENABLE_CROP_THRESHOLD
5235+
52425236
case GD_CROP_THRESHOLD:
52435237
if (color < 0) {
52445238
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Color argument missing with threshold mode");
52455239
RETURN_FALSE;
52465240
}
52475241
im_crop = gdImageCropThreshold(im, color, (float) threshold);
52485242
break;
5249-
#endif
5243+
52505244
default:
52515245
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown flip mode");
52525246
RETURN_FALSE;

ext/gd/libgd/gd_crop.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <gd.h>
2323
#include <stdlib.h>
2424
#include <string.h>
25+
#include <math.h>
2526

2627
static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color);
2728
static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold);
@@ -65,7 +66,6 @@ printf("rect->x: %i\nrect->y: %i\nrect->width: %i\nrect->height: %i\n", crop->x,
6566
return NULL;
6667
} else {
6768
int y = crop->y;
68-
unsigned int dst_y = 0;
6969
if (src->trueColor) {
7070
unsigned int dst_y = 0;
7171
while (y < (crop->y + (crop->height - 1))) {
@@ -336,9 +336,10 @@ static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold)
336336
const int dg = gdImageGreen(im, col1) - gdImageGreen(im, col2);
337337
const int db = gdImageBlue(im, col1) - gdImageBlue(im, col2);
338338
const int da = gdImageAlpha(im, col1) - gdImageAlpha(im, col2);
339-
const int dist = dr * dr + dg * dg + db * db + da * da;
340-
341-
return (100.0 * dist / 195075) < threshold;
339+
const double dist = sqrt(dr * dr + dg * dg + db * db + da * da);
340+
const double dist_perc = sqrt(dist / (255^2 + 255^2 + 255^2));
341+
return (dist_perc <= threshold);
342+
//return (100.0 * dist / 195075) < threshold;
342343
}
343344

344345
/*

0 commit comments

Comments
 (0)