note 11925 added to function.imagecopyresized.php

From: Date: Tue, 13 Mar 2001 17:33:10 +0000
Subject: note 11925 added to function.imagecopyresized.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-12343@lists.php.net to get a copy of this message
re: imagecopyresizedbicubic Yes, this function is really great! The thumbnails are much, much better. However the example posted above is for PHP4, and if you need it for PHP3 (and you really should use it), you have to do the following: (notice: I changed the name of this function to imagecopyresizedbb - it's just shorter to write this way) Edit the file functions/gd.c. Find the function named "function_entry gd_functions[]" and add this function definition there: (I suggest below the orginal imagecopyresized - around line 98) {"imagecopyresizedbb", php3_imagecopyresizedbb, NULL}, Then add this function (I suggest below the orginal imagecopyresized function - around line 2030) /* {{{ proto int imagecopyresizedbb(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 (bicubic) part of an image */ void php3_imagecopyresizedbb(INTERNAL_FUNCTION_PARAMETERS) { pval *SIM, *DIM, *SX, *SY, *SW, *SH, *DX, *DY, *DW, *DH; gdImagePtr im_dst; gdImagePtr im_src; int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX; int ind_type; int i, j; int r, g, b, c1,c2,c3,c4,color; float sX, sY; float scaleX,scaleY,scaleX2,scaleY2; GD_TLS_VARS; if (ARG_COUNT(ht) != 10 || getParameters(ht, 10, &DIM, &SIM, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long(SIM); convert_to_long(DIM); convert_to_long(SX); convert_to_long(SY); convert_to_long(SW); convert_to_long(SH); convert_to_long(DX); convert_to_long(DY); convert_to_long(DW); convert_to_long(DH); srcX = SX->value.lval; srcY = SY->value.lval; srcH = SH->value.lval; srcW = SW->value.lval; dstX = DX->value.lval; dstY = DY->value.lval; dstH = DH->value.lval; dstW = DW->value.lval; im_src = php3_list_find(SIM->value.lval, &ind_type); if (!im_src || ind_type != GD_GLOBAL(le_gd)) { php3_error(E_WARNING, "Unable to find image pointer"); RETURN_FALSE; } im_dst = php3_list_find(DIM->value.lval, &ind_type); if (!im_dst || ind_type != GD_GLOBAL(le_gd)) { php3_error(E_WARNING, "Unable to find image pointer"); RETURN_FALSE; } for( i=0; i<256; i++ ) { gdImageColorAllocate( im_dst, im_src->red[i], im_src->green[i], im_src->blue[i] ); }; 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; } /* }}} */ Then add this line in functions/php3_gd.h (I suggest below the orginal imagecopyresized function - around line 74) extern void php3_imagecopyresizedbb(INTERNAL_FUNCTION_PARAMETERS); Then recompile PHP and that's it! Later in PHP script just use imagecopyresizedbb instead of standard imagecopyresized function and your thumbnails will become smooth and shiny. http://www.php.net/manual/en/function.imagecopyresized.php

« previous php.notes (#12343) next »