]> granicus.if.org Git - php/commitdiff
Sync gdImageCrop() with upstream
authorChristoph M. Becker <cmbecker69@gmx.de>
Fri, 2 Feb 2018 16:39:23 +0000 (17:39 +0100)
committerChristoph M. Becker <cmbecker69@gmx.de>
Fri, 2 Feb 2018 16:39:23 +0000 (17:39 +0100)
Besides that we still hope to sync the complete bundled libgd sometime,
the upstream implementation of `gdImageCrop()` is much simpler than the
current one, and thus better readable and less error prone.

ext/gd/libgd/gd_crop.c

index 58b630317ddb7bdb0cd7a2df91ff28b82892dd4a..74d86669f5e77073cd7c9c7630812dcd3d927ae8 100644 (file)
@@ -43,55 +43,19 @@ static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold);
 gdImagePtr gdImageCrop(gdImagePtr src, const gdRectPtr crop)
 {
        gdImagePtr dst;
-       int y;
+       int alphaBlendingFlag;
 
-       /* allocate the requested size (could be only partially filled) */
-       if (src->trueColor) {
+       if (gdImageTrueColor(src)) {
                dst = gdImageCreateTrueColor(crop->width, crop->height);
-               if (dst == NULL) {
-                       return NULL;
-               }
-               gdImageSaveAlpha(dst, 1);
        } else {
                dst = gdImageCreate(crop->width, crop->height);
-               if (dst == NULL) {
-                       return NULL;
-               }
-               gdImagePaletteCopy(dst, src);
-       }
-       dst->transparent = src->transparent;
-
-       /* check position in the src image */
-       if (crop->x < 0 || crop->x>=src->sx || crop->y<0 || crop->y>=src->sy) {
-               return dst;
-       }
-
-       /* reduce size if needed */
-       if ((src->sx - crop->width) < crop->x) {
-               crop->width = src->sx - crop->x;
-       }
-       if ((src->sy - crop->height) < crop->y) {
-               crop->height = src->sy - crop->y;
        }
+       if (!dst) return NULL;
+       alphaBlendingFlag = dst->alphaBlendingFlag;
+       gdImageAlphaBlending(dst, gdEffectReplace);
+       gdImageCopy(dst, src, 0, 0, crop->x, crop->y, crop->width, crop->height);
+       gdImageAlphaBlending(dst, alphaBlendingFlag);
 
-#if 0
-printf("rect->x: %i\nrect->y: %i\nrect->width: %i\nrect->height: %i\n", crop->x, crop->y, crop->width, crop->height);
-#endif
-       y = crop->y;
-       if (src->trueColor) {
-               unsigned int dst_y = 0;
-               while (y < (crop->y + crop->height)) {
-                       /* TODO: replace 4 w/byte per channel||pitch once available */
-                       memcpy(dst->tpixels[dst_y++], src->tpixels[y++] + crop->x, crop->width * 4);
-               }
-       } else {
-               int x;
-               for (y = crop->y; y < (crop->y + crop->height); y++) {
-                       for (x = crop->x; x < (crop->x + crop->width); x++) {
-                               dst->pixels[y - crop->y][x - crop->x] = src->pixels[y][x];
-                       }
-               }
-       }
        return dst;
 }