PDA

View Full Version : FQ ADMIN LOOK HERE =) new thumbnail function


daclown
05-14-2001, 01:27 AM
I found this in the php.net function guides and was hoping that you could compile it into your existing php version .. here is all the info.... cut and pasted =)...

-----------------------------------------------------------
Here is my addition to imagecopyresized

it implements bicubic interpolation of pixels so the result is much better than the ordinary imagecopyresized ...

copy it into the src/ext/gd/gc.c and recompile the lib.
i wish someone could do that so it's included in the "official" release

it's a really good function to scale down pictures to thumbnails.

/* {{{ proto int imagecopyresizedbicubic(int dst_im, int src_im, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h)
Copy and resize part of an image */
PHP_FUNCTION(imagecopyresizedbicubic)
{
zval **SIM, **DIM, **SX, **SY, **SW, **SH, **DX, **DY, **DW, **DH;
gdImagePtr im_dst, im_src;
int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
int i, j;
int r, g, b, c1,c2,c3,c4,color;
float sX, sY;
float scaleX,scaleY,scaleX2,scaleY2;
GDLS_FETCH();

if (ZEND_NUM_ARGS() != 10 ||
zend_get_parameters_ex(10, &DIM, &SIM, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE)
{
WRONG_PARAM_COUNT;
}

ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, DIM, -1, "Image", le_gd);
ZEND_FETCH_RESOURCE(im_src, gdImagePtr, SIM, -1, "Image", le_gd);

convert_to_long_ex(SX);
convert_to_long_ex(SY);
convert_to_long_ex(SW);
convert_to_long_ex(SH);
convert_to_long_ex(DX);
convert_to_long_ex(DY);
convert_to_long_ex(DW);
convert_to_long_ex(DH);

srcX = Z_LVAL_PP(SX);
srcY = Z_LVAL_PP(SY);
srcH = Z_LVAL_PP(SH);
srcW = Z_LVAL_PP(SW);
dstX = Z_LVAL_PP(DX);
dstY = Z_LVAL_PP(DY);
dstH = Z_LVAL_PP(DH);
dstW = Z_LVAL_PP(DW);

// kopiera paletten

for( i=0; i<256; i++ ) {
gdImageColorAllocate( im_dst, im_src->red[i], im_src->green[i], im_src->blue[i] );
};

// gdImageCopyResized(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);

scaleX = (float)(srcW-1) / (float)dstW;
scaleY = (float)(srcH-1) / (float)dstH;

scaleX2 = scaleX/2.0f;
scaleY2 = scaleY/2.0f;

for( j=0; j<dstH; j++ ) {
for( i=0; i<dstW; i++ ) {

sX = (float)i * scaleX;
sY = (float)j * scaleY;

c1 = gdImageGetPixel(im_src, (int)(sX), (int)(sY+scaleY2) );
c2 = gdImageGetPixel(im_src, (int)(sX), (int)(sY) );
c3 = gdImageGetPixel(im_src, (int)(sX+scaleX2), (int)(sY+scaleY2) );
c4 = gdImageGetPixel(im_src, (int)(sX+scaleX2), (int)(sY) );

r = (im_src->red[c1] + im_src->red[c2] + im_src->red[c3] + im_src->red[c4]) / 4;
g = (im_src->green[c1] + im_src->green[c2] + im_src->green[c3] + im_src->green[c4]) / 4;
b = (im_src->blue[c1] + im_src->blue[c2] + im_src->blue[c3] + im_src->blue[c4]) / 4;

color=gdImageColorClosest(im_dst,r,g,b);
gdImageSetPixel( im_dst, (i+dstX),(j+dstY), color );

};
};

RETURN_TRUE;
}

#endif /* HAVE_LIBGD */

#----------------------------------------------------------
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] hehe wait theres more
#----------------------------------------------------------

One note on getting the ImageCopyResizedBicubic to work. Just adding the code block to gd.c won't do it. You have to do two other things as well:

In the first 150 or so lines of gd.c, you will find a section that starts like this:

function_entry gd_functions[]

Find the line that starts PHP_FE(imagecopyresized , copy this line, and paste a new copy below the original. Change the &quot;imagecopyresized&quot; to &quot;imagecopyresizedbicubic&quot; in the new copy.

Also, you will need to edit php_gd.h as follows:

Find the line that says:
PHP_FUNCTION(imagecopyresized);
(it was line 87 for me)
and add a line below it that says:
PHP_FUNCTION(imagecopyresizedbicubic);

Once you've made these changes, recompile and reinstall php, and you should be good to go. Scaling works much better with this function.

#---------------------------------------------------------
and just for kicks
#---------------------------------------------------------

also cut and pasted

Regarding imagecopyresizedbicubic. I have made a
tar.gz of the necessary changes, you can download it
from bicubic.ckool.org. Put it into your php install directory in /ext to be exact. Rename the gd directory
gd.old for ex. Untar the gd.tar.gz. Remake everything, make clean before...

And thats it...

James

This looks very promising for the future of gd created thumbnails!! should help us all.....

<!-- NO_AUTO_LINK -->

Terra
05-14-2001, 02:54 AM
I must decline this inclusion into our PHP core as it locks me into a particular version...

Scenario:
I add this
people start using it
another major memory leak is found in 4.0.5
4.0.6 is released to fix this
custom hack doesn't mesh in due to code base changes
Either:
a) live with the memory leak crashes due to dependence on thumbnail function
or
b) kick out the thumbnail function to facilitate the core upgrade
or
c) everything meshes in correctly

This is a: lose-lose-win proposition with a 66% chance of failure.

The best route to go is submit this to the maintainer of the gd module so that it makes it into the official code base...[nbsp][nbsp]At least then any changes will need to be folded in to later releases, it's been deemed as worthy for inclusion, and has been blessed into the PHP core for everyone to enjoy...

--
Terra
--Proper channels--
FutureQuest