gd.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  14. | Stig Bakken <ssb@php.net> |
  15. | Jim Winstead <jimw@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* gd 1.2 is copyright 1994, 1995, Quest Protein Database Center,
  19. Cold Spring Harbor Labs. */
  20. /* Note that there is no code from the gd package in this file */
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include "php.h"
  25. #include "php_ini.h"
  26. #include "ext/standard/head.h"
  27. #include <math.h>
  28. #include "SAPI.h"
  29. #include "php_gd.h"
  30. #include "ext/standard/php_image.h"
  31. #include "ext/standard/info.h"
  32. #include "php_open_temporary_file.h"
  33. #include "php_memory_streams.h"
  34. #include "zend_object_handlers.h"
  35. #ifdef HAVE_SYS_WAIT_H
  36. # include <sys/wait.h>
  37. #endif
  38. #ifdef HAVE_UNISTD_H
  39. # include <unistd.h>
  40. #endif
  41. #ifdef PHP_WIN32
  42. # include <io.h>
  43. # include <fcntl.h>
  44. # include <windows.h>
  45. # include <Winuser.h>
  46. # include <Wingdi.h>
  47. #endif
  48. #if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
  49. # include <X11/xpm.h>
  50. #endif
  51. #include "gd_compat.h"
  52. #ifdef HAVE_GD_BUNDLED
  53. # include "libgd/gd.h"
  54. # include "libgd/gd_errors.h"
  55. # include "libgd/gdfontt.h" /* 1 Tiny font */
  56. # include "libgd/gdfonts.h" /* 2 Small font */
  57. # include "libgd/gdfontmb.h" /* 3 Medium bold font */
  58. # include "libgd/gdfontl.h" /* 4 Large font */
  59. # include "libgd/gdfontg.h" /* 5 Giant font */
  60. #else
  61. # include <gd.h>
  62. # include <gd_errors.h>
  63. # include <gdfontt.h> /* 1 Tiny font */
  64. # include <gdfonts.h> /* 2 Small font */
  65. # include <gdfontmb.h> /* 3 Medium bold font */
  66. # include <gdfontl.h> /* 4 Large font */
  67. # include <gdfontg.h> /* 5 Giant font */
  68. #endif
  69. #if defined(HAVE_GD_FREETYPE) && defined(HAVE_GD_BUNDLED)
  70. # include <ft2build.h>
  71. # include FT_FREETYPE_H
  72. #endif
  73. #if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
  74. # include "X11/xpm.h"
  75. #endif
  76. #ifndef M_PI
  77. #define M_PI 3.14159265358979323846
  78. #endif
  79. /* don't used libgd constants, not used, so going to be removed */
  80. #define PHP_GD_FLIP_HORIZONTAL 1
  81. #define PHP_GD_FLIP_VERTICAL 2
  82. #define PHP_GD_FLIP_BOTH 3
  83. #ifdef HAVE_GD_FREETYPE
  84. static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int);
  85. #endif
  86. #include "gd_arginfo.h"
  87. /* as it is not really public, duplicate declaration here to avoid
  88. pointless warnings */
  89. int overflow2(int a, int b);
  90. /* Section Filters Declarations */
  91. /* IMPORTANT NOTE FOR NEW FILTER
  92. * Do not forget to update:
  93. * IMAGE_FILTER_MAX: define the last filter index
  94. * IMAGE_FILTER_MAX_ARGS: define the biggest amount of arguments
  95. * image_filter array in PHP_FUNCTION(imagefilter)
  96. * */
  97. #define IMAGE_FILTER_NEGATE 0
  98. #define IMAGE_FILTER_GRAYSCALE 1
  99. #define IMAGE_FILTER_BRIGHTNESS 2
  100. #define IMAGE_FILTER_CONTRAST 3
  101. #define IMAGE_FILTER_COLORIZE 4
  102. #define IMAGE_FILTER_EDGEDETECT 5
  103. #define IMAGE_FILTER_EMBOSS 6
  104. #define IMAGE_FILTER_GAUSSIAN_BLUR 7
  105. #define IMAGE_FILTER_SELECTIVE_BLUR 8
  106. #define IMAGE_FILTER_MEAN_REMOVAL 9
  107. #define IMAGE_FILTER_SMOOTH 10
  108. #define IMAGE_FILTER_PIXELATE 11
  109. #define IMAGE_FILTER_SCATTER 12
  110. #define IMAGE_FILTER_MAX 12
  111. #define IMAGE_FILTER_MAX_ARGS 6
  112. static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS);
  113. static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS);
  114. static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS);
  115. static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS);
  116. static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS);
  117. static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS);
  118. static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS);
  119. static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS);
  120. static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS);
  121. static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS);
  122. static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS);
  123. static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS);
  124. static void php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS);
  125. /* End Section filters declarations */
  126. static gdImagePtr _php_image_create_from_string(zend_string *Data, char *tn, gdImagePtr (*ioctx_func_p)(gdIOCtxPtr));
  127. static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(FILE *), gdImagePtr (*ioctx_func_p)(gdIOCtxPtr));
  128. static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn);
  129. static gdIOCtx *create_stream_context_from_zval(zval *to_zval);
  130. static gdIOCtx *create_stream_context(php_stream *stream, int close_stream);
  131. static gdIOCtx *create_output_context(void);
  132. static int _php_image_type(zend_string *data);
  133. /* output streaming (formerly gd_ctx.c) */
  134. static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn);
  135. /*********************************************************
  136. *
  137. * GD Object Representation
  138. *
  139. ********************************************************/
  140. zend_class_entry *gd_image_ce;
  141. typedef struct _gd_ext_image_object {
  142. gdImagePtr image;
  143. zend_object std;
  144. } php_gd_image_object;
  145. static zend_object_handlers php_gd_image_object_handlers;
  146. static zend_function *php_gd_image_object_get_constructor(zend_object *object)
  147. {
  148. zend_throw_error(NULL, "You cannot initialize a GdImage object except through helper functions");
  149. return NULL;
  150. }
  151. /**
  152. * Returns the underlying php_gd_image_object from a zend_object
  153. */
  154. static zend_always_inline php_gd_image_object* php_gd_exgdimage_from_zobj_p(zend_object* obj)
  155. {
  156. return (php_gd_image_object *) ((char *) (obj) - XtOffsetOf(php_gd_image_object, std));
  157. }
  158. /**
  159. * Converts an extension GdImage instance contained within a zval into the gdImagePtr
  160. * for use with library APIs
  161. */
  162. PHP_GD_API gdImagePtr php_gd_libgdimageptr_from_zval_p(zval* zp)
  163. {
  164. return php_gd_exgdimage_from_zobj_p(Z_OBJ_P(zp))->image;
  165. }
  166. zend_object *php_gd_image_object_create(zend_class_entry *class_type)
  167. {
  168. size_t block_len = sizeof(php_gd_image_object) + zend_object_properties_size(class_type);
  169. php_gd_image_object *intern = emalloc(block_len);
  170. memset(intern, 0, block_len);
  171. zend_object_std_init(&intern->std, class_type);
  172. object_properties_init(&intern->std, class_type);
  173. intern->std.handlers = &php_gd_image_object_handlers;
  174. return &intern->std;
  175. }
  176. static void php_gd_image_object_free(zend_object *intern)
  177. {
  178. php_gd_image_object *img_obj_ptr = php_gd_exgdimage_from_zobj_p(intern);
  179. if (img_obj_ptr->image) {
  180. gdImageDestroy(img_obj_ptr->image);
  181. }
  182. zend_object_std_dtor(intern);
  183. }
  184. /**
  185. * Creates a new GdImage object wrapping the gdImagePtr and attaches it
  186. * to the zval (usually return_value).
  187. *
  188. * This function must only be called once per valid gdImagePtr
  189. */
  190. void php_gd_assign_libgdimageptr_as_extgdimage(zval *val, gdImagePtr image)
  191. {
  192. object_init_ex(val, gd_image_ce);
  193. php_gd_exgdimage_from_zobj_p(Z_OBJ_P(val))->image = image;
  194. }
  195. static void php_gd_object_minit_helper(void)
  196. {
  197. gd_image_ce = register_class_GdImage();
  198. gd_image_ce->create_object = php_gd_image_object_create;
  199. /* setting up the object handlers for the GdImage class */
  200. memcpy(&php_gd_image_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  201. php_gd_image_object_handlers.clone_obj = NULL;
  202. php_gd_image_object_handlers.free_obj = php_gd_image_object_free;
  203. php_gd_image_object_handlers.get_constructor = php_gd_image_object_get_constructor;
  204. php_gd_image_object_handlers.compare = zend_objects_not_comparable;
  205. php_gd_image_object_handlers.offset = XtOffsetOf(php_gd_image_object, std);
  206. }
  207. static zend_class_entry *gd_font_ce = NULL;
  208. static zend_object_handlers php_gd_font_object_handlers;
  209. typedef struct _php_gd_font_object {
  210. gdFontPtr font;
  211. zend_object std;
  212. } php_gd_font_object;
  213. static php_gd_font_object *php_gd_font_object_from_zend_object(zend_object *zobj)
  214. {
  215. return ((php_gd_font_object*)(zobj + 1)) - 1;
  216. }
  217. static zend_object *php_gd_font_object_to_zend_object(php_gd_font_object *obj)
  218. {
  219. return ((zend_object*)(obj + 1)) - 1;
  220. }
  221. static zend_object *php_gd_font_object_create(zend_class_entry *ce)
  222. {
  223. php_gd_font_object *obj = zend_object_alloc(sizeof(php_gd_font_object), ce);
  224. zend_object *zobj = php_gd_font_object_to_zend_object(obj);
  225. obj->font = NULL;
  226. zend_object_std_init(zobj, ce);
  227. object_properties_init(zobj, ce);
  228. zobj->handlers = &php_gd_font_object_handlers;
  229. return zobj;
  230. }
  231. static void php_gd_font_object_free(zend_object *zobj)
  232. {
  233. php_gd_font_object *obj = php_gd_font_object_from_zend_object(zobj);
  234. if (obj->font) {
  235. if (obj->font->data) {
  236. efree(obj->font->data);
  237. }
  238. efree(obj->font);
  239. obj->font = NULL;
  240. }
  241. zend_object_std_dtor(zobj);
  242. }
  243. static zend_function *php_gd_font_object_get_constructor(zend_object *object)
  244. {
  245. zend_throw_error(NULL, "You cannot initialize a GdFont object except through helper functions");
  246. return NULL;
  247. }
  248. static void php_gd_font_minit_helper(void)
  249. {
  250. gd_font_ce = register_class_GdFont();
  251. gd_font_ce->create_object = php_gd_font_object_create;
  252. /* setting up the object handlers for the GdFont class */
  253. memcpy(&php_gd_font_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  254. php_gd_font_object_handlers.clone_obj = NULL;
  255. php_gd_font_object_handlers.free_obj = php_gd_font_object_free;
  256. php_gd_font_object_handlers.get_constructor = php_gd_font_object_get_constructor;
  257. php_gd_font_object_handlers.offset = XtOffsetOf(php_gd_font_object, std);
  258. }
  259. /*********************************************************
  260. *
  261. * Extension Implementation
  262. *
  263. ********************************************************/
  264. zend_module_entry gd_module_entry = {
  265. STANDARD_MODULE_HEADER,
  266. "gd",
  267. ext_functions,
  268. PHP_MINIT(gd),
  269. PHP_MSHUTDOWN(gd),
  270. NULL,
  271. PHP_RSHUTDOWN(gd),
  272. PHP_MINFO(gd),
  273. PHP_GD_VERSION,
  274. STANDARD_MODULE_PROPERTIES
  275. };
  276. #ifdef COMPILE_DL_GD
  277. ZEND_GET_MODULE(gd)
  278. #endif
  279. /* {{{ PHP_INI_BEGIN */
  280. PHP_INI_BEGIN()
  281. PHP_INI_ENTRY("gd.jpeg_ignore_warning", "1", PHP_INI_ALL, NULL)
  282. PHP_INI_END()
  283. /* }}} */
  284. /* {{{ php_gd_error_method */
  285. void php_gd_error_method(int type, const char *format, va_list args)
  286. {
  287. switch (type) {
  288. #ifndef PHP_WIN32
  289. case GD_DEBUG:
  290. case GD_INFO:
  291. #endif
  292. case GD_NOTICE:
  293. type = E_NOTICE;
  294. break;
  295. case GD_WARNING:
  296. type = E_WARNING;
  297. break;
  298. default:
  299. type = E_ERROR;
  300. }
  301. php_verror(NULL, "", type, format, args);
  302. }
  303. /* }}} */
  304. /* {{{ PHP_MINIT_FUNCTION */
  305. PHP_MINIT_FUNCTION(gd)
  306. {
  307. php_gd_object_minit_helper();
  308. php_gd_font_minit_helper();
  309. #if defined(HAVE_GD_FREETYPE) && defined(HAVE_GD_BUNDLED)
  310. gdFontCacheMutexSetup();
  311. #endif
  312. gdSetErrorMethod(php_gd_error_method);
  313. REGISTER_INI_ENTRIES();
  314. REGISTER_LONG_CONSTANT("IMG_AVIF", PHP_IMG_AVIF, CONST_CS | CONST_PERSISTENT);
  315. REGISTER_LONG_CONSTANT("IMG_GIF", PHP_IMG_GIF, CONST_CS | CONST_PERSISTENT);
  316. REGISTER_LONG_CONSTANT("IMG_JPG", PHP_IMG_JPG, CONST_CS | CONST_PERSISTENT);
  317. REGISTER_LONG_CONSTANT("IMG_JPEG", PHP_IMG_JPEG, CONST_CS | CONST_PERSISTENT);
  318. REGISTER_LONG_CONSTANT("IMG_PNG", PHP_IMG_PNG, CONST_CS | CONST_PERSISTENT);
  319. REGISTER_LONG_CONSTANT("IMG_WBMP", PHP_IMG_WBMP, CONST_CS | CONST_PERSISTENT);
  320. REGISTER_LONG_CONSTANT("IMG_XPM", PHP_IMG_XPM, CONST_CS | CONST_PERSISTENT);
  321. REGISTER_LONG_CONSTANT("IMG_WEBP", PHP_IMG_WEBP, CONST_CS | CONST_PERSISTENT);
  322. REGISTER_LONG_CONSTANT("IMG_BMP", PHP_IMG_BMP, CONST_CS | CONST_PERSISTENT);
  323. REGISTER_LONG_CONSTANT("IMG_TGA", PHP_IMG_TGA, CONST_CS | CONST_PERSISTENT);
  324. #ifdef gdWebpLossless
  325. /* constant for webp encoding */
  326. REGISTER_LONG_CONSTANT("IMG_WEBP_LOSSLESS", gdWebpLossless, CONST_CS | CONST_PERSISTENT);
  327. #endif
  328. /* special colours for gd */
  329. REGISTER_LONG_CONSTANT("IMG_COLOR_TILED", gdTiled, CONST_CS | CONST_PERSISTENT);
  330. REGISTER_LONG_CONSTANT("IMG_COLOR_STYLED", gdStyled, CONST_CS | CONST_PERSISTENT);
  331. REGISTER_LONG_CONSTANT("IMG_COLOR_BRUSHED", gdBrushed, CONST_CS | CONST_PERSISTENT);
  332. REGISTER_LONG_CONSTANT("IMG_COLOR_STYLEDBRUSHED", gdStyledBrushed, CONST_CS | CONST_PERSISTENT);
  333. REGISTER_LONG_CONSTANT("IMG_COLOR_TRANSPARENT", gdTransparent, CONST_CS | CONST_PERSISTENT);
  334. /* for imagefilledarc */
  335. REGISTER_LONG_CONSTANT("IMG_ARC_ROUNDED", gdArc, CONST_CS | CONST_PERSISTENT);
  336. REGISTER_LONG_CONSTANT("IMG_ARC_PIE", gdPie, CONST_CS | CONST_PERSISTENT);
  337. REGISTER_LONG_CONSTANT("IMG_ARC_CHORD", gdChord, CONST_CS | CONST_PERSISTENT);
  338. REGISTER_LONG_CONSTANT("IMG_ARC_NOFILL", gdNoFill, CONST_CS | CONST_PERSISTENT);
  339. REGISTER_LONG_CONSTANT("IMG_ARC_EDGED", gdEdged, CONST_CS | CONST_PERSISTENT);
  340. /* GD2 image format types */
  341. REGISTER_LONG_CONSTANT("IMG_GD2_RAW", GD2_FMT_RAW, CONST_CS | CONST_PERSISTENT);
  342. REGISTER_LONG_CONSTANT("IMG_GD2_COMPRESSED", GD2_FMT_COMPRESSED, CONST_CS | CONST_PERSISTENT);
  343. REGISTER_LONG_CONSTANT("IMG_FLIP_HORIZONTAL", PHP_GD_FLIP_HORIZONTAL, CONST_CS | CONST_PERSISTENT);
  344. REGISTER_LONG_CONSTANT("IMG_FLIP_VERTICAL", PHP_GD_FLIP_VERTICAL, CONST_CS | CONST_PERSISTENT);
  345. REGISTER_LONG_CONSTANT("IMG_FLIP_BOTH", PHP_GD_FLIP_BOTH, CONST_CS | CONST_PERSISTENT);
  346. REGISTER_LONG_CONSTANT("IMG_EFFECT_REPLACE", gdEffectReplace, CONST_CS | CONST_PERSISTENT);
  347. REGISTER_LONG_CONSTANT("IMG_EFFECT_ALPHABLEND", gdEffectAlphaBlend, CONST_CS | CONST_PERSISTENT);
  348. REGISTER_LONG_CONSTANT("IMG_EFFECT_NORMAL", gdEffectNormal, CONST_CS | CONST_PERSISTENT);
  349. REGISTER_LONG_CONSTANT("IMG_EFFECT_OVERLAY", gdEffectOverlay, CONST_CS | CONST_PERSISTENT);
  350. #ifdef gdEffectMultiply
  351. REGISTER_LONG_CONSTANT("IMG_EFFECT_MULTIPLY", gdEffectMultiply, CONST_CS | CONST_PERSISTENT);
  352. #endif
  353. REGISTER_LONG_CONSTANT("IMG_CROP_DEFAULT", GD_CROP_DEFAULT, CONST_CS | CONST_PERSISTENT);
  354. REGISTER_LONG_CONSTANT("IMG_CROP_TRANSPARENT", GD_CROP_TRANSPARENT, CONST_CS | CONST_PERSISTENT);
  355. REGISTER_LONG_CONSTANT("IMG_CROP_BLACK", GD_CROP_BLACK, CONST_CS | CONST_PERSISTENT);
  356. REGISTER_LONG_CONSTANT("IMG_CROP_WHITE", GD_CROP_WHITE, CONST_CS | CONST_PERSISTENT);
  357. REGISTER_LONG_CONSTANT("IMG_CROP_SIDES", GD_CROP_SIDES, CONST_CS | CONST_PERSISTENT);
  358. REGISTER_LONG_CONSTANT("IMG_CROP_THRESHOLD", GD_CROP_THRESHOLD, CONST_CS | CONST_PERSISTENT);
  359. REGISTER_LONG_CONSTANT("IMG_BELL", GD_BELL, CONST_CS | CONST_PERSISTENT);
  360. REGISTER_LONG_CONSTANT("IMG_BESSEL", GD_BESSEL, CONST_CS | CONST_PERSISTENT);
  361. REGISTER_LONG_CONSTANT("IMG_BILINEAR_FIXED", GD_BILINEAR_FIXED, CONST_CS | CONST_PERSISTENT);
  362. REGISTER_LONG_CONSTANT("IMG_BICUBIC", GD_BICUBIC, CONST_CS | CONST_PERSISTENT);
  363. REGISTER_LONG_CONSTANT("IMG_BICUBIC_FIXED", GD_BICUBIC_FIXED, CONST_CS | CONST_PERSISTENT);
  364. REGISTER_LONG_CONSTANT("IMG_BLACKMAN", GD_BLACKMAN, CONST_CS | CONST_PERSISTENT);
  365. REGISTER_LONG_CONSTANT("IMG_BOX", GD_BOX, CONST_CS | CONST_PERSISTENT);
  366. REGISTER_LONG_CONSTANT("IMG_BSPLINE", GD_BSPLINE, CONST_CS | CONST_PERSISTENT);
  367. REGISTER_LONG_CONSTANT("IMG_CATMULLROM", GD_CATMULLROM, CONST_CS | CONST_PERSISTENT);
  368. REGISTER_LONG_CONSTANT("IMG_GAUSSIAN", GD_GAUSSIAN, CONST_CS | CONST_PERSISTENT);
  369. REGISTER_LONG_CONSTANT("IMG_GENERALIZED_CUBIC", GD_GENERALIZED_CUBIC, CONST_CS | CONST_PERSISTENT);
  370. REGISTER_LONG_CONSTANT("IMG_HERMITE", GD_HERMITE, CONST_CS | CONST_PERSISTENT);
  371. REGISTER_LONG_CONSTANT("IMG_HAMMING", GD_HAMMING, CONST_CS | CONST_PERSISTENT);
  372. REGISTER_LONG_CONSTANT("IMG_HANNING", GD_HANNING, CONST_CS | CONST_PERSISTENT);
  373. REGISTER_LONG_CONSTANT("IMG_MITCHELL", GD_MITCHELL, CONST_CS | CONST_PERSISTENT);
  374. REGISTER_LONG_CONSTANT("IMG_POWER", GD_POWER, CONST_CS | CONST_PERSISTENT);
  375. REGISTER_LONG_CONSTANT("IMG_QUADRATIC", GD_QUADRATIC, CONST_CS | CONST_PERSISTENT);
  376. REGISTER_LONG_CONSTANT("IMG_SINC", GD_SINC, CONST_CS | CONST_PERSISTENT);
  377. REGISTER_LONG_CONSTANT("IMG_NEAREST_NEIGHBOUR", GD_NEAREST_NEIGHBOUR, CONST_CS | CONST_PERSISTENT);
  378. REGISTER_LONG_CONSTANT("IMG_WEIGHTED4", GD_WEIGHTED4, CONST_CS | CONST_PERSISTENT);
  379. REGISTER_LONG_CONSTANT("IMG_TRIANGLE", GD_TRIANGLE, CONST_CS | CONST_PERSISTENT);
  380. REGISTER_LONG_CONSTANT("IMG_AFFINE_TRANSLATE", GD_AFFINE_TRANSLATE, CONST_CS | CONST_PERSISTENT);
  381. REGISTER_LONG_CONSTANT("IMG_AFFINE_SCALE", GD_AFFINE_SCALE, CONST_CS | CONST_PERSISTENT);
  382. REGISTER_LONG_CONSTANT("IMG_AFFINE_ROTATE", GD_AFFINE_ROTATE, CONST_CS | CONST_PERSISTENT);
  383. REGISTER_LONG_CONSTANT("IMG_AFFINE_SHEAR_HORIZONTAL", GD_AFFINE_SHEAR_HORIZONTAL, CONST_CS | CONST_PERSISTENT);
  384. REGISTER_LONG_CONSTANT("IMG_AFFINE_SHEAR_VERTICAL", GD_AFFINE_SHEAR_VERTICAL, CONST_CS | CONST_PERSISTENT);
  385. #ifdef HAVE_GD_BUNDLED
  386. REGISTER_LONG_CONSTANT("GD_BUNDLED", 1, CONST_CS | CONST_PERSISTENT);
  387. #else
  388. REGISTER_LONG_CONSTANT("GD_BUNDLED", 0, CONST_CS | CONST_PERSISTENT);
  389. #endif
  390. /* Section Filters */
  391. REGISTER_LONG_CONSTANT("IMG_FILTER_NEGATE", IMAGE_FILTER_NEGATE, CONST_CS | CONST_PERSISTENT);
  392. REGISTER_LONG_CONSTANT("IMG_FILTER_GRAYSCALE", IMAGE_FILTER_GRAYSCALE, CONST_CS | CONST_PERSISTENT);
  393. REGISTER_LONG_CONSTANT("IMG_FILTER_BRIGHTNESS", IMAGE_FILTER_BRIGHTNESS, CONST_CS | CONST_PERSISTENT);
  394. REGISTER_LONG_CONSTANT("IMG_FILTER_CONTRAST", IMAGE_FILTER_CONTRAST, CONST_CS | CONST_PERSISTENT);
  395. REGISTER_LONG_CONSTANT("IMG_FILTER_COLORIZE", IMAGE_FILTER_COLORIZE, CONST_CS | CONST_PERSISTENT);
  396. REGISTER_LONG_CONSTANT("IMG_FILTER_EDGEDETECT", IMAGE_FILTER_EDGEDETECT, CONST_CS | CONST_PERSISTENT);
  397. REGISTER_LONG_CONSTANT("IMG_FILTER_GAUSSIAN_BLUR", IMAGE_FILTER_GAUSSIAN_BLUR, CONST_CS | CONST_PERSISTENT);
  398. REGISTER_LONG_CONSTANT("IMG_FILTER_SELECTIVE_BLUR", IMAGE_FILTER_SELECTIVE_BLUR, CONST_CS | CONST_PERSISTENT);
  399. REGISTER_LONG_CONSTANT("IMG_FILTER_EMBOSS", IMAGE_FILTER_EMBOSS, CONST_CS | CONST_PERSISTENT);
  400. REGISTER_LONG_CONSTANT("IMG_FILTER_MEAN_REMOVAL", IMAGE_FILTER_MEAN_REMOVAL, CONST_CS | CONST_PERSISTENT);
  401. REGISTER_LONG_CONSTANT("IMG_FILTER_SMOOTH", IMAGE_FILTER_SMOOTH, CONST_CS | CONST_PERSISTENT);
  402. REGISTER_LONG_CONSTANT("IMG_FILTER_PIXELATE", IMAGE_FILTER_PIXELATE, CONST_CS | CONST_PERSISTENT);
  403. REGISTER_LONG_CONSTANT("IMG_FILTER_SCATTER", IMAGE_FILTER_SCATTER, CONST_CS | CONST_PERSISTENT);
  404. /* End Section Filters */
  405. #ifdef GD_VERSION_STRING
  406. REGISTER_STRING_CONSTANT("GD_VERSION", GD_VERSION_STRING, CONST_CS | CONST_PERSISTENT);
  407. #endif
  408. #if defined(GD_MAJOR_VERSION) && defined(GD_MINOR_VERSION) && defined(GD_RELEASE_VERSION) && defined(GD_EXTRA_VERSION)
  409. REGISTER_LONG_CONSTANT("GD_MAJOR_VERSION", GD_MAJOR_VERSION, CONST_CS | CONST_PERSISTENT);
  410. REGISTER_LONG_CONSTANT("GD_MINOR_VERSION", GD_MINOR_VERSION, CONST_CS | CONST_PERSISTENT);
  411. REGISTER_LONG_CONSTANT("GD_RELEASE_VERSION", GD_RELEASE_VERSION, CONST_CS | CONST_PERSISTENT);
  412. REGISTER_STRING_CONSTANT("GD_EXTRA_VERSION", GD_EXTRA_VERSION, CONST_CS | CONST_PERSISTENT);
  413. #endif
  414. #ifdef HAVE_GD_PNG
  415. /*
  416. * cannot include #include "png.h"
  417. * /usr/include/pngconf.h:310:2: error: #error png.h already includes setjmp.h with some additional fixup.
  418. * as error, use the values for now...
  419. */
  420. REGISTER_LONG_CONSTANT("PNG_NO_FILTER", 0x00, CONST_CS | CONST_PERSISTENT);
  421. REGISTER_LONG_CONSTANT("PNG_FILTER_NONE", 0x08, CONST_CS | CONST_PERSISTENT);
  422. REGISTER_LONG_CONSTANT("PNG_FILTER_SUB", 0x10, CONST_CS | CONST_PERSISTENT);
  423. REGISTER_LONG_CONSTANT("PNG_FILTER_UP", 0x20, CONST_CS | CONST_PERSISTENT);
  424. REGISTER_LONG_CONSTANT("PNG_FILTER_AVG", 0x40, CONST_CS | CONST_PERSISTENT);
  425. REGISTER_LONG_CONSTANT("PNG_FILTER_PAETH", 0x80, CONST_CS | CONST_PERSISTENT);
  426. REGISTER_LONG_CONSTANT("PNG_ALL_FILTERS", 0x08 | 0x10 | 0x20 | 0x40 | 0x80, CONST_CS | CONST_PERSISTENT);
  427. #endif
  428. return SUCCESS;
  429. }
  430. /* }}} */
  431. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  432. PHP_MSHUTDOWN_FUNCTION(gd)
  433. {
  434. #if defined(HAVE_GD_FREETYPE) && defined(HAVE_GD_BUNDLED)
  435. gdFontCacheMutexShutdown();
  436. #endif
  437. UNREGISTER_INI_ENTRIES();
  438. return SUCCESS;
  439. }
  440. /* }}} */
  441. /* {{{ PHP_RSHUTDOWN_FUNCTION */
  442. PHP_RSHUTDOWN_FUNCTION(gd)
  443. {
  444. #ifdef HAVE_GD_FREETYPE
  445. gdFontCacheShutdown();
  446. #endif
  447. return SUCCESS;
  448. }
  449. /* }}} */
  450. #ifdef HAVE_GD_BUNDLED
  451. #define PHP_GD_VERSION_STRING "bundled (2.1.0 compatible)"
  452. #else
  453. # define PHP_GD_VERSION_STRING GD_VERSION_STRING
  454. #endif
  455. /* {{{ PHP_MINFO_FUNCTION */
  456. PHP_MINFO_FUNCTION(gd)
  457. {
  458. php_info_print_table_start();
  459. php_info_print_table_row(2, "GD Support", "enabled");
  460. /* need to use a PHPAPI function here because it is external module in windows */
  461. #ifdef HAVE_GD_BUNDLED
  462. php_info_print_table_row(2, "GD Version", PHP_GD_VERSION_STRING);
  463. #else
  464. php_info_print_table_row(2, "GD headers Version", PHP_GD_VERSION_STRING);
  465. #ifdef HAVE_GD_LIBVERSION
  466. php_info_print_table_row(2, "GD library Version", gdVersionString());
  467. #endif
  468. #endif
  469. #ifdef HAVE_GD_FREETYPE
  470. php_info_print_table_row(2, "FreeType Support", "enabled");
  471. php_info_print_table_row(2, "FreeType Linkage", "with freetype");
  472. #ifdef HAVE_GD_BUNDLED
  473. {
  474. char tmp[256];
  475. #ifdef FREETYPE_PATCH
  476. snprintf(tmp, sizeof(tmp), "%d.%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR, FREETYPE_PATCH);
  477. #elif defined(FREETYPE_MAJOR)
  478. snprintf(tmp, sizeof(tmp), "%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR);
  479. #else
  480. snprintf(tmp, sizeof(tmp), "1.x");
  481. #endif
  482. php_info_print_table_row(2, "FreeType Version", tmp);
  483. }
  484. #endif
  485. #endif
  486. php_info_print_table_row(2, "GIF Read Support", "enabled");
  487. php_info_print_table_row(2, "GIF Create Support", "enabled");
  488. #ifdef HAVE_GD_JPG
  489. {
  490. php_info_print_table_row(2, "JPEG Support", "enabled");
  491. #ifdef HAVE_GD_BUNDLED
  492. php_info_print_table_row(2, "libJPEG Version", gdJpegGetVersionString());
  493. #endif
  494. }
  495. #endif
  496. #ifdef HAVE_GD_PNG
  497. php_info_print_table_row(2, "PNG Support", "enabled");
  498. #ifdef HAVE_GD_BUNDLED
  499. php_info_print_table_row(2, "libPNG Version", gdPngGetVersionString());
  500. #endif
  501. #endif
  502. php_info_print_table_row(2, "WBMP Support", "enabled");
  503. #ifdef HAVE_GD_XPM
  504. php_info_print_table_row(2, "XPM Support", "enabled");
  505. #ifdef HAVE_GD_BUNDLED
  506. {
  507. char tmp[12];
  508. snprintf(tmp, sizeof(tmp), "%d", XpmLibraryVersion());
  509. php_info_print_table_row(2, "libXpm Version", tmp);
  510. }
  511. #endif
  512. #endif
  513. php_info_print_table_row(2, "XBM Support", "enabled");
  514. #ifdef USE_GD_JISX0208
  515. php_info_print_table_row(2, "JIS-mapped Japanese Font Support", "enabled");
  516. #endif
  517. #ifdef HAVE_GD_WEBP
  518. php_info_print_table_row(2, "WebP Support", "enabled");
  519. #endif
  520. #ifdef HAVE_GD_BMP
  521. php_info_print_table_row(2, "BMP Support", "enabled");
  522. #endif
  523. #ifdef HAVE_GD_AVIF
  524. php_info_print_table_row(2, "AVIF Support", "enabled");
  525. #endif
  526. #ifdef HAVE_GD_TGA
  527. php_info_print_table_row(2, "TGA Read Support", "enabled");
  528. #endif
  529. php_info_print_table_end();
  530. DISPLAY_INI_ENTRIES();
  531. }
  532. /* }}} */
  533. /* {{{ */
  534. PHP_FUNCTION(gd_info)
  535. {
  536. if (zend_parse_parameters_none() == FAILURE) {
  537. RETURN_THROWS();
  538. }
  539. array_init(return_value);
  540. add_assoc_string(return_value, "GD Version", PHP_GD_VERSION_STRING);
  541. #ifdef HAVE_GD_FREETYPE
  542. add_assoc_bool(return_value, "FreeType Support", 1);
  543. add_assoc_string(return_value, "FreeType Linkage", "with freetype");
  544. #else
  545. add_assoc_bool(return_value, "FreeType Support", 0);
  546. #endif
  547. add_assoc_bool(return_value, "GIF Read Support", 1);
  548. add_assoc_bool(return_value, "GIF Create Support", 1);
  549. #ifdef HAVE_GD_JPG
  550. add_assoc_bool(return_value, "JPEG Support", 1);
  551. #else
  552. add_assoc_bool(return_value, "JPEG Support", 0);
  553. #endif
  554. #ifdef HAVE_GD_PNG
  555. add_assoc_bool(return_value, "PNG Support", 1);
  556. #else
  557. add_assoc_bool(return_value, "PNG Support", 0);
  558. #endif
  559. add_assoc_bool(return_value, "WBMP Support", 1);
  560. #ifdef HAVE_GD_XPM
  561. add_assoc_bool(return_value, "XPM Support", 1);
  562. #else
  563. add_assoc_bool(return_value, "XPM Support", 0);
  564. #endif
  565. add_assoc_bool(return_value, "XBM Support", 1);
  566. #ifdef HAVE_GD_WEBP
  567. add_assoc_bool(return_value, "WebP Support", 1);
  568. #else
  569. add_assoc_bool(return_value, "WebP Support", 0);
  570. #endif
  571. #ifdef HAVE_GD_BMP
  572. add_assoc_bool(return_value, "BMP Support", 1);
  573. #else
  574. add_assoc_bool(return_value, "BMP Support", 0);
  575. #endif
  576. #ifdef HAVE_GD_AVIF
  577. add_assoc_bool(return_value, "AVIF Support", 1);
  578. #else
  579. add_assoc_bool(return_value, "AVIF Support", 0);
  580. #endif
  581. #ifdef HAVE_GD_TGA
  582. add_assoc_bool(return_value, "TGA Read Support", 1);
  583. #else
  584. add_assoc_bool(return_value, "TGA Read Support", 0);
  585. #endif
  586. #ifdef USE_GD_JISX0208
  587. add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 1);
  588. #else
  589. add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 0);
  590. #endif
  591. }
  592. /* }}} */
  593. #define FLIPWORD(a) (((a & 0xff000000) >> 24) | ((a & 0x00ff0000) >> 8) | ((a & 0x0000ff00) << 8) | ((a & 0x000000ff) << 24))
  594. /* {{{ Load a new font */
  595. PHP_FUNCTION(imageloadfont)
  596. {
  597. zend_string *file;
  598. int hdr_size = sizeof(gdFont) - sizeof(char *);
  599. int body_size, n = 0, b, i, body_size_check;
  600. gdFontPtr font;
  601. php_stream *stream;
  602. if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file) == FAILURE) {
  603. RETURN_THROWS();
  604. }
  605. stream = php_stream_open_wrapper(ZSTR_VAL(file), "rb", IGNORE_PATH | REPORT_ERRORS, NULL);
  606. if (stream == NULL) {
  607. RETURN_FALSE;
  608. }
  609. /* Only supports a architecture-dependent binary dump format
  610. * at the moment.
  611. * The file format is like this on machines with 32-byte integers:
  612. *
  613. * byte 0-3: (int) number of characters in the font
  614. * byte 4-7: (int) value of first character in the font (often 32, space)
  615. * byte 8-11: (int) pixel width of each character
  616. * byte 12-15: (int) pixel height of each character
  617. * bytes 16-: (char) array with character data, one byte per pixel
  618. * in each character, for a total of
  619. * (nchars*width*height) bytes.
  620. */
  621. font = (gdFontPtr) emalloc(sizeof(gdFont));
  622. b = 0;
  623. while (b < hdr_size && (n = php_stream_read(stream, (char*)&font[b], hdr_size - b)) > 0) {
  624. b += n;
  625. }
  626. if (n <= 0) {
  627. efree(font);
  628. if (php_stream_eof(stream)) {
  629. php_error_docref(NULL, E_WARNING, "End of file while reading header");
  630. } else {
  631. php_error_docref(NULL, E_WARNING, "Error while reading header");
  632. }
  633. php_stream_close(stream);
  634. RETURN_FALSE;
  635. }
  636. i = php_stream_tell(stream);
  637. php_stream_seek(stream, 0, SEEK_END);
  638. body_size_check = php_stream_tell(stream) - hdr_size;
  639. php_stream_seek(stream, i, SEEK_SET);
  640. if (overflow2(font->nchars, font->h) || overflow2(font->nchars * font->h, font->w )) {
  641. php_error_docref(NULL, E_WARNING, "Error reading font, invalid font header");
  642. efree(font);
  643. php_stream_close(stream);
  644. RETURN_FALSE;
  645. }
  646. body_size = font->w * font->h * font->nchars;
  647. if (body_size != body_size_check) {
  648. font->w = FLIPWORD(font->w);
  649. font->h = FLIPWORD(font->h);
  650. font->nchars = FLIPWORD(font->nchars);
  651. body_size = font->w * font->h * font->nchars;
  652. }
  653. if (body_size != body_size_check) {
  654. php_error_docref(NULL, E_WARNING, "Error reading font");
  655. efree(font);
  656. php_stream_close(stream);
  657. RETURN_FALSE;
  658. }
  659. font->data = emalloc(body_size);
  660. b = 0;
  661. while (b < body_size && (n = php_stream_read(stream, &font->data[b], body_size - b)) > 0) {
  662. b += n;
  663. }
  664. if (n <= 0) {
  665. efree(font->data);
  666. efree(font);
  667. if (php_stream_eof(stream)) {
  668. php_error_docref(NULL, E_WARNING, "End of file while reading body");
  669. } else {
  670. php_error_docref(NULL, E_WARNING, "Error while reading body");
  671. }
  672. php_stream_close(stream);
  673. RETURN_FALSE;
  674. }
  675. php_stream_close(stream);
  676. object_init_ex(return_value, gd_font_ce);
  677. php_gd_font_object_from_zend_object(Z_OBJ_P(return_value))->font = font;
  678. }
  679. /* }}} */
  680. /* {{{ Set the line drawing styles for use with imageline and IMG_COLOR_STYLED. */
  681. PHP_FUNCTION(imagesetstyle)
  682. {
  683. zval *IM, *styles, *item;
  684. gdImagePtr im;
  685. int *stylearr;
  686. int index = 0;
  687. uint32_t num_styles;
  688. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa", &IM, gd_image_ce, &styles) == FAILURE) {
  689. RETURN_THROWS();
  690. }
  691. im = php_gd_libgdimageptr_from_zval_p(IM);
  692. num_styles = zend_hash_num_elements(Z_ARRVAL_P(styles));
  693. if (num_styles == 0) {
  694. zend_argument_value_error(2, "cannot be empty");
  695. RETURN_THROWS();
  696. }
  697. /* copy the style values in the stylearr */
  698. stylearr = safe_emalloc(sizeof(int), num_styles, 0);
  699. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(styles), item) {
  700. stylearr[index++] = zval_get_long(item);
  701. } ZEND_HASH_FOREACH_END();
  702. gdImageSetStyle(im, stylearr, index);
  703. efree(stylearr);
  704. RETURN_TRUE;
  705. }
  706. /* }}} */
  707. /* {{{ Create a new true color image */
  708. PHP_FUNCTION(imagecreatetruecolor)
  709. {
  710. zend_long x_size, y_size;
  711. gdImagePtr im;
  712. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &x_size, &y_size) == FAILURE) {
  713. RETURN_THROWS();
  714. }
  715. if (x_size <= 0 || x_size >= INT_MAX) {
  716. zend_argument_value_error(1, "must be greater than 0");
  717. RETURN_THROWS();
  718. }
  719. if (y_size <= 0 || y_size >= INT_MAX) {
  720. zend_argument_value_error(2, "must be greater than 0");
  721. RETURN_THROWS();
  722. }
  723. im = gdImageCreateTrueColor(x_size, y_size);
  724. if (!im) {
  725. RETURN_FALSE;
  726. }
  727. php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
  728. }
  729. /* }}} */
  730. /* {{{ return true if the image uses truecolor */
  731. PHP_FUNCTION(imageistruecolor)
  732. {
  733. zval *IM;
  734. gdImagePtr im;
  735. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
  736. RETURN_THROWS();
  737. }
  738. im = php_gd_libgdimageptr_from_zval_p(IM);
  739. RETURN_BOOL(im->trueColor);
  740. }
  741. /* }}} */
  742. /* {{{ Convert a true color image to a palette based image with a number of colors, optionally using dithering. */
  743. PHP_FUNCTION(imagetruecolortopalette)
  744. {
  745. zval *IM;
  746. bool dither;
  747. zend_long ncolors;
  748. gdImagePtr im;
  749. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Obl", &IM, gd_image_ce, &dither, &ncolors) == FAILURE) {
  750. RETURN_THROWS();
  751. }
  752. im = php_gd_libgdimageptr_from_zval_p(IM);
  753. if (ncolors <= 0 || ZEND_LONG_INT_OVFL(ncolors)) {
  754. zend_argument_value_error(3, "must be greater than 0 and less than %d", INT_MAX);
  755. RETURN_THROWS();
  756. }
  757. if (gdImageTrueColorToPalette(im, dither, (int)ncolors)) {
  758. RETURN_TRUE;
  759. } else {
  760. php_error_docref(NULL, E_WARNING, "Couldn't convert to palette");
  761. RETURN_FALSE;
  762. }
  763. }
  764. /* }}} */
  765. /* {{{ Convert a palette based image to a true color image. */
  766. PHP_FUNCTION(imagepalettetotruecolor)
  767. {
  768. zval *IM;
  769. gdImagePtr im;
  770. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
  771. RETURN_THROWS();
  772. }
  773. im = php_gd_libgdimageptr_from_zval_p(IM);
  774. if (gdImagePaletteToTrueColor(im) == 0) {
  775. RETURN_FALSE;
  776. }
  777. RETURN_TRUE;
  778. }
  779. /* }}} */
  780. /* {{{ Makes the colors of the palette version of an image more closely match the true color version */
  781. PHP_FUNCTION(imagecolormatch)
  782. {
  783. zval *IM1, *IM2;
  784. gdImagePtr im1, im2;
  785. int result;
  786. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &IM1, gd_image_ce, &IM2, gd_image_ce) == FAILURE) {
  787. RETURN_THROWS();
  788. }
  789. im1 = php_gd_libgdimageptr_from_zval_p(IM1);
  790. im2 = php_gd_libgdimageptr_from_zval_p(IM2);
  791. result = gdImageColorMatch(im1, im2);
  792. switch (result) {
  793. case -1:
  794. zend_argument_value_error(1, "must be TrueColor");
  795. RETURN_THROWS();
  796. break;
  797. case -2:
  798. zend_argument_value_error(2, "must be Palette");
  799. RETURN_THROWS();
  800. break;
  801. case -3:
  802. zend_argument_value_error(2, "must be the same size as argument #1 ($im1)");
  803. RETURN_THROWS();
  804. break;
  805. case -4:
  806. zend_argument_value_error(2, "must have at least one color");
  807. RETURN_THROWS();
  808. break;
  809. }
  810. RETURN_TRUE;
  811. }
  812. /* }}} */
  813. /* {{{ Set line thickness for drawing lines, ellipses, rectangles, polygons etc. */
  814. PHP_FUNCTION(imagesetthickness)
  815. {
  816. zval *IM;
  817. zend_long thick;
  818. gdImagePtr im;
  819. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &thick) == FAILURE) {
  820. RETURN_THROWS();
  821. }
  822. im = php_gd_libgdimageptr_from_zval_p(IM);
  823. gdImageSetThickness(im, thick);
  824. RETURN_TRUE;
  825. }
  826. /* }}} */
  827. /* {{{ Draw an ellipse */
  828. PHP_FUNCTION(imagefilledellipse)
  829. {
  830. zval *IM;
  831. zend_long cx, cy, w, h, color;
  832. gdImagePtr im;
  833. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &color) == FAILURE) {
  834. RETURN_THROWS();
  835. }
  836. im = php_gd_libgdimageptr_from_zval_p(IM);
  837. gdImageFilledEllipse(im, cx, cy, w, h, color);
  838. RETURN_TRUE;
  839. }
  840. /* }}} */
  841. /* {{{ Draw a filled partial ellipse */
  842. PHP_FUNCTION(imagefilledarc)
  843. {
  844. zval *IM;
  845. zend_long cx, cy, w, h, ST, E, col, style;
  846. gdImagePtr im;
  847. int e, st;
  848. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollllllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &ST, &E, &col, &style) == FAILURE) {
  849. RETURN_THROWS();
  850. }
  851. im = php_gd_libgdimageptr_from_zval_p(IM);
  852. e = E;
  853. if (e < 0) {
  854. e %= 360;
  855. }
  856. st = ST;
  857. if (st < 0) {
  858. st %= 360;
  859. }
  860. gdImageFilledArc(im, cx, cy, w, h, st, e, col, style);
  861. RETURN_TRUE;
  862. }
  863. /* }}} */
  864. /* {{{ Turn alpha blending mode on or off for the given image */
  865. PHP_FUNCTION(imagealphablending)
  866. {
  867. zval *IM;
  868. bool blend;
  869. gdImagePtr im;
  870. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &blend) == FAILURE) {
  871. RETURN_THROWS();
  872. }
  873. im = php_gd_libgdimageptr_from_zval_p(IM);
  874. gdImageAlphaBlending(im, blend);
  875. RETURN_TRUE;
  876. }
  877. /* }}} */
  878. /* {{{ Include alpha channel to a saved image */
  879. PHP_FUNCTION(imagesavealpha)
  880. {
  881. zval *IM;
  882. bool save;
  883. gdImagePtr im;
  884. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &save) == FAILURE) {
  885. RETURN_THROWS();
  886. }
  887. im = php_gd_libgdimageptr_from_zval_p(IM);
  888. gdImageSaveAlpha(im, save);
  889. RETURN_TRUE;
  890. }
  891. /* }}} */
  892. /* {{{ Set the alpha blending flag to use the bundled libgd layering effects */
  893. PHP_FUNCTION(imagelayereffect)
  894. {
  895. zval *IM;
  896. zend_long effect;
  897. gdImagePtr im;
  898. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &effect) == FAILURE) {
  899. RETURN_THROWS();
  900. }
  901. im = php_gd_libgdimageptr_from_zval_p(IM);
  902. gdImageAlphaBlending(im, effect);
  903. RETURN_TRUE;
  904. }
  905. /* }}} */
  906. #define CHECK_RGBA_RANGE(component, name, argument_number) \
  907. if (component < 0 || component > gd##name##Max) { \
  908. zend_argument_value_error(argument_number, "must be between 0 and %d (inclusive)", gd##name##Max); \
  909. RETURN_THROWS(); \
  910. }
  911. /* {{{ Allocate a color with an alpha level. Works for true color and palette based images */
  912. PHP_FUNCTION(imagecolorallocatealpha)
  913. {
  914. zval *IM;
  915. zend_long red, green, blue, alpha;
  916. gdImagePtr im;
  917. int ct = (-1);
  918. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
  919. RETURN_THROWS();
  920. }
  921. im = php_gd_libgdimageptr_from_zval_p(IM);
  922. CHECK_RGBA_RANGE(red, Red, 2);
  923. CHECK_RGBA_RANGE(green, Green, 3);
  924. CHECK_RGBA_RANGE(blue, Blue, 4);
  925. CHECK_RGBA_RANGE(alpha, Alpha, 5);
  926. ct = gdImageColorAllocateAlpha(im, red, green, blue, alpha);
  927. if (ct < 0) {
  928. RETURN_FALSE;
  929. }
  930. RETURN_LONG((zend_long)ct);
  931. }
  932. /* }}} */
  933. /* {{{ Resolve/Allocate a colour with an alpha level. Works for true colour and palette based images */
  934. PHP_FUNCTION(imagecolorresolvealpha)
  935. {
  936. zval *IM;
  937. zend_long red, green, blue, alpha;
  938. gdImagePtr im;
  939. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
  940. RETURN_THROWS();
  941. }
  942. im = php_gd_libgdimageptr_from_zval_p(IM);
  943. CHECK_RGBA_RANGE(red, Red, 2);
  944. CHECK_RGBA_RANGE(green, Green, 3);
  945. CHECK_RGBA_RANGE(blue, Blue, 4);
  946. CHECK_RGBA_RANGE(alpha, Alpha, 5);
  947. RETURN_LONG(gdImageColorResolveAlpha(im, red, green, blue, alpha));
  948. }
  949. /* }}} */
  950. /* {{{ Find the closest matching colour with alpha transparency */
  951. PHP_FUNCTION(imagecolorclosestalpha)
  952. {
  953. zval *IM;
  954. zend_long red, green, blue, alpha;
  955. gdImagePtr im;
  956. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
  957. RETURN_THROWS();
  958. }
  959. im = php_gd_libgdimageptr_from_zval_p(IM);
  960. CHECK_RGBA_RANGE(red, Red, 2);
  961. CHECK_RGBA_RANGE(green, Green, 3);
  962. CHECK_RGBA_RANGE(blue, Blue, 4);
  963. CHECK_RGBA_RANGE(alpha, Alpha, 5);
  964. RETURN_LONG(gdImageColorClosestAlpha(im, red, green, blue, alpha));
  965. }
  966. /* }}} */
  967. /* {{{ Find exact match for colour with transparency */
  968. PHP_FUNCTION(imagecolorexactalpha)
  969. {
  970. zval *IM;
  971. zend_long red, green, blue, alpha;
  972. gdImagePtr im;
  973. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &red, &green, &blue, &alpha) == FAILURE) {
  974. RETURN_THROWS();
  975. }
  976. im = php_gd_libgdimageptr_from_zval_p(IM);
  977. CHECK_RGBA_RANGE(red, Red, 2);
  978. CHECK_RGBA_RANGE(green, Green, 3);
  979. CHECK_RGBA_RANGE(blue, Blue, 4);
  980. CHECK_RGBA_RANGE(alpha, Alpha, 5);
  981. RETURN_LONG(gdImageColorExactAlpha(im, red, green, blue, alpha));
  982. }
  983. /* }}} */
  984. /* {{{ Copy and resize part of an image using resampling to help ensure clarity */
  985. PHP_FUNCTION(imagecopyresampled)
  986. {
  987. zval *SIM, *DIM;
  988. zend_long SX, SY, SW, SH, DX, DY, DW, DH;
  989. gdImagePtr im_dst, im_src;
  990. int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
  991. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOllllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) {
  992. RETURN_THROWS();
  993. }
  994. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  995. im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
  996. srcX = SX;
  997. srcY = SY;
  998. srcH = SH;
  999. srcW = SW;
  1000. dstX = DX;
  1001. dstY = DY;
  1002. dstH = DH;
  1003. dstW = DW;
  1004. gdImageCopyResampled(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
  1005. RETURN_TRUE;
  1006. }
  1007. /* }}} */
  1008. #ifdef PHP_WIN32
  1009. /* {{{ Grab a window or its client area using a windows handle (HWND property in COM instance) */
  1010. PHP_FUNCTION(imagegrabwindow)
  1011. {
  1012. HWND window;
  1013. bool client_area = 0;
  1014. RECT rc = {0};
  1015. int Width, Height;
  1016. HDC hdc;
  1017. HDC memDC;
  1018. HBITMAP memBM;
  1019. HBITMAP hOld;
  1020. zend_long lwindow_handle;
  1021. gdImagePtr im = NULL;
  1022. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &lwindow_handle, &client_area) == FAILURE) {
  1023. RETURN_THROWS();
  1024. }
  1025. window = (HWND) lwindow_handle;
  1026. if (!IsWindow(window)) {
  1027. php_error_docref(NULL, E_NOTICE, "Invalid window handle");
  1028. RETURN_FALSE;
  1029. }
  1030. hdc = GetDC(0);
  1031. if (client_area) {
  1032. GetClientRect(window, &rc);
  1033. Width = rc.right;
  1034. Height = rc.bottom;
  1035. } else {
  1036. GetWindowRect(window, &rc);
  1037. Width = rc.right - rc.left;
  1038. Height = rc.bottom - rc.top;
  1039. }
  1040. Width = (Width/4)*4;
  1041. memDC = CreateCompatibleDC(hdc);
  1042. memBM = CreateCompatibleBitmap(hdc, Width, Height);
  1043. hOld = (HBITMAP) SelectObject (memDC, memBM);
  1044. PrintWindow(window, memDC, (UINT) client_area);
  1045. im = gdImageCreateTrueColor(Width, Height);
  1046. if (im) {
  1047. int x,y;
  1048. for (y=0; y <= Height; y++) {
  1049. for (x=0; x <= Width; x++) {
  1050. int c = GetPixel(memDC, x,y);
  1051. gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
  1052. }
  1053. }
  1054. }
  1055. SelectObject(memDC,hOld);
  1056. DeleteObject(memBM);
  1057. DeleteDC(memDC);
  1058. ReleaseDC( 0, hdc );
  1059. if (!im) {
  1060. RETURN_FALSE;
  1061. }
  1062. php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
  1063. }
  1064. /* }}} */
  1065. /* {{{ Grab a screenshot */
  1066. PHP_FUNCTION(imagegrabscreen)
  1067. {
  1068. HWND window = GetDesktopWindow();
  1069. RECT rc = {0};
  1070. int Width, Height;
  1071. HDC hdc;
  1072. HDC memDC;
  1073. HBITMAP memBM;
  1074. HBITMAP hOld;
  1075. gdImagePtr im;
  1076. hdc = GetDC(0);
  1077. if (zend_parse_parameters_none() == FAILURE) {
  1078. RETURN_THROWS();
  1079. }
  1080. if (!hdc) {
  1081. RETURN_FALSE;
  1082. }
  1083. GetWindowRect(window, &rc);
  1084. Width = rc.right - rc.left;
  1085. Height = rc.bottom - rc.top;
  1086. Width = (Width/4)*4;
  1087. memDC = CreateCompatibleDC(hdc);
  1088. memBM = CreateCompatibleBitmap(hdc, Width, Height);
  1089. hOld = (HBITMAP) SelectObject (memDC, memBM);
  1090. BitBlt( memDC, 0, 0, Width, Height , hdc, rc.left, rc.top , SRCCOPY );
  1091. im = gdImageCreateTrueColor(Width, Height);
  1092. if (im) {
  1093. int x,y;
  1094. for (y=0; y <= Height; y++) {
  1095. for (x=0; x <= Width; x++) {
  1096. int c = GetPixel(memDC, x,y);
  1097. gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
  1098. }
  1099. }
  1100. }
  1101. SelectObject(memDC,hOld);
  1102. DeleteObject(memBM);
  1103. DeleteDC(memDC);
  1104. ReleaseDC( 0, hdc );
  1105. if (!im) {
  1106. RETURN_FALSE;
  1107. }
  1108. php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
  1109. }
  1110. /* }}} */
  1111. #endif /* PHP_WIN32 */
  1112. /* {{{ Rotate an image using a custom angle */
  1113. PHP_FUNCTION(imagerotate)
  1114. {
  1115. zval *SIM;
  1116. gdImagePtr im_dst, im_src;
  1117. double degrees;
  1118. zend_long color;
  1119. bool ignoretransparent = 0;
  1120. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Odl|b", &SIM, gd_image_ce, &degrees, &color, &ignoretransparent) == FAILURE) {
  1121. RETURN_THROWS();
  1122. }
  1123. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  1124. im_dst = gdImageRotateInterpolated(im_src, (const float)degrees, color);
  1125. if (im_dst == NULL) {
  1126. RETURN_FALSE;
  1127. }
  1128. php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_dst);
  1129. }
  1130. /* }}} */
  1131. /* {{{ Set the tile image to $tile when filling $image with the "IMG_COLOR_TILED" color */
  1132. PHP_FUNCTION(imagesettile)
  1133. {
  1134. zval *IM, *TILE;
  1135. gdImagePtr im, tile;
  1136. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &IM, gd_image_ce, &TILE, gd_image_ce) == FAILURE) {
  1137. RETURN_THROWS();
  1138. }
  1139. im = php_gd_libgdimageptr_from_zval_p(IM);
  1140. tile = php_gd_libgdimageptr_from_zval_p(TILE);
  1141. gdImageSetTile(im, tile);
  1142. RETURN_TRUE;
  1143. }
  1144. /* }}} */
  1145. /* {{{ Set the brush image to $brush when filling $image with the "IMG_COLOR_BRUSHED" color */
  1146. PHP_FUNCTION(imagesetbrush)
  1147. {
  1148. zval *IM, *TILE;
  1149. gdImagePtr im, tile;
  1150. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &IM, gd_image_ce, &TILE, gd_image_ce) == FAILURE) {
  1151. RETURN_THROWS();
  1152. }
  1153. im = php_gd_libgdimageptr_from_zval_p(IM);
  1154. tile = php_gd_libgdimageptr_from_zval_p(TILE);
  1155. gdImageSetBrush(im, tile);
  1156. RETURN_TRUE;
  1157. }
  1158. /* }}} */
  1159. /* {{{ Create a new image */
  1160. PHP_FUNCTION(imagecreate)
  1161. {
  1162. zend_long x_size, y_size;
  1163. gdImagePtr im;
  1164. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &x_size, &y_size) == FAILURE) {
  1165. RETURN_THROWS();
  1166. }
  1167. if (x_size <= 0 || x_size >= INT_MAX) {
  1168. zend_argument_value_error(1, "must be greater than 0");
  1169. RETURN_THROWS();
  1170. }
  1171. if (y_size <= 0 || y_size >= INT_MAX) {
  1172. zend_argument_value_error(2, "must be greater than 0");
  1173. RETURN_THROWS();
  1174. }
  1175. im = gdImageCreate(x_size, y_size);
  1176. if (!im) {
  1177. RETURN_FALSE;
  1178. }
  1179. php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
  1180. }
  1181. /* }}} */
  1182. /* {{{ Return the types of images supported in a bitfield - 1=GIF, 2=JPEG, 4=PNG, 8=WBMP, 16=XPM, etc */
  1183. PHP_FUNCTION(imagetypes)
  1184. {
  1185. int ret = 0;
  1186. ret = PHP_IMG_GIF;
  1187. #ifdef HAVE_GD_JPG
  1188. ret |= PHP_IMG_JPG;
  1189. #endif
  1190. #ifdef HAVE_GD_PNG
  1191. ret |= PHP_IMG_PNG;
  1192. #endif
  1193. ret |= PHP_IMG_WBMP;
  1194. #ifdef HAVE_GD_XPM
  1195. ret |= PHP_IMG_XPM;
  1196. #endif
  1197. #ifdef HAVE_GD_WEBP
  1198. ret |= PHP_IMG_WEBP;
  1199. #endif
  1200. #ifdef HAVE_GD_BMP
  1201. ret |= PHP_IMG_BMP;
  1202. #endif
  1203. #ifdef HAVE_GD_TGA
  1204. ret |= PHP_IMG_TGA;
  1205. #endif
  1206. #ifdef HAVE_GD_AVIF
  1207. ret |= PHP_IMG_AVIF;
  1208. #endif
  1209. if (zend_parse_parameters_none() == FAILURE) {
  1210. RETURN_THROWS();
  1211. }
  1212. RETURN_LONG(ret);
  1213. }
  1214. /* }}} */
  1215. /* {{{ _php_ctx_getmbi */
  1216. static int _php_ctx_getmbi(gdIOCtx *ctx)
  1217. {
  1218. int i, mbi = 0;
  1219. do {
  1220. i = (ctx->getC)(ctx);
  1221. if (i < 0) {
  1222. return -1;
  1223. }
  1224. mbi = (mbi << 7) | (i & 0x7f);
  1225. } while (i & 0x80);
  1226. return mbi;
  1227. }
  1228. /* }}} */
  1229. /* {{{ _php_image_type
  1230. * Based on ext/standard/image.c
  1231. */
  1232. static const char php_sig_gd2[3] = {'g', 'd', '2'};
  1233. static int _php_image_type(zend_string *data)
  1234. {
  1235. if (ZSTR_LEN(data) < 12) {
  1236. /* Handle this the same way as an unknown image type. */
  1237. return -1;
  1238. }
  1239. if (!memcmp(ZSTR_VAL(data), php_sig_gd2, sizeof(php_sig_gd2))) {
  1240. return PHP_GDIMG_TYPE_GD2;
  1241. } else if (!memcmp(ZSTR_VAL(data), php_sig_jpg, sizeof(php_sig_jpg))) {
  1242. return PHP_GDIMG_TYPE_JPG;
  1243. } else if (!memcmp(ZSTR_VAL(data), php_sig_png, sizeof(php_sig_png))) {
  1244. return PHP_GDIMG_TYPE_PNG;
  1245. } else if (!memcmp(ZSTR_VAL(data), php_sig_gif, sizeof(php_sig_gif))) {
  1246. return PHP_GDIMG_TYPE_GIF;
  1247. } else if (!memcmp(ZSTR_VAL(data), php_sig_bmp, sizeof(php_sig_bmp))) {
  1248. return PHP_GDIMG_TYPE_BMP;
  1249. } else if(!memcmp(ZSTR_VAL(data), php_sig_riff, sizeof(php_sig_riff)) && !memcmp(ZSTR_VAL(data) + sizeof(php_sig_riff) + sizeof(uint32_t), php_sig_webp, sizeof(php_sig_webp))) {
  1250. return PHP_GDIMG_TYPE_WEBP;
  1251. }
  1252. php_stream *image_stream = php_stream_memory_open(TEMP_STREAM_READONLY, data);
  1253. if (image_stream != NULL) {
  1254. bool is_avif = php_is_image_avif(image_stream);
  1255. php_stream_close(image_stream);
  1256. if (is_avif) {
  1257. return PHP_GDIMG_TYPE_AVIF;
  1258. }
  1259. }
  1260. gdIOCtx *io_ctx;
  1261. io_ctx = gdNewDynamicCtxEx(8, ZSTR_VAL(data), 0);
  1262. if (io_ctx) {
  1263. if (_php_ctx_getmbi(io_ctx) == 0 && _php_ctx_getmbi(io_ctx) >= 0) {
  1264. io_ctx->gd_free(io_ctx);
  1265. return PHP_GDIMG_TYPE_WBM;
  1266. } else {
  1267. io_ctx->gd_free(io_ctx);
  1268. }
  1269. }
  1270. return -1;
  1271. }
  1272. /* }}} */
  1273. /* {{{ _php_image_create_from_string */
  1274. gdImagePtr _php_image_create_from_string(zend_string *data, char *tn, gdImagePtr (*ioctx_func_p)(gdIOCtxPtr))
  1275. {
  1276. gdImagePtr im;
  1277. gdIOCtx *io_ctx;
  1278. io_ctx = gdNewDynamicCtxEx(ZSTR_LEN(data), ZSTR_VAL(data), 0);
  1279. if (!io_ctx) {
  1280. return NULL;
  1281. }
  1282. im = (*ioctx_func_p)(io_ctx);
  1283. if (!im) {
  1284. php_error_docref(NULL, E_WARNING, "Passed data is not in \"%s\" format", tn);
  1285. io_ctx->gd_free(io_ctx);
  1286. return NULL;
  1287. }
  1288. io_ctx->gd_free(io_ctx);
  1289. return im;
  1290. }
  1291. /* }}} */
  1292. /* {{{ Create a new image from the image stream in the string */
  1293. PHP_FUNCTION(imagecreatefromstring)
  1294. {
  1295. zend_string *data;
  1296. gdImagePtr im;
  1297. int imtype;
  1298. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &data) == FAILURE) {
  1299. RETURN_THROWS();
  1300. }
  1301. imtype = _php_image_type(data);
  1302. switch (imtype) {
  1303. case PHP_GDIMG_TYPE_JPG:
  1304. #ifdef HAVE_GD_JPG
  1305. im = _php_image_create_from_string(data, "JPEG", gdImageCreateFromJpegCtx);
  1306. #else
  1307. php_error_docref(NULL, E_WARNING, "No JPEG support in this PHP build");
  1308. RETURN_FALSE;
  1309. #endif
  1310. break;
  1311. case PHP_GDIMG_TYPE_PNG:
  1312. #ifdef HAVE_GD_PNG
  1313. im = _php_image_create_from_string(data, "PNG", gdImageCreateFromPngCtx);
  1314. #else
  1315. php_error_docref(NULL, E_WARNING, "No PNG support in this PHP build");
  1316. RETURN_FALSE;
  1317. #endif
  1318. break;
  1319. case PHP_GDIMG_TYPE_GIF:
  1320. im = _php_image_create_from_string(data, "GIF", gdImageCreateFromGifCtx);
  1321. break;
  1322. case PHP_GDIMG_TYPE_WBM:
  1323. im = _php_image_create_from_string(data, "WBMP", gdImageCreateFromWBMPCtx);
  1324. break;
  1325. case PHP_GDIMG_TYPE_GD2:
  1326. im = _php_image_create_from_string(data, "GD2", gdImageCreateFromGd2Ctx);
  1327. break;
  1328. case PHP_GDIMG_TYPE_BMP:
  1329. im = _php_image_create_from_string(data, "BMP", gdImageCreateFromBmpCtx);
  1330. break;
  1331. case PHP_GDIMG_TYPE_WEBP:
  1332. #ifdef HAVE_GD_WEBP
  1333. im = _php_image_create_from_string(data, "WEBP", gdImageCreateFromWebpCtx);
  1334. break;
  1335. #else
  1336. php_error_docref(NULL, E_WARNING, "No WEBP support in this PHP build");
  1337. RETURN_FALSE;
  1338. #endif
  1339. case PHP_GDIMG_TYPE_AVIF:
  1340. #ifdef HAVE_GD_AVIF
  1341. im = _php_image_create_from_string(data, "AVIF", gdImageCreateFromAvifCtx);
  1342. break;
  1343. #else
  1344. php_error_docref(NULL, E_WARNING, "No AVIF support in this PHP build");
  1345. RETURN_FALSE;
  1346. #endif
  1347. default:
  1348. php_error_docref(NULL, E_WARNING, "Data is not in a recognized format");
  1349. RETURN_FALSE;
  1350. }
  1351. if (!im) {
  1352. php_error_docref(NULL, E_WARNING, "Couldn't create GD Image Stream out of Data");
  1353. RETURN_FALSE;
  1354. }
  1355. php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
  1356. }
  1357. /* }}} */
  1358. /* {{{ _php_image_create_from */
  1359. static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(FILE *), gdImagePtr (*ioctx_func_p)(gdIOCtxPtr))
  1360. {
  1361. char *file;
  1362. size_t file_len;
  1363. zend_long srcx, srcy, width, height;
  1364. gdImagePtr im = NULL;
  1365. php_stream *stream;
  1366. FILE * fp = NULL;
  1367. #ifdef HAVE_GD_JPG
  1368. long ignore_warning;
  1369. #endif
  1370. if (image_type == PHP_GDIMG_TYPE_GD2PART) {
  1371. if (zend_parse_parameters(ZEND_NUM_ARGS(), "pllll", &file, &file_len, &srcx, &srcy, &width, &height) == FAILURE) {
  1372. RETURN_THROWS();
  1373. }
  1374. if (width < 1) {
  1375. zend_argument_value_error(4, "must be greater than or equal to 1");
  1376. RETURN_THROWS();
  1377. }
  1378. if (height < 1) {
  1379. zend_argument_value_error(5, "must be greater than or equal to 1");
  1380. RETURN_THROWS();
  1381. }
  1382. } else {
  1383. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &file, &file_len) == FAILURE) {
  1384. RETURN_THROWS();
  1385. }
  1386. }
  1387. stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|IGNORE_PATH, NULL);
  1388. if (stream == NULL) {
  1389. RETURN_FALSE;
  1390. }
  1391. /* try and avoid allocating a FILE* if the stream is not naturally a FILE* */
  1392. if (php_stream_is(stream, PHP_STREAM_IS_STDIO)) {
  1393. if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {
  1394. goto out_err;
  1395. }
  1396. } else if (ioctx_func_p || image_type == PHP_GDIMG_TYPE_GD2PART) {
  1397. /* we can create an io context */
  1398. gdIOCtx* io_ctx;
  1399. zend_string *buff;
  1400. char *pstr;
  1401. buff = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
  1402. if (!buff) {
  1403. php_error_docref(NULL, E_WARNING,"Cannot read image data");
  1404. goto out_err;
  1405. }
  1406. /* needs to be malloc (persistent) - GD will free() it later */
  1407. pstr = pestrndup(ZSTR_VAL(buff), ZSTR_LEN(buff), 1);
  1408. io_ctx = gdNewDynamicCtxEx(ZSTR_LEN(buff), pstr, 0);
  1409. if (!io_ctx) {
  1410. pefree(pstr, 1);
  1411. zend_string_release_ex(buff, 0);
  1412. php_error_docref(NULL, E_WARNING,"Cannot allocate GD IO context");
  1413. goto out_err;
  1414. }
  1415. if (image_type == PHP_GDIMG_TYPE_GD2PART) {
  1416. im = gdImageCreateFromGd2PartCtx(io_ctx, srcx, srcy, width, height);
  1417. } else {
  1418. im = (*ioctx_func_p)(io_ctx);
  1419. }
  1420. io_ctx->gd_free(io_ctx);
  1421. pefree(pstr, 1);
  1422. zend_string_release_ex(buff, 0);
  1423. }
  1424. else if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO)) {
  1425. /* try and force the stream to be FILE* */
  1426. if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_TRY_HARD, (void **) &fp, REPORT_ERRORS)) {
  1427. goto out_err;
  1428. }
  1429. }
  1430. if (!im && fp) {
  1431. switch (image_type) {
  1432. case PHP_GDIMG_TYPE_GD2PART:
  1433. im = gdImageCreateFromGd2Part(fp, srcx, srcy, width, height);
  1434. break;
  1435. #ifdef HAVE_GD_XPM
  1436. case PHP_GDIMG_TYPE_XPM:
  1437. im = gdImageCreateFromXpm(file);
  1438. break;
  1439. #endif
  1440. #ifdef HAVE_GD_JPG
  1441. case PHP_GDIMG_TYPE_JPG:
  1442. ignore_warning = INI_INT("gd.jpeg_ignore_warning");
  1443. im = gdImageCreateFromJpegEx(fp, ignore_warning);
  1444. break;
  1445. #endif
  1446. default:
  1447. im = (*func_p)(fp);
  1448. break;
  1449. }
  1450. fflush(fp);
  1451. }
  1452. /* register_im: */
  1453. if (im) {
  1454. php_stream_close(stream);
  1455. php_gd_assign_libgdimageptr_as_extgdimage(return_value, im);
  1456. return;
  1457. }
  1458. php_error_docref(NULL, E_WARNING, "\"%s\" is not a valid %s file", file, tn);
  1459. out_err:
  1460. php_stream_close(stream);
  1461. RETURN_FALSE;
  1462. }
  1463. /* }}} */
  1464. /* {{{ Create a new image from GIF file or URL */
  1465. PHP_FUNCTION(imagecreatefromgif)
  1466. {
  1467. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageCreateFromGif, gdImageCreateFromGifCtx);
  1468. }
  1469. /* }}} */
  1470. #ifdef HAVE_GD_JPG
  1471. /* {{{ Create a new image from JPEG file or URL */
  1472. PHP_FUNCTION(imagecreatefromjpeg)
  1473. {
  1474. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageCreateFromJpeg, gdImageCreateFromJpegCtx);
  1475. }
  1476. /* }}} */
  1477. #endif /* HAVE_GD_JPG */
  1478. #ifdef HAVE_GD_PNG
  1479. /* {{{ Create a new image from PNG file or URL */
  1480. PHP_FUNCTION(imagecreatefrompng)
  1481. {
  1482. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImageCreateFromPng, gdImageCreateFromPngCtx);
  1483. }
  1484. /* }}} */
  1485. #endif /* HAVE_GD_PNG */
  1486. #ifdef HAVE_GD_WEBP
  1487. /* {{{ Create a new image from WEBP file or URL */
  1488. PHP_FUNCTION(imagecreatefromwebp)
  1489. {
  1490. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WEBP, "WEBP", gdImageCreateFromWebp, gdImageCreateFromWebpCtx);
  1491. }
  1492. /* }}} */
  1493. #endif /* HAVE_GD_WEBP */
  1494. /* {{{ Create a new image from XBM file or URL */
  1495. PHP_FUNCTION(imagecreatefromxbm)
  1496. {
  1497. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageCreateFromXbm, NULL);
  1498. }
  1499. /* }}} */
  1500. #ifdef HAVE_GD_AVIF
  1501. /* {{{ Create a new image from AVIF file or URL */
  1502. PHP_FUNCTION(imagecreatefromavif)
  1503. {
  1504. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_AVIF, "AVIF", gdImageCreateFromAvif, gdImageCreateFromAvifCtx);
  1505. }
  1506. /* }}} */
  1507. #endif /* HAVE_GD_AVIF */
  1508. #ifdef HAVE_GD_XPM
  1509. /* {{{ Create a new image from XPM file or URL */
  1510. PHP_FUNCTION(imagecreatefromxpm)
  1511. {
  1512. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XPM, "XPM", NULL, NULL);
  1513. }
  1514. /* }}} */
  1515. #endif
  1516. /* {{{ Create a new image from WBMP file or URL */
  1517. PHP_FUNCTION(imagecreatefromwbmp)
  1518. {
  1519. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageCreateFromWBMP, gdImageCreateFromWBMPCtx);
  1520. }
  1521. /* }}} */
  1522. /* {{{ Create a new image from GD file or URL */
  1523. PHP_FUNCTION(imagecreatefromgd)
  1524. {
  1525. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageCreateFromGd, gdImageCreateFromGdCtx);
  1526. }
  1527. /* }}} */
  1528. /* {{{ Create a new image from GD2 file or URL */
  1529. PHP_FUNCTION(imagecreatefromgd2)
  1530. {
  1531. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageCreateFromGd2, gdImageCreateFromGd2Ctx);
  1532. }
  1533. /* }}} */
  1534. /* {{{ Create a new image from a given part of GD2 file or URL */
  1535. PHP_FUNCTION(imagecreatefromgd2part)
  1536. {
  1537. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2PART, "GD2", NULL, NULL);
  1538. }
  1539. /* }}} */
  1540. #ifdef HAVE_GD_BMP
  1541. /* {{{ Create a new image from BMP file or URL */
  1542. PHP_FUNCTION(imagecreatefrombmp)
  1543. {
  1544. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_BMP, "BMP", gdImageCreateFromBmp, gdImageCreateFromBmpCtx);
  1545. }
  1546. /* }}} */
  1547. #endif
  1548. #ifdef HAVE_GD_TGA
  1549. /* {{{ Create a new image from TGA file or URL */
  1550. PHP_FUNCTION(imagecreatefromtga)
  1551. {
  1552. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_TGA, "TGA", gdImageCreateFromTga, gdImageCreateFromTgaCtx);
  1553. }
  1554. /* }}} */
  1555. #endif
  1556. /* {{{ _php_image_output */
  1557. static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn)
  1558. {
  1559. zval *imgind;
  1560. char *file = NULL;
  1561. zend_long quality = 0, type = 0;
  1562. gdImagePtr im;
  1563. FILE *fp;
  1564. size_t file_len = 0;
  1565. int argc = ZEND_NUM_ARGS();
  1566. int q = -1, t = 1;
  1567. /* The quality parameter for gd2 stands for chunk size */
  1568. switch (image_type) {
  1569. case PHP_GDIMG_TYPE_GD:
  1570. if (zend_parse_parameters(argc, "O|p!", &imgind, gd_image_ce, &file, &file_len) == FAILURE) {
  1571. RETURN_THROWS();
  1572. }
  1573. break;
  1574. case PHP_GDIMG_TYPE_GD2:
  1575. if (zend_parse_parameters(argc, "O|p!ll", &imgind, gd_image_ce, &file, &file_len, &quality, &type) == FAILURE) {
  1576. RETURN_THROWS();
  1577. }
  1578. break;
  1579. EMPTY_SWITCH_DEFAULT_CASE()
  1580. }
  1581. im = php_gd_libgdimageptr_from_zval_p(imgind);
  1582. if (argc >= 3) {
  1583. q = quality;
  1584. if (argc == 4) {
  1585. t = type;
  1586. }
  1587. }
  1588. if (file_len) {
  1589. PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename");
  1590. fp = VCWD_FOPEN(file, "wb");
  1591. if (!fp) {
  1592. php_error_docref(NULL, E_WARNING, "Unable to open \"%s\" for writing", file);
  1593. RETURN_FALSE;
  1594. }
  1595. switch (image_type) {
  1596. case PHP_GDIMG_TYPE_GD:
  1597. gdImageGd(im, fp);
  1598. break;
  1599. case PHP_GDIMG_TYPE_GD2:
  1600. if (q == -1) {
  1601. q = 128;
  1602. }
  1603. gdImageGd2(im, fp, q, t);
  1604. break;
  1605. EMPTY_SWITCH_DEFAULT_CASE()
  1606. }
  1607. fflush(fp);
  1608. fclose(fp);
  1609. } else {
  1610. int b;
  1611. FILE *tmp;
  1612. char buf[4096];
  1613. zend_string *path;
  1614. tmp = php_open_temporary_file(NULL, NULL, &path);
  1615. if (tmp == NULL) {
  1616. php_error_docref(NULL, E_WARNING, "Unable to open temporary file");
  1617. RETURN_FALSE;
  1618. }
  1619. switch (image_type) {
  1620. case PHP_GDIMG_TYPE_GD:
  1621. gdImageGd(im, tmp);
  1622. break;
  1623. case PHP_GDIMG_TYPE_GD2:
  1624. if (q == -1) {
  1625. q = 128;
  1626. }
  1627. gdImageGd2(im, tmp, q, t);
  1628. break;
  1629. EMPTY_SWITCH_DEFAULT_CASE()
  1630. }
  1631. fseek(tmp, 0, SEEK_SET);
  1632. while ((b = fread(buf, 1, sizeof(buf), tmp)) > 0) {
  1633. php_write(buf, b);
  1634. }
  1635. fclose(tmp);
  1636. VCWD_UNLINK((const char *)ZSTR_VAL(path)); /* make sure that the temporary file is removed */
  1637. zend_string_release_ex(path, 0);
  1638. }
  1639. RETURN_TRUE;
  1640. }
  1641. /* }}} */
  1642. /* {{{ Output XBM image to browser or file */
  1643. PHP_FUNCTION(imagexbm)
  1644. {
  1645. zval *imgind;
  1646. char *file = NULL;
  1647. size_t file_len = 0;
  1648. zend_long foreground_color;
  1649. bool foreground_color_is_null = 1;
  1650. gdImagePtr im;
  1651. int i;
  1652. gdIOCtx *ctx = NULL;
  1653. php_stream *stream;
  1654. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Op!|l!", &imgind, gd_image_ce, &file, &file_len, &foreground_color, &foreground_color_is_null) == FAILURE) {
  1655. RETURN_THROWS();
  1656. }
  1657. im = php_gd_libgdimageptr_from_zval_p(imgind);
  1658. if (file != NULL) {
  1659. stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH, NULL);
  1660. if (stream == NULL) {
  1661. RETURN_FALSE;
  1662. }
  1663. ctx = create_stream_context(stream, 1);
  1664. } else {
  1665. ctx = create_output_context();
  1666. }
  1667. if (foreground_color_is_null) {
  1668. for (i=0; i < gdImageColorsTotal(im); i++) {
  1669. if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
  1670. break;
  1671. }
  1672. }
  1673. foreground_color = i;
  1674. }
  1675. gdImageXbmCtx(im, file ? file : "", (int) foreground_color, ctx);
  1676. ctx->gd_free(ctx);
  1677. RETURN_TRUE;
  1678. }
  1679. /* }}} */
  1680. /* {{{ Output GIF image to browser or file */
  1681. PHP_FUNCTION(imagegif)
  1682. {
  1683. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF");
  1684. }
  1685. /* }}} */
  1686. #ifdef HAVE_GD_PNG
  1687. /* {{{ Output PNG image to browser or file */
  1688. PHP_FUNCTION(imagepng)
  1689. {
  1690. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG");
  1691. }
  1692. /* }}} */
  1693. #endif /* HAVE_GD_PNG */
  1694. #ifdef HAVE_GD_WEBP
  1695. /* {{{ Output WEBP image to browser or file */
  1696. PHP_FUNCTION(imagewebp)
  1697. {
  1698. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WEBP, "WEBP");
  1699. }
  1700. /* }}} */
  1701. #endif /* HAVE_GD_WEBP */
  1702. #ifdef HAVE_GD_AVIF
  1703. /* {{{ Output AVIF image to browser or file */
  1704. PHP_FUNCTION(imageavif)
  1705. {
  1706. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_AVIF, "AVIF");
  1707. }
  1708. /* }}} */
  1709. #endif /* HAVE_GD_AVIF */
  1710. #ifdef HAVE_GD_JPG
  1711. /* {{{ Output JPEG image to browser or file */
  1712. PHP_FUNCTION(imagejpeg)
  1713. {
  1714. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG");
  1715. }
  1716. /* }}} */
  1717. #endif /* HAVE_GD_JPG */
  1718. /* {{{ Output WBMP image to browser or file */
  1719. PHP_FUNCTION(imagewbmp)
  1720. {
  1721. zval *imgind;
  1722. zend_long foreground_color;
  1723. zend_long foreground_color_is_null = 1;
  1724. gdImagePtr im;
  1725. int i;
  1726. gdIOCtx *ctx = NULL;
  1727. zval *to_zval = NULL;
  1728. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l!", &imgind, gd_image_ce, &to_zval, &foreground_color, &foreground_color_is_null) == FAILURE) {
  1729. RETURN_THROWS();
  1730. }
  1731. im = php_gd_libgdimageptr_from_zval_p(imgind);
  1732. if (to_zval != NULL) {
  1733. ctx = create_stream_context_from_zval(to_zval);
  1734. if (!ctx) {
  1735. RETURN_FALSE;
  1736. }
  1737. } else {
  1738. ctx = create_output_context();
  1739. }
  1740. if (foreground_color_is_null) {
  1741. for (i=0; i < gdImageColorsTotal(im); i++) {
  1742. if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
  1743. break;
  1744. }
  1745. }
  1746. foreground_color = i;
  1747. }
  1748. gdImageWBMPCtx(im, foreground_color, ctx);
  1749. ctx->gd_free(ctx);
  1750. RETURN_TRUE;
  1751. }
  1752. /* }}} */
  1753. /* {{{ Output GD image to browser or file */
  1754. PHP_FUNCTION(imagegd)
  1755. {
  1756. _php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD");
  1757. }
  1758. /* }}} */
  1759. /* {{{ Output GD2 image to browser or file */
  1760. PHP_FUNCTION(imagegd2)
  1761. {
  1762. _php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2");
  1763. }
  1764. /* }}} */
  1765. #ifdef HAVE_GD_BMP
  1766. /* {{{ Output BMP image to browser or file */
  1767. PHP_FUNCTION(imagebmp)
  1768. {
  1769. zval *imgind;
  1770. bool compressed = 1;
  1771. gdImagePtr im;
  1772. gdIOCtx *ctx = NULL;
  1773. zval *to_zval = NULL;
  1774. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!b", &imgind, gd_image_ce, &to_zval, &compressed) == FAILURE) {
  1775. RETURN_THROWS();
  1776. }
  1777. im = php_gd_libgdimageptr_from_zval_p(imgind);
  1778. if (to_zval != NULL) {
  1779. ctx = create_stream_context_from_zval(to_zval);
  1780. if (!ctx) {
  1781. RETURN_FALSE;
  1782. }
  1783. } else {
  1784. ctx = create_output_context();
  1785. }
  1786. gdImageBmpCtx(im, ctx, (int) compressed);
  1787. ctx->gd_free(ctx);
  1788. RETURN_TRUE;
  1789. }
  1790. /* }}} */
  1791. #endif
  1792. /* {{{ Destroy an image - No effect as of PHP 8.0 */
  1793. PHP_FUNCTION(imagedestroy)
  1794. {
  1795. /* This function used to free the resource, as resources are no longer used, it does nothing */
  1796. zval *IM;
  1797. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
  1798. RETURN_THROWS();
  1799. }
  1800. RETURN_TRUE;
  1801. }
  1802. /* }}} */
  1803. /* {{{ Allocate a color for an image */
  1804. PHP_FUNCTION(imagecolorallocate)
  1805. {
  1806. zval *IM;
  1807. zend_long red, green, blue;
  1808. gdImagePtr im;
  1809. int ct = (-1);
  1810. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
  1811. RETURN_THROWS();
  1812. }
  1813. im = php_gd_libgdimageptr_from_zval_p(IM);
  1814. CHECK_RGBA_RANGE(red, Red, 2);
  1815. CHECK_RGBA_RANGE(green, Green, 3);
  1816. CHECK_RGBA_RANGE(blue, Blue, 4);
  1817. ct = gdImageColorAllocate(im, red, green, blue);
  1818. if (ct < 0) {
  1819. RETURN_FALSE;
  1820. }
  1821. RETURN_LONG(ct);
  1822. }
  1823. /* }}} */
  1824. /* {{{ Copy the palette from the src image onto the dst image */
  1825. PHP_FUNCTION(imagepalettecopy)
  1826. {
  1827. zval *dstim, *srcim;
  1828. gdImagePtr dst, src;
  1829. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &dstim, gd_image_ce, &srcim, gd_image_ce) == FAILURE) {
  1830. RETURN_THROWS();
  1831. }
  1832. src = php_gd_libgdimageptr_from_zval_p(srcim);
  1833. dst = php_gd_libgdimageptr_from_zval_p(dstim);
  1834. gdImagePaletteCopy(dst, src);
  1835. }
  1836. /* }}} */
  1837. /* {{{ Get the index of the color of a pixel */
  1838. PHP_FUNCTION(imagecolorat)
  1839. {
  1840. zval *IM;
  1841. zend_long x, y;
  1842. gdImagePtr im;
  1843. ZEND_PARSE_PARAMETERS_START(3, 3)
  1844. Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
  1845. Z_PARAM_LONG(x)
  1846. Z_PARAM_LONG(y)
  1847. ZEND_PARSE_PARAMETERS_END();
  1848. im = php_gd_libgdimageptr_from_zval_p(IM);
  1849. if (gdImageTrueColor(im)) {
  1850. if (im->tpixels && gdImageBoundsSafe(im, x, y)) {
  1851. RETURN_LONG(gdImageTrueColorPixel(im, x, y));
  1852. } else {
  1853. php_error_docref(NULL, E_NOTICE, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y);
  1854. RETURN_FALSE;
  1855. }
  1856. } else {
  1857. if (im->pixels && gdImageBoundsSafe(im, x, y)) {
  1858. RETURN_LONG(im->pixels[y][x]);
  1859. } else {
  1860. php_error_docref(NULL, E_NOTICE, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y);
  1861. RETURN_FALSE;
  1862. }
  1863. }
  1864. }
  1865. /* }}} */
  1866. /* {{{ Get the index of the closest color to the specified color */
  1867. PHP_FUNCTION(imagecolorclosest)
  1868. {
  1869. zval *IM;
  1870. zend_long red, green, blue;
  1871. gdImagePtr im;
  1872. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
  1873. RETURN_THROWS();
  1874. }
  1875. im = php_gd_libgdimageptr_from_zval_p(IM);
  1876. CHECK_RGBA_RANGE(red, Red, 2);
  1877. CHECK_RGBA_RANGE(green, Green, 3);
  1878. CHECK_RGBA_RANGE(blue, Blue, 4);
  1879. RETURN_LONG(gdImageColorClosest(im, red, green, blue));
  1880. }
  1881. /* }}} */
  1882. /* {{{ Get the index of the color which has the hue, white and blackness nearest to the given color */
  1883. PHP_FUNCTION(imagecolorclosesthwb)
  1884. {
  1885. zval *IM;
  1886. zend_long red, green, blue;
  1887. gdImagePtr im;
  1888. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
  1889. RETURN_THROWS();
  1890. }
  1891. im = php_gd_libgdimageptr_from_zval_p(IM);
  1892. CHECK_RGBA_RANGE(red, Red, 2);
  1893. CHECK_RGBA_RANGE(green, Green, 3);
  1894. CHECK_RGBA_RANGE(blue, Blue, 4);
  1895. RETURN_LONG(gdImageColorClosestHWB(im, red, green, blue));
  1896. }
  1897. /* }}} */
  1898. /* {{{ De-allocate a color for an image */
  1899. PHP_FUNCTION(imagecolordeallocate)
  1900. {
  1901. zval *IM;
  1902. zend_long index;
  1903. int col;
  1904. gdImagePtr im;
  1905. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &index) == FAILURE) {
  1906. RETURN_THROWS();
  1907. }
  1908. im = php_gd_libgdimageptr_from_zval_p(IM);
  1909. /* We can return right away for a truecolor image as deallocating colours is meaningless here */
  1910. if (gdImageTrueColor(im)) {
  1911. RETURN_TRUE;
  1912. }
  1913. col = index;
  1914. if (col >= 0 && col < gdImageColorsTotal(im)) {
  1915. gdImageColorDeallocate(im, col);
  1916. RETURN_TRUE;
  1917. } else {
  1918. zend_argument_value_error(2, "must be between 0 and %d", gdImageColorsTotal(im));
  1919. RETURN_THROWS();
  1920. }
  1921. }
  1922. /* }}} */
  1923. /* {{{ Get the index of the specified color or its closest possible alternative */
  1924. PHP_FUNCTION(imagecolorresolve)
  1925. {
  1926. zval *IM;
  1927. zend_long red, green, blue;
  1928. gdImagePtr im;
  1929. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
  1930. RETURN_THROWS();
  1931. }
  1932. im = php_gd_libgdimageptr_from_zval_p(IM);
  1933. CHECK_RGBA_RANGE(red, Red, 2);
  1934. CHECK_RGBA_RANGE(green, Green, 3);
  1935. CHECK_RGBA_RANGE(blue, Blue, 4);
  1936. RETURN_LONG(gdImageColorResolve(im, red, green, blue));
  1937. }
  1938. /* }}} */
  1939. /* {{{ Get the index of the specified color */
  1940. PHP_FUNCTION(imagecolorexact)
  1941. {
  1942. zval *IM;
  1943. zend_long red, green, blue;
  1944. gdImagePtr im;
  1945. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &red, &green, &blue) == FAILURE) {
  1946. RETURN_THROWS();
  1947. }
  1948. im = php_gd_libgdimageptr_from_zval_p(IM);
  1949. CHECK_RGBA_RANGE(red, Red, 2);
  1950. CHECK_RGBA_RANGE(green, Green, 3);
  1951. CHECK_RGBA_RANGE(blue, Blue, 4);
  1952. RETURN_LONG(gdImageColorExact(im, red, green, blue));
  1953. }
  1954. /* }}} */
  1955. /* {{{ Set the color for the specified palette index */
  1956. PHP_FUNCTION(imagecolorset)
  1957. {
  1958. zval *IM;
  1959. zend_long color, red, green, blue, alpha = 0;
  1960. int col;
  1961. gdImagePtr im;
  1962. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll|l", &IM, gd_image_ce, &color, &red, &green, &blue, &alpha) == FAILURE) {
  1963. RETURN_THROWS();
  1964. }
  1965. im = php_gd_libgdimageptr_from_zval_p(IM);
  1966. CHECK_RGBA_RANGE(red, Red, 2);
  1967. CHECK_RGBA_RANGE(green, Green, 3);
  1968. CHECK_RGBA_RANGE(blue, Blue, 4);
  1969. col = color;
  1970. if (col >= 0 && col < gdImageColorsTotal(im)) {
  1971. im->red[col] = red;
  1972. im->green[col] = green;
  1973. im->blue[col] = blue;
  1974. im->alpha[col] = alpha;
  1975. } else {
  1976. RETURN_FALSE;
  1977. }
  1978. }
  1979. /* }}} */
  1980. /* {{{ Get the colors for an index */
  1981. PHP_FUNCTION(imagecolorsforindex)
  1982. {
  1983. zval *IM;
  1984. zend_long index;
  1985. int col;
  1986. gdImagePtr im;
  1987. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &index) == FAILURE) {
  1988. RETURN_THROWS();
  1989. }
  1990. im = php_gd_libgdimageptr_from_zval_p(IM);
  1991. col = index;
  1992. if ((col >= 0 && gdImageTrueColor(im)) || (!gdImageTrueColor(im) && col >= 0 && col < gdImageColorsTotal(im))) {
  1993. array_init(return_value);
  1994. add_assoc_long(return_value,"red", gdImageRed(im,col));
  1995. add_assoc_long(return_value,"green", gdImageGreen(im,col));
  1996. add_assoc_long(return_value,"blue", gdImageBlue(im,col));
  1997. add_assoc_long(return_value,"alpha", gdImageAlpha(im,col));
  1998. } else {
  1999. zend_argument_value_error(2, "is out of range");
  2000. RETURN_THROWS();
  2001. }
  2002. }
  2003. /* }}} */
  2004. /* {{{ Apply a gamma correction to a GD image */
  2005. PHP_FUNCTION(imagegammacorrect)
  2006. {
  2007. zval *IM;
  2008. gdImagePtr im;
  2009. int i;
  2010. double input, output, gamma;
  2011. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Odd", &IM, gd_image_ce, &input, &output) == FAILURE) {
  2012. RETURN_THROWS();
  2013. }
  2014. if (input <= 0.0) {
  2015. zend_argument_value_error(2, "must be greater than 0");
  2016. RETURN_THROWS();
  2017. }
  2018. if (output <= 0.0) {
  2019. zend_argument_value_error(3, "must be greater than 0");
  2020. RETURN_THROWS();
  2021. }
  2022. gamma = input / output;
  2023. im = php_gd_libgdimageptr_from_zval_p(IM);
  2024. if (gdImageTrueColor(im)) {
  2025. int x, y, c;
  2026. for (y = 0; y < gdImageSY(im); y++) {
  2027. for (x = 0; x < gdImageSX(im); x++) {
  2028. c = gdImageGetPixel(im, x, y);
  2029. gdImageSetPixel(im, x, y,
  2030. gdTrueColorAlpha(
  2031. (int) ((pow((gdTrueColorGetRed(c) / 255.0), gamma) * 255) + .5),
  2032. (int) ((pow((gdTrueColorGetGreen(c) / 255.0), gamma) * 255) + .5),
  2033. (int) ((pow((gdTrueColorGetBlue(c) / 255.0), gamma) * 255) + .5),
  2034. gdTrueColorGetAlpha(c)
  2035. )
  2036. );
  2037. }
  2038. }
  2039. RETURN_TRUE;
  2040. }
  2041. for (i = 0; i < gdImageColorsTotal(im); i++) {
  2042. im->red[i] = (int)((pow((im->red[i] / 255.0), gamma) * 255) + .5);
  2043. im->green[i] = (int)((pow((im->green[i] / 255.0), gamma) * 255) + .5);
  2044. im->blue[i] = (int)((pow((im->blue[i] / 255.0), gamma) * 255) + .5);
  2045. }
  2046. RETURN_TRUE;
  2047. }
  2048. /* }}} */
  2049. /* {{{ Set a single pixel */
  2050. PHP_FUNCTION(imagesetpixel)
  2051. {
  2052. zval *IM;
  2053. zend_long x, y, col;
  2054. gdImagePtr im;
  2055. ZEND_PARSE_PARAMETERS_START(4, 4)
  2056. Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
  2057. Z_PARAM_LONG(x)
  2058. Z_PARAM_LONG(y)
  2059. Z_PARAM_LONG(col)
  2060. ZEND_PARSE_PARAMETERS_END();
  2061. im = php_gd_libgdimageptr_from_zval_p(IM);
  2062. gdImageSetPixel(im, x, y, col);
  2063. RETURN_TRUE;
  2064. }
  2065. /* }}} */
  2066. /* {{{ Draw a line */
  2067. PHP_FUNCTION(imageline)
  2068. {
  2069. zval *IM;
  2070. zend_long x1, y1, x2, y2, col;
  2071. gdImagePtr im;
  2072. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
  2073. RETURN_THROWS();
  2074. }
  2075. im = php_gd_libgdimageptr_from_zval_p(IM);
  2076. if (im->AA) {
  2077. gdImageSetAntiAliased(im, col);
  2078. col = gdAntiAliased;
  2079. }
  2080. gdImageLine(im, x1, y1, x2, y2, col);
  2081. RETURN_TRUE;
  2082. }
  2083. /* }}} */
  2084. /* {{{ Draw a dashed line */
  2085. PHP_FUNCTION(imagedashedline)
  2086. {
  2087. zval *IM;
  2088. zend_long x1, y1, x2, y2, col;
  2089. gdImagePtr im;
  2090. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
  2091. RETURN_THROWS();
  2092. }
  2093. im = php_gd_libgdimageptr_from_zval_p(IM);
  2094. gdImageDashedLine(im, x1, y1, x2, y2, col);
  2095. RETURN_TRUE;
  2096. }
  2097. /* }}} */
  2098. /* {{{ Draw a rectangle */
  2099. PHP_FUNCTION(imagerectangle)
  2100. {
  2101. zval *IM;
  2102. zend_long x1, y1, x2, y2, col;
  2103. gdImagePtr im;
  2104. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
  2105. RETURN_THROWS();
  2106. }
  2107. im = php_gd_libgdimageptr_from_zval_p(IM);
  2108. gdImageRectangle(im, x1, y1, x2, y2, col);
  2109. RETURN_TRUE;
  2110. }
  2111. /* }}} */
  2112. /* {{{ Draw a filled rectangle */
  2113. PHP_FUNCTION(imagefilledrectangle)
  2114. {
  2115. zval *IM;
  2116. zend_long x1, y1, x2, y2, col;
  2117. gdImagePtr im;
  2118. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &x1, &y1, &x2, &y2, &col) == FAILURE) {
  2119. RETURN_THROWS();
  2120. }
  2121. im = php_gd_libgdimageptr_from_zval_p(IM);
  2122. gdImageFilledRectangle(im, x1, y1, x2, y2, col);
  2123. RETURN_TRUE;
  2124. }
  2125. /* }}} */
  2126. /* {{{ Draw a partial ellipse */
  2127. PHP_FUNCTION(imagearc)
  2128. {
  2129. zval *IM;
  2130. zend_long cx, cy, w, h, ST, E, col;
  2131. gdImagePtr im;
  2132. int e, st;
  2133. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &ST, &E, &col) == FAILURE) {
  2134. RETURN_THROWS();
  2135. }
  2136. im = php_gd_libgdimageptr_from_zval_p(IM);
  2137. e = E;
  2138. if (e < 0) {
  2139. e %= 360;
  2140. }
  2141. st = ST;
  2142. if (st < 0) {
  2143. st %= 360;
  2144. }
  2145. gdImageArc(im, cx, cy, w, h, st, e, col);
  2146. RETURN_TRUE;
  2147. }
  2148. /* }}} */
  2149. /* {{{ Draw an ellipse */
  2150. PHP_FUNCTION(imageellipse)
  2151. {
  2152. zval *IM;
  2153. zend_long cx, cy, w, h, color;
  2154. gdImagePtr im;
  2155. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olllll", &IM, gd_image_ce, &cx, &cy, &w, &h, &color) == FAILURE) {
  2156. RETURN_THROWS();
  2157. }
  2158. im = php_gd_libgdimageptr_from_zval_p(IM);
  2159. gdImageEllipse(im, cx, cy, w, h, color);
  2160. RETURN_TRUE;
  2161. }
  2162. /* }}} */
  2163. /* {{{ Flood fill to specific color */
  2164. PHP_FUNCTION(imagefilltoborder)
  2165. {
  2166. zval *IM;
  2167. zend_long x, y, border, col;
  2168. gdImagePtr im;
  2169. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &IM, gd_image_ce, &x, &y, &border, &col) == FAILURE) {
  2170. RETURN_THROWS();
  2171. }
  2172. im = php_gd_libgdimageptr_from_zval_p(IM);
  2173. gdImageFillToBorder(im, x, y, border, col);
  2174. RETURN_TRUE;
  2175. }
  2176. /* }}} */
  2177. /* {{{ Flood fill */
  2178. PHP_FUNCTION(imagefill)
  2179. {
  2180. zval *IM;
  2181. zend_long x, y, col;
  2182. gdImagePtr im;
  2183. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll", &IM, gd_image_ce, &x, &y, &col) == FAILURE) {
  2184. RETURN_THROWS();
  2185. }
  2186. im = php_gd_libgdimageptr_from_zval_p(IM);
  2187. gdImageFill(im, x, y, col);
  2188. RETURN_TRUE;
  2189. }
  2190. /* }}} */
  2191. /* {{{ Find out the number of colors in an image's palette */
  2192. PHP_FUNCTION(imagecolorstotal)
  2193. {
  2194. zval *IM;
  2195. gdImagePtr im;
  2196. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
  2197. RETURN_THROWS();
  2198. }
  2199. im = php_gd_libgdimageptr_from_zval_p(IM);
  2200. RETURN_LONG(gdImageColorsTotal(im));
  2201. }
  2202. /* }}} */
  2203. /* {{{ Define a color as transparent */
  2204. PHP_FUNCTION(imagecolortransparent)
  2205. {
  2206. zval *IM;
  2207. zend_long COL = 0;
  2208. bool COL_IS_NULL = 1;
  2209. gdImagePtr im;
  2210. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!", &IM, gd_image_ce, &COL, &COL_IS_NULL) == FAILURE) {
  2211. RETURN_THROWS();
  2212. }
  2213. im = php_gd_libgdimageptr_from_zval_p(IM);
  2214. if (!COL_IS_NULL) {
  2215. gdImageColorTransparent(im, COL);
  2216. }
  2217. RETURN_LONG(gdImageGetTransparent(im));
  2218. }
  2219. /* }}} */
  2220. /* {{{ Enable or disable interlace */
  2221. PHP_FUNCTION(imageinterlace)
  2222. {
  2223. zval *IM;
  2224. bool INT = 0;
  2225. bool INT_IS_NULL = 1;
  2226. gdImagePtr im;
  2227. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b!", &IM, gd_image_ce, &INT, &INT_IS_NULL) == FAILURE) {
  2228. RETURN_THROWS();
  2229. }
  2230. im = php_gd_libgdimageptr_from_zval_p(IM);
  2231. if (!INT_IS_NULL) {
  2232. gdImageInterlace(im, INT);
  2233. }
  2234. RETURN_BOOL(gdImageGetInterlaced(im));
  2235. }
  2236. /* }}} */
  2237. /* {{{ php_imagepolygon
  2238. arg = -1 open polygon
  2239. arg = 0 normal polygon
  2240. arg = 1 filled polygon */
  2241. /* im, points, num_points, col */
  2242. static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
  2243. {
  2244. zval *IM, *POINTS;
  2245. zend_long NPOINTS, COL;
  2246. bool COL_IS_NULL = 1;
  2247. zval *var = NULL;
  2248. gdImagePtr im;
  2249. gdPointPtr points;
  2250. int npoints, col, nelem, i;
  2251. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l!", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL, &COL_IS_NULL) == FAILURE) {
  2252. RETURN_THROWS();
  2253. }
  2254. if (COL_IS_NULL) {
  2255. COL = NPOINTS;
  2256. NPOINTS = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
  2257. if (NPOINTS % 2 != 0) {
  2258. zend_argument_value_error(2, "must have an even number of elements");
  2259. RETURN_THROWS();
  2260. }
  2261. NPOINTS /= 2;
  2262. } else {
  2263. php_error_docref(NULL, E_DEPRECATED, "Using the $num_points parameter is deprecated");
  2264. }
  2265. im = php_gd_libgdimageptr_from_zval_p(IM);
  2266. npoints = NPOINTS;
  2267. col = COL;
  2268. nelem = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
  2269. if (npoints < 3) {
  2270. zend_argument_value_error(3, "must be greater than or equal to 3");
  2271. RETURN_THROWS();
  2272. }
  2273. if (nelem < npoints * 2) {
  2274. zend_value_error("Trying to use %d points in array with only %d points", npoints, nelem/2);
  2275. RETURN_THROWS();
  2276. }
  2277. points = (gdPointPtr) safe_emalloc(npoints, sizeof(gdPoint), 0);
  2278. for (i = 0; i < npoints; i++) {
  2279. if ((var = zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2))) != NULL) {
  2280. points[i].x = zval_get_long(var);
  2281. }
  2282. if ((var = zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2) + 1)) != NULL) {
  2283. points[i].y = zval_get_long(var);
  2284. }
  2285. }
  2286. if (im->AA) {
  2287. gdImageSetAntiAliased(im, col);
  2288. col = gdAntiAliased;
  2289. }
  2290. switch (filled) {
  2291. case -1:
  2292. gdImageOpenPolygon(im, points, npoints, col);
  2293. break;
  2294. case 0:
  2295. gdImagePolygon(im, points, npoints, col);
  2296. break;
  2297. case 1:
  2298. gdImageFilledPolygon(im, points, npoints, col);
  2299. break;
  2300. }
  2301. efree(points);
  2302. RETURN_TRUE;
  2303. }
  2304. /* }}} */
  2305. /* {{{ Draw a polygon */
  2306. PHP_FUNCTION(imagepolygon)
  2307. {
  2308. php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2309. }
  2310. /* }}} */
  2311. /* {{{ Draw a polygon */
  2312. PHP_FUNCTION(imageopenpolygon)
  2313. {
  2314. php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, -1);
  2315. }
  2316. /* }}} */
  2317. /* {{{ Draw a filled polygon */
  2318. PHP_FUNCTION(imagefilledpolygon)
  2319. {
  2320. php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2321. }
  2322. /* }}} */
  2323. /* {{{ php_find_gd_font */
  2324. static gdFontPtr php_find_gd_font(zend_object *font_obj, zend_long font_int)
  2325. {
  2326. if (font_obj) {
  2327. return php_gd_font_object_from_zend_object(font_obj)->font;
  2328. }
  2329. switch (font_int) {
  2330. case 1: return gdFontTiny;
  2331. case 2: return gdFontSmall;
  2332. case 3: return gdFontMediumBold;
  2333. case 4: return gdFontLarge;
  2334. case 5: return gdFontGiant;
  2335. }
  2336. return font_int < 1 ? gdFontTiny : gdFontGiant;
  2337. }
  2338. /* }}} */
  2339. /* {{{ php_imagefontsize
  2340. * arg = 0 ImageFontWidth
  2341. * arg = 1 ImageFontHeight
  2342. */
  2343. static void php_imagefontsize(INTERNAL_FUNCTION_PARAMETERS, int arg)
  2344. {
  2345. zend_object *font_obj;
  2346. zend_long font_int;
  2347. gdFontPtr font;
  2348. ZEND_PARSE_PARAMETERS_START(1, 1)
  2349. Z_PARAM_OBJ_OF_CLASS_OR_LONG(font_obj, gd_font_ce, font_int)
  2350. ZEND_PARSE_PARAMETERS_END();
  2351. font = php_find_gd_font(font_obj, font_int);
  2352. RETURN_LONG(arg ? font->h : font->w);
  2353. }
  2354. /* }}} */
  2355. /* {{{ Get font width */
  2356. PHP_FUNCTION(imagefontwidth)
  2357. {
  2358. php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2359. }
  2360. /* }}} */
  2361. /* {{{ Get font height */
  2362. PHP_FUNCTION(imagefontheight)
  2363. {
  2364. php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2365. }
  2366. /* }}} */
  2367. /* {{{ php_gdimagecharup
  2368. * workaround for a bug in gd 1.2 */
  2369. static void php_gdimagecharup(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color)
  2370. {
  2371. int cx, cy, px, py, fline;
  2372. cx = 0;
  2373. cy = 0;
  2374. if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
  2375. return;
  2376. }
  2377. fline = (c - f->offset) * f->h * f->w;
  2378. for (py = y; (py > (y - f->w)); py--) {
  2379. for (px = x; (px < (x + f->h)); px++) {
  2380. if (f->data[fline + cy * f->w + cx]) {
  2381. gdImageSetPixel(im, px, py, color);
  2382. }
  2383. cy++;
  2384. }
  2385. cy = 0;
  2386. cx++;
  2387. }
  2388. }
  2389. /* }}} */
  2390. /* {{{ php_imagechar
  2391. * arg = 0 ImageChar
  2392. * arg = 1 ImageCharUp
  2393. * arg = 2 ImageString
  2394. * arg = 3 ImageStringUp
  2395. */
  2396. static void php_imagechar(INTERNAL_FUNCTION_PARAMETERS, int mode)
  2397. {
  2398. zval *IM;
  2399. zend_long X, Y, COL;
  2400. char *C;
  2401. size_t C_len;
  2402. gdImagePtr im;
  2403. int ch = 0, col, x, y, i, l = 0;
  2404. unsigned char *str = NULL;
  2405. zend_object *font_obj;
  2406. zend_long font_int;
  2407. gdFontPtr font;
  2408. ZEND_PARSE_PARAMETERS_START(6, 6)
  2409. Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
  2410. Z_PARAM_OBJ_OF_CLASS_OR_LONG(font_obj, gd_font_ce, font_int)
  2411. Z_PARAM_LONG(X)
  2412. Z_PARAM_LONG(Y)
  2413. Z_PARAM_STRING(C, C_len)
  2414. Z_PARAM_LONG(COL)
  2415. ZEND_PARSE_PARAMETERS_END();
  2416. im = php_gd_libgdimageptr_from_zval_p(IM);
  2417. col = COL;
  2418. if (mode < 2) {
  2419. ch = (int)((unsigned char)*C);
  2420. } else {
  2421. str = (unsigned char *) estrndup(C, C_len);
  2422. l = strlen((char *)str);
  2423. }
  2424. y = Y;
  2425. x = X;
  2426. font = php_find_gd_font(font_obj, font_int);
  2427. switch (mode) {
  2428. case 0:
  2429. gdImageChar(im, font, x, y, ch, col);
  2430. break;
  2431. case 1:
  2432. php_gdimagecharup(im, font, x, y, ch, col);
  2433. break;
  2434. case 2:
  2435. for (i = 0; (i < l); i++) {
  2436. gdImageChar(im, font, x, y, (int) ((unsigned char) str[i]), col);
  2437. x += font->w;
  2438. }
  2439. break;
  2440. case 3: {
  2441. for (i = 0; (i < l); i++) {
  2442. /* php_gdimagecharup(im, font, x, y, (int) str[i], col); */
  2443. gdImageCharUp(im, font, x, y, (int) str[i], col);
  2444. y -= font->w;
  2445. }
  2446. break;
  2447. }
  2448. }
  2449. if (str) {
  2450. efree(str);
  2451. }
  2452. RETURN_TRUE;
  2453. }
  2454. /* }}} */
  2455. /* {{{ Draw a character */
  2456. PHP_FUNCTION(imagechar)
  2457. {
  2458. php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2459. }
  2460. /* }}} */
  2461. /* {{{ Draw a character rotated 90 degrees counter-clockwise */
  2462. PHP_FUNCTION(imagecharup)
  2463. {
  2464. php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2465. }
  2466. /* }}} */
  2467. /* {{{ Draw a string horizontally */
  2468. PHP_FUNCTION(imagestring)
  2469. {
  2470. php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
  2471. }
  2472. /* }}} */
  2473. /* {{{ Draw a string vertically - rotated 90 degrees counter-clockwise */
  2474. PHP_FUNCTION(imagestringup)
  2475. {
  2476. php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
  2477. }
  2478. /* }}} */
  2479. /* {{{ Copy part of an image */
  2480. PHP_FUNCTION(imagecopy)
  2481. {
  2482. zval *SIM, *DIM;
  2483. zend_long SX, SY, SW, SH, DX, DY;
  2484. gdImagePtr im_dst, im_src;
  2485. int srcH, srcW, srcY, srcX, dstY, dstX;
  2486. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &SW, &SH) == FAILURE) {
  2487. RETURN_THROWS();
  2488. }
  2489. im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
  2490. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  2491. srcX = SX;
  2492. srcY = SY;
  2493. srcH = SH;
  2494. srcW = SW;
  2495. dstX = DX;
  2496. dstY = DY;
  2497. gdImageCopy(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH);
  2498. RETURN_TRUE;
  2499. }
  2500. /* }}} */
  2501. /* {{{ Merge one part of an image with another */
  2502. PHP_FUNCTION(imagecopymerge)
  2503. {
  2504. zval *SIM, *DIM;
  2505. zend_long SX, SY, SW, SH, DX, DY, PCT;
  2506. gdImagePtr im_dst, im_src;
  2507. int srcH, srcW, srcY, srcX, dstY, dstX, pct;
  2508. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOlllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
  2509. RETURN_THROWS();
  2510. }
  2511. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  2512. im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
  2513. srcX = SX;
  2514. srcY = SY;
  2515. srcH = SH;
  2516. srcW = SW;
  2517. dstX = DX;
  2518. dstY = DY;
  2519. pct = PCT;
  2520. gdImageCopyMerge(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
  2521. RETURN_TRUE;
  2522. }
  2523. /* }}} */
  2524. /* {{{ Merge one part of an image with another */
  2525. PHP_FUNCTION(imagecopymergegray)
  2526. {
  2527. zval *SIM, *DIM;
  2528. zend_long SX, SY, SW, SH, DX, DY, PCT;
  2529. gdImagePtr im_dst, im_src;
  2530. int srcH, srcW, srcY, srcX, dstY, dstX, pct;
  2531. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOlllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
  2532. RETURN_THROWS();
  2533. }
  2534. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  2535. im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
  2536. srcX = SX;
  2537. srcY = SY;
  2538. srcH = SH;
  2539. srcW = SW;
  2540. dstX = DX;
  2541. dstY = DY;
  2542. pct = PCT;
  2543. gdImageCopyMergeGray(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
  2544. RETURN_TRUE;
  2545. }
  2546. /* }}} */
  2547. /* {{{ Copy and resize part of an image */
  2548. PHP_FUNCTION(imagecopyresized)
  2549. {
  2550. zval *SIM, *DIM;
  2551. zend_long SX, SY, SW, SH, DX, DY, DW, DH;
  2552. gdImagePtr im_dst, im_src;
  2553. int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
  2554. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOllllllll", &DIM, gd_image_ce, &SIM, gd_image_ce, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) {
  2555. RETURN_THROWS();
  2556. }
  2557. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  2558. im_dst = php_gd_libgdimageptr_from_zval_p(DIM);
  2559. srcX = SX;
  2560. srcY = SY;
  2561. srcH = SH;
  2562. srcW = SW;
  2563. dstX = DX;
  2564. dstY = DY;
  2565. dstH = DH;
  2566. dstW = DW;
  2567. if (dstW <= 0) {
  2568. zend_argument_value_error(7, "must be greater than 0");
  2569. RETURN_THROWS();
  2570. }
  2571. if (dstH <= 0) {
  2572. zend_argument_value_error(8, "must be greater than 0");
  2573. RETURN_THROWS();
  2574. }
  2575. if (srcW <= 0) {
  2576. zend_argument_value_error(9, "must be greater than 0");
  2577. RETURN_THROWS();
  2578. }
  2579. if (srcH <= 0) {
  2580. zend_argument_value_error(10, "must be greater than 0");
  2581. RETURN_THROWS();
  2582. }
  2583. gdImageCopyResized(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
  2584. RETURN_TRUE;
  2585. }
  2586. /* }}} */
  2587. /* {{{ Get image width */
  2588. PHP_FUNCTION(imagesx)
  2589. {
  2590. zval *IM;
  2591. gdImagePtr im;
  2592. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
  2593. RETURN_THROWS();
  2594. }
  2595. im = php_gd_libgdimageptr_from_zval_p(IM);
  2596. RETURN_LONG(gdImageSX(im));
  2597. }
  2598. /* }}} */
  2599. /* {{{ Get image height */
  2600. PHP_FUNCTION(imagesy)
  2601. {
  2602. zval *IM;
  2603. gdImagePtr im;
  2604. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
  2605. RETURN_THROWS();
  2606. }
  2607. im = php_gd_libgdimageptr_from_zval_p(IM);
  2608. RETURN_LONG(gdImageSY(im));
  2609. }
  2610. /* }}} */
  2611. /* {{{ Set the clipping rectangle. */
  2612. PHP_FUNCTION(imagesetclip)
  2613. {
  2614. zval *im_zval;
  2615. gdImagePtr im;
  2616. zend_long x1, y1, x2, y2;
  2617. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll", &im_zval, gd_image_ce, &x1, &y1, &x2, &y2) == FAILURE) {
  2618. RETURN_THROWS();
  2619. }
  2620. im = php_gd_libgdimageptr_from_zval_p(im_zval);
  2621. gdImageSetClip(im, x1, y1, x2, y2);
  2622. RETURN_TRUE;
  2623. }
  2624. /* }}} */
  2625. /* {{{ Get the clipping rectangle. */
  2626. PHP_FUNCTION(imagegetclip)
  2627. {
  2628. zval *im_zval;
  2629. gdImagePtr im;
  2630. int x1, y1, x2, y2;
  2631. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &im_zval, gd_image_ce) == FAILURE) {
  2632. RETURN_THROWS();
  2633. }
  2634. im = php_gd_libgdimageptr_from_zval_p(im_zval);
  2635. gdImageGetClip(im, &x1, &y1, &x2, &y2);
  2636. array_init(return_value);
  2637. add_next_index_long(return_value, x1);
  2638. add_next_index_long(return_value, y1);
  2639. add_next_index_long(return_value, x2);
  2640. add_next_index_long(return_value, y2);
  2641. }
  2642. /* }}} */
  2643. #define TTFTEXT_DRAW 0
  2644. #define TTFTEXT_BBOX 1
  2645. #ifdef HAVE_GD_FREETYPE
  2646. /* {{{ Give the bounding box of a text using fonts via freetype2 */
  2647. PHP_FUNCTION(imageftbbox)
  2648. {
  2649. php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_BBOX);
  2650. }
  2651. /* }}} */
  2652. /* {{{ Write text to the image using fonts via freetype2 */
  2653. PHP_FUNCTION(imagefttext)
  2654. {
  2655. php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_DRAW);
  2656. }
  2657. /* }}} */
  2658. /* {{{ php_imagettftext_common */
  2659. static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int mode)
  2660. {
  2661. zval *IM, *EXT = NULL;
  2662. gdImagePtr im=NULL;
  2663. zend_long col = -1, x = 0, y = 0;
  2664. size_t str_len, fontname_len;
  2665. int i, brect[8];
  2666. double ptsize, angle;
  2667. char *str = NULL, *fontname = NULL;
  2668. char *error = NULL;
  2669. gdFTStringExtra strex = {0};
  2670. if (mode == TTFTEXT_BBOX) {
  2671. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ddss|a", &ptsize, &angle, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
  2672. RETURN_THROWS();
  2673. }
  2674. } else {
  2675. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oddlllss|a", &IM, gd_image_ce, &ptsize, &angle, &x, &y, &col, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
  2676. RETURN_THROWS();
  2677. }
  2678. im = php_gd_libgdimageptr_from_zval_p(IM);
  2679. }
  2680. /* convert angle to radians */
  2681. angle = angle * (M_PI/180);
  2682. if (EXT) { /* parse extended info */
  2683. zval *item;
  2684. zend_string *key;
  2685. /* walk the assoc array */
  2686. ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(EXT), key, item) {
  2687. if (key == NULL) {
  2688. continue;
  2689. }
  2690. if (zend_string_equals_literal(key, "linespacing")) {
  2691. strex.flags |= gdFTEX_LINESPACE;
  2692. strex.linespacing = zval_get_double(item);
  2693. }
  2694. } ZEND_HASH_FOREACH_END();
  2695. }
  2696. #ifdef VIRTUAL_DIR
  2697. {
  2698. char tmp_font_path[MAXPATHLEN];
  2699. if (!VCWD_REALPATH(fontname, tmp_font_path)) {
  2700. fontname = NULL;
  2701. }
  2702. }
  2703. #endif /* VIRTUAL_DIR */
  2704. PHP_GD_CHECK_OPEN_BASEDIR(fontname, "Invalid font filename");
  2705. if (EXT) {
  2706. error = gdImageStringFTEx(im, brect, col, fontname, ptsize, angle, x, y, str, &strex);
  2707. } else {
  2708. error = gdImageStringFT(im, brect, col, fontname, ptsize, angle, x, y, str);
  2709. }
  2710. if (error) {
  2711. php_error_docref(NULL, E_WARNING, "%s", error);
  2712. RETURN_FALSE;
  2713. }
  2714. array_init(return_value);
  2715. /* return array with the text's bounding box */
  2716. for (i = 0; i < 8; i++) {
  2717. add_next_index_long(return_value, brect[i]);
  2718. }
  2719. }
  2720. /* }}} */
  2721. #endif /* HAVE_GD_FREETYPE */
  2722. /* Section Filters */
  2723. #define PHP_GD_SINGLE_RES \
  2724. zval *SIM; \
  2725. gdImagePtr im_src; \
  2726. if (zend_parse_parameters(1, "O", &SIM, gd_image_ce) == FAILURE) { \
  2727. RETURN_THROWS(); \
  2728. } \
  2729. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  2730. static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS)
  2731. {
  2732. PHP_GD_SINGLE_RES
  2733. if (gdImageNegate(im_src) == 1) {
  2734. RETURN_TRUE;
  2735. }
  2736. RETURN_FALSE;
  2737. }
  2738. static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS)
  2739. {
  2740. PHP_GD_SINGLE_RES
  2741. if (gdImageGrayScale(im_src) == 1) {
  2742. RETURN_TRUE;
  2743. }
  2744. RETURN_FALSE;
  2745. }
  2746. static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS)
  2747. {
  2748. zval *SIM;
  2749. gdImagePtr im_src;
  2750. zend_long brightness, tmp;
  2751. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll", &SIM, gd_image_ce, &tmp, &brightness) == FAILURE) {
  2752. RETURN_THROWS();
  2753. }
  2754. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  2755. if (gdImageBrightness(im_src, (int)brightness) == 1) {
  2756. RETURN_TRUE;
  2757. }
  2758. RETURN_FALSE;
  2759. }
  2760. static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS)
  2761. {
  2762. zval *SIM;
  2763. gdImagePtr im_src;
  2764. zend_long contrast, tmp;
  2765. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll", &SIM, gd_image_ce, &tmp, &contrast) == FAILURE) {
  2766. RETURN_THROWS();
  2767. }
  2768. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  2769. if (gdImageContrast(im_src, (int)contrast) == 1) {
  2770. RETURN_TRUE;
  2771. }
  2772. RETURN_FALSE;
  2773. }
  2774. static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS)
  2775. {
  2776. zval *SIM;
  2777. gdImagePtr im_src;
  2778. zend_long r,g,b,tmp;
  2779. zend_long a = 0;
  2780. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ollll|l", &SIM, gd_image_ce, &tmp, &r, &g, &b, &a) == FAILURE) {
  2781. RETURN_THROWS();
  2782. }
  2783. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  2784. if (gdImageColor(im_src, (int) r, (int) g, (int) b, (int) a) == 1) {
  2785. RETURN_TRUE;
  2786. }
  2787. RETURN_FALSE;
  2788. }
  2789. static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS)
  2790. {
  2791. PHP_GD_SINGLE_RES
  2792. if (gdImageEdgeDetectQuick(im_src) == 1) {
  2793. RETURN_TRUE;
  2794. }
  2795. RETURN_FALSE;
  2796. }
  2797. static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS)
  2798. {
  2799. PHP_GD_SINGLE_RES
  2800. if (gdImageEmboss(im_src) == 1) {
  2801. RETURN_TRUE;
  2802. }
  2803. RETURN_FALSE;
  2804. }
  2805. static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS)
  2806. {
  2807. PHP_GD_SINGLE_RES
  2808. if (gdImageGaussianBlur(im_src) == 1) {
  2809. RETURN_TRUE;
  2810. }
  2811. RETURN_FALSE;
  2812. }
  2813. static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS)
  2814. {
  2815. PHP_GD_SINGLE_RES
  2816. if (gdImageSelectiveBlur(im_src) == 1) {
  2817. RETURN_TRUE;
  2818. }
  2819. RETURN_FALSE;
  2820. }
  2821. static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS)
  2822. {
  2823. PHP_GD_SINGLE_RES
  2824. if (gdImageMeanRemoval(im_src) == 1) {
  2825. RETURN_TRUE;
  2826. }
  2827. RETURN_FALSE;
  2828. }
  2829. static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS)
  2830. {
  2831. zval *SIM;
  2832. zend_long tmp;
  2833. gdImagePtr im_src;
  2834. double weight;
  2835. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Old", &SIM, gd_image_ce, &tmp, &weight) == FAILURE) {
  2836. RETURN_THROWS();
  2837. }
  2838. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  2839. if (gdImageSmooth(im_src, (float)weight)==1) {
  2840. RETURN_TRUE;
  2841. }
  2842. RETURN_FALSE;
  2843. }
  2844. static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS)
  2845. {
  2846. zval *IM;
  2847. gdImagePtr im;
  2848. zend_long tmp, blocksize;
  2849. bool mode = 0;
  2850. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oll|b", &IM, gd_image_ce, &tmp, &blocksize, &mode) == FAILURE) {
  2851. RETURN_THROWS();
  2852. }
  2853. im = php_gd_libgdimageptr_from_zval_p(IM);
  2854. if (gdImagePixelate(im, (int) blocksize, (const unsigned int) mode)) {
  2855. RETURN_TRUE;
  2856. }
  2857. RETURN_FALSE;
  2858. }
  2859. static void php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS)
  2860. {
  2861. zval *IM;
  2862. zval *hash_colors = NULL;
  2863. gdImagePtr im;
  2864. zend_long tmp;
  2865. zend_long scatter_sub, scatter_plus;
  2866. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olll|a", &IM, gd_image_ce, &tmp, &scatter_sub, &scatter_plus, &hash_colors) == FAILURE) {
  2867. RETURN_THROWS();
  2868. }
  2869. im = php_gd_libgdimageptr_from_zval_p(IM);
  2870. if (hash_colors) {
  2871. uint32_t i = 0;
  2872. uint32_t num_colors = zend_hash_num_elements(Z_ARRVAL_P(hash_colors));
  2873. zval *color;
  2874. int *colors;
  2875. if (num_colors == 0) {
  2876. RETURN_BOOL(gdImageScatter(im, (int)scatter_sub, (int)scatter_plus));
  2877. }
  2878. colors = emalloc(num_colors * sizeof(int));
  2879. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hash_colors), color) {
  2880. *(colors + i++) = (int) zval_get_long(color);
  2881. } ZEND_HASH_FOREACH_END();
  2882. RETVAL_BOOL(gdImageScatterColor(im, (int)scatter_sub, (int)scatter_plus, colors, num_colors));
  2883. efree(colors);
  2884. } else {
  2885. RETURN_BOOL(gdImageScatter(im, (int) scatter_sub, (int) scatter_plus));
  2886. }
  2887. }
  2888. /* {{{ Applies Filter an image using a custom angle */
  2889. PHP_FUNCTION(imagefilter)
  2890. {
  2891. zval *tmp;
  2892. typedef void (*image_filter)(INTERNAL_FUNCTION_PARAMETERS);
  2893. zend_long filtertype;
  2894. image_filter filters[] =
  2895. {
  2896. php_image_filter_negate ,
  2897. php_image_filter_grayscale,
  2898. php_image_filter_brightness,
  2899. php_image_filter_contrast,
  2900. php_image_filter_colorize,
  2901. php_image_filter_edgedetect,
  2902. php_image_filter_emboss,
  2903. php_image_filter_gaussian_blur,
  2904. php_image_filter_selective_blur,
  2905. php_image_filter_mean_removal,
  2906. php_image_filter_smooth,
  2907. php_image_filter_pixelate,
  2908. php_image_filter_scatter
  2909. };
  2910. if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > IMAGE_FILTER_MAX_ARGS) {
  2911. WRONG_PARAM_COUNT;
  2912. } else if (zend_parse_parameters(2, "Ol", &tmp, gd_image_ce, &filtertype) == FAILURE) {
  2913. RETURN_THROWS();
  2914. }
  2915. if (filtertype >= 0 && filtertype <= IMAGE_FILTER_MAX) {
  2916. filters[filtertype](INTERNAL_FUNCTION_PARAM_PASSTHRU);
  2917. }
  2918. }
  2919. /* }}} */
  2920. /* {{{ Apply a 3x3 convolution matrix, using coefficient div and offset */
  2921. PHP_FUNCTION(imageconvolution)
  2922. {
  2923. zval *SIM, *hash_matrix;
  2924. zval *var = NULL, *var2 = NULL;
  2925. gdImagePtr im_src = NULL;
  2926. double div, offset;
  2927. int nelem, i, j, res;
  2928. float matrix[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};
  2929. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oadd", &SIM, gd_image_ce, &hash_matrix, &div, &offset) == FAILURE) {
  2930. RETURN_THROWS();
  2931. }
  2932. im_src = php_gd_libgdimageptr_from_zval_p(SIM);
  2933. nelem = zend_hash_num_elements(Z_ARRVAL_P(hash_matrix));
  2934. if (nelem != 3) {
  2935. zend_argument_value_error(2, "must be a 3x3 array");
  2936. RETURN_THROWS();
  2937. }
  2938. for (i=0; i<3; i++) {
  2939. if ((var = zend_hash_index_find(Z_ARRVAL_P(hash_matrix), (i))) != NULL && Z_TYPE_P(var) == IS_ARRAY) {
  2940. if (zend_hash_num_elements(Z_ARRVAL_P(var)) != 3 ) {
  2941. zend_argument_value_error(2, "must be a 3x3 array, matrix[%d] only has %d elements", i, zend_hash_num_elements(Z_ARRVAL_P(var)));
  2942. RETURN_THROWS();
  2943. }
  2944. for (j=0; j<3; j++) {
  2945. if ((var2 = zend_hash_index_find(Z_ARRVAL_P(var), j)) != NULL) {
  2946. matrix[i][j] = (float) zval_get_double(var2);
  2947. } else {
  2948. zend_argument_value_error(2, "must be a 3x3 array, matrix[%d][%d] cannot be found (missing integer key)", i, j);
  2949. RETURN_THROWS();
  2950. }
  2951. }
  2952. }
  2953. }
  2954. res = gdImageConvolution(im_src, matrix, (float)div, (float)offset);
  2955. if (res) {
  2956. RETURN_TRUE;
  2957. } else {
  2958. RETURN_FALSE;
  2959. }
  2960. }
  2961. /* }}} */
  2962. /* End section: Filters */
  2963. /* {{{ Flip an image (in place) horizontally, vertically or both directions. */
  2964. PHP_FUNCTION(imageflip)
  2965. {
  2966. zval *IM;
  2967. zend_long mode;
  2968. gdImagePtr im;
  2969. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &IM, gd_image_ce, &mode) == FAILURE) {
  2970. RETURN_THROWS();
  2971. }
  2972. im = php_gd_libgdimageptr_from_zval_p(IM);
  2973. switch (mode) {
  2974. case PHP_GD_FLIP_VERTICAL:
  2975. gdImageFlipVertical(im);
  2976. break;
  2977. case PHP_GD_FLIP_HORIZONTAL:
  2978. gdImageFlipHorizontal(im);
  2979. break;
  2980. case PHP_GD_FLIP_BOTH:
  2981. gdImageFlipBoth(im);
  2982. break;
  2983. default:
  2984. zend_argument_value_error(2, "must be one of IMG_FLIP_VERTICAL, IMG_FLIP_HORIZONTAL, or IMG_FLIP_BOTH");
  2985. RETURN_THROWS();
  2986. }
  2987. RETURN_TRUE;
  2988. }
  2989. /* }}} */
  2990. /* {{{ Should antialiased functions used or not*/
  2991. PHP_FUNCTION(imageantialias)
  2992. {
  2993. zval *IM;
  2994. bool alias;
  2995. gdImagePtr im;
  2996. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &IM, gd_image_ce, &alias) == FAILURE) {
  2997. RETURN_THROWS();
  2998. }
  2999. im = php_gd_libgdimageptr_from_zval_p(IM);
  3000. if (im->trueColor) {
  3001. im->AA = alias;
  3002. }
  3003. RETURN_TRUE;
  3004. }
  3005. /* }}} */
  3006. /* {{{ Crop an image using the given coordinates and size, x, y, width and height. */
  3007. PHP_FUNCTION(imagecrop)
  3008. {
  3009. zval *IM;
  3010. gdImagePtr im;
  3011. gdImagePtr im_crop;
  3012. gdRect rect;
  3013. zval *z_rect;
  3014. zval *tmp;
  3015. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa", &IM, gd_image_ce, &z_rect) == FAILURE) {
  3016. RETURN_THROWS();
  3017. }
  3018. im = php_gd_libgdimageptr_from_zval_p(IM);
  3019. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") -1)) != NULL) {
  3020. rect.x = zval_get_long(tmp);
  3021. } else {
  3022. zend_argument_value_error(2, "must have an \"x\" key");
  3023. RETURN_THROWS();
  3024. }
  3025. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) {
  3026. rect.y = zval_get_long(tmp);
  3027. } else {
  3028. zend_argument_value_error(2, "must have a \"y\" key");
  3029. RETURN_THROWS();
  3030. }
  3031. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) {
  3032. rect.width = zval_get_long(tmp);
  3033. } else {
  3034. zend_argument_value_error(2, "must have a \"width\" key");
  3035. RETURN_THROWS();
  3036. }
  3037. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) {
  3038. rect.height = zval_get_long(tmp);
  3039. } else {
  3040. zend_argument_value_error(2, "must have a \"height\" key");
  3041. RETURN_THROWS();
  3042. }
  3043. im_crop = gdImageCrop(im, &rect);
  3044. if (im_crop == NULL) {
  3045. RETURN_FALSE;
  3046. }
  3047. php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_crop);
  3048. }
  3049. /* }}} */
  3050. /* {{{ Crop an image automatically using one of the available modes. */
  3051. PHP_FUNCTION(imagecropauto)
  3052. {
  3053. zval *IM;
  3054. zend_long mode = GD_CROP_DEFAULT;
  3055. zend_long color = -1;
  3056. double threshold = 0.5f;
  3057. gdImagePtr im;
  3058. gdImagePtr im_crop;
  3059. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|ldl", &IM, gd_image_ce, &mode, &threshold, &color) == FAILURE) {
  3060. RETURN_THROWS();
  3061. }
  3062. im = php_gd_libgdimageptr_from_zval_p(IM);
  3063. switch (mode) {
  3064. case GD_CROP_DEFAULT:
  3065. case GD_CROP_TRANSPARENT:
  3066. case GD_CROP_BLACK:
  3067. case GD_CROP_WHITE:
  3068. case GD_CROP_SIDES:
  3069. im_crop = gdImageCropAuto(im, mode);
  3070. break;
  3071. case GD_CROP_THRESHOLD:
  3072. if (color < 0 || (!gdImageTrueColor(im) && color >= gdImageColorsTotal(im))) {
  3073. zend_argument_value_error(4, "must be greater than or equal to 0 when using the threshold mode");
  3074. RETURN_THROWS();
  3075. }
  3076. im_crop = gdImageCropThreshold(im, color, (float) threshold);
  3077. break;
  3078. default:
  3079. zend_argument_value_error(2, "must be a valid mode");
  3080. RETURN_THROWS();
  3081. }
  3082. if (im_crop == NULL) {
  3083. RETURN_FALSE;
  3084. }
  3085. php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_crop);
  3086. }
  3087. /* }}} */
  3088. /* {{{ Scale an image using the given new width and height. */
  3089. PHP_FUNCTION(imagescale)
  3090. {
  3091. zval *IM;
  3092. gdImagePtr im;
  3093. gdImagePtr im_scaled = NULL;
  3094. int new_width, new_height;
  3095. zend_long tmp_w, tmp_h=-1, tmp_m = GD_BILINEAR_FIXED;
  3096. gdInterpolationMethod method, old_method;
  3097. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol|ll", &IM, gd_image_ce, &tmp_w, &tmp_h, &tmp_m) == FAILURE) {
  3098. RETURN_THROWS();
  3099. }
  3100. method = tmp_m;
  3101. im = php_gd_libgdimageptr_from_zval_p(IM);
  3102. if (tmp_h < 0 || tmp_w < 0) {
  3103. /* preserve ratio */
  3104. long src_x, src_y;
  3105. src_x = gdImageSX(im);
  3106. src_y = gdImageSY(im);
  3107. if (src_x && tmp_h < 0) {
  3108. tmp_h = tmp_w * src_y / src_x;
  3109. }
  3110. if (src_y && tmp_w < 0) {
  3111. tmp_w = tmp_h * src_x / src_y;
  3112. }
  3113. }
  3114. if (tmp_h <= 0 || tmp_h > INT_MAX || tmp_w <= 0 || tmp_w > INT_MAX) {
  3115. RETURN_FALSE;
  3116. }
  3117. new_width = tmp_w;
  3118. new_height = tmp_h;
  3119. /* gdImageGetInterpolationMethod() is only available as of GD 2.1.1 */
  3120. old_method = im->interpolation_id;
  3121. if (gdImageSetInterpolationMethod(im, method)) {
  3122. im_scaled = gdImageScale(im, new_width, new_height);
  3123. }
  3124. gdImageSetInterpolationMethod(im, old_method);
  3125. if (im_scaled == NULL) {
  3126. RETURN_FALSE;
  3127. }
  3128. php_gd_assign_libgdimageptr_as_extgdimage(return_value, im_scaled);
  3129. }
  3130. /* }}} */
  3131. /* {{{ Return an image containing the affine tramsformed src image, using an optional clipping area */
  3132. PHP_FUNCTION(imageaffine)
  3133. {
  3134. zval *IM;
  3135. gdImagePtr src;
  3136. gdImagePtr dst;
  3137. gdRect rect;
  3138. gdRectPtr pRect = NULL;
  3139. zval *z_rect = NULL;
  3140. zval *z_affine;
  3141. zval *tmp;
  3142. double affine[6];
  3143. int i, nelems;
  3144. zval *zval_affine_elem = NULL;
  3145. if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa|a!", &IM, gd_image_ce, &z_affine, &z_rect) == FAILURE) {
  3146. RETURN_THROWS();
  3147. }
  3148. src = php_gd_libgdimageptr_from_zval_p(IM);
  3149. if ((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_affine))) != 6) {
  3150. zend_argument_value_error(2, "must have 6 elements");
  3151. RETURN_THROWS();
  3152. }
  3153. for (i = 0; i < nelems; i++) {
  3154. if ((zval_affine_elem = zend_hash_index_find(Z_ARRVAL_P(z_affine), i)) != NULL) {
  3155. switch (Z_TYPE_P(zval_affine_elem)) {
  3156. case IS_LONG:
  3157. affine[i] = Z_LVAL_P(zval_affine_elem);
  3158. break;
  3159. case IS_DOUBLE:
  3160. affine[i] = Z_DVAL_P(zval_affine_elem);
  3161. break;
  3162. case IS_STRING:
  3163. affine[i] = zval_get_double(zval_affine_elem);
  3164. break;
  3165. default:
  3166. zend_argument_type_error(3, "contains invalid type for element %i", i);
  3167. RETURN_THROWS();
  3168. }
  3169. }
  3170. }
  3171. if (z_rect != NULL) {
  3172. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") - 1)) != NULL) {
  3173. rect.x = zval_get_long(tmp);
  3174. } else {
  3175. zend_argument_value_error(3, "must have an \"x\" key");
  3176. RETURN_THROWS();
  3177. }
  3178. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) {
  3179. rect.y = zval_get_long(tmp);
  3180. } else {
  3181. zend_argument_value_error(3, "must have a \"y\" key");
  3182. RETURN_THROWS();
  3183. }
  3184. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) {
  3185. rect.width = zval_get_long(tmp);
  3186. } else {
  3187. zend_argument_value_error(3, "must have a \"width\" key");
  3188. RETURN_THROWS();
  3189. }
  3190. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) {
  3191. rect.height = zval_get_long(tmp);
  3192. } else {
  3193. zend_argument_value_error(3, "must have a \"height\" key");
  3194. RETURN_THROWS();
  3195. }
  3196. pRect = &rect;
  3197. }
  3198. if (gdTransformAffineGetImage(&dst, src, pRect, affine) != GD_TRUE) {
  3199. RETURN_FALSE;
  3200. }
  3201. if (dst == NULL) {
  3202. RETURN_FALSE;
  3203. }
  3204. php_gd_assign_libgdimageptr_as_extgdimage(return_value, dst);
  3205. }
  3206. /* }}} */
  3207. /* {{{ Return an image containing the affine tramsformed src image, using an optional clipping area */
  3208. PHP_FUNCTION(imageaffinematrixget)
  3209. {
  3210. double affine[6];
  3211. zend_long type;
  3212. zval *options = NULL;
  3213. zval *tmp;
  3214. int res = GD_FALSE, i;
  3215. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz", &type, &options) == FAILURE) {
  3216. RETURN_THROWS();
  3217. }
  3218. switch((gdAffineStandardMatrix)type) {
  3219. case GD_AFFINE_TRANSLATE:
  3220. case GD_AFFINE_SCALE: {
  3221. double x, y;
  3222. if (Z_TYPE_P(options) != IS_ARRAY) {
  3223. zend_argument_type_error(1, "must be of type array when using translate or scale");
  3224. RETURN_THROWS();
  3225. }
  3226. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "x", sizeof("x") - 1)) != NULL) {
  3227. x = zval_get_double(tmp);
  3228. } else {
  3229. zend_argument_value_error(2, "must have an \"x\" key");
  3230. RETURN_THROWS();
  3231. }
  3232. if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "y", sizeof("y") - 1)) != NULL) {
  3233. y = zval_get_double(tmp);
  3234. } else {
  3235. zend_argument_value_error(2, "must have a \"y\" key");
  3236. RETURN_THROWS();
  3237. }
  3238. if (type == GD_AFFINE_TRANSLATE) {
  3239. res = gdAffineTranslate(affine, x, y);
  3240. } else {
  3241. res = gdAffineScale(affine, x, y);
  3242. }
  3243. break;
  3244. }
  3245. case GD_AFFINE_ROTATE:
  3246. case GD_AFFINE_SHEAR_HORIZONTAL:
  3247. case GD_AFFINE_SHEAR_VERTICAL: {
  3248. double angle;
  3249. angle = zval_get_double(options);
  3250. if (type == GD_AFFINE_SHEAR_HORIZONTAL) {
  3251. res = gdAffineShearHorizontal(affine, angle);
  3252. } else if (type == GD_AFFINE_SHEAR_VERTICAL) {
  3253. res = gdAffineShearVertical(affine, angle);
  3254. } else {
  3255. res = gdAffineRotate(affine, angle);
  3256. }
  3257. break;
  3258. }
  3259. default:
  3260. zend_argument_value_error(1, "must be a valid element type");
  3261. RETURN_THROWS();
  3262. }
  3263. if (res == GD_FALSE) {
  3264. RETURN_FALSE;
  3265. } else {
  3266. array_init(return_value);
  3267. for (i = 0; i < 6; i++) {
  3268. add_index_double(return_value, i, affine[i]);
  3269. }
  3270. }
  3271. } /* }}} */
  3272. /* {{{ Concat two matrices (as in doing many ops in one go) */
  3273. PHP_FUNCTION(imageaffinematrixconcat)
  3274. {
  3275. double m1[6];
  3276. double m2[6];
  3277. double mr[6];
  3278. zval *tmp;
  3279. zval *z_m1;
  3280. zval *z_m2;
  3281. int i;
  3282. if (zend_parse_parameters(ZEND_NUM_ARGS(), "aa", &z_m1, &z_m2) == FAILURE) {
  3283. RETURN_THROWS();
  3284. }
  3285. if (zend_hash_num_elements(Z_ARRVAL_P(z_m1)) != 6) {
  3286. zend_argument_value_error(1, "must have 6 elements");
  3287. RETURN_THROWS();
  3288. }
  3289. if (zend_hash_num_elements(Z_ARRVAL_P(z_m2)) != 6) {
  3290. zend_argument_value_error(1, "must have 6 elements");
  3291. RETURN_THROWS();
  3292. }
  3293. for (i = 0; i < 6; i++) {
  3294. if ((tmp = zend_hash_index_find(Z_ARRVAL_P(z_m1), i)) != NULL) {
  3295. switch (Z_TYPE_P(tmp)) {
  3296. case IS_LONG:
  3297. m1[i] = Z_LVAL_P(tmp);
  3298. break;
  3299. case IS_DOUBLE:
  3300. m1[i] = Z_DVAL_P(tmp);
  3301. break;
  3302. case IS_STRING:
  3303. m1[i] = zval_get_double(tmp);
  3304. break;
  3305. default:
  3306. zend_argument_type_error(1, "contains invalid type for element %i", i);
  3307. RETURN_THROWS();
  3308. }
  3309. }
  3310. if ((tmp = zend_hash_index_find(Z_ARRVAL_P(z_m2), i)) != NULL) {
  3311. switch (Z_TYPE_P(tmp)) {
  3312. case IS_LONG:
  3313. m2[i] = Z_LVAL_P(tmp);
  3314. break;
  3315. case IS_DOUBLE:
  3316. m2[i] = Z_DVAL_P(tmp);
  3317. break;
  3318. case IS_STRING:
  3319. m2[i] = zval_get_double(tmp);
  3320. break;
  3321. default:
  3322. zend_argument_type_error(2, "contains invalid type for element %i", i);
  3323. RETURN_THROWS();
  3324. }
  3325. }
  3326. }
  3327. if (gdAffineConcat (mr, m1, m2) != GD_TRUE) {
  3328. RETURN_FALSE;
  3329. }
  3330. array_init(return_value);
  3331. for (i = 0; i < 6; i++) {
  3332. add_index_double(return_value, i, mr[i]);
  3333. }
  3334. } /* }}} */
  3335. /* {{{ Get the default interpolation method. */
  3336. PHP_FUNCTION(imagegetinterpolation)
  3337. {
  3338. zval *IM;
  3339. gdImagePtr im;
  3340. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &IM, gd_image_ce) == FAILURE) {
  3341. RETURN_THROWS();
  3342. }
  3343. im = php_gd_libgdimageptr_from_zval_p(IM);
  3344. #ifdef HAVE_GD_GET_INTERPOLATION
  3345. RETURN_LONG(gdImageGetInterpolationMethod(im));
  3346. #else
  3347. RETURN_LONG(im->interpolation_id);
  3348. #endif
  3349. }
  3350. /* }}} */
  3351. /* {{{ Set the default interpolation method, passing -1 or 0 sets it to the libgd default (bilinear). */
  3352. PHP_FUNCTION(imagesetinterpolation)
  3353. {
  3354. zval *IM;
  3355. gdImagePtr im;
  3356. zend_long method = GD_BILINEAR_FIXED;
  3357. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l", &IM, gd_image_ce, &method) == FAILURE) {
  3358. RETURN_THROWS();
  3359. }
  3360. im = php_gd_libgdimageptr_from_zval_p(IM);
  3361. if (method == -1) {
  3362. method = GD_BILINEAR_FIXED;
  3363. }
  3364. RETURN_BOOL(gdImageSetInterpolationMethod(im, (gdInterpolationMethod) method));
  3365. }
  3366. /* }}} */
  3367. /* {{{ Get or set the resolution of the image in DPI. */
  3368. PHP_FUNCTION(imageresolution)
  3369. {
  3370. zval *IM;
  3371. gdImagePtr im;
  3372. zend_long res_x, res_y;
  3373. bool res_x_is_null = 1, res_y_is_null = 1;
  3374. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!l!", &IM, gd_image_ce, &res_x, &res_x_is_null, &res_y, &res_y_is_null) == FAILURE) {
  3375. RETURN_THROWS();
  3376. }
  3377. im = php_gd_libgdimageptr_from_zval_p(IM);
  3378. if (!res_x_is_null && !res_y_is_null) {
  3379. gdImageSetResolution(im, res_x, res_y);
  3380. RETURN_TRUE;
  3381. } else if (!res_x_is_null && res_y_is_null) {
  3382. gdImageSetResolution(im, res_x, res_x);
  3383. RETURN_TRUE;
  3384. } else if (res_x_is_null && !res_y_is_null) {
  3385. gdImageSetResolution(im, res_y, res_y);
  3386. RETURN_TRUE;
  3387. }
  3388. array_init(return_value);
  3389. add_next_index_long(return_value, gdImageResolutionX(im));
  3390. add_next_index_long(return_value, gdImageResolutionY(im));
  3391. }
  3392. /* }}} */
  3393. /*********************************************************
  3394. *
  3395. * Stream Handling
  3396. * Formerly contained within ext/gd/gd_ctx.c and included
  3397. * at the the top of this file
  3398. *
  3399. ********************************************************/
  3400. #define CTX_PUTC(c,ctx) ctx->putC(ctx, c)
  3401. static void _php_image_output_putc(struct gdIOCtx *ctx, int c) /* {{{ */
  3402. {
  3403. /* without the following downcast, the write will fail
  3404. * (i.e., will write a zero byte) for all
  3405. * big endian architectures:
  3406. */
  3407. unsigned char ch = (unsigned char) c;
  3408. php_write(&ch, 1);
  3409. } /* }}} */
  3410. static int _php_image_output_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
  3411. {
  3412. return php_write((void *)buf, l);
  3413. } /* }}} */
  3414. static void _php_image_output_ctxfree(struct gdIOCtx *ctx) /* {{{ */
  3415. {
  3416. if(ctx) {
  3417. efree(ctx);
  3418. }
  3419. } /* }}} */
  3420. static void _php_image_stream_putc(struct gdIOCtx *ctx, int c) /* {{{ */ {
  3421. char ch = (char) c;
  3422. php_stream * stream = (php_stream *)ctx->data;
  3423. php_stream_write(stream, &ch, 1);
  3424. } /* }}} */
  3425. static int _php_image_stream_putbuf(struct gdIOCtx *ctx, const void* buf, int l) /* {{{ */
  3426. {
  3427. php_stream * stream = (php_stream *)ctx->data;
  3428. return php_stream_write(stream, (void *)buf, l);
  3429. } /* }}} */
  3430. static void _php_image_stream_ctxfree(struct gdIOCtx *ctx) /* {{{ */
  3431. {
  3432. if(ctx->data) {
  3433. ctx->data = NULL;
  3434. }
  3435. if(ctx) {
  3436. efree(ctx);
  3437. }
  3438. } /* }}} */
  3439. static void _php_image_stream_ctxfreeandclose(struct gdIOCtx *ctx) /* {{{ */
  3440. {
  3441. if(ctx->data) {
  3442. php_stream_close((php_stream *) ctx->data);
  3443. ctx->data = NULL;
  3444. }
  3445. if(ctx) {
  3446. efree(ctx);
  3447. }
  3448. } /* }}} */
  3449. static gdIOCtx *create_stream_context_from_zval(zval *to_zval) {
  3450. php_stream *stream;
  3451. int close_stream = 1;
  3452. if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
  3453. php_stream_from_zval_no_verify(stream, to_zval);
  3454. if (stream == NULL) {
  3455. return NULL;
  3456. }
  3457. close_stream = 0;
  3458. } else if (Z_TYPE_P(to_zval) == IS_STRING) {
  3459. if (CHECK_ZVAL_NULL_PATH(to_zval)) {
  3460. zend_argument_type_error(2, "must not contain null bytes");
  3461. return NULL;
  3462. }
  3463. stream = php_stream_open_wrapper(Z_STRVAL_P(to_zval), "wb", REPORT_ERRORS|IGNORE_PATH, NULL);
  3464. if (stream == NULL) {
  3465. return NULL;
  3466. }
  3467. } else {
  3468. zend_argument_type_error(2, "must be a file name or a stream resource, %s given", zend_zval_type_name(to_zval));
  3469. return NULL;
  3470. }
  3471. return create_stream_context(stream, close_stream);
  3472. }
  3473. static gdIOCtx *create_stream_context(php_stream *stream, int close_stream) {
  3474. gdIOCtx *ctx = ecalloc(1, sizeof(gdIOCtx));
  3475. ctx->putC = _php_image_stream_putc;
  3476. ctx->putBuf = _php_image_stream_putbuf;
  3477. if (close_stream) {
  3478. ctx->gd_free = _php_image_stream_ctxfreeandclose;
  3479. } else {
  3480. ctx->gd_free = _php_image_stream_ctxfree;
  3481. }
  3482. ctx->data = (void *)stream;
  3483. return ctx;
  3484. }
  3485. static gdIOCtx *create_output_context() {
  3486. gdIOCtx *ctx = ecalloc(1, sizeof(gdIOCtx));
  3487. ctx->putC = _php_image_output_putc;
  3488. ctx->putBuf = _php_image_output_putbuf;
  3489. ctx->gd_free = _php_image_output_ctxfree;
  3490. return ctx;
  3491. }
  3492. static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn)
  3493. {
  3494. zval *imgind;
  3495. zend_long quality = -1, basefilter = -1, speed = -1;
  3496. gdImagePtr im;
  3497. gdIOCtx *ctx = NULL;
  3498. zval *to_zval = NULL;
  3499. if (image_type == PHP_GDIMG_TYPE_GIF) {
  3500. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!", &imgind, gd_image_ce, &to_zval) == FAILURE) {
  3501. RETURN_THROWS();
  3502. }
  3503. } else if (image_type == PHP_GDIMG_TYPE_PNG) {
  3504. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &basefilter) == FAILURE) {
  3505. RETURN_THROWS();
  3506. }
  3507. } else if (image_type == PHP_GDIMG_TYPE_AVIF) {
  3508. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &speed) == FAILURE) {
  3509. RETURN_THROWS();
  3510. }
  3511. } else {
  3512. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l", &imgind, gd_image_ce, &to_zval, &quality) == FAILURE) {
  3513. RETURN_THROWS();
  3514. }
  3515. }
  3516. im = php_gd_libgdimageptr_from_zval_p(imgind);
  3517. if (to_zval != NULL) {
  3518. ctx = create_stream_context_from_zval(to_zval);
  3519. if (!ctx) {
  3520. RETURN_FALSE;
  3521. }
  3522. } else {
  3523. ctx = create_output_context();
  3524. }
  3525. switch (image_type) {
  3526. #ifdef HAVE_GD_JPG
  3527. case PHP_GDIMG_TYPE_JPG:
  3528. gdImageJpegCtx(im, ctx, (int) quality);
  3529. break;
  3530. #endif
  3531. #ifdef HAVE_GD_WEBP
  3532. case PHP_GDIMG_TYPE_WEBP:
  3533. if (quality == -1) {
  3534. quality = 80;
  3535. }
  3536. gdImageWebpCtx(im, ctx, (int) quality);
  3537. break;
  3538. #endif
  3539. #ifdef HAVE_GD_AVIF
  3540. case PHP_GDIMG_TYPE_AVIF:
  3541. if (speed == -1) {
  3542. speed = 6;
  3543. }
  3544. gdImageAvifCtx(im, ctx, (int) quality, (int) speed);
  3545. break;
  3546. #endif
  3547. #ifdef HAVE_GD_PNG
  3548. case PHP_GDIMG_TYPE_PNG:
  3549. #ifdef HAVE_GD_BUNDLED
  3550. gdImagePngCtxEx(im, ctx, (int) quality, (int) basefilter);
  3551. #else
  3552. gdImagePngCtxEx(im, ctx, (int) quality);
  3553. #endif
  3554. break;
  3555. #endif
  3556. case PHP_GDIMG_TYPE_GIF:
  3557. gdImageGifCtx(im, ctx);
  3558. break;
  3559. EMPTY_SWITCH_DEFAULT_CASE()
  3560. }
  3561. ctx->gd_free(ctx);
  3562. RETURN_TRUE;
  3563. }
  3564. /* }}} */