Quick and dirty PHP7 port.

Piotr Pawłow [2016-09-08 21:05:41]
Quick and dirty PHP7 port.

Using compatibility shims and macros. Compiles both on PHP5 and 7.
Done in one afternoon without much testing, so use at your own risk!
Filename
filters/php_bumpmap.c
filters/php_colormatrix.c
filters/php_colormod.c
filters/php_hsbcolor.c
filters/php_testfilter.c
php_imlib.c
php_imlib.h
diff --git a/filters/php_bumpmap.c b/filters/php_bumpmap.c
index 6910f82..24e548d 100644
--- a/filters/php_bumpmap.c
+++ b/filters/php_bumpmap.c
@@ -44,7 +44,9 @@ _php_bumpmap_exec(char* filter, HashTable* params,int index)
 	int *paramtypes;
 	char *script;
 	Imlib_Image im=NULL;
-	HashPosition pos;
+	ulong num_index;
+	zend_string *str_index;
+	imlib_zval *data;

 	paramnames=allpnames[index];
 	paramtypes=allptypes[index];
@@ -52,36 +54,28 @@ _php_bumpmap_exec(char* filter, HashTable* params,int index)
 	script=estrdup(filter);
 	script=_php_bumpmap_stradd(script,"(");

-	zend_hash_internal_pointer_reset_ex(params,&pos);
-	while (pos)
-	{
-		char *str_index;
-		ulong num_index;
-		int retval;
-
-		retval=zend_hash_get_current_key_ex(params,&str_index,NULL,&num_index,0,&pos);
-		if (retval==HASH_KEY_IS_STRING)
+	ZEND_HASH_FOREACH_KEY_VAL(params, num_index, str_index, data)
+        {
+		if (str_index)
 		{
 			int match=0;
 			int index=0;
 			while (paramnames[index])
 			{
-				if (strcmp(str_index,paramnames[index])==0)
+				if (strcmp(STR_VAL(str_index),paramnames[index])==0)
 				{
 					char val[32];
-					zval **data;
-					zend_hash_get_current_data_ex(params,(void**)&data,&pos);

 					switch(paramtypes[index])
 					{
 					    case 'I':
-						    im=(Imlib_Image)_php_imlib_get_image(data); /* Why is cast to Imlib_Image needed? */
+						    im=(Imlib_Image)_php_imlib_get_image(Z_RES_P(data)); /* Why is cast to Imlib_Image needed? */
 						    script=_php_bumpmap_stradd(script,"map=[]");
 						    break;
 					    case 'i':
 						    convert_to_long_ex(data);
 						    sprintf(val,"%d",(int)Z_LVAL_PP(data));
-				    		    script=_php_bumpmap_stradd(script,str_index);
+				    		    script=_php_bumpmap_stradd(script,STR_VAL(str_index));
 						    script=_php_bumpmap_stradd(script,"=");
 						    script=_php_bumpmap_stradd(script,val);
 						    break;
@@ -97,8 +91,7 @@ _php_bumpmap_exec(char* filter, HashTable* params,int index)
 				php_error(E_NOTICE,"Unknown argument %s ignored",str_index);
 			}
 		}
-		zend_hash_move_forward_ex(params,&pos);
-	}
+	} ZEND_HASH_FOREACH_END();

 	if (script[strlen(script)-1]==',') script[strlen(script)-1]='\0';
 	script=_php_bumpmap_stradd(script,");");
@@ -121,6 +114,9 @@ php_filter_deinit()
 	return;
 }

+int
+_php_bumpmap_getfilterindex(char*);
+
 int
 php_filter_exec(Imlib_Image im, char *filter, HashTable *params)
 {
diff --git a/filters/php_colormatrix.c b/filters/php_colormatrix.c
index 1cd28b2..f5dab0e 100644
--- a/filters/php_colormatrix.c
+++ b/filters/php_colormatrix.c
@@ -264,42 +264,36 @@ static void _php_colormatrix_saturatemat(double mat[16], double sat)
     mat[15] = 1.0;
 }

-static void _php_colormatrix_usermat(double mat[16], zval** data)
+static void _php_colormatrix_usermat(double mat[16], imlib_zval* data)
 {
 	HashTable* mat_ht;
-	HashPosition pos;
-
-	convert_to_array_ex(data);
-
+	ulong num_index;
+	zend_string *str_index;
+	imlib_zval *value;
+
+	convert_to_array_ex(data);
+#if PHP_VERSION_ID >= 70000
+	mat_ht = HASH_OF(data);
+#else
 	mat_ht=(*data)->value.ht; // FIXME: should really use HASH_OF macro.
 	                          // Filter API change needed to supply TSRMLS to filters
-	zend_hash_internal_pointer_reset_ex(mat_ht,&pos);
-	while (pos)
+#endif
+	ZEND_HASH_FOREACH_KEY_VAL(mat_ht, num_index, str_index, value)
 	{
-	        char *str_index;
-        	ulong num_index;
-	        int retval;
-		zval **value;
-
-        	retval=zend_hash_get_current_key_ex(mat_ht,&str_index,NULL,&num_index,0,&pos);
-		if (retval==HASH_KEY_IS_LONG)
+		if ((num_index>=0)&&(num_index<16))
 		{
-			if ((num_index>=0)&&(num_index<16))
-			{
-				zend_hash_get_current_data_ex(mat_ht,(void**)&value,&pos);
-				convert_to_double_ex(value);
-				mat[num_index]=Z_DVAL_PP(value);
-			}
+			convert_to_double_ex(value);
+			mat[num_index]=Z_DVAL_PP(value);
 		}
-
-		zend_hash_move_forward_ex(mat_ht,&pos);
-	}
+	} ZEND_HASH_FOREACH_END();
 }

 static int
 _php_colormatrix_exec(Imlib_Image im, char* filter, HashTable* params)
 {
-      HashPosition pos;
+      ulong num_index;
+      zend_string *str_index;
+      imlib_zval *data;

       double mat[16];
       int x=0,y=0,w,h,imgw,imgh;
@@ -310,38 +304,28 @@ _php_colormatrix_exec(Imlib_Image im, char* filter, HashTable* params)
       w = imgw = imlib_image_get_width();
       h = imgh = imlib_image_get_height();

-      zend_hash_internal_pointer_reset_ex(params,&pos);
-      while (pos)
+      ZEND_HASH_FOREACH_KEY_VAL(params, num_index, str_index, data)
       {
-        char *str_index;
-        ulong num_index;
-        int retval;
-
-        retval=zend_hash_get_current_key_ex(params,&str_index,NULL,&num_index,0,&pos);
-        if (retval==HASH_KEY_IS_STRING)
+        if (str_index)
         {
-          zval **data;
-          zend_hash_get_current_data_ex(params,(void**)&data,&pos);
-
-          if (!strcmp(str_index,"x")) { convert_to_long_ex(data); x=Z_LVAL_PP(data); };
-          if (!strcmp(str_index,"y")) { convert_to_long_ex(data); y=Z_LVAL_PP(data); };
-          if (!strcmp(str_index,"w")) { convert_to_long_ex(data); w=Z_LVAL_PP(data); };
-          if (!strcmp(str_index,"h")) { convert_to_long_ex(data); h=Z_LVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"x")) { convert_to_long_ex(data); x=Z_LVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"y")) { convert_to_long_ex(data); y=Z_LVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"w")) { convert_to_long_ex(data); w=Z_LVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"h")) { convert_to_long_ex(data); h=Z_LVAL_PP(data); };
           if (!strcmp(filter,"cm_user"))
 	  {
-	          if (!strcmp(str_index,"matrix")) _php_colormatrix_usermat(mat,data);
+	          if (!strcmp(STR_VAL(str_index),"matrix")) _php_colormatrix_usermat(mat,data);
 	  }
           if (!strcmp(filter,"cm_saturation"))
 	  {
-	          if (!strcmp(str_index,"sat")) { convert_to_double_ex(data); _php_colormatrix_saturatemat(mat,Z_DVAL_PP(data)); };
+	          if (!strcmp(STR_VAL(str_index),"sat")) { convert_to_double_ex(data); _php_colormatrix_saturatemat(mat,Z_DVAL_PP(data)); };
 	  }
           if (!strcmp(filter,"cm_huerot"))
 	  {
-	          if (!strcmp(str_index,"angle")) { convert_to_double_ex(data); _php_colormatrix_huerotatemat(mat,Z_DVAL_PP(data)); };
+	          if (!strcmp(STR_VAL(str_index),"angle")) { convert_to_double_ex(data); _php_colormatrix_huerotatemat(mat,Z_DVAL_PP(data)); };
 	  }
         }
-      zend_hash_move_forward_ex(params,&pos);
-      }
+      } ZEND_HASH_FOREACH_END();

       if (x<0) x=0;
       if (y<0) y=0;
diff --git a/filters/php_colormod.c b/filters/php_colormod.c
index c240f1f..a93ae84 100644
--- a/filters/php_colormod.c
+++ b/filters/php_colormod.c
@@ -32,43 +32,37 @@ _php_colormod_exec(HashTable* params)
 	char *paramnames[]={"x","y","w","h","brightness","contrast","gamma","brightness_r","contrast_r","gamma_r","brightness_g","contrast_g","gamma_g","brightness_b","contrast_b","gamma_b","brightness_a","contrast_a","gamma_a","tint","tint_r","tint_g","tint_b","tint_a",0};
 	int paramtypes[]=  {'i','i','i','i','d',         'd',       'd',    'd',           'd',         'd',      'd',           'd',         'd',      'd',           'd',         'd',      'd',           'd',         'd',      'd',   'd',     'd',     'd',     'd'       };
 	char *script;
+	ulong num_index;
+	zend_string *str_index;
+	imlib_zval *data;

-	HashPosition pos;
 	script=estrdup("colormod(");

