summaryrefslogtreecommitdiffhomepage
path: root/php_imlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'php_imlib.c')
-rw-r--r--php_imlib.c3210
1 files changed, 3210 insertions, 0 deletions
diff --git a/php_imlib.c b/php_imlib.c
new file mode 100644
index 0000000..fdb60b4
--- /dev/null
+++ b/php_imlib.c
@@ -0,0 +1,3210 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP version 4.0 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 2.02 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available at through the world-wide-web at |
+ | http://www.php.net/license/2_02.txt. |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Authors: Matt McClanahan <cardinal@dodds.net> |
+ +----------------------------------------------------------------------+
+ */
+
+#include "php.h"
+#include "php_ini.h"
+#include "php_imlib.h"
+#include "php_globals.h"
+
+/* #define IMLIB_DEBUG */
+
+#ifdef IMLIB_DEBUG
+#define ID_PRINTF(format, args...) \
+fprintf(stderr, "php-imlib: " format "\n" , ## args); fflush(stdout)
+#endif
+
+#if HAVE_IMLIB
+#ifdef ZTS
+int gd_imlib_id;
+#else
+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 int le_imlib_cr;
+static int le_imlib_font;
+static int le_imlib_img;
+static int le_imlib_poly;
+static int le_imlib_filter;
+static int le_imlib_cm;
+#if HAVE_LIBT1
+static int le_ps_font, le_ps_enc;
+#endif
+
+function_entry imlib_functions[] = {
+ PHP_FE(imlib_add_color_to_color_range,NULL)
+ PHP_FE(imlib_blend_image_onto_image,NULL)
+ PHP_FE(imlib_clone_image,NULL)
+ PHP_FE(imlib_create_color_range,NULL)
+ PHP_FE(imlib_create_color_modifier,NULL)
+ PHP_FE(imlib_create_cropped_image,NULL)
+ PHP_FE(imlib_create_cropped_scaled_image,NULL)
+ PHP_FE(imlib_create_image,NULL)
+ PHP_FE(imlib_create_rotated_image,NULL)
+ PHP_FE(imlib_create_scaled_image,NULL)
+ PHP_FE(imlib_dump_image,second_arg_force_ref)
+ PHP_FE(imlib_free_color_range,NULL)
+ PHP_FE(imlib_free_color_modifier,NULL)
+ PHP_FE(imlib_free_font,NULL)
+ PHP_FE(imlib_free_image,NULL)
+ PHP_FE(imlib_get_text_size,third_and_fourth_arg_force_ref)
+ PHP_FE(imlib_get_text_advance,third_and_fourth_arg_force_ref)
+ PHP_FE(imlib_image_blur,NULL)
+ PHP_FE(imlib_image_draw_ellipse,NULL)
+ PHP_FE(imlib_image_draw_line,NULL)
+ PHP_FE(imlib_image_draw_polygon,NULL)
+ PHP_FE(imlib_image_draw_rectangle,NULL)
+ PHP_FE(imlib_image_fill_color_range_rectangle,NULL)
+ PHP_FE(imlib_image_fill_ellipse,NULL)
+ PHP_FE(imlib_image_fill_polygon,NULL)
+ PHP_FE(imlib_image_fill_rectangle,NULL)
+ PHP_FE(imlib_image_flip_horizontal,NULL)
+ PHP_FE(imlib_image_flip_vertical,NULL)
+ PHP_FE(imlib_image_flip_diagonal,NULL)
+ PHP_FE(imlib_image_orientate,NULL)
+ PHP_FE(imlib_image_format,NULL)
+ PHP_FE(imlib_image_get_filename,NULL)
+ PHP_FE(imlib_image_get_height,NULL)
+ PHP_FE(imlib_image_get_width,NULL)
+ PHP_FE(imlib_image_has_alpha,NULL)
+ PHP_FE(imlib_image_modify_alpha,NULL)
+ PHP_FE(imlib_image_sharpen,NULL)
+ PHP_FE(imlib_image_set_format,NULL)
+ PHP_FE(imlib_image_tile_horizontal,NULL)
+ PHP_FE(imlib_image_tile_vertical,NULL)
+ PHP_FE(imlib_image_tile,NULL)
+ PHP_FE(imlib_list_fonts,NULL)
+ PHP_FE(imlib_load_font,NULL)
+ PHP_FE(imlib_load_image,second_arg_force_ref)
+ PHP_FALIAS(imlib_load_image_with_error_return,imlib_load_image,second_arg_force_ref)
+ PHP_FE(imlib_polygon_add_point,NULL)
+ PHP_FE(imlib_polygon_contains_point,NULL)
+ PHP_FE(imlib_polygon_free,NULL)
+ PHP_FE(imlib_polygon_get_bounds,second_through_fifth_arg_force_ref)
+ PHP_FE(imlib_polygon_new,NULL)
+ PHP_FE(imlib_save_image,third_arg_force_ref)
+ PHP_FALIAS(imlib_save_image_with_error_return,imlib_save_image,third_arg_force_ref)
+ PHP_FE(imlib_text_draw,NULL)
+ PHP_FE(imlib_get_cache_size,NULL)
+ PHP_FE(imlib_set_cache_size,NULL)
+ PHP_FE(imlib_create_filter,NULL)
+ PHP_FE(imlib_free_filter,NULL)
+ PHP_FE(imlib_image_filter,NULL)
+ PHP_FE(imlib_filter_set,NULL)
+ PHP_FE(imlib_filter_set_alpha,NULL)
+ PHP_FE(imlib_filter_set_red,NULL)
+ PHP_FE(imlib_filter_set_green,NULL)
+ PHP_FE(imlib_filter_set_blue,NULL)
+ PHP_FE(imlib_filter_constants,NULL)
+ PHP_FE(imlib_filter_divisors,NULL)
+ PHP_FE(imlib_apply_filter,NULL)
+ PHP_FE(imlib_modify_color_modifier_gamma,NULL)
+ PHP_FE(imlib_modify_color_modifier_brightness,NULL)
+ PHP_FE(imlib_modify_color_modifier_contrast,NULL)
+ PHP_FE(imlib_reset_color_modifier,NULL)
+ PHP_FE(imlib_apply_color_modifier,NULL)
+ PHP_FE(imlib_set_color_modifier,NULL)
+ PHP_FE(imlib_get_color_modifier,third_through_sixth_arg_force_ref)
+ PHP_FE(imlib_psloadfont,NULL)
+ PHP_FE(imlib_psfreefont,NULL)
+ PHP_FE(imlib_psencodefont,NULL)
+ PHP_FE(imlib_psextendfont,NULL)
+ PHP_FE(imlib_psslantfont,NULL)
+ PHP_FE(imlib_pstext,NULL)
+ PHP_FE(imlib_psbbox,NULL)
+
+ {NULL, NULL, NULL}
+};
+
+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)
+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()
+
+zend_module_entry imlib_module_entry = {
+ STANDARD_MODULE_HEADER,
+ "imlib",
+ imlib_functions,
+ PHP_MINIT(imlib),
+ PHP_MSHUTDOWN(imlib),
+ PHP_RINIT(imlib),
+ PHP_RSHUTDOWN(imlib),
+ PHP_MINFO(imlib),
+ NO_VERSION_YET,
+ STANDARD_MODULE_PROPERTIES
+};
+
+PHP_INI_MH(OnUpdateFontCacheSize)
+{
+ int size;
+ if (sscanf(new_value,"%d",&size)==1)
+ {
+ imlib_set_font_cache_size(size);
+ return SUCCESS;
+ }
+ else
+ {
+ return FAILURE;
+ }
+}
+
+static void _php_imlib_free_cm(PHP_Imlib_Color_Modifier mod)
+{
+ imlib_context_set_color_modifier(mod->cm);
+ imlib_free_color_modifier();
+ efree(mod);
+}
+
+static void _php_imlib_free_filter(Imlib_Filter fil)
+{
+ imlib_context_set_filter(fil);
+ imlib_free_filter();
+}
+
+static void _php_imlib_free_cr(Imlib_Color_Range cr)
+{
+ imlib_context_set_color_range(cr);
+ imlib_free_color_range();
+}
+
+static void _php_imlib_free_font(PHP_Imlib_Font fn)
+{
+ imlib_context_set_font(fn->font);
+ imlib_free_font();
+ efree(fn);
+}
+
+static void _php_imlib_free_img(Imlib_Image im)
+{
+ imlib_context_set_image(im);
+ imlib_free_image();
+}
+
+static void _php_imlib_free_poly(ImlibPolygon poly)
+{
+ imlib_polygon_free(poly);
+}
+
+void _php_imlib_set_cache_size(int size)
+{
+ IMLIBLS_FETCH();
+ if (size>IMLIBG(max_cache_size)) {
+ size=IMLIBG(max_cache_size);
+ }
+ if (size<IMLIBG(min_cache_size)) {
+ size=IMLIBG(min_cache_size);
+ }
+ imlib_set_cache_size(size);
+}
+
+struct php_imlib_filter* _php_imlib_load_filter(char* file)
+{
+ struct php_imlib_filter* ptr=NULL;
+ struct php_imlib_filter_info* info=NULL;
+ char* path;
+ int initrv=FAILURE;
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Loading library %s...",file);
+#endif
+ ptr=calloc(1,sizeof(struct php_imlib_filter));
+ if (ptr==NULL) goto error;
+ ptr->filename=strdup(file);
+ ptr->handle=lt_dlopenext(file);
+ if (!ptr->handle) goto error;
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Library loaded, handle=%p. Looking for entry points...",ptr->handle);
+#endif
+ ptr->init=lt_dlsym(ptr->handle,"php_filter_init");
+ ptr->deinit=lt_dlsym(ptr->handle,"php_filter_deinit");
+ ptr->exec=lt_dlsym(ptr->handle,"php_filter_exec");
+ ptr->getfiltercount=lt_dlsym(ptr->handle,"php_filter_getfiltercount");
+ ptr->getfilternames=lt_dlsym(ptr->handle,"php_filter_getfilternames");
+ ptr->getextinfo=lt_dlsym(ptr->handle,"php_filter_getextinfo");
+ ptr->getparams=lt_dlsym(ptr->handle,"php_filter_getparams");
+
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Function pointers: init=%p, deinit=%p, exec=%p, count=%p, names=%p",ptr->init,ptr->deinit,ptr->exec,ptr->getfiltercount,ptr->getfilternames);
+#endif
+ if (!ptr->getfiltercount || !ptr->getfilternames || !ptr->exec) goto error;
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Getting filter info...");
+#endif
+ if (ptr->init)
+ {
+ initrv=ptr->init();
+ if (initrv==FAILURE) goto error;
+ }
+ ptr->filters=ptr->getfiltercount();
+ ptr->names=malloc(ptr->filters*sizeof(char*));
+ if (!ptr->names) goto error;
+ ptr->getfilternames(ptr->names);
+ ptr->next=NULL;
+ return ptr;
+error:
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Error loading library!");
+#endif
+ if (ptr)
+ {
+ if (ptr->handle)
+ {
+ if (ptr->deinit)
+ {
+ if (initrv==SUCCESS) ptr->deinit();
+ }
+ lt_dlclose(ptr->handle);
+ }
+ if (ptr->names) free(ptr->names);
+ if (ptr->filename) free(ptr->filename);
+ free(ptr);
+ }
+ return NULL;
+}
+
+void _php_imlib_unload_filter(struct php_imlib_filter* ptr)
+{
+ if (ptr==NULL) return;
+ if (ptr->deinit) ptr->deinit();
+ if (ptr->names) free(ptr->names);
+ if (ptr->filename) free(ptr->filename);
+ lt_dlclose(ptr->handle);
+ free(ptr);
+}
+
+void _php_imlib_unload_filters()
+{
+ struct php_imlib_filter* f_ptr;
+
+ IMLIBLS_FETCH();
+ f_ptr=IMLIBG(filters);
+ while (f_ptr!=NULL)
+ {
+ struct php_imlib_filter* f_ptr_next;
+ f_ptr_next=f_ptr->next;
+ _php_imlib_unload_filter(f_ptr);
+ f_ptr=f_ptr_next;
+ }
+ IMLIBG(filters)=NULL;
+
+ lt_dlexit();
+}
+
+void _php_imlib_load_filters()
+{
+ struct php_imlib_filter* f_ptr;
+ char* path;
+ DIR* dirp=NULL;
+ struct dirent *dp;
+ char** names=NULL;
+ int names_size,names_index,i;
+ int ltinitrv=0;
+
+ IMLIBLS_FETCH();
+ f_ptr=IMLIBG(filters);
+ if (f_ptr!=NULL) return;
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Initializing libltdl...");
+#endif
+ if ((ltinitrv=lt_dlinit())>0) goto error;
+
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Loading filters...");
+#endif
+ path=IMLIBG(filter_path);
+ lt_dladdsearchdir(path);
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Opening directory %s...",path);
+#endif
+ if ((dirp=opendir(path))==NULL) goto error;
+
+ names_size=20;
+ names_index=0;
+ if ((names=malloc(names_size*sizeof(char*)))==NULL) goto error;
+
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Scanning directory...");
+#endif
+ errno=0;
+ while ((dp=readdir(dirp))!=NULL)
+ {
+ char* name;
+
+ if ((name=strdup(dp->d_name))==NULL) goto error;
+ if ((strcmp(name, ".")) && (strcmp(name, "..")))
+ {
+ char* ext;
+
+ ext=strchr(name,'.');
+ if (ext)
+ {
+ *ext='\0';
+ for (i=0;i<names_index;i++)
+ {
+ if (strcmp(names[i],name)==0)
+ break;
+ }
+ if (i==names_index)
+ {
+ if (names_index==names_size)
+ {
+ char** names_new;
+
+ names_size*=2;
+ names_new=realloc(names,names_size*sizeof(char*));
+ if (names_new==NULL)
+ {
+ free(name);
+ goto error;
+ }
+ names=names_new;
+ }
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Found library %s",name);
+#endif
+ names[names_index++]=name;
+ }
+ }
+ }
+ }
+ if (errno!=0) goto error;
+ if (names_index>0)
+ {
+ IMLIBG(filters)=f_ptr=malloc(sizeof(struct php_imlib_filter));
+
+ for (i=0;i<names_index;i++)
+ {
+ if ((f_ptr->next=_php_imlib_load_filter(names[i]))!=NULL)
+ f_ptr=f_ptr->next;
+ }
+ IMLIBG(filters)=IMLIBG(filters)->next;
+ }
+ goto cleanup;
+
+error:
+#ifdef IMLIB_DEBUG
+ ID_PRINTF("Error loading filters!");
+#endif
+ _php_imlib_unload_filters();
+cleanup:
+ if (names!=NULL)
+ {
+ for (i=0;i<names_index;i++)
+ {
+ free(names[i]);
+ }
+ free(names);
+ }
+ if (dirp!=NULL) closedir(dirp);
+}
+
+struct php_imlib_filter* _php_imlib_find_filter(char *filter_name)
+{
+ struct php_imlib_filter* f_ptr;
+ int i;
+
+ _php_imlib_load_filters();
+ IMLIBLS_FETCH();
+ f_ptr=IMLIBG(filters);
+ while (f_ptr!=NULL)
+ {
+ for (i=0;i<f_ptr->filters;i++)
+ {
+ if (strcmp(f_ptr->names[i],filter_name)==0)
+ {
+ return f_ptr;
+ }
+ }
+ f_ptr=f_ptr->next;
+ }
+ return NULL;
+}
+
+Imlib_Image _php_imlib_get_image(zval** im_resource)
+{
+ Imlib_Image im;
+ TSRMLS_FETCH();
+ MY_ZEND_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 fil;
+ TSRMLS_FETCH();
+ MY_ZEND_FETCH_RESOURCE(fil, Imlib_Filter, fil_resource, -1, "Imlib Filter", le_imlib_filter);
+ return fil;
+}
+
+void _php_imlib_color_modifier_synch(PHP_Imlib_Color_Modifier cm)
+{
+ if ((cm->modified)&&(!cm->valid))
+ {
+ fprintf(stderr,"Panic!! Color Modifier in invalid state!");
+ return;
+ }
+ imlib_context_set_color_modifier(cm->cm);
+ if (cm->modified)
+ {
+ imlib_set_color_modifier_tables(cm->channels[0],cm->channels[1],cm->channels[2],cm->channels[3]);
+ cm->modified=0;
+ }
+ if (!cm->valid)
+ {
+ imlib_get_color_modifier_tables(cm->channels[0],cm->channels[1],cm->channels[2],cm->channels[3]);
+ cm->valid=1;
+ }
+ imlib_context_set_color_modifier(NULL);
+}
+
+Imlib_Color_Modifier _php_imlib_get_cm(zval** 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);
+ if (cm->modified) _php_imlib_color_modifier_synch(cm);
+ cm->valid=0;
+ return cm->cm;
+}
+
+Imlib_Font _php_imlib_get_font(zval** 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);
+ return php_font->font;
+}
+
+Imlib_TTF_Encoding _php_imlib_get_encoding(zval** 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);
+ return php_font->enc;
+}
+
+Imlib_Color_Range _php_imlib_get_cr(zval** 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);
+ return cr;
+}
+
+ImlibPolygon _php_imlib_get_poly(zval** poly_resource)
+{
+ ImlibPolygon poly;
+ TSRMLS_FETCH();
+ MY_ZEND_FETCH_RESOURCE(poly, ImlibPolygon, poly_resource, -1, "Imlib Polygon", le_imlib_poly);
+ return poly;
+}
+
+#if HAVE_LIBT1
+
+/* {{{ _php_imlib_free_ps_font
+ */
+void _php_imlib_free_ps_font(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+{
+ int *font = (int *)rsrc->ptr;
+
+ T1_DeleteFont(*font);
+ efree(font);
+}
+/* }}} */
+
+/* {{{ _php_imlib_free_ps_enc
+ */
+void _php_imlib_free_ps_enc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
+{
+ char **enc = (char **)rsrc->ptr;
+
+ T1_DeleteEncoding(enc);
+}
+/* }}} */
+
+#endif
+
+
+#ifdef COMPILE_DL_IMLIB
+ZEND_GET_MODULE(imlib)
+#endif
+
+PHP_MINIT_FUNCTION(imlib)
+{
+ IMLIBLS_FETCH();
+ IMLIBG(filters)=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);
+ le_imlib_poly = register_list_destructors(_php_imlib_free_poly,NULL);
+ le_imlib_filter = register_list_destructors(_php_imlib_free_filter,NULL);
+ le_imlib_cm = register_list_destructors(_php_imlib_free_cm,NULL);
+#if HAVE_LIBT1
+ T1_SetBitmapPad(8);
+ T1_InitLib(NO_LOGFILE|IGNORE_CONFIGFILE|IGNORE_FONTDATABASE);
+ T1_SetLogLevel(T1LOG_DEBUG);
+ le_ps_font = zend_register_list_destructors_ex(_php_imlib_free_ps_font, NULL, "imlib PS font", module_number);
+ le_ps_enc = zend_register_list_destructors_ex(_php_imlib_free_ps_enc, NULL, "imlib PS encoding", module_number);
+#endif
+
+ REGISTER_LONG_CONSTANT("IMLIB_TEXT_TO_RIGHT", IMLIB_TEXT_TO_RIGHT, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_TEXT_TO_LEFT", IMLIB_TEXT_TO_LEFT, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_TEXT_TO_DOWN", IMLIB_TEXT_TO_DOWN, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_TEXT_TO_UP", IMLIB_TEXT_TO_UP, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_TEXT_TO_ANGLE", IMLIB_TEXT_TO_ANGLE, CONST_CS|CONST_PERSISTENT);
+
+ REGISTER_LONG_CONSTANT("IMLIB_TTF_ENCODING_ISO_8859_1", IMLIB_TTF_ENCODING_ISO_8859_1, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_TTF_ENCODING_ISO_8859_2", IMLIB_TTF_ENCODING_ISO_8859_2, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_TTF_ENCODING_ISO_8859_3", IMLIB_TTF_ENCODING_ISO_8859_3, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_TTF_ENCODING_ISO_8859_4", IMLIB_TTF_ENCODING_ISO_8859_4, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_TTF_ENCODING_ISO_8859_5", IMLIB_TTF_ENCODING_ISO_8859_5, CONST_CS|CONST_PERSISTENT);
+
+ REGISTER_LONG_CONSTANT("IMLIB_CHANNEL_RED", 1, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_CHANNEL_GREEN", 2, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_CHANNEL_BLUE", 4, CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_CHANNEL_ALPHA", 8, CONST_CS|CONST_PERSISTENT);
+
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_NONE", IMLIB_LOAD_ERROR_NONE,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST",
+ IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY",
+ IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ",
+ IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT",
+ IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_PATH_TOO_LONG",
+ IMLIB_LOAD_ERROR_PATH_TOO_LONG,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT",
+ IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY",
+ IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE",
+ IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS",
+ IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_OUT_OF_MEMORY",
+ IMLIB_LOAD_ERROR_OUT_OF_MEMORY,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS",
+ IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE",
+ IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE",
+ IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE,
+ CONST_CS|CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("IMLIB_LOAD_ERROR_UNKNOWN", IMLIB_LOAD_ERROR_UNKNOWN,
+ CONST_CS|CONST_PERSISTENT);
+
+ REGISTER_INI_ENTRIES();
+
+ return SUCCESS;
+}
+
+PHP_MSHUTDOWN_FUNCTION(imlib)
+{
+ _php_imlib_unload_filters();
+
+ UNREGISTER_INI_ENTRIES();
+
+ return SUCCESS;
+}
+
+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));
+ font_list = imlib_list_font_path(&font_count);
+ if (!font_count) {
+ IMLIBG(saved_path) = NULL;
+ } else {
+ /* Save the current path where we can retrieve it later. */
+ IMLIBG(saved_path) = (char **) emalloc(sizeof(char *) * (font_count + 1));
+ for (i = 0; i < font_count; i++) {
+ IMLIBG(saved_path)[i] = estrdup(font_list[i]);
+ }
+ IMLIBG(saved_path)[i] = NULL;
+
+ /* Now clear the imlib path... */
+ for (i = 0; i < font_count; i++) {
+ imlib_remove_path_from_font_path(IMLIBG(saved_path)[i]);
+ }
+ }
+
+ if (!IMLIBG(font_path)) {
+ return SUCCESS;
+ }
+
+ /* And repopulate it with the ini-specified default. */
+ ptr = pathbuf = estrdup(IMLIBG(font_path));
+
+ while (ptr && *ptr) {
+ end = strchr(ptr, ':');
+
+ if (end != NULL) {
+ *end = '\0';
+ end++;
+ }
+ imlib_add_path_to_font_path(ptr);
+ ptr = end;
+ }
+ efree(pathbuf);
+
+ return SUCCESS;
+}
+
+PHP_RSHUTDOWN_FUNCTION(imlib)
+{
+ char **font_list;
+ int i, font_count;
+ IMLIBLS_FETCH();
+
+ /* Restore cache size */
+ _php_imlib_set_cache_size(0);
+ /* Retrieve the current path and clear it out. */
+ font_list = imlib_list_font_path(&font_count);
+ for (i = 0; i < font_count; i++) {
+ imlib_remove_path_from_font_path(font_list[i]);
+ }
+
+ /* Restore the saved font path. */
+ if (!IMLIBG(saved_path)) {
+ return SUCCESS;
+ }
+ for (i = 0; IMLIBG(saved_path)[i] != NULL; i++) {
+ imlib_add_path_to_font_path(IMLIBG(saved_path)[i]);
+ efree(IMLIBG(saved_path)[i]);
+ }
+
+ efree(IMLIBG(saved_path));
+ return SUCCESS;
+}
+
+PHP_MINFO_FUNCTION(imlib)
+{
+ struct php_imlib_filter* f_ptr;
+ int i;
+
+ _php_imlib_load_filters();
+ IMLIBLS_FETCH();
+ f_ptr=IMLIBG(filters);
+
+ DISPLAY_INI_ENTRIES();
+
+#ifdef HAVE_LIBT1
+ php_info_print_table_start();
+ php_info_print_table_row(2, "T1Lib Support", "enabled");
+ php_info_print_table_end();
+#endif
+
+ while (f_ptr!=NULL)
+ {
+ char* extinfo[3] = {NULL, NULL, NULL};
+ php_info_print_table_start();
+ if (f_ptr->getextinfo) f_ptr->getextinfo(extinfo);
+ if (extinfo[0])
+ {
+ char* infostr;
+ f_ptr->getextinfo(extinfo);
+ infostr=emalloc((strlen(extinfo[0])+strlen(f_ptr->filename)+4)*sizeof(char));
+ strcpy(infostr,extinfo[0]);
+ strcat(infostr," (");
+ strcat(infostr,f_ptr->filename);
+ strcat(infostr,")");
+ php_info_print_table_row(2, "Filter name", infostr);
+ efree(infostr);
+ }
+ else
+ {
+ php_info_print_table_row(2, "Filter name", f_ptr->filename);
+ }
+ if (extinfo[1]) php_info_print_table_row(2,"Description",extinfo[1]);
+ if (extinfo[2]) php_info_print_table_row(2,"Author",extinfo[2]);
+
+ {
+ char* filters;
+ filters=estrdup("");
+ for (i=0;i<f_ptr->filters;i++)
+ {
+ int newlen;
+ char* params=NULL;
+ if (f_ptr->getparams) params=f_ptr->getparams(f_ptr->names[i]);
+ newlen=strlen(filters)+strlen(f_ptr->names[i])+2;
+ if (params) newlen+=strlen(params)+3;
+ filters=erealloc(filters,newlen*sizeof(char));
+ if (filters)
+ {
+ strcat(filters,f_ptr->names[i]);
+ if (params)
+ {
+ strcat(filters," (");
+ strcat(filters,params);
+ strcat(filters,")");
+ }
+ strcat(filters,"\n");
+ }
+ }
+ php_info_print_table_row(2,"Filters",filters);
+ efree(filters);
+ }
+ f_ptr=f_ptr->next;
+ php_info_print_table_end();
+ }
+}
+
+static void _php_convert_four_longs(zval **zone, zval **ztwo, zval **zthree,
+ zval **zfour, int *one, int *two,
+ int *three, int *four)
+{
+ 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);
+}
+
+static int _php_handle_cliprect_array(zval **dbox, char *func_name,
+ int *x, int *y, int *w, int *h)
+{
+ zval **element, ***box_coords;
+ int i,arrcount;
+ HashTable *box;
+
+ box = HASH_OF(*dbox);
+ if (!box) {
+ php_error(E_WARNING, "Wrong datatype in call to %s, need array",func_name);
+ return 0;
+ }
+
+ arrcount = zend_hash_num_elements(box);
+ if (arrcount != 4) {
+ php_error(E_WARNING, "Wrong number of array elements in call to %s, need four (x,y,w,h)",
+ func_name);
+ return 0;
+ }
+
+ box_coords = (zval ***)emalloc(arrcount * sizeof(zval **));
+
+ for (i = 0; i < arrcount; i++)
+ {
+ if (zend_hash_index_find(box, i, (void **)&element) == SUCCESS)
+ {
+ convert_to_long_ex(element);
+ box_coords[i] = element;
+ }
+ }
+ *x = Z_LVAL_PP(box_coords[0]);
+ *y = Z_LVAL_PP(box_coords[1]);
+ *w = Z_LVAL_PP(box_coords[2]);
+ *h = Z_LVAL_PP(box_coords[3]);
+
+ efree(box_coords);
+ return 1;
+}
+
+static int _php_handle_imlib_error(INTERNAL_FUNCTION_PARAMETERS,
+ Imlib_Load_Error err, const char *img)
+{
+ int errnum;
+
+ switch (err)
+ {
+ case IMLIB_LOAD_ERROR_NONE:
+ errnum = 0;
+ break;
+ case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST:
+ php_error(E_WARNING, "%s - File does not exist", img);
+ errnum = 1;
+ break;
+ case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY:
+ php_error(E_WARNING, "%s - Directory specified for image filename", img);
+ errnum = 2;
+ break;
+ case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ:
+ php_error(E_WARNING, "%s - No read access to directory", img);
+ errnum = 3;
+ break;
+ case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT:
+ php_error(E_WARNING, "%s - No Imlib2 loader for that file format", img);
+ errnum = 4;
+ break;
+ case IMLIB_LOAD_ERROR_PATH_TOO_LONG:
+ php_error(E_WARNING, "%s - Path specified is too long", img);
+ errnum = 5;
+ break;
+ case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT:
+ php_error(E_WARNING, "%s - Path component does not exist", img);
+ errnum = 6;
+ break;
+ case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY:
+ php_error(E_WARNING, "%s - Path component is not a directory", img);
+ errnum = 7;
+ break;
+ case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE:
+ php_error(E_WARNING, "%s - Path points outside address space", img);
+ errnum = 8;
+ break;
+ case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS:
+ php_error(E_WARNING, "%s - Too many levels of symbolic links", img);
+ errnum = 9;
+ break;
+ case IMLIB_LOAD_ERROR_OUT_OF_MEMORY:
+ php_error(E_WARNING, "While loading %s - Out of memory", img);
+ errnum = 10;
+ break;
+ case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS:
+ php_error(E_WARNING, "While loading %s - Out of file descriptors", img);
+ errnum = 11;
+ break;
+ case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE:
+ php_error(E_WARNING, "%s - Cannot write to directory", img);
+ errnum = 12;
+ break;
+ case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE:
+ php_error(E_WARNING, "%s - Cannot write - out of disk space", img);
+ errnum = 13;
+ break;
+ case IMLIB_LOAD_ERROR_UNKNOWN:
+ default:
+ php_error(E_WARNING, "While loading %s - Unknown error.", img);
+ errnum = 14;
+ }
+ return errnum;
+}
+
+static void _php_imlib_draw_something(INTERNAL_FUNCTION_PARAMETERS, void (*func)(), char *func_name)
+{
+ 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) {
+ WRONG_PARAM_COUNT;
+ }
+
+ ZEND_FETCH_RESOURCE(im, Imlib_Image, img, -1, "Imlib Image", le_imlib_img);
+
+ _php_convert_four_longs(d1,d2,d3,d4,&x,&y,&w,&h);
+ _php_convert_four_longs(dr,dg,db,da,&r,&g,&b,&a);
+
+ imlib_context_set_image(im);
+ imlib_context_set_color(r,g,b,a);
+
+ /* FIXME: This should take an optional antialias option, with on as default */
+ imlib_context_set_anti_alias(0);
+
+ if (argc > 9)
+ {
+ if (!_php_handle_cliprect_array(dbox, func_name, &cx,&cy,&cw,&ch))
+ RETURN_FALSE;
+ imlib_context_set_cliprect(cx,cy,cw,ch);
+ }
+
+ (*func)(x,y,w,h);
+ imlib_context_set_cliprect(0,0,0,0);
+ RETURN_TRUE;
+}
+
+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;
+ }
+
+ ZEND_FETCH_RESOURCE(im, Imlib_Image, img, -1, "Imlib Image", le_imlib_img);
+
+ imlib_context_set_image(im);
+ (*func)();
+}
+
+/* We wrap imlib_image_draw_line, because it doesn't return a void like all
+ the other drawing functions do. So, since we don't care about
+ Imlib_Updates, we're going to return our own void. Nyeh. */
+static void _php_wrap_draw_line(int x1, int y1, int x2, int y2)
+{
+ imlib_image_draw_line(x1,y1,x2,y2,0);
+}
+
+/* {{{ proto void imlib_add_color_to_color_range(int cr, int x, int r, int g, int b, int a)
+ 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, **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;
+ }
+
+ ZEND_FETCH_RESOURCE(range, Imlib_Color_Range, crange, -1, "Imlib Color Range", le_imlib_cr);
+
+ convert_to_long_ex(cx);
+ x = Z_LVAL_PP(cx);
+ _php_convert_four_longs(cr,cg,cb,ca,&r,&g,&b,&a);
+
+ imlib_context_set_color_range(range);
+ imlib_context_set_color(r,g,b,a);
+ imlib_add_color_to_color_range(x);
+}
+/* }}} */
+
+
+/* {{{ proto void imlib_blend_image_onto_image(int dstimg, int srcimg, int malpha, int srcx, int srcy, int srcw, int srch, int dstx, int dsty, int dstw, int dsth, char dither, char blend, char alias)
+ 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, **malpha, **srcx, **srcy, **srcw, **srch, **dstx, **dsty, **dstw, **dsth, **dither, **blend, **alias;
+ 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;
+ }
+
+ 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);
+
+ _php_convert_four_longs(srcx,srcy,srcw,srch,&sx,&sy,&sw,&sh);
+ _php_convert_four_longs(dstx,dsty,dstw,dsth,&dx,&dy,&dw,&dh);
+
+ convert_to_long_ex(malpha);
+ convert_to_long_ex(dither);
+ convert_to_long_ex(blend);
+ convert_to_long_ex(alias);
+ calpha = Z_LVAL_PP(malpha);
+ cdither = Z_LVAL_PP(dither);
+ cblend = Z_LVAL_PP(blend);
+ calias = Z_LVAL_PP(alias);
+
+ imlib_context_set_image(dst);
+ imlib_context_set_anti_alias(calias);
+ imlib_context_set_dither(cdither);
+ imlib_context_set_blend(cblend);
+ imlib_context_set_angle(0);
+ imlib_blend_image_onto_image(src,calpha,sx,sy,sw,sh,dx,dy,dw,dh);
+}
+/* }}} */
+
+
+/* {{{ proto int imlib_clone_image(int img)
+ Duplicate an image */
+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;
+ }
+
+ ZEND_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);
+}
+/* }}} */
+
+
+/* {{{ proto int imlib_create_color_range()
+ Create a new color range */
+PHP_FUNCTION(imlib_create_color_range)
+{
+ Imlib_Color_Range cr;
+ IMLIBLS_FETCH();
+
+ if (ZEND_NUM_ARGS() != 0) {
+ WRONG_PARAM_COUNT;
+ }
+
+ cr = imlib_create_color_range();
+
+ if (cr) ZEND_REGISTER_RESOURCE(return_value, cr, le_imlib_cr);
+}
+/* }}} */
+
+
+/* {{{ proto int imlib_create_color_modifier(void)
+ Create a new color modifier */
+PHP_FUNCTION(imlib_create_color_modifier)
+{
+ PHP_Imlib_Color_Modifier cm;
+ IMLIBLS_FETCH();
+
+ if (ZEND_NUM_ARGS() != 0) {
+ WRONG_PARAM_COUNT;
+ }
+
+ cm=(PHP_Imlib_Color_Modifier)emalloc(sizeof(PHP_Imlib_Color_Modifier_struct));
+ if (!cm) RETURN_FALSE;
+
+ cm->cm = imlib_create_color_modifier();
+ cm->modified=0;
+ cm->valid=0;
+
+ if (cm->cm) ZEND_REGISTER_RESOURCE(return_value, cm, le_imlib_cm);
+}
+/* }}} */
+
+/* {{{ proto int imlib_create_cropped_image(int img, int srcx, int srcy, int srcw, int srch)
+ Create an image from a cropped region of another image */
+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;
+ }
+</