- first try at making it compatible with PHP5

pp [2004-12-22 00:54:56]
- first try at making it compatible with PHP5


git-svn-id: https://siedziba.pl:790/svn/repos/php-imlib/trunk@90 455248ca-bdda-0310-9134-f4ebb693071a
Filename
filters/php_colormatrix.c
php_imlib.c
php_imlib.h
diff --git a/filters/php_colormatrix.c b/filters/php_colormatrix.c
index d1fe377..bdb8adf 100644
--- a/filters/php_colormatrix.c
+++ b/filters/php_colormatrix.c
@@ -271,7 +271,7 @@ static void _php_colormatrix_usermat(double mat[16], zval** data)

 	convert_to_array_ex(data);

-	mat_ht=HASH_OF(*data);
+	mat_ht=(*data)->value.ht;
 	zend_hash_internal_pointer_reset_ex(mat_ht,&pos);
 	while (pos)
 	{
diff --git a/php_imlib.c b/php_imlib.c
index 97e86d4..14a7c46 100644
--- a/php_imlib.c
+++ b/php_imlib.c
@@ -20,6 +20,7 @@
 #include "php_ini.h"
 #include "php_imlib.h"
 #include "php_globals.h"
+#include "php_open_temporary_file.h"

 /* #define IMLIB_DEBUG */

@@ -35,10 +36,33 @@ int gd_imlib_id;
 zend_imlib_globals imlib_globals;
 #endif

-static unsigned char third_arg_force_ref[] = { 3, BYREF_NONE, BYREF_NONE, BYREF_FORCE };
-static unsigned char third_and_fourth_arg_force_ref[] = { 4, BYREF_NONE, BYREF_NONE, BYREF_FORCE, BYREF_FORCE };
-static unsigned char second_through_fifth_arg_force_ref[] = { 5, BYREF_NONE, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE };
-static unsigned char third_through_sixth_arg_force_ref[] = { 6, BYREF_NONE, BYREF_NONE, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE };
+static
+        ZEND_BEGIN_ARG_INFO(third_and_fourth_arg_force_ref, 0)
+                ZEND_ARG_PASS_INFO(0)
+                ZEND_ARG_PASS_INFO(0)
+                ZEND_ARG_PASS_INFO(1)
+                ZEND_ARG_PASS_INFO(1)
+        ZEND_END_ARG_INFO();
+
+static
+        ZEND_BEGIN_ARG_INFO(second_through_fifth_arg_force_ref, 0)
+                ZEND_ARG_PASS_INFO(0)
+                ZEND_ARG_PASS_INFO(1)
+                ZEND_ARG_PASS_INFO(1)
+                ZEND_ARG_PASS_INFO(1)
+                ZEND_ARG_PASS_INFO(1)
+        ZEND_END_ARG_INFO();
+
+static
+        ZEND_BEGIN_ARG_INFO(third_through_sixth_arg_force_ref, 0)
+                ZEND_ARG_PASS_INFO(0)
+                ZEND_ARG_PASS_INFO(0)
+                ZEND_ARG_PASS_INFO(1)
+                ZEND_ARG_PASS_INFO(1)
+                ZEND_ARG_PASS_INFO(1)
+                ZEND_ARG_PASS_INFO(1)
+        ZEND_END_ARG_INFO();
+
 static int le_imlib_cr;
 static int le_imlib_font;
 static int le_imlib_img;
@@ -138,13 +162,26 @@ ZEND_DECLARE_MODULE_GLOBALS(imlib)

 PHP_INI_BEGIN()
 STD_PHP_INI_ENTRY("imlib.font_path","/usr/local/share/fonts",PHP_INI_SYSTEM|PHP_INI_PERDIR,OnUpdateString,font_path,zend_imlib_globals,imlib_globals)
-STD_PHP_INI_ENTRY("imlib.min_cache_size","0",PHP_INI_SYSTEM,OnUpdateInt,min_cache_size,zend_imlib_globals,imlib_globals)
-STD_PHP_INI_ENTRY("imlib.max_cache_size","4194304",PHP_INI_SYSTEM,OnUpdateInt,max_cache_size,zend_imlib_globals,imlib_globals)
-STD_PHP_INI_ENTRY("imlib.cache_size","0",PHP_INI_SYSTEM|PHP_INI_PERDIR,OnUpdateInt,cache_size,zend_imlib_globals,imlib_globals)
+STD_PHP_INI_ENTRY("imlib.min_cache_size","0",PHP_INI_SYSTEM,OnUpdateLong,min_cache_size,zend_imlib_globals,imlib_globals)
+STD_PHP_INI_ENTRY("imlib.max_cache_size","4194304",PHP_INI_SYSTEM,OnUpdateLong,max_cache_size,zend_imlib_globals,imlib_globals)
+STD_PHP_INI_ENTRY("imlib.cache_size","0",PHP_INI_SYSTEM|PHP_INI_PERDIR,OnUpdateLong,cache_size,zend_imlib_globals,imlib_globals)
 PHP_INI_ENTRY("imlib.font_cache_size","524288",PHP_INI_SYSTEM,OnUpdateFontCacheSize)
 STD_PHP_INI_ENTRY("imlib.filter_path",PHP_IMLIB_FILTER_PATH,PHP_INI_SYSTEM,OnUpdateString,filter_path,zend_imlib_globals,imlib_globals)
 PHP_INI_END()

+/* {{{ php_imlib_init_globals
+ */
+static void php_imlib_init_globals(zend_imlib_globals *imlib_globals)
+{
+        imlib_globals->font_path = NULL;
+	imlib_globals->min_cache_size = 0;
+	imlib_globals->max_cache_size = 4194304;
+	imlib_globals->cache_size = 0;
+	imlib_globals->filter_path = NULL;
+	imlib_globals->saved_path = NULL;
+	imlib_globals->filters = NULL;
+}
+
 zend_module_entry imlib_module_entry = {
 	STANDARD_MODULE_HEADER,
 	"imlib",
@@ -209,9 +246,8 @@ static void _php_imlib_free_poly(ImlibPolygon poly)
    imlib_polygon_free(poly);
 }

-void _php_imlib_set_cache_size(int size)
+void _php_imlib_set_cache_size(int size TSRMLS_DC)
 {
-   IMLIBLS_FETCH();
    if (size>IMLIBG(max_cache_size)) {
       size=IMLIBG(max_cache_size);
    }
@@ -295,11 +331,10 @@ void _php_imlib_unload_filter(struct php_imlib_filter* ptr)
 	free(ptr);
 }

-void _php_imlib_unload_filters()
+void _php_imlib_unload_filters(TSRMLS_D)
 {
 	struct php_imlib_filter* f_ptr;

-	IMLIBLS_FETCH();
 	f_ptr=IMLIBG(filters);
 	while (f_ptr!=NULL)
 	{
@@ -313,7 +348,7 @@ void _php_imlib_unload_filters()
 	lt_dlexit();
 }

-void _php_imlib_load_filters()
+void _php_imlib_load_filters(TSRMLS_D)
 {
 	struct php_imlib_filter* f_ptr;
 	char* path;
@@ -323,7 +358,6 @@ void _php_imlib_load_filters()
 	int names_size,names_index,i;
 	int ltinitrv=0;

-	IMLIBLS_FETCH();
 	f_ptr=IMLIBG(filters);
 	if (f_ptr!=NULL) return;
 #ifdef IMLIB_DEBUG
@@ -408,7 +442,7 @@ error:
 #ifdef IMLIB_DEBUG
 	ID_PRINTF("Error loading filters!");
 #endif
-	_php_imlib_unload_filters();
+	_php_imlib_unload_filters(TSRMLS_C);
 cleanup:
 	if (names!=NULL)
 	{
@@ -421,13 +455,12 @@ cleanup:
 	if (dirp!=NULL) closedir(dirp);
 }

-struct php_imlib_filter* _php_imlib_find_filter(char *filter_name)
+struct php_imlib_filter* _php_imlib_find_filter(char *filter_name TSRMLS_DC)
 {
 	struct php_imlib_filter* f_ptr;
 	int i;

-	_php_imlib_load_filters();
-	IMLIBLS_FETCH();
+	_php_imlib_load_filters(TSRMLS_C);
 	f_ptr=IMLIBG(filters);
 	while (f_ptr!=NULL)
 	{
@@ -552,10 +585,12 @@ void _php_imlib_free_ps_enc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
 ZEND_GET_MODULE(imlib)
 #endif

-PHP_MINIT_FUNCTION(imlib)
+/* {{{ PHP_MINIT_FUNCTION
+ */
+ZEND_MODULE_STARTUP_D(imlib)
 {
-	IMLIBLS_FETCH();
-	IMLIBG(filters)=NULL;
+	ZEND_INIT_MODULE_GLOBALS(imlib, php_imlib_init_globals, NULL);
+
 	le_imlib_cr = register_list_destructors(_php_imlib_free_cr,NULL);
 	le_imlib_font = register_list_destructors(_php_imlib_free_font,NULL);
 	le_imlib_img = register_list_destructors(_php_imlib_free_img,NULL);
@@ -638,7 +673,7 @@ PHP_MINIT_FUNCTION(imlib)

 PHP_MSHUTDOWN_FUNCTION(imlib)
 {
-	_php_imlib_unload_filters();
+	_php_imlib_unload_filters(TSRMLS_C);

 	UNREGISTER_INI_ENTRIES();

@@ -650,10 +685,9 @@ PHP_RINIT_FUNCTION(imlib)
 	char **font_list;
 	char *pathbuf, *ptr, *end;
 	int i, font_count;
-	IMLIBLS_FETCH();

 	/* Set default cache size */
-        _php_imlib_set_cache_size(IMLIBG(cache_size));
+        _php_imlib_set_cache_size(IMLIBG(cache_size) TSRMLS_CC);
 	font_list = imlib_list_font_path(&font_count);
 	if (!font_count) {
 		IMLIBG(saved_path) = NULL;
@@ -697,10 +731,9 @@ PHP_RSHUTDOWN_FUNCTION(imlib)
 {
 	char **font_list;
 	int i, font_count;
-	IMLIBLS_FETCH();

 	/* Restore cache size */
-        _php_imlib_set_cache_size(0);
+        _php_imlib_set_cache_size(0 TSRMLS_CC);
 	/* Retrieve the current path and clear it out. */
 	font_list = imlib_list_font_path(&font_count);
 	for (i = 0; i < font_count; i++) {
@@ -725,8 +758,7 @@ PHP_MINFO_FUNCTION(imlib)
         struct php_imlib_filter* f_ptr;
         int i;

-        _php_imlib_load_filters();
-        IMLIBLS_FETCH();
+        _php_imlib_load_filters(TSRMLS_C);
         f_ptr=IMLIBG(filters);

 	DISPLAY_INI_ENTRIES();
@@ -807,7 +839,7 @@ static void _php_convert_four_longs(zval **zone, zval **ztwo, zval **zthree,
 }

 static int _php_handle_cliprect_array(zval **dbox, char *func_name,
-                                      int *x, int *y, int *w, int *h)
+                                      int *x, int *y, int *w, int *h TSRMLS_DC)
 {
    zval **element, ***box_coords;
    int i,arrcount;
@@ -920,7 +952,6 @@ static void _php_imlib_draw_something(INTERNAL_FUNCTION_PARAMETERS, void (*func)
    zval **img, **d1, **d2, **d3, **d4, **dr, **dg, **db, **da, **dbox;
    int x,y,w,h,r,g,b,a,cx,cy,cw,ch,argc;
    Imlib_Image im;
-   IMLIBLS_FETCH();

    argc = ZEND_NUM_ARGS();
    if (argc < 9 || argc > 10 || zend_get_parameters_ex(argc, &img, &d1, &d2, &d3, &d4, &dr, &dg, &db, &da, &dbox) == FAILURE) {
@@ -940,7 +971,7 @@ static void _php_imlib_draw_something(INTERNAL_FUNCTION_PARAMETERS, void (*func)

    if (argc > 9)
    {
-      if (!_php_handle_cliprect_array(dbox, func_name, &cx,&cy,&cw,&ch))
+      if (!_php_handle_cliprect_array(dbox, func_name, &cx,&cy,&cw,&ch TSRMLS_CC))
          RETURN_FALSE;
       imlib_context_set_cliprect(cx,cy,cw,ch);
    }
@@ -954,7 +985,6 @@ static void _php_imlib_single_arg(INTERNAL_FUNCTION_PARAMETERS, void (*func)())
 {
    zval **img;
    Imlib_Image im;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &img) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -981,7 +1011,6 @@ PHP_FUNCTION(imlib_add_color_to_color_range)
    zval **crange, **cx, **cr, **cg, **cb, **ca;
    int x,r,g,b,a;
    Imlib_Color_Range range;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 6 || zend_get_parameters_ex(6, &crange, &cx, &cr, &cg, &cb, &ca) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1008,7 +1037,6 @@ PHP_FUNCTION(imlib_blend_image_onto_image)
    Imlib_Image dst,src;
    int sx,sy,sw,sh,dx,dy,dw,dh;
    int calias, calpha, cblend, cdither;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 14 || zend_get_parameters_ex(14, &dstimg, &srcimg, &malpha, &srcx, &srcy, &srcw, &srch, &dstx, &dsty, &dstw, &dsth, &dither, &blend, &alias) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1045,7 +1073,6 @@ PHP_FUNCTION(imlib_clone_image)
 {
    zval **img;
    Imlib_Image src,dst;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &img) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1066,7 +1093,6 @@ PHP_FUNCTION(imlib_clone_image)
 PHP_FUNCTION(imlib_create_color_range)
 {
    Imlib_Color_Range cr;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 0) {
       WRONG_PARAM_COUNT;
@@ -1084,7 +1110,6 @@ PHP_FUNCTION(imlib_create_color_range)
 PHP_FUNCTION(imlib_create_color_modifier)
 {
    PHP_Imlib_Color_Modifier cm;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 0) {
       WRONG_PARAM_COUNT;
@@ -1108,7 +1133,6 @@ PHP_FUNCTION(imlib_create_cropped_image)
    zval **img, **srcx, **srcy, **srcw, **srch;
    int sx,sy,sw,sh;
    Imlib_Image src,dst;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 5 || zend_get_parameters_ex(5, &img, &srcx, &srcy, &srcw, &srch) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1133,7 +1157,6 @@ PHP_FUNCTION(imlib_create_cropped_scaled_image)
    zval **img, **srcx, **srcy, **srcw, **srch, **dstw, **dsth;
    int sx,sy,sw,sh,dw,dh;
    Imlib_Image src,dst;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 7 || zend_get_parameters_ex(7, &img, &srcx, &srcy, &srcw,
                                                       &srch, &dstw, &dsth) == FAILURE) {
@@ -1163,7 +1186,6 @@ PHP_FUNCTION(imlib_create_image)
    zval **nx, **ny;
    Imlib_Image im;
    int x,y;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &nx, &ny) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1190,11 +1212,13 @@ PHP_FUNCTION(imlib_create_image)
    Create a rotated copy of an image.  If radians is specified, degrees will be ignored. */
 PHP_FUNCTION(imlib_create_rotated_image)
 {
+
+  /* Contributed by Gareth Ardron */
+
    zval **srcimg, **cangle, **crads;
    double angle, radians, pi = 3.141592654;
    int argc;
    Imlib_Image src, dst;
-   IMLIBLS_FETCH();

    argc = ZEND_NUM_ARGS();
    if (argc < 2 || argc > 3 || zend_get_parameters_ex(argc, &srcimg, &cangle, &crads) == FAILURE) {
@@ -1230,7 +1254,6 @@ PHP_FUNCTION(imlib_create_scaled_image)
    zval **img, **dstw, **dsth;
    int sw,sh,dw,dh;
    Imlib_Image src,dst;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &img, &dstw, &dsth) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1268,12 +1291,12 @@ PHP_FUNCTION(imlib_create_scaled_image)
    Output an image at an optional quality setting */
 PHP_FUNCTION(imlib_dump_image)
 {
-   int argc, q, fd, retval, output;
+   int argc, q, retval;
+   FILE *tmp;
    char *tmpfile;
    zval **img, **quality, **err;
    Imlib_Image im;
    Imlib_Load_Error im_err;
-   IMLIBLS_FETCH();

    argc = ZEND_NUM_ARGS();
    if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &img, &err, &quality) == FAILURE) {
@@ -1287,20 +1310,9 @@ PHP_FUNCTION(imlib_dump_image)

    ZEND_FETCH_RESOURCE(im, Imlib_Image, img, -1, "Imlib Image", le_imlib_img);

-   tmpfile = estrdup("/tmp/phpimlib.XXXXXX");
-
-   if ((fd = mkstemp(tmpfile)) < 0) {
-      php_error(E_WARNING, "%s: unable to open temporary file", get_active_function_name(TSRMLS_C));
-      efree(tmpfile);
-      RETURN_FALSE;
-   }
-
-   /* TODO: notify in case of errors? */
-   retval = fchmod(fd, S_IRUSR|S_IWUSR);
-   if (retval != 0) {
-      php_error(E_WARNING, "%s: could not change permissions on temporary file", get_active_function_name(TSRMLS_C));
-      close(fd);
-      efree(tmpfile);
+   tmp = php_open_temporary_file("", "", &tmpfile TSRMLS_CC);
+   if (tmp == NULL) {
+      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open temporary file");
       RETURN_FALSE;
    }

@@ -1320,43 +1332,38 @@ PHP_FUNCTION(imlib_dump_image)

    if (im_err)
    {
-      close(fd);
-      unlink(tmpfile);
-      efree(tmpfile);
       if (argc > 1)
       {
          ZVAL_LONG(*err,im_err);
       }
       _php_handle_imlib_error(INTERNAL_FUNCTION_PARAM_PASSTHRU,
                               im_err,tmpfile);
+      fclose(tmp);
+      VCWD_UNLINK((const char *)tmpfile); /* make sure that the temporary file is removed */
+      efree(tmpfile);
       RETURN_FALSE;
    }
-
-   /* fseek or something, then read the file and dump it out. */
-   lseek(fd, 0, SEEK_SET);
-
-   output = php_header();
-   if (output) {
+   else
+   {
       char buf[4096];

+      /* fseek or something, then read the file and dump it out. */
+      fseek(tmp, 0, SEEK_SET);
+
 #if APACHE && defined(CHARSET_EBCDIC)
       SLS_FETCH();
       /* This is a binary file already: avoid EBCDIC->ASCII conversion */
       ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
 #endif
-      while ((retval = read(fd, buf, sizeof(buf))) > 0) {
+      while ((retval = fread(buf, 1, sizeof(buf), tmp)) > 0) {
          php_write(buf, retval TSRMLS_CC);
       }
-   }

-   close(fd);
-   unlink(tmpfile);
-   efree(tmpfile);
+      fclose(tmp);
+      VCWD_UNLINK((const char *)tmpfile); /* make sure that the temporary file is removed */
+      efree(tmpfile);

-   if (output) {
       RETURN_TRUE;
-   } else {
-      RETURN_FALSE;
    }
 }
 /* }}} */
@@ -1368,7 +1375,6 @@ PHP_FUNCTION(imlib_free_color_range)
 {
    zval **fcr;
    Imlib_Color_Range cr;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &fcr) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1386,7 +1392,6 @@ PHP_FUNCTION(imlib_free_color_modifier)
 {
    zval **fcm;
    PHP_Imlib_Color_Modifier cm;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &fcm) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1403,7 +1408,6 @@ PHP_FUNCTION(imlib_free_font)
 {
    zval **font;
    PHP_Imlib_Font fn;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &font) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1421,7 +1425,6 @@ PHP_FUNCTION(imlib_free_image)
 {
    zval **img;
    Imlib_Image im;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &img) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1442,7 +1445,6 @@ PHP_FUNCTION(imlib_get_text_advance)
    const char *text = NULL;
    int horiz_adv, vert_adv, dir;
    Imlib_TTF_Encoding old_enc;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 5 || zend_get_parameters_ex(5, &font, &textstr, &thoriz_adv, &tvert_adv, &tdir) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1484,7 +1486,6 @@ PHP_FUNCTION(imlib_get_text_size)
    const char *text = NULL;
    int w,h,dir;
    Imlib_TTF_Encoding old_enc;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 5 || zend_get_parameters_ex(5, &font, &textstr, &tw, &th, &tdir) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1524,7 +1525,6 @@ PHP_FUNCTION(imlib_image_blur)
    zval **img, **radius;
    int r;
    Imlib_Image im;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &img, &radius) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1570,7 +1570,6 @@ PHP_FUNCTION(imlib_image_draw_polygon)
    Imlib_Image im;
    ImlibPolygon poly;
    int closed;
-   IMLIBLS_FETCH();

    argc = ZEND_NUM_ARGS();
    if (argc < 7 || argc > 8 || zend_get_parameters_ex(argc, &img, &polygon, &pclosed, &pr, &pg, &pb, &pa, &dbox) == FAILURE) {
@@ -1590,7 +1589,7 @@ PHP_FUNCTION(imlib_image_draw_polygon)

    if (argc > 7)
    {
-      if (!_php_handle_cliprect_array(dbox, "imlib_image_draw_polygon", &cx, &cy, &cw, &ch))
+      if (!_php_handle_cliprect_array(dbox, "imlib_image_draw_polygon", &cx, &cy, &cw, &ch TSRMLS_CC))
          RETURN_FALSE;
       imlib_context_set_cliprect(cx,cy,cw,ch);
    }
@@ -1620,7 +1619,6 @@ PHP_FUNCTION(imlib_image_fill_color_range_rectangle)
    double angle;
    Imlib_Image im;
    Imlib_Color_Range cr;
-   IMLIBLS_FETCH();

    argc = ZEND_NUM_ARGS();
    if (argc < 7 || argc > 8 || zend_get_parameters_ex(argc, &fim, &fcr, &fx, &fy, &fwidth, &fheight, &fangle, &fbox) == FAILURE) {
@@ -1639,7 +1637,7 @@ PHP_FUNCTION(imlib_image_fill_color_range_rectangle)

    if (argc > 7)
    {
-      if (!_php_handle_cliprect_array(fbox, "imlib_image_fill_color_range_rectangle", &cx,&cy,&cw,&ch))
+      if (!_php_handle_cliprect_array(fbox, "imlib_image_fill_color_range_rectangle", &cx,&cy,&cw,&ch TSRMLS_CC))
          RETURN_FALSE;
       imlib_context_set_cliprect(cx,cy,cw,ch);
    }
@@ -1669,7 +1667,6 @@ PHP_FUNCTION(imlib_image_fill_polygon)
    int r,g,b,a,cx,cy,cw,ch,argc;
    Imlib_Image im;
    ImlibPolygon poly;
-   IMLIBLS_FETCH();

    argc = ZEND_NUM_ARGS();
    if (argc < 6 || argc > 7 || zend_get_parameters_ex(argc, &img, &polygon, &pr, &pg, &pb, &pa, &dbox) == FAILURE) {
@@ -1686,7 +1683,7 @@ PHP_FUNCTION(imlib_image_fill_polygon)

    if (argc > 6)
    {
-      if (!_php_handle_cliprect_array(dbox, "imlib_image_fill_polygon", &cx, &cy, &cw, &ch))
+      if (!_php_handle_cliprect_array(dbox, "imlib_image_fill_polygon", &cx, &cy, &cw, &ch TSRMLS_CC))
          RETURN_FALSE;
       imlib_context_set_cliprect(cx,cy,cw,ch);
    }
@@ -1745,7 +1742,6 @@ PHP_FUNCTION(imlib_image_orientate)
    zval **img, **stepping;
    int r;
    Imlib_Image im;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &img, &stepping) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1767,7 +1763,6 @@ PHP_FUNCTION(imlib_image_format)
    zval **img;
    Imlib_Image im;
    char *name;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &img) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1792,7 +1787,6 @@ PHP_FUNCTION(imlib_image_get_filename)
    zval **img;
    Imlib_Image im;
    const char *name = NULL;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &img) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1817,7 +1811,6 @@ PHP_FUNCTION(imlib_image_get_height)
    zval **img;
    Imlib_Image im;
    int height;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &img) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1840,7 +1833,6 @@ PHP_FUNCTION(imlib_image_get_width)
    zval **img;
    Imlib_Image im;
    int width;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &img) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1932,7 +1924,6 @@ PHP_FUNCTION(imlib_image_set_format)
    zval **img, **zformat;
    Imlib_Image im;
    char *format;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &img, &zformat) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -1961,7 +1952,6 @@ PHP_FUNCTION(imlib_image_sharpen)
    zval **img, **radius;
    int r;
    Imlib_Image im;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &img, &radius) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -2039,8 +2029,6 @@ PHP_FUNCTION(imlib_load_font)
    PHP_Imlib_Font fn;
    int argc;

-   IMLIBLS_FETCH();
-
    argc=ZEND_NUM_ARGS();
    if (argc < 1 || argc > 2 || zend_get_parameters_ex(argc, &fontname, &encoding) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -2089,7 +2077,6 @@ PHP_FUNCTION(imlib_load_image)
    int argc;
    Imlib_Image im;
    Imlib_Load_Error im_err;
-   IMLIBLS_FETCH();

    argc = ZEND_NUM_ARGS();
    if (argc < 1 || argc > 2 || zend_get_parameters_ex(argc, &img, &err) == FAILURE) {
@@ -2128,7 +2115,6 @@ PHP_FUNCTION(imlib_polygon_add_point)
    zval **polygon, **px, **py;
    int x,y;
    ImlibPolygon poly;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &polygon, &px, &py) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -2153,7 +2139,6 @@ PHP_FUNCTION(imlib_polygon_contains_point)
    zval **polygon, **px, **py;
    int x,y,ret;
    ImlibPolygon poly;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &polygon, &px, &py) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -2186,7 +2171,6 @@ PHP_FUNCTION(imlib_polygon_free)
 {
    zval **polygon;
    ImlibPolygon poly;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &polygon) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -2205,7 +2189,6 @@ PHP_FUNCTION(imlib_polygon_get_bounds)
    zval **polygon, **px1, **py1, **px2, **py2;
    int x1,y1,x2,y2;
    ImlibPolygon poly;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 5 || zend_get_parameters_ex(5, &polygon, &px1, &py1, &px2, &py2) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -2233,7 +2216,6 @@ PHP_FUNCTION(imlib_polygon_get_bounds)
 PHP_FUNCTION(imlib_polygon_new)
 {
    ImlibPolygon poly;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 0) {
       WRONG_PARAM_COUNT;
@@ -2253,7 +2235,6 @@ PHP_FUNCTION(imlib_save_image)
    Imlib_Image im;
    Imlib_Load_Error im_err;
    int argc, q;
-   IMLIBLS_FETCH();

    argc = ZEND_NUM_ARGS();
    if (argc < 2 || argc > 4 || zend_get_parameters_ex(argc, &img, &name, &err, &quality) == FAILURE) {
@@ -2305,7 +2286,6 @@ PHP_FUNCTION(imlib_text_draw)
    char *text;
    int x,y,dir,r,g,b,a;
    Imlib_TTF_Encoding old_enc;
-   IMLIBLS_FETCH();

    if (ZEND_NUM_ARGS() != 10 || zend_get_parameters_ex(10, &img, &font, &tx, &ty, &textstr, &tdir, &tr, &tg, &tb, &ta) == FAILURE) {
       WRONG_PARAM_COUNT;
@@ -2357,7 +2337,7 @@ PHP_FUNCTION(imlib_set_cache_size)
    }
    convert_to_long_ex(tsize);
    size = Z_LVAL_PP(tsize);
-   _php_imlib_set_cache_size(size);
+   _php_imlib_set_cache_size(size TSRMLS_CC);
    RETURN_TRUE;
 }
 /* }}} */
@@ -2379,7 +2359,7 @@ PHP_FUNCTION(imlib_apply_filter)
    convert_to_string_ex(tfilter_name);
    filter_name = Z_STRVAL_PP(tfilter_name);

-   filter = _php_imlib_find_filter(filter_name);
+   filter = _php_imlib_find_filter(filter_name TSRMLS_CC);

    if (!filter) {
       php_error(E_WARNING, "%s - Filter not found.", filter_name);
diff --git a/php_imlib.h b/php_imlib.h
index e1aad2c..0d188ed 100644
--- a/php_imlib.h
+++ b/php_imlib.h
@@ -35,6 +35,10 @@

 #if HAVE_IMLIB

+#ifdef ZTS
+#include "TSRM.h"
+#endif
+
 extern zend_module_entry imlib_module_entry;
 #define phpext_imlib_ptr &imlib_module_entry

@@ -177,18 +181,16 @@ ZEND_BEGIN_MODULE_GLOBALS(imlib)
 	char *font_path;
 	char **saved_path;
 	char *filter_path;
-	int cache_size;
-	int min_cache_size;
-	int max_cache_size;
+	long cache_size;
+	long min_cache_size;
+	long max_cache_size;
         struct php_imlib_filter *filters;
 ZEND_END_MODULE_GLOBALS(imlib)

 #ifdef ZTS
-#define IMLIBG(v) (imlib_globals->v)
-#define IMLIBLS_FETCH() zend_imlib_globals *imlib_globals = ts_resource(gd_imlib_id)
+# define IMLIBG(v) TSRMG(imlib_globals_id, zend_imlib_globals *, v)
 #else
-#define IMLIBG(v) (imlib_globals.v)
-#define IMLIBLS_FETCH()
+# define IMLIBG(v) (imlib_globals.v)
 #endif

 #else
ViewGit