-	zend_hash_internal_pointer_reset_ex(params,&pos);
-	while (pos)
+	ZEND_HASH_FOREACH_KEY_VAL(params, num_index, str_index, data)
 	{
-		char *str_index;
-		ulong num_index;
-		int retval;
-
-		retval=zend_hash_get_current_key_ex(params,&str_index,NULL,&num_index,0,&pos);
-		if (retval==HASH_KEY_IS_STRING)
+		if (str_index)
 		{
 			int match=0;
 			int index=0;
 			while (paramnames[index])
 			{
-				if (strcmp(str_index,paramnames[index])==0)
+				if (strcmp(STR_VAL(str_index),paramnames[index])==0)
 				{
 					char val[32];
-					zval **data;
-					zend_hash_get_current_data_ex(params,(void**)&data,&pos);

 					switch(paramtypes[index])
 					{
 					    case 'i':
 						    convert_to_long_ex(data);
 						    sprintf(val,"%d",(int)Z_LVAL_PP(data));
-				    		    script=_php_colormod_stradd(script,str_index);
+				    		    script=_php_colormod_stradd(script,STR_VAL(str_index));
 						    script=_php_colormod_stradd(script,"=");
 						    script=_php_colormod_stradd(script,val);
 						    break;
 					    case 'd':
 						    convert_to_double_ex(data);
 						    sprintf(val,"%f",(double)Z_DVAL_PP(data));
-				    		    script=_php_colormod_stradd(script,str_index);
+				    		    script=_php_colormod_stradd(script,STR_VAL(str_index));
 						    script=_php_colormod_stradd(script,"=");
 						    script=_php_colormod_stradd(script,val);
 						    break;
@@ -84,8 +78,7 @@ _php_colormod_exec(HashTable* params)
 				php_error(E_NOTICE,"Unknown argument %s ignored",str_index);
 			}
 		}
-		zend_hash_move_forward_ex(params,&pos);
-	}
+	} ZEND_HASH_FOREACH_END();

 	if (script[strlen(script)-1]==',') script[strlen(script)-1]='\0';
 	script=_php_colormod_stradd(script,");");
diff --git a/filters/php_hsbcolor.c b/filters/php_hsbcolor.c
index 577afc2..288c7c1 100644
--- a/filters/php_hsbcolor.c
+++ b/filters/php_hsbcolor.c
@@ -101,7 +101,9 @@ static void _php_hsbcolor_hsb2rgb(double hue, double saturation, double brightne
 static int
 _php_hsbcolor_exec(Imlib_Image im, HashTable* params)
 {
-      HashPosition pos;
+      ulong num_index;
+      zend_string *str_index;
+      imlib_zval *data;

       double h_add = 0, s_add = 0, b_add = 0;
       double h_mul = 1.0, s_mul = 1.0, b_mul = 1.0;
@@ -111,33 +113,23 @@ _php_hsbcolor_exec(Imlib_Image im, HashTable* params)
       w = imgw = imlib_image_get_width();
       h = imgh = imlib_image_get_height();

-      zend_hash_internal_pointer_reset_ex(params,&pos);
-      while (pos)
+      ZEND_HASH_FOREACH_KEY_VAL(params, num_index, str_index, data)
       {
-        char *str_index;
-        ulong num_index;
-        int retval;
-
-        retval=zend_hash_get_current_key_ex(params,&str_index,NULL,&num_index,0,&pos);
-        if (retval==HASH_KEY_IS_STRING)
+        if (str_index)
         {
-          zval **data;
-          zend_hash_get_current_data_ex(params,(void*)&data,&pos);
-
-          if (!strcmp(str_index,"x")) { convert_to_long_ex(data); x=Z_LVAL_PP(data); };
-          if (!strcmp(str_index,"y")) { convert_to_long_ex(data); y=Z_LVAL_PP(data); };
-          if (!strcmp(str_index,"w")) { convert_to_long_ex(data); w=Z_LVAL_PP(data); };
-          if (!strcmp(str_index,"h")) { convert_to_long_ex(data); h=Z_LVAL_PP(data); };
-
-          if (!strcmp(str_index,"h_add")) { convert_to_double_ex(data); h_add=Z_DVAL_PP(data); };
-          if (!strcmp(str_index,"s_add")) { convert_to_double_ex(data); s_add=Z_DVAL_PP(data); };
-          if (!strcmp(str_index,"b_add")) { convert_to_double_ex(data); b_add=Z_DVAL_PP(data); };
-          if (!strcmp(str_index,"h_mul")) { convert_to_double_ex(data); h_mul=Z_DVAL_PP(data); };
-          if (!strcmp(str_index,"s_mul")) { convert_to_double_ex(data); s_mul=Z_DVAL_PP(data); };
-          if (!strcmp(str_index,"b_mul")) { convert_to_double_ex(data); b_mul=Z_DVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"x")) { convert_to_long_ex(data); x=Z_LVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"y")) { convert_to_long_ex(data); y=Z_LVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"w")) { convert_to_long_ex(data); w=Z_LVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"h")) { convert_to_long_ex(data); h=Z_LVAL_PP(data); };
+
+          if (!strcmp(STR_VAL(str_index),"h_add")) { convert_to_double_ex(data); h_add=Z_DVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"s_add")) { convert_to_double_ex(data); s_add=Z_DVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"b_add")) { convert_to_double_ex(data); b_add=Z_DVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"h_mul")) { convert_to_double_ex(data); h_mul=Z_DVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"s_mul")) { convert_to_double_ex(data); s_mul=Z_DVAL_PP(data); };
+          if (!strcmp(STR_VAL(str_index),"b_mul")) { convert_to_double_ex(data); b_mul=Z_DVAL_PP(data); };
         }
-      zend_hash_move_forward_ex(params,&pos);
-      }
+      } ZEND_HASH_FOREACH_END();

       if (x<0) x=0;
       if (y<0) y=0;
diff --git a/filters/php_testfilter.c b/filters/php_testfilter.c
index ca844b3..c9e4b5b 100644
--- a/filters/php_testfilter.c
+++ b/filters/php_testfilter.c
@@ -14,7 +14,9 @@ _php_testfilter_tint(Imlib_Image im, HashTable* params)
 {
       Imlib_Image imge = im;
       Imlib_Image anoim;
-      HashPosition pos;
+      ulong num_index;
+      zend_string *str_index;
+      imlib_zval *data;

       Imlib_Color_Modifier cm;
       DATA8 atab[256];
@@ -28,30 +30,21 @@ _php_testfilter_tint(Imlib_Image im, HashTable* params)
       w = imlib_image_get_width();
       h = imlib_image_get_height();

-      zend_hash_internal_pointer_reset_ex(params,&pos);
-      while (pos)
+      ZEND_HASH_FOREACH_KEY_VAL(params, num_index, str_index, data)
       {
-        char *str_index;
-        ulong num_index;
-        int retval;
-
-        retval=zend_hash_get_current_key_ex(params,&str_index,NULL,&num_index,0,&pos);
-        if (retval==HASH_KEY_IS_STRING)
+        if (str_index)
         {
-          zval **data;
-          zend_hash_get_current_data_ex(params,(void*)&data,&pos);
           convert_to_long_ex(data);
-          if (!strcmp(str_index,"red")) r=Z_LVAL_PP(data);
-          if (!strcmp(str_index,"blue")) b=Z_LVAL_PP(data);
-          if (!strcmp(str_index,"green")) g=Z_LVAL_PP(data);
-          if (!strcmp(str_index,"x")) x=Z_LVAL_PP(data);
-          if (!strcmp(str_index,"y")) y=Z_LVAL_PP(data);
-          if (!strcmp(str_index,"w")) w=Z_LVAL_PP(data);
-          if (!strcmp(str_index,"h")) h=Z_LVAL_PP(data);
-          if (!strcmp(str_index,"alpha")) a=Z_LVAL_PP(data);
+          if (!strcmp(STR_VAL(str_index),"red")) r=Z_LVAL_PP(data);
+          if (!strcmp(STR_VAL(str_index),"blue")) b=Z_LVAL_PP(data);
+          if (!strcmp(STR_VAL(str_index),"green")) g=Z_LVAL_PP(data);
+          if (!strcmp(STR_VAL(str_index),"x")) x=Z_LVAL_PP(data);
+          if (!strcmp(STR_VAL(str_index),"y")) y=Z_LVAL_PP(data);
+          if (!strcmp(STR_VAL(str_index),"w")) w=Z_LVAL_PP(data);
+          if (!strcmp(STR_VAL(str_index),"h")) h=Z_LVAL_PP(data);
+          if (!strcmp(STR_VAL(str_index),"alpha")) a=Z_LVAL_PP(data);
         }
-      zend_hash_move_forward_ex(params,&pos);
-      }
+      } ZEND_HASH_FOREACH_END();
 /*
       zend_printf( "Using values red=%d,blue=%d,green=%d,x=%d,y=%d,height=%d,width=%d,alpha=%d\n", r,b,g,x,y,w,h,a );
  */
diff --git a/php_imlib.c b/php_imlib.c
index e1a2828..2e387a1 100644
--- a/php_imlib.c
+++ b/php_imlib.c
@@ -22,6 +22,7 @@
 #include "php_imlib.h"
 #include "php_globals.h"
 #include "php_open_temporary_file.h"
+#include "ext/standard/info.h"

 /* #define IMLIB_DEBUG */

@@ -207,7 +208,7 @@ zend_module_entry imlib_module_entry = {
 PHP_INI_MH(OnUpdateFontCacheSize)
 {
     int size;
-    if (sscanf(new_value,"%d",&size)==1)
+    if (sscanf(STR_VAL(new_value),"%d",&size)==1)
     {
         imlib_set_font_cache_size(size);
         return SUCCESS;
@@ -218,41 +219,41 @@ PHP_INI_MH(OnUpdateFontCacheSize)
     }
 }

-static void _php_imlib_free_cm(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+IMLIB_RSRC_DTOR_FUNC(_php_imlib_free_cm)
 {
-   imlib_context_set_color_modifier(((PHP_Imlib_Color_Modifier)rsrc->ptr)->cm);
-   efree(rsrc->ptr);
+   imlib_context_set_color_modifier(((PHP_Imlib_Color_Modifier)res->ptr)->cm);
+   efree(res->ptr);
    imlib_free_color_modifier();
 }

-static void _php_imlib_free_filter(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+IMLIB_RSRC_DTOR_FUNC(_php_imlib_free_filter)
 {
-   imlib_context_set_filter(rsrc->ptr);
+   imlib_context_set_filter(res->ptr);
    imlib_free_filter();
 }

-static void _php_imlib_free_cr(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+IMLIB_RSRC_DTOR_FUNC(_php_imlib_free_cr)
 {
-   imlib_context_set_color_range(rsrc->ptr);
+   imlib_context_set_color_range(res->ptr);
    imlib_free_color_range();
 }

-static void _php_imlib_free_font(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+IMLIB_RSRC_DTOR_FUNC(_php_imlib_free_font)
 {
-   imlib_context_set_font(((PHP_Imlib_Font)rsrc->ptr)->font);
-   efree(rsrc->ptr);
+   imlib_context_set_font(((PHP_Imlib_Font)res->ptr)->font);
+   efree(res->ptr);
    imlib_free_font();
 }

-static void _php_imlib_free_img(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+IMLIB_RSRC_DTOR_FUNC(_php_imlib_free_img)
 {
-   imlib_context_set_image(rsrc->ptr);
+   imlib_context_set_image(res->ptr);
    imlib_free_image();
 }

-static void _php_imlib_free_poly(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+IMLIB_RSRC_DTOR_FUNC(_php_imlib_free_poly)
 {
-   imlib_polygon_free(rsrc->ptr);
+   imlib_polygon_free(res->ptr);
 }

 void _php_imlib_set_cache_size(int size TSRMLS_DC)
@@ -485,19 +486,19 @@ struct php_imlib_filter* _php_imlib_find_filter(char *filter_name TSRMLS_DC)
 	return NULL;
 }

-Imlib_Image _php_imlib_get_image(zval** im_resource)
+Imlib_Image _php_imlib_get_image(imlib_resource *im_resource)
 {
 	Imlib_Image im;
 	TSRMLS_FETCH();
-	MY_ZEND_FETCH_RESOURCE(im, Imlib_Image, im_resource, -1, "Imlib Image", le_imlib_img);
+	IMLIB_FETCH_RESOURCE(im, Imlib_Image, im_resource, -1, "Imlib Image", le_imlib_img);
 	return im;
 }

-Imlib_Filter _php_imlib_get_filter(zval** fil_resource)
+Imlib_Filter _php_imlib_get_filter(imlib_resource *fil_resource)
 {
 	Imlib_Filter fil;
 	TSRMLS_FETCH();
-	MY_ZEND_FETCH_RESOURCE(fil, Imlib_Filter, fil_resource, -1, "Imlib Filter", le_imlib_filter);
+	IMLIB_FETCH_RESOURCE(fil, Imlib_Filter, fil_resource, -1, "Imlib Filter", le_imlib_filter);
 	return fil;
 }

@@ -522,37 +523,37 @@ void _php_imlib_color_modifier_synch(PHP_Imlib_Color_Modifier cm)
 	imlib_context_set_color_modifier(NULL);
 }

-Imlib_Color_Modifier _php_imlib_get_cm(zval** cm_resource)
+Imlib_Color_Modifier _php_imlib_get_cm(imlib_resource *cm_resource)
 {
 	PHP_Imlib_Color_Modifier cm;
 	TSRMLS_FETCH();
-	MY_ZEND_FETCH_RESOURCE(cm, Imlib_Color_Modifier, cm_resource, -1, "Imlib Color Modifier", le_imlib_cm);
+	IMLIB_FETCH_RESOURCE(cm, Imlib_Color_Modifier, cm_resource, -1, "Imlib Color Modifier", le_imlib_cm);
 	if (cm->modified) _php_imlib_color_modifier_synch(cm);
 	cm->valid=0;
 	return cm->cm;
 }

-Imlib_Font _php_imlib_get_font(zval** font_resource)
+Imlib_Font _php_imlib_get_font(imlib_resource *font_resource)
 {
 	PHP_Imlib_Font php_font;
 	TSRMLS_FETCH();
-	MY_ZEND_FETCH_RESOURCE(php_font, PHP_Imlib_Font, font_resource, -1, "Imlib Font", le_imlib_font);
+	IMLIB_FETCH_RESOURCE(php_font, PHP_Imlib_Font, font_resource, -1, "Imlib Font", le_imlib_font);
 	return php_font->font;
 }

-Imlib_Color_Range _php_imlib_get_cr(zval** cr_resource)
+Imlib_Color_Range _php_imlib_get_cr(imlib_resource *cr_resource)
 {
 	Imlib_Color_Range cr;
 	TSRMLS_FETCH();
-	MY_ZEND_FETCH_RESOURCE(cr, Imlib_Color_Range, cr_resource, -1, "Imlib Color Range", le_imlib_cr);
+	IMLIB_FETCH_RESOURCE(cr, Imlib_Color_Range, cr_resource, -1, "Imlib Color Range", le_imlib_cr);
 	return cr;
 }

-ImlibPolygon _php_imlib_get_poly(zval** poly_resource)
+ImlibPolygon _php_imlib_get_poly(imlib_resource *poly_resource)
 {
 	ImlibPolygon poly;
 	TSRMLS_FETCH();
-	MY_ZEND_FETCH_RESOURCE(poly, ImlibPolygon, poly_resource, -1, "Imlib Polygon", le_imlib_poly);
+	IMLIB_FETCH_RESOURCE(poly, ImlibPolygon, poly_resource, -1, "Imlib Polygon", le_imlib_poly);
 	return poly;
 }

@@ -560,9 +561,9 @@ ImlibPolygon _php_imlib_get_poly(zval** poly_resource)

 /* {{{ _php_imlib_free_ps_font
  */
-void _php_imlib_free_ps_font(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+IMLIB_RSRC_DTOR_FUNC(_php_imlib_free_ps_font)
 {
-        int *font = (int *)rsrc->ptr;
+        int *font = (int *)res->ptr;

         T1_DeleteFont(*font);
         efree(font);
@@ -571,9 +572,9 @@ void _php_imlib_free_ps_font(zend_rsrc_list_entry *rsrc TSRMLS_DC)

 /* {{{ _php_imlib_free_ps_enc
  */
-void _php_imlib_free_ps_enc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+IMLIB_RSRC_DTOR_FUNC(_php_imlib_free_ps_enc)
 {
-        char **enc = (char **)rsrc->ptr;
+        char **enc = (char **)res->ptr;

         T1_DeleteEncoding(enc);
 }
@@ -819,24 +820,20 @@ PHP_MINFO_FUNCTION(imlib)
 	}
 }

-static void _php_convert_four_longs(zval **zone, zval **ztwo, zval **zthree,
-                                    zval **zfour, int *one, int *two,
-                                    int *three, int *four)
+static inline int zend_hash_index_find_wrapper(HashTable *ht, int index, imlib_zval **value)
 {
-   convert_to_long_ex(zone);
-   convert_to_long_ex(ztwo);
-   convert_to_long_ex(zthree);
-   convert_to_long_ex(zfour);
-   *one = Z_LVAL_PP(zone);
-   *two = Z_LVAL_PP(ztwo);
-   *three = Z_LVAL_PP(zthree);
-   *four = Z_LVAL_PP(zfour);
+#if PHP_VERSION_ID >= 70000
+	*value = zend_hash_index_find(ht, index);
+	return (*value != NULL ? SUCCESS : FAILURE);
+#else
+	return zend_hash_index_find(ht, index, (void **)value);
+#endif
 }

 static int _php_handle_cliprect_array(zval **dbox, char *func_name,
                                       int *x, int *y, int *w, int *h TSRMLS_DC)
 {
-   zval **element, ***box_coords;
+   imlib_zval *element, **box_coords;
    int i,arrcount;
    HashTable *box;

@@ -853,11 +850,11 @@ static int _php_handle_cliprect_array(zval **dbox, char *func_name,
       return 0;
    }

-   box_coords = (zval ***)emalloc(arrcount * sizeof(zval **));
+   box_coords = (imlib_zval **)emalloc(arrcount * sizeof(imlib_zval *));

    for (i = 0; i < arrcount; i++)
    {
-      if (zend_hash_index_find(box, i, (void **)&element) == SUCCESS)
+      if (zend_hash_index_find_wrapper(box, i, &element) == SUCCESS)
       {
          convert_to_long_ex(element);
          box_coords[i] = element;
@@ -946,7 +943,8 @@ static void _php_imlib_draw_something(INTERNAL_FUNCTION_PARAMETERS, void (*func)
 {
    int argc;
    int cx, cy, cw, ch;
-   zval *img, *dbox;
+   imlib_resource img;
+   zval *dbox;
    long x,y,w,h,r,g,b,a;
    Imlib_Image im;

@@ -955,7 +953,7 @@ static void _php_imlib_draw_something(INTERNAL_FUNCTION_PARAMETERS, void (*func)
       return;
    }

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

    imlib_context_set_image(im);
    imlib_context_set_color(r,g,b,a);
@@ -977,12 +975,12 @@ static void _php_imlib_draw_something(INTERNAL_FUNCTION_PARAMETERS, void (*func)

 static void _php_imlib_single_arg(INTERNAL_FUNCTION_PARAMETERS, void (*func)())
 {
-   zval *img;
+   imlib_resource img;
    Imlib_Image im;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &img) == FAILURE) return;

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

    imlib_context_set_image(im);
    (*func)();
@@ -1000,13 +998,13 @@ static void _php_wrap_draw_line(int x1, int y1, int x2, int y2)
    Add a color to a color range at a specified distance from the previous color in the range.  A distance of 0 centers it */
 PHP_FUNCTION(imlib_add_color_to_color_range)
 {
-   zval *crange;
+   imlib_resource crange;
    long x,r,g,b,a;
    Imlib_Color_Range range;

    if (zend_parse_parameters(6 TSRMLS_CC, "rlllll", &crange, &x, &r, &g, &b, &a) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(range, Imlib_Color_Range, &crange, -1, "Imlib Color Range", le_imlib_cr);
+   IMLIB_FETCH_RESOURCE(range, Imlib_Color_Range, &crange, -1, "Imlib Color Range", le_imlib_cr);

    imlib_context_set_color_range(range);
    imlib_context_set_color(r,g,b,a);
@@ -1019,15 +1017,15 @@ PHP_FUNCTION(imlib_add_color_to_color_range)
    Blend a rectangular area from an image onto an area of another image, scaling as necessary */
 PHP_FUNCTION(imlib_blend_image_onto_image)
 {
-   zval *dstimg, *srcimg;
+   imlib_resource dstimg, srcimg;
    Imlib_Image dst,src;
    long sx,sy,sw,sh,dx,dy,dw,dh;
    long calias, calpha, cblend, cdither;

    if (zend_parse_parameters(14 TSRMLS_CC, "rrllllllllllll", &dstimg, &srcimg, &calpha, &sx, &sy, &sw, &sh, &dx, &dy, &dw, &dh, &cdither, &cblend, &calias) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(src, Imlib_Image, &srcimg, -1, "Imlib Image", le_imlib_img);
-   ZEND_FETCH_RESOURCE(dst, Imlib_Image, &dstimg, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(src, Imlib_Image, &srcimg, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(dst, Imlib_Image, &dstimg, -1, "Imlib Image", le_imlib_img);

    imlib_context_set_image(dst);
    imlib_context_set_anti_alias(calias);
@@ -1043,17 +1041,17 @@ PHP_FUNCTION(imlib_blend_image_onto_image)
    Duplicate an image */
 PHP_FUNCTION(imlib_clone_image)
 {
-   zval *img;
+   imlib_resource img;
    Imlib_Image src,dst;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &img) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(src, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(src, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);

    imlib_context_set_image(src);
    dst = imlib_clone_image();

-   if (dst) ZEND_REGISTER_RESOURCE(return_value, dst, le_imlib_img);
+   if (dst) IMLIB_REGISTER_RESOURCE(dst, le_imlib_img);
 }
 /* }}} */

@@ -1070,7 +1068,7 @@ PHP_FUNCTION(imlib_create_color_range)

    cr = imlib_create_color_range();

-   if (cr) ZEND_REGISTER_RESOURCE(return_value, cr, le_imlib_cr);
+   if (cr) IMLIB_REGISTER_RESOURCE(cr, le_imlib_cr);
 }
 /* }}} */

@@ -1097,7 +1095,7 @@ PHP_FUNCTION(imlib_create_color_modifier)
       RETURN_FALSE;
    }

-   ZEND_REGISTER_RESOURCE(return_value, cm, le_imlib_cm);
+   IMLIB_REGISTER_RESOURCE(cm, le_imlib_cm);
 }
 /* }}} */

@@ -1105,18 +1103,18 @@ PHP_FUNCTION(imlib_create_color_modifier)
    Create an image from a cropped region of another image */
 PHP_FUNCTION(imlib_create_cropped_image)
 {
-   zval *img;
+   imlib_resource img;
    long sx,sy,sw,sh;
    Imlib_Image src,dst;

    if (zend_parse_parameters(5 TSRMLS_CC, "rllll", &img, &sx, &sy, &sw, &sh) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(src, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(src, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);

    imlib_context_set_image(src);
    dst = imlib_create_cropped_image(sx,sy,sw,sh);

-   if (dst) ZEND_REGISTER_RESOURCE(return_value, dst, le_imlib_img);
+   if (dst) IMLIB_REGISTER_RESOURCE(dst, le_imlib_img);
 }
 /* }}} */

@@ -1125,18 +1123,18 @@ PHP_FUNCTION(imlib_create_cropped_image)
    Create a scaled image from a cropped region of another image */
 PHP_FUNCTION(imlib_create_cropped_scaled_image)
 {
-   zval *img;
+   imlib_resource img;
    long sx,sy,sw,sh,dw,dh;
    Imlib_Image src,dst;

    if (zend_parse_parameters(7 TSRMLS_CC, "rllllll", &img, &sx, &sy, &sw, &sh, &dw, &dh) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(src, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(src, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);

    imlib_context_set_image(src);
    dst = imlib_create_cropped_scaled_image(sx,sy, sw,sh, dw,dh);

-   if (dst) ZEND_REGISTER_RESOURCE(return_value, dst, le_imlib_img);
+   if (dst) IMLIB_REGISTER_RESOURCE(dst, le_imlib_img);
 }
 /* }}} */

@@ -1156,7 +1154,7 @@ PHP_FUNCTION(imlib_create_image)
       imlib_context_set_image(im);
       memset(imlib_image_get_data(), '\0', x * y * sizeof(DATA32));

-      ZEND_REGISTER_RESOURCE(return_value, im, le_imlib_img);
+      IMLIB_REGISTER_RESOURCE(im, le_imlib_img);
    }
 }
 /* }}} */
@@ -1169,7 +1167,7 @@ PHP_FUNCTION(imlib_create_rotated_image)

   /* Contributed by Gareth Ardron */

-   zval *srcimg;
+   imlib_resource srcimg;
    double angle, radians;
    int argc;
    Imlib_Image src, dst;
@@ -1177,14 +1175,14 @@ PHP_FUNCTION(imlib_create_rotated_image)
    argc = ZEND_NUM_ARGS();
    if (zend_parse_parameters(argc TSRMLS_CC, "rd|d", &srcimg, &angle, &radians) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(src, Imlib_Image, &srcimg, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(src, Imlib_Image, &srcimg, -1, "Imlib Image", le_imlib_img);

    if (argc == 2) radians = angle * M_PI/180;

    imlib_context_set_image(src);
    dst = imlib_create_rotated_image(radians);

-   if (dst) ZEND_REGISTER_RESOURCE(return_value, dst, le_imlib_img);
+   if (dst) IMLIB_REGISTER_RESOURCE(dst, le_imlib_img);
 }
 /* }}} */

@@ -1193,14 +1191,14 @@ PHP_FUNCTION(imlib_create_rotated_image)
    Create a scaled copy of an image.  If dstw or dsth is left blank, the aspect ratio of the source image will be preserved. */
 PHP_FUNCTION(imlib_create_scaled_image)
 {
-   zval *img;
-   zval **dstw, **dsth;
+   imlib_resource img;
+   imlib_zval *dstw, *dsth;
    long sw,sh,dw,dh;
    Imlib_Image src,dst;

    if (zend_parse_parameters(3 TSRMLS_CC, "rZZ", &img, &dstw, &dsth) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(src, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(src, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);

    convert_to_long_ex(dstw);
    convert_to_long_ex(dsth);
@@ -1223,7 +1221,7 @@ PHP_FUNCTION(imlib_create_scaled_image)

    dst = imlib_create_cropped_scaled_image(0,0, sw,sh, dw,dh);

-   if (dst) ZEND_REGISTER_RESOURCE(return_value, dst, le_imlib_img);
+   if (dst) IMLIB_REGISTER_RESOURCE(dst, le_imlib_img);
 }
 /* }}} */

@@ -1235,8 +1233,9 @@ PHP_FUNCTION(imlib_dump_image)
    int argc, retval;
    long q;
    FILE *tmp;
-   char *tmpfile;
-   zval *img, **err;
+   zend_string *tmpfile;
+   imlib_resource img;
+   zval **err;
    Imlib_Image im;
    Imlib_Load_Error im_err;

@@ -1248,7 +1247,7 @@ PHP_FUNCTION(imlib_dump_image)
       ZVAL_LONG(*err,0);
    }

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

    tmp = php_open_temporary_file("", "", &tmpfile TSRMLS_CC);
    if (tmp == NULL) {
@@ -1266,7 +1265,7 @@ PHP_FUNCTION(imlib_dump_image)
       imlib_image_attach_data_value("quality",NULL,q,NULL);
    }

-   imlib_save_image_with_error_return(tmpfile,&im_err);
+   imlib_save_image_with_error_return(STR_VAL(tmpfile),&im_err);

    if (im_err)
    {
@@ -1275,7 +1274,7 @@ PHP_FUNCTION(imlib_dump_image)
          ZVAL_LONG(*err,im_err);
       }
       _php_handle_imlib_error(INTERNAL_FUNCTION_PARAM_PASSTHRU,
-                              im_err,tmpfile);
+                              im_err,STR_VAL(tmpfile));
       fclose(tmp);
       VCWD_UNLINK((const char *)tmpfile); /* make sure that the temporary file is removed */
       efree(tmpfile);
@@ -1311,13 +1310,13 @@ PHP_FUNCTION(imlib_dump_image)
    Free a color range */
 PHP_FUNCTION(imlib_free_color_range)
 {
-   zval *fcr;
+   imlib_resource fcr;
    Imlib_Color_Range cr;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &fcr) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(cr, Imlib_Color_Range, &fcr, -1, "Imlib Color Range", le_imlib_cr);
-   zend_list_delete(Z_LVAL_PP(&fcr));
+   IMLIB_FETCH_RESOURCE(cr, Imlib_Color_Range, &fcr, -1, "Imlib Color Range", le_imlib_cr);
+   IMLIB_DELETE_RESOURCE(&fcr);
 }
 /* }}} */

@@ -1326,13 +1325,13 @@ PHP_FUNCTION(imlib_free_color_range)
    Free a color modifier */
 PHP_FUNCTION(imlib_free_color_modifier)
 {
-   zval *fcm;
+   imlib_resource fcm;
    PHP_Imlib_Color_Modifier cm;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &fcm) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &fcm, -1, "Imlib Color Modifier", le_imlib_cm);
-   zend_list_delete(Z_LVAL_PP(&fcm));
+   IMLIB_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &fcm, -1, "Imlib Color Modifier", le_imlib_cm);
+   IMLIB_DELETE_RESOURCE(&fcm);
 }
 /* }}} */

@@ -1340,13 +1339,13 @@ PHP_FUNCTION(imlib_free_color_modifier)
    Free a font */
 PHP_FUNCTION(imlib_free_font)
 {
-   zval *font;
+   imlib_resource font;
    PHP_Imlib_Font fn;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &font) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(fn, PHP_Imlib_Font, &font, -1, "Imlib Font", le_imlib_font);
-   zend_list_delete(Z_LVAL_PP(&font));
+   IMLIB_FETCH_RESOURCE(fn, PHP_Imlib_Font, &font, -1, "Imlib Font", le_imlib_font);
+   IMLIB_DELETE_RESOURCE(&font);
 }
 /* }}} */

@@ -1355,13 +1354,13 @@ PHP_FUNCTION(imlib_free_font)
    Free an image */
 PHP_FUNCTION(imlib_free_image)
 {
-   zval *img;
+   imlib_resource img;
    Imlib_Image im;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &img) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(im, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
-   zend_list_delete(Z_LVAL_PP(&img));
+   IMLIB_FETCH_RESOURCE(im, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
+   IMLIB_DELETE_RESOURCE(&img);
 }
 /* }}} */

@@ -1370,7 +1369,7 @@ PHP_FUNCTION(imlib_free_image)
    Determines the horizontal and vertical advance of a string if drawn with a given font in the specified direction*/
 PHP_FUNCTION(imlib_get_text_advance)
 {
-   zval *font;
+   imlib_resource font;
    zval **thoriz_adv, **tvert_adv;
    PHP_Imlib_Font fn;
    const char *text = NULL;
@@ -1379,7 +1378,7 @@ PHP_FUNCTION(imlib_get_text_advance)

    if (zend_parse_parameters(5 TSRMLS_CC, "rsZZl", &font, &text, &text_len, &thoriz_adv, &tvert_adv, &dir) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(fn, PHP_Imlib_Font, &font, -1, "Imlib Font", le_imlib_font);
+   IMLIB_FETCH_RESOURCE(fn, PHP_Imlib_Font, &font, -1, "Imlib Font", le_imlib_font);

    imlib_context_set_font(fn->font);
    imlib_context_set_direction(dir);
@@ -1397,7 +1396,7 @@ PHP_FUNCTION(imlib_get_text_advance)
    Determines the width and height of a string if drawn with a given font in the specified direction */
 PHP_FUNCTION(imlib_get_text_size)
 {
-   zval *font;
+   imlib_resource font;
    zval **tw, **th;
    PHP_Imlib_Font fn;
    const char *text = NULL;
@@ -1406,7 +1405,7 @@ PHP_FUNCTION(imlib_get_text_size)

    if (zend_parse_parameters(5 TSRMLS_CC, "rsZZl", &font, &text, &text_len, &tw, &th, &dir) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(fn, PHP_Imlib_Font, &font, -1, "Imlib Font", le_imlib_font);
+   IMLIB_FETCH_RESOURCE(fn, PHP_Imlib_Font, &font, -1, "Imlib Font", le_imlib_font);

    zval_dtor(*tw);
    zval_dtor(*th);
@@ -1425,13 +1424,13 @@ PHP_FUNCTION(imlib_get_text_size)
    Blur an image with a given blur radius */
 PHP_FUNCTION(imlib_image_blur)
 {
-   zval *img;
+   imlib_resource img;
    long r;
    Imlib_Image im;

    if (zend_parse_parameters(2 TSRMLS_CC, "rl", &img, &r) == FAILURE) return;

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

    imlib_context_set_image(im);
    imlib_image_blur(r);
@@ -1463,7 +1462,8 @@ PHP_FUNCTION(imlib_image_draw_line)
    Draw the defined polygon on an image */
 PHP_FUNCTION(imlib_image_draw_polygon)
 {
-   zval *img, *polygon, *dbox;
+   imlib_resource img, polygon;
+   zval *dbox;
    long r,g,b,a;
    int cx,cy,cw,ch,argc;
    Imlib_Image im;
@@ -1473,8 +1473,8 @@ PHP_FUNCTION(imlib_image_draw_polygon)
    argc = ZEND_NUM_ARGS();
    if (zend_parse_parameters(argc TSRMLS_CC, "rrbllll|a", &img, &polygon, &closed, &r, &g, &b, &a, &dbox) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(im, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
-   ZEND_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);
+   IMLIB_FETCH_RESOURCE(im, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);

    imlib_context_set_image(im);
    imlib_context_set_color(r,g,b,a);
@@ -1506,7 +1506,8 @@ PHP_FUNCTION(imlib_image_draw_rectangle)
    Fill a rectangle with a color range at a given angle on an image */
 PHP_FUNCTION(imlib_image_fill_color_range_rectangle)
 {
-   zval *fim, *fcr, *fbox;
+   imlib_resource fim, fcr;
+   zval *fbox;
    long x,y,width,height;
    int argc,cx,cy,cw,ch;
    double angle;
@@ -1516,8 +1517,8 @@ PHP_FUNCTION(imlib_image_fill_color_range_rectangle)
    argc = ZEND_NUM_ARGS();
    if (zend_parse_parameters(argc TSRMLS_CC, "rrlllld|a", &fim, &fcr, &x, &y, &width, &height, &angle, &fbox) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(cr, Imlib_Color_Range, &fcr, -1, "Imlib Color Range", le_imlib_cr);
-   ZEND_FETCH_RESOURCE(im, Imlib_Image, &fim, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(cr, Imlib_Color_Range, &fcr, -1, "Imlib Color Range", le_imlib_cr);
+   IMLIB_FETCH_RESOURCE(im, Imlib_Image, &fim, -1, "Imlib Image", le_imlib_img);

    imlib_context_set_color_range(cr);
    imlib_context_set_image(im);
@@ -1550,7 +1551,8 @@ PHP_FUNCTION(imlib_image_fill_ellipse)
    Draw and fill the defined polygon on an image */
 PHP_FUNCTION(imlib_image_fill_polygon)
 {
-   zval *img, *polygon, *dbox;
+   imlib_resource img, polygon;
+   zval *dbox;
    long r,g,b,a;
    int cx,cy,cw,ch,argc;
    Imlib_Image im;
@@ -1559,8 +1561,8 @@ PHP_FUNCTION(imlib_image_fill_polygon)
    argc = ZEND_NUM_ARGS();
    if (zend_parse_parameters(argc TSRMLS_CC, "rrllll|a", &img, &polygon, &r, &g, &b, &a, &dbox) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(im, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
-   ZEND_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);
+   IMLIB_FETCH_RESOURCE(im, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);

    imlib_context_set_image(im);
    imlib_context_set_color(r,g,b,a);
@@ -1623,13 +1625,13 @@ PHP_FUNCTION(imlib_image_orientate)

   /* Contributed by Gareth Ardron */

-   zval *img;
+   imlib_resource img;
    long r;
    Imlib_Image im;

    if (zend_parse_parameters(2 TSRMLS_CC, "rl", &img, &r) == FAILURE) return;

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

    imlib_context_set_image(im);
    imlib_image_orientate(r);
@@ -1641,20 +1643,20 @@ PHP_FUNCTION(imlib_image_orientate)
    Returns the image format of an image */
 PHP_FUNCTION(imlib_image_format)
 {
-   zval *img;
+   imlib_resource img;
    Imlib_Image im;
    char *name;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &img) == FAILURE) return;

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

    imlib_context_set_image(im);
    name = imlib_image_format();
    if (!name)
       RETURN_FALSE;

-   RETURN_STRING(name,strlen(name));
+   IMLIB_RETURN_STRING(name);
 }
 /* }}} */

@@ -1663,20 +1665,20 @@ PHP_FUNCTION(imlib_image_format)
    Returns the filename of an image */
 PHP_FUNCTION(imlib_image_get_filename)
 {
-   zval *img;
+   imlib_resource img;
    Imlib_Image im;
    const char *name = NULL;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &img) == FAILURE) return;

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

    imlib_context_set_image(im);
    name = imlib_image_get_filename();
    if (!name)
       RETURN_FALSE;

-   RETURN_STRING((char*)name,strlen(name));
+   IMLIB_RETURN_STRING((char*)name);
 }
 /* }}} */

@@ -1685,13 +1687,13 @@ PHP_FUNCTION(imlib_image_get_filename)
    Returns the height of an image */
 PHP_FUNCTION(imlib_image_get_height)
 {
-   zval *img;
+   imlib_resource img;
    Imlib_Image im;
    int height;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &img) == FAILURE) return;

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

    imlib_context_set_image(im);
    height = imlib_image_get_height();
@@ -1705,13 +1707,13 @@ PHP_FUNCTION(imlib_image_get_height)
    Returns the width of an image */
 PHP_FUNCTION(imlib_image_get_width)
 {
-   zval *img;
+   imlib_resource img;
    Imlib_Image im;
    int width;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &img) == FAILURE) return;

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

    imlib_context_set_image(im);
    width = imlib_image_get_width();
@@ -1725,12 +1727,12 @@ PHP_FUNCTION(imlib_image_get_width)
    Return a boolean for whether or not an image has an alpha channel */
 PHP_FUNCTION(imlib_image_has_alpha)
 {
-   zval *img;
+   imlib_resource img;
    Imlib_Image im;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &img) == FAILURE) return;

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

    imlib_context_set_image(im);
    if (imlib_image_has_alpha())
@@ -1749,7 +1751,7 @@ PHP_FUNCTION(imlib_image_has_alpha)
    Set the alpha channel of an image, or modify it if one was already present */
 PHP_FUNCTION(imlib_image_modify_alpha)
 {
-   zval *img;
+   imlib_resource img;
    Imlib_Image im;
    DATA8 map[256];
    Imlib_Color_Modifier *cmod;
@@ -1759,7 +1761,7 @@ PHP_FUNCTION(imlib_image_modify_alpha)

    if (zend_parse_parameters(2 TSRMLS_CC, "rl", &img, &malpha) == FAILURE) return;

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

    ratio = ((double)malpha) / 255;

@@ -1789,14 +1791,14 @@ PHP_FUNCTION(imlib_image_modify_alpha)
    Sets the image format of an image. */
 PHP_FUNCTION(imlib_image_set_format)
 {
-   zval *img;
+   imlib_resource img;
    Imlib_Image im;
    char *format;
    int format_len;

    if (zend_parse_parameters(2 TSRMLS_CC, "rs", &img, &format, &format_len) == FAILURE) return;

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

    imlib_context_set_image(im);
    imlib_image_set_format(format);
@@ -1810,13 +1812,13 @@ PHP_FUNCTION(imlib_image_set_format)
    Sharpen an image with a given sharpen radius */
 PHP_FUNCTION(imlib_image_sharpen)
 {
-   zval *img;
+   imlib_resource img;
    long r;
    Imlib_Image im;

    if (zend_parse_parameters(2 TSRMLS_CC, "rl", &img, &r) == FAILURE) return;

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

    imlib_context_set_image(im);
    imlib_image_sharpen(r);
@@ -1869,7 +1871,11 @@ PHP_FUNCTION(imlib_list_fonts)
    for (i = 0; i < fcount; i++)
    {
       /* FIXME: Is 1 the right parameter here? */
+#if PHP_VERSION_ID >= 70000
+      add_next_index_string(return_value, flist[i]);
+#else
       add_next_index_string(return_value, flist[i], 1);
+#endif
    }

    imlib_free_font_list(flist,fcount);
@@ -1900,7 +1906,7 @@ PHP_FUNCTION(imlib_load_font)
       RETURN_FALSE;
    }

-   ZEND_REGISTER_RESOURCE(return_value, fn, le_imlib_font);
+   IMLIB_REGISTER_RESOURCE(fn, le_imlib_font);
 }
 /* }}} */

@@ -1914,7 +1920,7 @@ PHP_FUNCTION(imlib_load_image)
    Imlib_Image im;
    Imlib_Load_Error im_err;
    FILE* f;
-   char* filename;
+   zend_string* filename;
    char* img;
    int img_len;

@@ -1929,10 +1935,10 @@ PHP_FUNCTION(imlib_load_image)
    f = php_stream_open_wrapper_as_file(img, "rb", IGNORE_PATH | ENFORCE_SAFE_MODE | REPORT_ERRORS, &filename);
    if (f == NULL) RETURN_FALSE;

-   im = imlib_load_image_with_error_return(filename, &im_err);
+   im = imlib_load_image_with_error_return(STR_VAL(filename), &im_err);

    fclose(f);
-   efree(filename);
+   zend_string_release(filename);

    if ((im_err) || (!im))
    {
@@ -1945,7 +1951,7 @@ PHP_FUNCTION(imlib_load_image)
    }
    else
    {
-      ZEND_REGISTER_RESOURCE(return_value, im, le_imlib_img);
+      IMLIB_REGISTER_RESOURCE(im, le_imlib_img);
    }
 }
 /* }}} */
@@ -1955,13 +1961,13 @@ PHP_FUNCTION(imlib_load_image)
    Add a point to a given polygon */
 PHP_FUNCTION(imlib_polygon_add_point)
 {
-   zval *polygon;
+   imlib_resource polygon;
    long x,y;
    ImlibPolygon poly;

    if (zend_parse_parameters(3 TSRMLS_CC, "rll", &polygon, &x, &y) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);
+   IMLIB_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);

    imlib_polygon_add_point(poly,x,y);
 }
@@ -1972,14 +1978,14 @@ PHP_FUNCTION(imlib_polygon_add_point)
    Check if a give point is inside a polygon */
 PHP_FUNCTION(imlib_polygon_contains_point)
 {
-   zval *polygon;
+   imlib_resource polygon;
    long x,y;
    int ret;
    ImlibPolygon poly;

    if (zend_parse_parameters(3 TSRMLS_CC, "rll", &polygon, &x, &y) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);
+   IMLIB_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);

    ret = imlib_polygon_contains_point(poly,x,y);

@@ -1999,13 +2005,13 @@ PHP_FUNCTION(imlib_polygon_contains_point)
    Free a polygon */
 PHP_FUNCTION(imlib_polygon_free)
 {
-   zval *polygon;
+   imlib_resource polygon;
    ImlibPolygon poly;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &polygon) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);
-   zend_list_delete(Z_LVAL_PP(&polygon));
+   IMLIB_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);
+   IMLIB_DELETE_RESOURCE(&polygon);
 }
 /* }}} */

@@ -2014,14 +2020,14 @@ PHP_FUNCTION(imlib_polygon_free)
    Get the bounding coords of a polygon */
 PHP_FUNCTION(imlib_polygon_get_bounds)
 {
-   zval *polygon;
+   imlib_resource polygon;
    zval **px1, **py1, **px2, **py2;
    int x1,y1,x2,y2;
    ImlibPolygon poly;

    if (zend_parse_parameters(5 TSRMLS_CC, "rZZZZ", &polygon, &px1, &py1, &px2, &py2) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);
+   IMLIB_FETCH_RESOURCE(poly, ImlibPolygon, &polygon, -1, "Imlib Polygon", le_imlib_poly);

    zval_dtor(*px1);
    zval_dtor(*py1);
@@ -2049,7 +2055,7 @@ PHP_FUNCTION(imlib_polygon_new)
    }

    poly = imlib_polygon_new();
-   if (poly) ZEND_REGISTER_RESOURCE(return_value, poly, le_imlib_poly);
+   if (poly) IMLIB_REGISTER_RESOURCE( poly, le_imlib_poly);
 }
 /* }}} */

@@ -2058,20 +2064,21 @@ PHP_FUNCTION(imlib_polygon_new)
    Save an image to a file, at an optional quality level (1-100) for jpegs.  For pngs, the value will be converted to a compression level (0-9) */
 PHP_FUNCTION(imlib_save_image)
 {
-   zval *img, **err;
+   imlib_resource img;
+   zval **err;
    char* name;
    int name_len;
    Imlib_Image im;
    Imlib_Load_Error im_err;
    int argc;
    long q;
-   char* filename;
+   zend_string* filename;
    FILE* f;

    argc = ZEND_NUM_ARGS();
    if (zend_parse_parameters(argc TSRMLS_CC, "rs|Zl", &img, &name, &name_len, &err, &q) == FAILURE) return;

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

    imlib_context_set_image(im);

@@ -2088,10 +2095,11 @@ PHP_FUNCTION(imlib_save_image)
    f = php_stream_open_wrapper_as_file(name, "wb", IGNORE_PATH | ENFORCE_SAFE_MODE | REPORT_ERRORS, &filename);
    if (f == NULL) RETURN_FALSE;

-   imlib_save_image_with_error_return(filename, &im_err);
+   imlib_save_image_with_error_return(STR_VAL(filename), &im_err);

    fclose(f);
-   efree(filename);
+
+   zend_string_release(filename);

    if (im_err)
    {
@@ -2113,7 +2121,7 @@ PHP_FUNCTION(imlib_save_image)
    Draw a text string using a font onto an image */
 PHP_FUNCTION(imlib_text_draw)
 {
-   zval *img, *font;
+   imlib_resource img, font;
    Imlib_Image im;
    PHP_Imlib_Font fn;
    char *text;
@@ -2122,8 +2130,8 @@ PHP_FUNCTION(imlib_text_draw)

    if (zend_parse_parameters(10 TSRMLS_CC, "rrllslllll", &img, &font, &x, &y, &text, &text_len, &dir, &r, &g, &b, &a) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(im, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
-   ZEND_FETCH_RESOURCE(fn, PHP_Imlib_Font, &font, -1, "Imlib Font", le_imlib_font);
+   IMLIB_FETCH_RESOURCE(im, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(fn, PHP_Imlib_Font, &font, -1, "Imlib Font", le_imlib_font);

    imlib_context_set_image(im);
    imlib_context_set_color(r,g,b,a);
@@ -2158,7 +2166,8 @@ PHP_FUNCTION(imlib_set_cache_size)
    Apply external filter to an image */
 PHP_FUNCTION(imlib_apply_filter)
 {
-   zval *img, *tparams;
+   imlib_resource img;
+   zval *tparams;
    Imlib_Image im;
    struct php_imlib_filter* filter;
    char* filter_name;
@@ -2166,7 +2175,7 @@ PHP_FUNCTION(imlib_apply_filter)
    HashTable* arg_ht;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|a", &img, &filter_name, &filter_name_len, &tparams) == FAILURE) return;

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

    filter = _php_imlib_find_filter(filter_name TSRMLS_CC);

@@ -2201,7 +2210,7 @@ PHP_FUNCTION(imlib_create_filter)

    fil = imlib_create_filter(0);

-   if (fil) ZEND_REGISTER_RESOURCE(return_value, fil, le_imlib_filter);
+   if (fil) IMLIB_REGISTER_RESOURCE( fil, le_imlib_filter);
 }
 /* }}} */

@@ -2210,13 +2219,13 @@ PHP_FUNCTION(imlib_create_filter)
    Free a filter */
 PHP_FUNCTION(imlib_free_filter)
 {
-   zval *filter;
+   imlib_resource filter;
    Imlib_Filter fil;

    if (zend_parse_parameters(1 TSRMLS_CC, "r", &filter) == FAILURE) return;

-   ZEND_FETCH_RESOURCE(fil, Imlib_Filter, &filter, -1, "Imlib Filter", le_imlib_filter);
-   zend_list_delete(Z_LVAL_PP(&filter));
+   IMLIB_FETCH_RESOURCE(fil, Imlib_Filter, &filter, -1, "Imlib Filter", le_imlib_filter);
+   IMLIB_DELETE_RESOURCE(&filter);
 }
 /* }}} */

@@ -2224,14 +2233,14 @@ PHP_FUNCTION(imlib_free_filter)
    Apply filter to an image */
 PHP_FUNCTION(imlib_image_filter)
 {
-   zval *filter, *img;
+   imlib_resource filter, img;
    Imlib_Filter fil;
    Imlib_Image im;

    if (zend_parse_parameters(2 TSRMLS_CC, "rr", &img, &filter)  == FAILURE) return;

-   ZEND_FETCH_RESOURCE(im, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
-   ZEND_FETCH_RESOURCE(fil, Imlib_Filter, &filter, -1, "Imlib Filter", le_imlib_filter);
+   IMLIB_FETCH_RESOURCE(im, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
+   IMLIB_FETCH_RESOURCE(fil, Imlib_Filter, &filter, -1, "Imlib Filter", le_imlib_filter);

    imlib_context_set_image(im);
    imlib_context_set_filter(fil);
@@ -2241,14 +2250,14 @@ PHP_FUNCTION(imlib_image_filter)

 static void _php_imlib_filter_set(INTERNAL_FUNCTION_PARAMETERS, int type)
 {
-    zval *filter;
+    imlib_resource filter;
     int argc;
     long xoff,yoff,a,r,g,b;
     Imlib_Filter fil;

     if (zend_parse_parameters(7 TSRMLS_CC, "rllllll", &filter, &xoff, &yoff, &a, &r, &g, &b) == FAILURE) return;

-    ZEND_FETCH_RESOURCE(fil, Imlib_Filter, &filter, -1, "Imlib Filter", le_imlib_filter);
+    IMLIB_FETCH_RESOURCE(fil, Imlib_Filter, &filter, -1, "Imlib Filter", le_imlib_filter);

     imlib_context_set_filter(fil);

@@ -2305,13 +2314,13 @@ PHP_FUNCTION(imlib_filter_set_alpha)
    Set filter constants */
 PHP_FUNCTION(imlib_filter_constants)
 {
-	zval *filter;
+	imlib_resource filter;
         long a, r, g, b;
 	Imlib_Filter fil;

         if (zend_parse_parameters(5 TSRMLS_CC, "rllll", &filter, &a, &r, &g, &b) == FAILURE) return;

-        ZEND_FETCH_RESOURCE(fil, Imlib_Filter, &filter, -1, "Imlib Filter", le_imlib_filter);
+        IMLIB_FETCH_RESOURCE(fil, Imlib_Filter, &filter, -1, "Imlib Filter", le_imlib_filter);

 	imlib_context_set_filter(fil);
 	imlib_filter_constants(a,r,g,b);
@@ -2322,13 +2331,13 @@ PHP_FUNCTION(imlib_filter_constants)
    Set filter divisors */
 PHP_FUNCTION(imlib_filter_divisors)
 {
-	zval *filter;
+	imlib_resource filter;
         long a, r, g, b;
 	Imlib_Filter fil;

         if (zend_parse_parameters(5 TSRMLS_CC, "rllll", &filter, &a, &r, &g, &b) == FAILURE) return;

-        ZEND_FETCH_RESOURCE(fil, Imlib_Filter, &filter, -1, "Imlib Filter", le_imlib_filter);
+        IMLIB_FETCH_RESOURCE(fil, Imlib_Filter, &filter, -1, "Imlib Filter", le_imlib_filter);

 	imlib_context_set_filter(fil);
 	imlib_filter_divisors(a,r,g,b);
@@ -2341,12 +2350,12 @@ PHP_FUNCTION(imlib_filter_divisors)
 PHP_FUNCTION(imlib_modify_color_modifier_gamma)
 {
     double gamma;
-    zval *color_modifier;
+    imlib_resource color_modifier;
     PHP_Imlib_Color_Modifier cm;

     if (zend_parse_parameters(2 TSRMLS_CC, "rd", &color_modifier, &gamma) == FAILURE) return;

-    ZEND_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
+    IMLIB_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
     if (cm->modified) _php_imlib_color_modifier_synch(cm);
     imlib_context_set_color_modifier(cm->cm);
     imlib_modify_color_modifier_gamma(gamma);
@@ -2361,12 +2370,12 @@ PHP_FUNCTION(imlib_modify_color_modifier_gamma)
 PHP_FUNCTION(imlib_modify_color_modifier_brightness)
 {
     double brightness;
-    zval *color_modifier;
+    imlib_resource color_modifier;
     PHP_Imlib_Color_Modifier cm;

     if (zend_parse_parameters(2 TSRMLS_CC, "rd", &color_modifier, &brightness) == FAILURE) return;

-    ZEND_FETCH_RESOURCE(cm, Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
+    IMLIB_FETCH_RESOURCE(cm, Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
     if (cm->modified) _php_imlib_color_modifier_synch(cm);
     imlib_context_set_color_modifier(cm->cm);
     imlib_modify_color_modifier_brightness(brightness);
@@ -2381,12 +2390,12 @@ PHP_FUNCTION(imlib_modify_color_modifier_brightness)
 PHP_FUNCTION(imlib_modify_color_modifier_contrast)
 {
     double contrast;
-    zval *color_modifier;
+    imlib_resource color_modifier;
     PHP_Imlib_Color_Modifier cm;

     if (zend_parse_parameters(2 TSRMLS_CC, "rd", &color_modifier, &contrast) == FAILURE) return;

-    ZEND_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
+    IMLIB_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
     if (cm->modified) _php_imlib_color_modifier_synch(cm);
     imlib_context_set_color_modifier(cm->cm);
     imlib_modify_color_modifier_contrast(contrast);
@@ -2400,12 +2409,12 @@ PHP_FUNCTION(imlib_modify_color_modifier_contrast)
    Reset color modifier to default (one-to-one) mapping */
 PHP_FUNCTION(imlib_reset_color_modifier)
 {
-    zval *color_modifier;
+    imlib_resource color_modifier;
     PHP_Imlib_Color_Modifier cm;

     if (zend_parse_parameters(1 TSRMLS_CC, "r", &color_modifier) == FAILURE) return;

-    ZEND_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
+    IMLIB_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
     imlib_context_set_color_modifier(cm->cm);
     imlib_reset_color_modifier();
     imlib_context_set_color_modifier(NULL);
@@ -2418,7 +2427,7 @@ PHP_FUNCTION(imlib_reset_color_modifier)
    Apply color modifier to an image or its part */
 PHP_FUNCTION(imlib_apply_color_modifier)
 {
-    zval *color_modifier, *image;
+    imlib_resource color_modifier, image;
     long x, y, width, height;
     PHP_Imlib_Color_Modifier cm;
     Imlib_Image img;
@@ -2427,8 +2436,8 @@ PHP_FUNCTION(imlib_apply_color_modifier)
     argc=ZEND_NUM_ARGS();
     if (zend_parse_parameters(argc TSRMLS_CC, "rr|llll", &image, &color_modifier, &x, &y, &width, &height) == FAILURE) return;

-    ZEND_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
-    ZEND_FETCH_RESOURCE(img, Imlib_Image, &image, -1, "Imlib Image", le_imlib_img);
+    IMLIB_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
+    IMLIB_FETCH_RESOURCE(img, Imlib_Image, &image, -1, "Imlib Image", le_imlib_img);
     if (cm->modified) _php_imlib_color_modifier_synch(cm);
     imlib_context_set_image(img);

@@ -2447,14 +2456,14 @@ PHP_FUNCTION(imlib_apply_color_modifier)

 PHP_FUNCTION(imlib_set_color_modifier)
 {
-    zval *color_modifier;
+    imlib_resource color_modifier;
     long index, value, channels = 15;
     PHP_Imlib_Color_Modifier cm;
     int argc,i=0;

     argc=ZEND_NUM_ARGS();
     if (zend_parse_parameters(argc TSRMLS_CC, "rll|l", &color_modifier, &index, &value, &channels) == FAILURE) return;
-    ZEND_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
+    IMLIB_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
     if (!cm->valid) _php_imlib_color_modifier_synch(cm);
     while (i<4) {
 	if (channels&1)	{
@@ -2473,13 +2482,13 @@ PHP_FUNCTION(imlib_set_color_modifier)

 PHP_FUNCTION(imlib_get_color_modifier)
 {
-    zval *color_modifier;
+    imlib_resource color_modifier;
     zval **tred, **tgreen, **tblue, **talpha;
     long index;
     PHP_Imlib_Color_Modifier cm;

     if (zend_parse_parameters(6 TSRMLS_CC, "rlZZZZ", &color_modifier, &index, &tred, &tgreen, &tblue, &talpha) == FAILURE) return;
-    ZEND_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
+    IMLIB_FETCH_RESOURCE(cm, PHP_Imlib_Color_Modifier, &color_modifier, -1, "Imlib Color Modifier", le_imlib_cm);
     if (!cm->valid) _php_imlib_color_modifier_synch(cm);

     zval_dtor(*tred);
@@ -2504,7 +2513,7 @@ PHP_FUNCTION(imlib_pstext)
 {
 #if HAVE_LIBT1

-	zval *img, *fnt;
+	imlib_resource img, fnt;
 	int i, j, argc;
         long x, y, sz, r, g, b, a, space = 0, width = 0, aa_steps = 4;
 	unsigned long color;
@@ -2527,8 +2536,8 @@ PHP_FUNCTION(imlib_pstext)

 	color=(r<<16)|(g<<8)|b;

-	ZEND_FETCH_RESOURCE(bg_img, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
-        ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
+	IMLIB_FETCH_RESOURCE(bg_img, Imlib_Image, &img, -1, "Imlib Image", le_imlib_img);
+        IMLIB_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);

 	T1_errno = 0;
 	T1_AASetBitsPerPixel(32);
@@ -2631,7 +2640,7 @@ PHP_FUNCTION(imlib_psloadfont)
         char* file;
         int file_len;
         int f_ind, *font;
-        char* filename;
+        zend_string* filename;
         FILE* f;

         if (zend_parse_parameters(1 TSRMLS_CC, "s", &file, &file_len) == FAILURE) return;
@@ -2639,10 +2648,10 @@ PHP_FUNCTION(imlib_psloadfont)
         f = php_stream_open_wrapper_as_file(file, "rb", IGNORE_PATH | ENFORCE_SAFE_MODE | REPORT_ERRORS, &filename);
         if (f == NULL) RETURN_FALSE;

-        f_ind = T1_AddFont(filename);
+        f_ind = T1_AddFont(STR_VAL(filename));

         fclose(f);
-        efree(filename);
+        zend_string_release(filename);

         if (f_ind < 0) {
                 switch (f_ind) {
@@ -2668,7 +2677,7 @@ PHP_FUNCTION(imlib_psloadfont)
         }
         font = (int *) emalloc(sizeof(int));
         *font = f_ind;
-        ZEND_REGISTER_RESOURCE(return_value, font, le_ps_font);
+        IMLIB_REGISTER_RESOURCE(font, le_ps_font);
 #else
         php_error(E_WARNING, "imlib_psloadfont: No T1lib support in this PHP build");
         RETURN_FALSE;
@@ -2681,14 +2690,14 @@ PHP_FUNCTION(imlib_psloadfont)
 PHP_FUNCTION(imlib_psfreefont)
 {
 #if HAVE_LIBT1
-        zval *fnt;
+        imlib_resource fnt;
         int *f_ind;

         if (zend_parse_parameters(1 TSRMLS_CC, "r", &fnt) == FAILURE) return;

-        ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
+        IMLIB_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);

-        zend_list_delete(Z_LVAL_PP(&fnt));
+        IMLIB_DELETE_RESOURCE(&fnt);
         RETURN_TRUE;
 #else
         php_error(E_WARNING, "imlib_psfreefont: No T1lib support in this PHP build");
@@ -2702,25 +2711,25 @@ PHP_FUNCTION(imlib_psfreefont)
 PHP_FUNCTION(imlib_psencodefont)
 {
 #if HAVE_LIBT1
-        zval *fnt;
+        imlib_resource fnt;
         char *enc;
         int enc_len;
         char **enc_vector;
         int *f_ind;
-        char* filename;
+        zend_string* filename;
         FILE* f;

         if (zend_parse_parameters(2 TSRMLS_CC, "rs", &fnt, &enc, &enc_len) == FAILURE) return;

-        ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
+        IMLIB_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);

         f = php_stream_open_wrapper_as_file(enc, "rb", IGNORE_PATH | ENFORCE_SAFE_MODE | REPORT_ERRORS, &filename);
         if (f == NULL) RETURN_FALSE;

-        enc_vector = T1_LoadEncoding(filename);
+        enc_vector = T1_LoadEncoding(STR_VAL(filename));

         fclose(f);
-        efree(filename);
+        zend_string_release(filename);

         if (enc_vector == NULL) {
                 php_error(E_WARNING, "Couldn't load encoding vector from %s", enc);
@@ -2747,13 +2756,13 @@ PHP_FUNCTION(imlib_psencodefont)
 PHP_FUNCTION(imlib_psextendfont)
 {
 #if HAVE_LIBT1
-        zval *fnt;
+        imlib_resource fnt;
         double ext;
         int *f_ind;

         if (zend_parse_parameters(2 TSRMLS_CC, "rd", &fnt, &ext) == FAILURE) return;

-        ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
+        IMLIB_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);

         if (T1_ExtendFont(*f_ind, ext) != 0) RETURN_FALSE;

@@ -2770,13 +2779,13 @@ PHP_FUNCTION(imlib_psextendfont)
 PHP_FUNCTION(imlib_psslantfont)
 {
 #if HAVE_LIBT1
-        zval *fnt;
+        imlib_resource fnt;
         double slt;
         int *f_ind;

         if (zend_parse_parameters(2 TSRMLS_CC, "rd", &fnt, &slt) == FAILURE) return;

-        ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
+        IMLIB_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);

         if (T1_SlantFont(*f_ind, slt) != 0) RETURN_FALSE;
         RETURN_TRUE;
@@ -2792,7 +2801,7 @@ PHP_FUNCTION(imlib_psslantfont)
 PHP_FUNCTION(imlib_psbbox)
 {
 #if HAVE_LIBT1
-	zval *fnt;
+	imlib_resource fnt;
         char* str;
         int str_len;
         long sz, space = 0, add_width = 0;
@@ -2810,7 +2819,7 @@ PHP_FUNCTION(imlib_psbbox)
 	cos_a = cos(angle);
 	per_char =  add_width || angle ? 1 : 0;

-	ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
+	IMLIB_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);

 #define max(a, b) (a > b ? a : b)
 #define min(a, b) (a < b ? a : b)
diff --git a/php_imlib.h b/php_imlib.h
index 5f66094..7df29c5 100644
--- a/php_imlib.h
+++ b/php_imlib.h
@@ -23,6 +23,7 @@
 #include "X11/Xlib.h"
 #include <Imlib2.h>
 #include "ltdl.h"
+#include "php_ini.h"
 #include "php_config.h"
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -154,13 +155,56 @@ typedef struct
 } PHP_Imlib_Color_Modifier_struct;
 typedef PHP_Imlib_Color_Modifier_struct *PHP_Imlib_Color_Modifier;

-Imlib_Image _php_imlib_get_image(zval** im_resource);
-Imlib_Filter _php_imlib_get_filter(zval** fil_resource);
-Imlib_Font _php_imlib_get_font(zval** font_resource);
-Imlib_TTF_Encoding _php_imlib_get_font_encoding(zval** font_resource);
-Imlib_Color_Range _php_imlib_get_cr(zval** cr_resource);
-ImlibPolygon _php_imlib_get_poly(zval** poly_resource);
-Imlib_Color_Modifier _php_imlib_get_cm(zval** cm_resource);
+#if PHP_VERSION_ID >= 70000
+  typedef zval imlib_zval;
+  typedef zend_resource imlib_resource;
+# define IMLIB_TO_ZVAL(v) v
+# define Z_LVAL_PP(v) Z_LVAL_P(v)
+# define Z_DVAL_PP(v) Z_DVAL_P(v)
+# define STR_VAL(k) (k)->val
+# define IMLIB_DELETE_RESOURCE(v) zend_list_delete(v)
+# define IMLIB_RETURN_STRING(v) RETURN_STRING(v)
+# define ENFORCE_SAFE_MODE 0
+# define IMLIB_FETCH_RESOURCE(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type)	\
+	rsrc = (rsrc_type) zend_fetch_resource(passed_id TSRMLS_CC, resource_type_name, resource_type)
+# define IMLIB_REGISTER_RESOURCE(result, le_result) RETURN_RES(zend_register_resource(result, le_result))
+# define IMLIB_RSRC_DTOR_FUNC(name) ZEND_RSRC_DTOR_FUNC(name)
+#else
+  typedef zval* imlib_zval;
+  typedef imlib_zval imlib_resource;
+  typedef char zend_string;
+# define IMLIB_TO_ZVAL(v) *v
+# define STR_VAL(k) (k)
+# define IMLIB_DELETE_RESOURCE(v) zend_list_delete(Z_LVAL_PP(v))
+# define IMLIB_RETURN_STRING(v) RETURN_STRING(v, 1)
+# define IMLIB_FETCH_RESOURCE(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type)	\
+	rsrc = (rsrc_type) zend_fetch_resource(passed_id TSRMLS_CC, default_id, resource_type_name, NULL, 1, resource_type)
+# define IMLIB_REGISTER_RESOURCE(result, le_result) ZEND_REGISTER_RESOURCE(return_value, result, le_result)
+  static inline void zend_string_release(zend_string *s)
+  {
+    efree(s);
+  }
+# define IMLIB_RSRC_DTOR_FUNC(name) void name(zend_rsrc_list_entry *res TSRMLS_DC)
+# define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
+  HashPosition _pos; \
+  zend_hash_internal_pointer_reset_ex(ht, &_pos); \
+  for(; _pos; zend_hash_move_forward_ex(ht, &_pos)) \
+  { \
+    int _retval=zend_hash_get_current_key_ex(ht, &_key, NULL, &_h, 0, &_pos); \
+    if (_retval!=HASH_KEY_IS_STRING) _key = NULL; \
+    zend_hash_get_current_data_ex(ht, (void**)&_val, &_pos);
+# define ZEND_HASH_FOREACH_END() \
+  }
+# define Z_RES_P(v) v
+#endif
+
+Imlib_Image _php_imlib_get_image(imlib_resource *im_resource);
+Imlib_Filter _php_imlib_get_filter(imlib_resource *fil_resource);
+Imlib_Font _php_imlib_get_font(imlib_resource *font_resource);
+Imlib_TTF_Encoding _php_imlib_get_font_encoding(imlib_resource *font_resource);
+Imlib_Color_Range _php_imlib_get_cr(imlib_resource *cr_resource);
+ImlibPolygon _php_imlib_get_poly(imlib_resource *poly_resource);
+Imlib_Color_Modifier _php_imlib_get_cm(imlib_resource *cm_resource);

 struct php_imlib_filter
 {
@@ -194,14 +238,11 @@ ZEND_END_MODULE_GLOBALS(imlib)
 # define IMLIBG(v) (imlib_globals.v)
 #endif

-#else
+#else	/* HAVE_IMLIB */

 #define phpext_imlib_ptr NULL

-#endif
-
-#define MY_ZEND_FETCH_RESOURCE(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type)	\
-	rsrc = (rsrc_type) zend_fetch_resource(passed_id TSRMLS_CC, default_id, resource_type_name, NULL, 1, resource_type)
+#endif	/* HAVE_IMLIB */

 #endif	/* _PHP_IMLIB_H */
ViewGit