gd.c 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  16. | Stig Bakken <ssb@php.net> |
  17. | Jim Winstead <jimw@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. /* gd 1.2 is copyright 1994, 1995, Quest Protein Database Center,
  22. Cold Spring Harbor Labs. */
  23. /* Note that there is no code from the gd package in this file */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "php.h"
  28. #include "php_ini.h"
  29. #include "ext/standard/head.h"
  30. #include <math.h>
  31. #include "SAPI.h"
  32. #include "php_gd.h"
  33. #include "ext/standard/info.h"
  34. #include "php_open_temporary_file.h"
  35. #if HAVE_SYS_WAIT_H
  36. # include <sys/wait.h>
  37. #endif
  38. #if 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. #ifdef HAVE_GD_XPM
  49. # include <X11/xpm.h>
  50. #endif
  51. # include "gd_compat.h"
  52. static int le_gd, le_gd_font;
  53. #if HAVE_LIBT1
  54. #include <t1lib.h>
  55. static int le_ps_font, le_ps_enc;
  56. static void php_free_ps_font(zend_rsrc_list_entry *rsrc TSRMLS_DC);
  57. static void php_free_ps_enc(zend_rsrc_list_entry *rsrc TSRMLS_DC);
  58. #endif
  59. #include <gd.h>
  60. #ifndef HAVE_GD_BUNDLED
  61. # include <gd_errors.h>
  62. #endif
  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. #ifdef ENABLE_GD_TTF
  69. # ifdef HAVE_LIBFREETYPE
  70. # include <ft2build.h>
  71. # include FT_FREETYPE_H
  72. # endif
  73. #endif
  74. #if defined(HAVE_GD_XPM) && defined(HAVE_GD_BUNDLED)
  75. # include "X11/xpm.h"
  76. #endif
  77. #ifndef M_PI
  78. #define M_PI 3.14159265358979323846
  79. #endif
  80. #ifdef ENABLE_GD_TTF
  81. static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int, int);
  82. #endif
  83. #include "gd_ctx.c"
  84. /* as it is not really public, duplicate declaration here to avoid
  85. pointless warnings */
  86. int overflow2(int a, int b);
  87. /* Section Filters Declarations */
  88. /* IMPORTANT NOTE FOR NEW FILTER
  89. * Do not forget to update:
  90. * IMAGE_FILTER_MAX: define the last filter index
  91. * IMAGE_FILTER_MAX_ARGS: define the biggest amount of arguments
  92. * image_filter array in PHP_FUNCTION(imagefilter)
  93. * */
  94. #define IMAGE_FILTER_NEGATE 0
  95. #define IMAGE_FILTER_GRAYSCALE 1
  96. #define IMAGE_FILTER_BRIGHTNESS 2
  97. #define IMAGE_FILTER_CONTRAST 3
  98. #define IMAGE_FILTER_COLORIZE 4
  99. #define IMAGE_FILTER_EDGEDETECT 5
  100. #define IMAGE_FILTER_EMBOSS 6
  101. #define IMAGE_FILTER_GAUSSIAN_BLUR 7
  102. #define IMAGE_FILTER_SELECTIVE_BLUR 8
  103. #define IMAGE_FILTER_MEAN_REMOVAL 9
  104. #define IMAGE_FILTER_SMOOTH 10
  105. #define IMAGE_FILTER_PIXELATE 11
  106. #define IMAGE_FILTER_MAX 11
  107. #define IMAGE_FILTER_MAX_ARGS 6
  108. static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS);
  109. static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS);
  110. static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS);
  111. static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS);
  112. static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS);
  113. static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS);
  114. static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS);
  115. static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS);
  116. static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS);
  117. static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS);
  118. static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS);
  119. static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS);
  120. /* End Section filters declarations */
  121. static gdImagePtr _php_image_create_from_string (zval **Data, char *tn, gdImagePtr (*ioctx_func_p)() TSRMLS_DC);
  122. static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(), gdImagePtr (*ioctx_func_p)());
  123. static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)());
  124. static int _php_image_type(char data[8]);
  125. static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type);
  126. static void _php_image_bw_convert(gdImagePtr im_org, gdIOCtx *out, int threshold);
  127. /* {{{ arginfo */
  128. ZEND_BEGIN_ARG_INFO(arginfo_gd_info, 0)
  129. ZEND_END_ARG_INFO()
  130. ZEND_BEGIN_ARG_INFO(arginfo_imageloadfont, 0)
  131. ZEND_ARG_INFO(0, filename)
  132. ZEND_END_ARG_INFO()
  133. ZEND_BEGIN_ARG_INFO(arginfo_imagesetstyle, 0)
  134. ZEND_ARG_INFO(0, im)
  135. ZEND_ARG_INFO(0, styles) /* ARRAY_INFO(0, styles, 0) */
  136. ZEND_END_ARG_INFO()
  137. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatetruecolor, 0)
  138. ZEND_ARG_INFO(0, x_size)
  139. ZEND_ARG_INFO(0, y_size)
  140. ZEND_END_ARG_INFO()
  141. ZEND_BEGIN_ARG_INFO(arginfo_imageistruecolor, 0)
  142. ZEND_ARG_INFO(0, im)
  143. ZEND_END_ARG_INFO()
  144. ZEND_BEGIN_ARG_INFO(arginfo_imagetruecolortopalette, 0)
  145. ZEND_ARG_INFO(0, im)
  146. ZEND_ARG_INFO(0, ditherFlag)
  147. ZEND_ARG_INFO(0, colorsWanted)
  148. ZEND_END_ARG_INFO()
  149. ZEND_BEGIN_ARG_INFO(arginfo_imagepalettetotruecolor, 0)
  150. ZEND_ARG_INFO(0, im)
  151. ZEND_END_ARG_INFO()
  152. ZEND_BEGIN_ARG_INFO(arginfo_imagecolormatch, 0)
  153. ZEND_ARG_INFO(0, im1)
  154. ZEND_ARG_INFO(0, im2)
  155. ZEND_END_ARG_INFO()
  156. ZEND_BEGIN_ARG_INFO(arginfo_imagesetthickness, 0)
  157. ZEND_ARG_INFO(0, im)
  158. ZEND_ARG_INFO(0, thickness)
  159. ZEND_END_ARG_INFO()
  160. ZEND_BEGIN_ARG_INFO(arginfo_imagefilledellipse, 0)
  161. ZEND_ARG_INFO(0, im)
  162. ZEND_ARG_INFO(0, cx)
  163. ZEND_ARG_INFO(0, cy)
  164. ZEND_ARG_INFO(0, w)
  165. ZEND_ARG_INFO(0, h)
  166. ZEND_ARG_INFO(0, color)
  167. ZEND_END_ARG_INFO()
  168. ZEND_BEGIN_ARG_INFO(arginfo_imagefilledarc, 0)
  169. ZEND_ARG_INFO(0, im)
  170. ZEND_ARG_INFO(0, cx)
  171. ZEND_ARG_INFO(0, cy)
  172. ZEND_ARG_INFO(0, w)
  173. ZEND_ARG_INFO(0, h)
  174. ZEND_ARG_INFO(0, s)
  175. ZEND_ARG_INFO(0, e)
  176. ZEND_ARG_INFO(0, col)
  177. ZEND_ARG_INFO(0, style)
  178. ZEND_END_ARG_INFO()
  179. ZEND_BEGIN_ARG_INFO(arginfo_imagealphablending, 0)
  180. ZEND_ARG_INFO(0, im)
  181. ZEND_ARG_INFO(0, blend)
  182. ZEND_END_ARG_INFO()
  183. ZEND_BEGIN_ARG_INFO(arginfo_imagesavealpha, 0)
  184. ZEND_ARG_INFO(0, im)
  185. ZEND_ARG_INFO(0, save)
  186. ZEND_END_ARG_INFO()
  187. ZEND_BEGIN_ARG_INFO(arginfo_imagelayereffect, 0)
  188. ZEND_ARG_INFO(0, im)
  189. ZEND_ARG_INFO(0, effect)
  190. ZEND_END_ARG_INFO()
  191. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorallocatealpha, 0)
  192. ZEND_ARG_INFO(0, im)
  193. ZEND_ARG_INFO(0, red)
  194. ZEND_ARG_INFO(0, green)
  195. ZEND_ARG_INFO(0, blue)
  196. ZEND_ARG_INFO(0, alpha)
  197. ZEND_END_ARG_INFO()
  198. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorresolvealpha, 0)
  199. ZEND_ARG_INFO(0, im)
  200. ZEND_ARG_INFO(0, red)
  201. ZEND_ARG_INFO(0, green)
  202. ZEND_ARG_INFO(0, blue)
  203. ZEND_ARG_INFO(0, alpha)
  204. ZEND_END_ARG_INFO()
  205. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorclosestalpha, 0)
  206. ZEND_ARG_INFO(0, im)
  207. ZEND_ARG_INFO(0, red)
  208. ZEND_ARG_INFO(0, green)
  209. ZEND_ARG_INFO(0, blue)
  210. ZEND_ARG_INFO(0, alpha)
  211. ZEND_END_ARG_INFO()
  212. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorexactalpha, 0)
  213. ZEND_ARG_INFO(0, im)
  214. ZEND_ARG_INFO(0, red)
  215. ZEND_ARG_INFO(0, green)
  216. ZEND_ARG_INFO(0, blue)
  217. ZEND_ARG_INFO(0, alpha)
  218. ZEND_END_ARG_INFO()
  219. ZEND_BEGIN_ARG_INFO(arginfo_imagecopyresampled, 0)
  220. ZEND_ARG_INFO(0, dst_im)
  221. ZEND_ARG_INFO(0, src_im)
  222. ZEND_ARG_INFO(0, dst_x)
  223. ZEND_ARG_INFO(0, dst_y)
  224. ZEND_ARG_INFO(0, src_x)
  225. ZEND_ARG_INFO(0, src_y)
  226. ZEND_ARG_INFO(0, dst_w)
  227. ZEND_ARG_INFO(0, dst_h)
  228. ZEND_ARG_INFO(0, src_w)
  229. ZEND_ARG_INFO(0, src_h)
  230. ZEND_END_ARG_INFO()
  231. #ifdef PHP_WIN32
  232. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagegrabwindow, 0, 0, 1)
  233. ZEND_ARG_INFO(0, handle)
  234. ZEND_ARG_INFO(0, client_area)
  235. ZEND_END_ARG_INFO()
  236. ZEND_BEGIN_ARG_INFO(arginfo_imagegrabscreen, 0)
  237. ZEND_END_ARG_INFO()
  238. #endif
  239. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagerotate, 0, 0, 3)
  240. ZEND_ARG_INFO(0, im)
  241. ZEND_ARG_INFO(0, angle)
  242. ZEND_ARG_INFO(0, bgdcolor)
  243. ZEND_ARG_INFO(0, ignoretransparent)
  244. ZEND_END_ARG_INFO()
  245. ZEND_BEGIN_ARG_INFO(arginfo_imagesettile, 0)
  246. ZEND_ARG_INFO(0, im)
  247. ZEND_ARG_INFO(0, tile)
  248. ZEND_END_ARG_INFO()
  249. ZEND_BEGIN_ARG_INFO(arginfo_imagesetbrush, 0)
  250. ZEND_ARG_INFO(0, im)
  251. ZEND_ARG_INFO(0, brush)
  252. ZEND_END_ARG_INFO()
  253. ZEND_BEGIN_ARG_INFO(arginfo_imagecreate, 0)
  254. ZEND_ARG_INFO(0, x_size)
  255. ZEND_ARG_INFO(0, y_size)
  256. ZEND_END_ARG_INFO()
  257. ZEND_BEGIN_ARG_INFO(arginfo_imagetypes, 0)
  258. ZEND_END_ARG_INFO()
  259. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromstring, 0)
  260. ZEND_ARG_INFO(0, image)
  261. ZEND_END_ARG_INFO()
  262. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromgif, 0)
  263. ZEND_ARG_INFO(0, filename)
  264. ZEND_END_ARG_INFO()
  265. #ifdef HAVE_GD_JPG
  266. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromjpeg, 0)
  267. ZEND_ARG_INFO(0, filename)
  268. ZEND_END_ARG_INFO()
  269. #endif
  270. #ifdef HAVE_GD_PNG
  271. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefrompng, 0)
  272. ZEND_ARG_INFO(0, filename)
  273. ZEND_END_ARG_INFO()
  274. #endif
  275. #ifdef HAVE_GD_WEBP
  276. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromwebp, 0)
  277. ZEND_ARG_INFO(0, filename)
  278. ZEND_END_ARG_INFO()
  279. #endif
  280. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromxbm, 0)
  281. ZEND_ARG_INFO(0, filename)
  282. ZEND_END_ARG_INFO()
  283. #if defined(HAVE_GD_XPM)
  284. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromxpm, 0)
  285. ZEND_ARG_INFO(0, filename)
  286. ZEND_END_ARG_INFO()
  287. #endif
  288. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromwbmp, 0)
  289. ZEND_ARG_INFO(0, filename)
  290. ZEND_END_ARG_INFO()
  291. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromgd, 0)
  292. ZEND_ARG_INFO(0, filename)
  293. ZEND_END_ARG_INFO()
  294. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromgd2, 0)
  295. ZEND_ARG_INFO(0, filename)
  296. ZEND_END_ARG_INFO()
  297. ZEND_BEGIN_ARG_INFO(arginfo_imagecreatefromgd2part, 0)
  298. ZEND_ARG_INFO(0, filename)
  299. ZEND_ARG_INFO(0, srcX)
  300. ZEND_ARG_INFO(0, srcY)
  301. ZEND_ARG_INFO(0, width)
  302. ZEND_ARG_INFO(0, height)
  303. ZEND_END_ARG_INFO()
  304. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagexbm, 0, 0, 2)
  305. ZEND_ARG_INFO(0, im)
  306. ZEND_ARG_INFO(0, filename)
  307. ZEND_ARG_INFO(0, foreground)
  308. ZEND_END_ARG_INFO()
  309. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagegif, 0, 0, 1)
  310. ZEND_ARG_INFO(0, im)
  311. ZEND_ARG_INFO(0, filename)
  312. ZEND_END_ARG_INFO()
  313. #ifdef HAVE_GD_PNG
  314. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagepng, 0, 0, 1)
  315. ZEND_ARG_INFO(0, im)
  316. ZEND_ARG_INFO(0, filename)
  317. ZEND_END_ARG_INFO()
  318. #endif
  319. #ifdef HAVE_GD_WEBP
  320. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagewebp, 0, 0, 1)
  321. ZEND_ARG_INFO(0, im)
  322. ZEND_ARG_INFO(0, filename)
  323. ZEND_END_ARG_INFO()
  324. #endif
  325. #ifdef HAVE_GD_JPG
  326. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagejpeg, 0, 0, 1)
  327. ZEND_ARG_INFO(0, im)
  328. ZEND_ARG_INFO(0, filename)
  329. ZEND_ARG_INFO(0, quality)
  330. ZEND_END_ARG_INFO()
  331. #endif
  332. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagewbmp, 0, 0, 1)
  333. ZEND_ARG_INFO(0, im)
  334. ZEND_ARG_INFO(0, filename)
  335. ZEND_ARG_INFO(0, foreground)
  336. ZEND_END_ARG_INFO()
  337. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagegd, 0, 0, 1)
  338. ZEND_ARG_INFO(0, im)
  339. ZEND_ARG_INFO(0, filename)
  340. ZEND_END_ARG_INFO()
  341. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagegd2, 0, 0, 1)
  342. ZEND_ARG_INFO(0, im)
  343. ZEND_ARG_INFO(0, filename)
  344. ZEND_ARG_INFO(0, chunk_size)
  345. ZEND_ARG_INFO(0, type)
  346. ZEND_END_ARG_INFO()
  347. ZEND_BEGIN_ARG_INFO(arginfo_imagedestroy, 0)
  348. ZEND_ARG_INFO(0, im)
  349. ZEND_END_ARG_INFO()
  350. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorallocate, 0)
  351. ZEND_ARG_INFO(0, im)
  352. ZEND_ARG_INFO(0, red)
  353. ZEND_ARG_INFO(0, green)
  354. ZEND_ARG_INFO(0, blue)
  355. ZEND_END_ARG_INFO()
  356. ZEND_BEGIN_ARG_INFO(arginfo_imagepalettecopy, 0)
  357. ZEND_ARG_INFO(0, dst)
  358. ZEND_ARG_INFO(0, src)
  359. ZEND_END_ARG_INFO()
  360. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorat, 0)
  361. ZEND_ARG_INFO(0, im)
  362. ZEND_ARG_INFO(0, x)
  363. ZEND_ARG_INFO(0, y)
  364. ZEND_END_ARG_INFO()
  365. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorclosest, 0)
  366. ZEND_ARG_INFO(0, im)
  367. ZEND_ARG_INFO(0, red)
  368. ZEND_ARG_INFO(0, green)
  369. ZEND_ARG_INFO(0, blue)
  370. ZEND_END_ARG_INFO()
  371. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorclosesthwb, 0)
  372. ZEND_ARG_INFO(0, im)
  373. ZEND_ARG_INFO(0, red)
  374. ZEND_ARG_INFO(0, green)
  375. ZEND_ARG_INFO(0, blue)
  376. ZEND_END_ARG_INFO()
  377. ZEND_BEGIN_ARG_INFO(arginfo_imagecolordeallocate, 0)
  378. ZEND_ARG_INFO(0, im)
  379. ZEND_ARG_INFO(0, index)
  380. ZEND_END_ARG_INFO()
  381. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorresolve, 0)
  382. ZEND_ARG_INFO(0, im)
  383. ZEND_ARG_INFO(0, red)
  384. ZEND_ARG_INFO(0, green)
  385. ZEND_ARG_INFO(0, blue)
  386. ZEND_END_ARG_INFO()
  387. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorexact, 0)
  388. ZEND_ARG_INFO(0, im)
  389. ZEND_ARG_INFO(0, red)
  390. ZEND_ARG_INFO(0, green)
  391. ZEND_ARG_INFO(0, blue)
  392. ZEND_END_ARG_INFO()
  393. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagecolorset, 0, 0, 5)
  394. ZEND_ARG_INFO(0, im)
  395. ZEND_ARG_INFO(0, color)
  396. ZEND_ARG_INFO(0, red)
  397. ZEND_ARG_INFO(0, green)
  398. ZEND_ARG_INFO(0, blue)
  399. ZEND_ARG_INFO(0, alpha)
  400. ZEND_END_ARG_INFO()
  401. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorsforindex, 0)
  402. ZEND_ARG_INFO(0, im)
  403. ZEND_ARG_INFO(0, index)
  404. ZEND_END_ARG_INFO()
  405. ZEND_BEGIN_ARG_INFO(arginfo_imagegammacorrect, 0)
  406. ZEND_ARG_INFO(0, im)
  407. ZEND_ARG_INFO(0, inputgamma)
  408. ZEND_ARG_INFO(0, outputgamma)
  409. ZEND_END_ARG_INFO()
  410. ZEND_BEGIN_ARG_INFO(arginfo_imagesetpixel, 0)
  411. ZEND_ARG_INFO(0, im)
  412. ZEND_ARG_INFO(0, x)
  413. ZEND_ARG_INFO(0, y)
  414. ZEND_ARG_INFO(0, col)
  415. ZEND_END_ARG_INFO()
  416. ZEND_BEGIN_ARG_INFO(arginfo_imageline, 0)
  417. ZEND_ARG_INFO(0, im)
  418. ZEND_ARG_INFO(0, x1)
  419. ZEND_ARG_INFO(0, y1)
  420. ZEND_ARG_INFO(0, x2)
  421. ZEND_ARG_INFO(0, y2)
  422. ZEND_ARG_INFO(0, col)
  423. ZEND_END_ARG_INFO()
  424. ZEND_BEGIN_ARG_INFO(arginfo_imagedashedline, 0)
  425. ZEND_ARG_INFO(0, im)
  426. ZEND_ARG_INFO(0, x1)
  427. ZEND_ARG_INFO(0, y1)
  428. ZEND_ARG_INFO(0, x2)
  429. ZEND_ARG_INFO(0, y2)
  430. ZEND_ARG_INFO(0, col)
  431. ZEND_END_ARG_INFO()
  432. ZEND_BEGIN_ARG_INFO(arginfo_imagerectangle, 0)
  433. ZEND_ARG_INFO(0, im)
  434. ZEND_ARG_INFO(0, x1)
  435. ZEND_ARG_INFO(0, y1)
  436. ZEND_ARG_INFO(0, x2)
  437. ZEND_ARG_INFO(0, y2)
  438. ZEND_ARG_INFO(0, col)
  439. ZEND_END_ARG_INFO()
  440. ZEND_BEGIN_ARG_INFO(arginfo_imagefilledrectangle, 0)
  441. ZEND_ARG_INFO(0, im)
  442. ZEND_ARG_INFO(0, x1)
  443. ZEND_ARG_INFO(0, y1)
  444. ZEND_ARG_INFO(0, x2)
  445. ZEND_ARG_INFO(0, y2)
  446. ZEND_ARG_INFO(0, col)
  447. ZEND_END_ARG_INFO()
  448. ZEND_BEGIN_ARG_INFO(arginfo_imagearc, 0)
  449. ZEND_ARG_INFO(0, im)
  450. ZEND_ARG_INFO(0, cx)
  451. ZEND_ARG_INFO(0, cy)
  452. ZEND_ARG_INFO(0, w)
  453. ZEND_ARG_INFO(0, h)
  454. ZEND_ARG_INFO(0, s)
  455. ZEND_ARG_INFO(0, e)
  456. ZEND_ARG_INFO(0, col)
  457. ZEND_END_ARG_INFO()
  458. ZEND_BEGIN_ARG_INFO(arginfo_imageellipse, 0)
  459. ZEND_ARG_INFO(0, im)
  460. ZEND_ARG_INFO(0, cx)
  461. ZEND_ARG_INFO(0, cy)
  462. ZEND_ARG_INFO(0, w)
  463. ZEND_ARG_INFO(0, h)
  464. ZEND_ARG_INFO(0, color)
  465. ZEND_END_ARG_INFO()
  466. ZEND_BEGIN_ARG_INFO(arginfo_imagefilltoborder, 0)
  467. ZEND_ARG_INFO(0, im)
  468. ZEND_ARG_INFO(0, x)
  469. ZEND_ARG_INFO(0, y)
  470. ZEND_ARG_INFO(0, border)
  471. ZEND_ARG_INFO(0, col)
  472. ZEND_END_ARG_INFO()
  473. ZEND_BEGIN_ARG_INFO(arginfo_imagefill, 0)
  474. ZEND_ARG_INFO(0, im)
  475. ZEND_ARG_INFO(0, x)
  476. ZEND_ARG_INFO(0, y)
  477. ZEND_ARG_INFO(0, col)
  478. ZEND_END_ARG_INFO()
  479. ZEND_BEGIN_ARG_INFO(arginfo_imagecolorstotal, 0)
  480. ZEND_ARG_INFO(0, im)
  481. ZEND_END_ARG_INFO()
  482. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagecolortransparent, 0, 0, 1)
  483. ZEND_ARG_INFO(0, im)
  484. ZEND_ARG_INFO(0, col)
  485. ZEND_END_ARG_INFO()
  486. ZEND_BEGIN_ARG_INFO_EX(arginfo_imageinterlace, 0, 0, 1)
  487. ZEND_ARG_INFO(0, im)
  488. ZEND_ARG_INFO(0, interlace)
  489. ZEND_END_ARG_INFO()
  490. ZEND_BEGIN_ARG_INFO(arginfo_imagepolygon, 0)
  491. ZEND_ARG_INFO(0, im)
  492. ZEND_ARG_INFO(0, points) /* ARRAY_INFO(0, points, 0) */
  493. ZEND_ARG_INFO(0, num_pos)
  494. ZEND_ARG_INFO(0, col)
  495. ZEND_END_ARG_INFO()
  496. ZEND_BEGIN_ARG_INFO(arginfo_imagefilledpolygon, 0)
  497. ZEND_ARG_INFO(0, im)
  498. ZEND_ARG_INFO(0, points) /* ARRAY_INFO(0, points, 0) */
  499. ZEND_ARG_INFO(0, num_pos)
  500. ZEND_ARG_INFO(0, col)
  501. ZEND_END_ARG_INFO()
  502. ZEND_BEGIN_ARG_INFO(arginfo_imagefontwidth, 0)
  503. ZEND_ARG_INFO(0, font)
  504. ZEND_END_ARG_INFO()
  505. ZEND_BEGIN_ARG_INFO(arginfo_imagefontheight, 0)
  506. ZEND_ARG_INFO(0, font)
  507. ZEND_END_ARG_INFO()
  508. ZEND_BEGIN_ARG_INFO(arginfo_imagechar, 0)
  509. ZEND_ARG_INFO(0, im)
  510. ZEND_ARG_INFO(0, font)
  511. ZEND_ARG_INFO(0, x)
  512. ZEND_ARG_INFO(0, y)
  513. ZEND_ARG_INFO(0, c)
  514. ZEND_ARG_INFO(0, col)
  515. ZEND_END_ARG_INFO()
  516. ZEND_BEGIN_ARG_INFO(arginfo_imagecharup, 0)
  517. ZEND_ARG_INFO(0, im)
  518. ZEND_ARG_INFO(0, font)
  519. ZEND_ARG_INFO(0, x)
  520. ZEND_ARG_INFO(0, y)
  521. ZEND_ARG_INFO(0, c)
  522. ZEND_ARG_INFO(0, col)
  523. ZEND_END_ARG_INFO()
  524. ZEND_BEGIN_ARG_INFO(arginfo_imagestring, 0)
  525. ZEND_ARG_INFO(0, im)
  526. ZEND_ARG_INFO(0, font)
  527. ZEND_ARG_INFO(0, x)
  528. ZEND_ARG_INFO(0, y)
  529. ZEND_ARG_INFO(0, str)
  530. ZEND_ARG_INFO(0, col)
  531. ZEND_END_ARG_INFO()
  532. ZEND_BEGIN_ARG_INFO(arginfo_imagestringup, 0)
  533. ZEND_ARG_INFO(0, im)
  534. ZEND_ARG_INFO(0, font)
  535. ZEND_ARG_INFO(0, x)
  536. ZEND_ARG_INFO(0, y)
  537. ZEND_ARG_INFO(0, str)
  538. ZEND_ARG_INFO(0, col)
  539. ZEND_END_ARG_INFO()
  540. ZEND_BEGIN_ARG_INFO(arginfo_imagecopy, 0)
  541. ZEND_ARG_INFO(0, dst_im)
  542. ZEND_ARG_INFO(0, src_im)
  543. ZEND_ARG_INFO(0, dst_x)
  544. ZEND_ARG_INFO(0, dst_y)
  545. ZEND_ARG_INFO(0, src_x)
  546. ZEND_ARG_INFO(0, src_y)
  547. ZEND_ARG_INFO(0, src_w)
  548. ZEND_ARG_INFO(0, src_h)
  549. ZEND_END_ARG_INFO()
  550. ZEND_BEGIN_ARG_INFO(arginfo_imagecopymerge, 0)
  551. ZEND_ARG_INFO(0, src_im)
  552. ZEND_ARG_INFO(0, dst_im)
  553. ZEND_ARG_INFO(0, dst_x)
  554. ZEND_ARG_INFO(0, dst_y)
  555. ZEND_ARG_INFO(0, src_x)
  556. ZEND_ARG_INFO(0, src_y)
  557. ZEND_ARG_INFO(0, src_w)
  558. ZEND_ARG_INFO(0, src_h)
  559. ZEND_ARG_INFO(0, pct)
  560. ZEND_END_ARG_INFO()
  561. ZEND_BEGIN_ARG_INFO(arginfo_imagecopymergegray, 0)
  562. ZEND_ARG_INFO(0, src_im)
  563. ZEND_ARG_INFO(0, dst_im)
  564. ZEND_ARG_INFO(0, dst_x)
  565. ZEND_ARG_INFO(0, dst_y)
  566. ZEND_ARG_INFO(0, src_x)
  567. ZEND_ARG_INFO(0, src_y)
  568. ZEND_ARG_INFO(0, src_w)
  569. ZEND_ARG_INFO(0, src_h)
  570. ZEND_ARG_INFO(0, pct)
  571. ZEND_END_ARG_INFO()
  572. ZEND_BEGIN_ARG_INFO(arginfo_imagecopyresized, 0)
  573. ZEND_ARG_INFO(0, dst_im)
  574. ZEND_ARG_INFO(0, src_im)
  575. ZEND_ARG_INFO(0, dst_x)
  576. ZEND_ARG_INFO(0, dst_y)
  577. ZEND_ARG_INFO(0, src_x)
  578. ZEND_ARG_INFO(0, src_y)
  579. ZEND_ARG_INFO(0, dst_w)
  580. ZEND_ARG_INFO(0, dst_h)
  581. ZEND_ARG_INFO(0, src_w)
  582. ZEND_ARG_INFO(0, src_h)
  583. ZEND_END_ARG_INFO()
  584. ZEND_BEGIN_ARG_INFO(arginfo_imagesx, 0)
  585. ZEND_ARG_INFO(0, im)
  586. ZEND_END_ARG_INFO()
  587. ZEND_BEGIN_ARG_INFO(arginfo_imagesy, 0)
  588. ZEND_ARG_INFO(0, im)
  589. ZEND_END_ARG_INFO()
  590. #ifdef ENABLE_GD_TTF
  591. #if HAVE_LIBFREETYPE
  592. ZEND_BEGIN_ARG_INFO_EX(arginfo_imageftbbox, 0, 0, 4)
  593. ZEND_ARG_INFO(0, size)
  594. ZEND_ARG_INFO(0, angle)
  595. ZEND_ARG_INFO(0, font_file)
  596. ZEND_ARG_INFO(0, text)
  597. ZEND_ARG_INFO(0, extrainfo) /* ARRAY_INFO(0, extrainfo, 0) */
  598. ZEND_END_ARG_INFO()
  599. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagefttext, 0, 0, 8)
  600. ZEND_ARG_INFO(0, im)
  601. ZEND_ARG_INFO(0, size)
  602. ZEND_ARG_INFO(0, angle)
  603. ZEND_ARG_INFO(0, x)
  604. ZEND_ARG_INFO(0, y)
  605. ZEND_ARG_INFO(0, col)
  606. ZEND_ARG_INFO(0, font_file)
  607. ZEND_ARG_INFO(0, text)
  608. ZEND_ARG_INFO(0, extrainfo) /* ARRAY_INFO(0, extrainfo, 0) */
  609. ZEND_END_ARG_INFO()
  610. #endif
  611. ZEND_BEGIN_ARG_INFO(arginfo_imagettfbbox, 0)
  612. ZEND_ARG_INFO(0, size)
  613. ZEND_ARG_INFO(0, angle)
  614. ZEND_ARG_INFO(0, font_file)
  615. ZEND_ARG_INFO(0, text)
  616. ZEND_END_ARG_INFO()
  617. ZEND_BEGIN_ARG_INFO(arginfo_imagettftext, 0)
  618. ZEND_ARG_INFO(0, im)
  619. ZEND_ARG_INFO(0, size)
  620. ZEND_ARG_INFO(0, angle)
  621. ZEND_ARG_INFO(0, x)
  622. ZEND_ARG_INFO(0, y)
  623. ZEND_ARG_INFO(0, col)
  624. ZEND_ARG_INFO(0, font_file)
  625. ZEND_ARG_INFO(0, text)
  626. ZEND_END_ARG_INFO()
  627. #endif
  628. #ifdef HAVE_LIBT1
  629. ZEND_BEGIN_ARG_INFO(arginfo_imagepsloadfont, 0)
  630. ZEND_ARG_INFO(0, pathname)
  631. ZEND_END_ARG_INFO()
  632. /*
  633. ZEND_BEGIN_ARG_INFO(arginfo_imagepscopyfont, 0)
  634. ZEND_ARG_INFO(0, font_index)
  635. ZEND_END_ARG_INFO()
  636. */
  637. ZEND_BEGIN_ARG_INFO(arginfo_imagepsfreefont, 0)
  638. ZEND_ARG_INFO(0, font_index)
  639. ZEND_END_ARG_INFO()
  640. ZEND_BEGIN_ARG_INFO(arginfo_imagepsencodefont, 0)
  641. ZEND_ARG_INFO(0, font_index)
  642. ZEND_ARG_INFO(0, filename)
  643. ZEND_END_ARG_INFO()
  644. ZEND_BEGIN_ARG_INFO(arginfo_imagepsextendfont, 0)
  645. ZEND_ARG_INFO(0, font_index)
  646. ZEND_ARG_INFO(0, extend)
  647. ZEND_END_ARG_INFO()
  648. ZEND_BEGIN_ARG_INFO(arginfo_imagepsslantfont, 0)
  649. ZEND_ARG_INFO(0, font_index)
  650. ZEND_ARG_INFO(0, slant)
  651. ZEND_END_ARG_INFO()
  652. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagepstext, 0, 0, 8)
  653. ZEND_ARG_INFO(0, im)
  654. ZEND_ARG_INFO(0, text)
  655. ZEND_ARG_INFO(0, font)
  656. ZEND_ARG_INFO(0, size)
  657. ZEND_ARG_INFO(0, foreground)
  658. ZEND_ARG_INFO(0, background)
  659. ZEND_ARG_INFO(0, xcoord)
  660. ZEND_ARG_INFO(0, ycoord)
  661. ZEND_ARG_INFO(0, space)
  662. ZEND_ARG_INFO(0, tightness)
  663. ZEND_ARG_INFO(0, angle)
  664. ZEND_ARG_INFO(0, antialias)
  665. ZEND_END_ARG_INFO()
  666. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagepsbbox, 0, 0, 3)
  667. ZEND_ARG_INFO(0, text)
  668. ZEND_ARG_INFO(0, font)
  669. ZEND_ARG_INFO(0, size)
  670. ZEND_ARG_INFO(0, space)
  671. ZEND_ARG_INFO(0, tightness)
  672. ZEND_ARG_INFO(0, angle)
  673. ZEND_END_ARG_INFO()
  674. #endif
  675. ZEND_BEGIN_ARG_INFO_EX(arginfo_image2wbmp, 0, 0, 1)
  676. ZEND_ARG_INFO(0, im)
  677. ZEND_ARG_INFO(0, filename)
  678. ZEND_ARG_INFO(0, threshold)
  679. ZEND_END_ARG_INFO()
  680. #if defined(HAVE_GD_JPG)
  681. ZEND_BEGIN_ARG_INFO(arginfo_jpeg2wbmp, 0)
  682. ZEND_ARG_INFO(0, f_org)
  683. ZEND_ARG_INFO(0, f_dest)
  684. ZEND_ARG_INFO(0, d_height)
  685. ZEND_ARG_INFO(0, d_width)
  686. ZEND_ARG_INFO(0, d_threshold)
  687. ZEND_END_ARG_INFO()
  688. #endif
  689. #if defined(HAVE_GD_PNG)
  690. ZEND_BEGIN_ARG_INFO(arginfo_png2wbmp, 0)
  691. ZEND_ARG_INFO(0, f_org)
  692. ZEND_ARG_INFO(0, f_dest)
  693. ZEND_ARG_INFO(0, d_height)
  694. ZEND_ARG_INFO(0, d_width)
  695. ZEND_ARG_INFO(0, d_threshold)
  696. ZEND_END_ARG_INFO()
  697. #endif
  698. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagefilter, 0, 0, 2)
  699. ZEND_ARG_INFO(0, im)
  700. ZEND_ARG_INFO(0, filtertype)
  701. ZEND_ARG_INFO(0, arg1)
  702. ZEND_ARG_INFO(0, arg2)
  703. ZEND_ARG_INFO(0, arg3)
  704. ZEND_ARG_INFO(0, arg4)
  705. ZEND_END_ARG_INFO()
  706. ZEND_BEGIN_ARG_INFO(arginfo_imageconvolution, 0)
  707. ZEND_ARG_INFO(0, im)
  708. ZEND_ARG_INFO(0, matrix3x3) /* ARRAY_INFO(0, matrix3x3, 0) */
  709. ZEND_ARG_INFO(0, div)
  710. ZEND_ARG_INFO(0, offset)
  711. ZEND_END_ARG_INFO()
  712. ZEND_BEGIN_ARG_INFO(arginfo_imageflip, 0)
  713. ZEND_ARG_INFO(0, im)
  714. ZEND_ARG_INFO(0, mode)
  715. ZEND_END_ARG_INFO()
  716. #ifdef HAVE_GD_BUNDLED
  717. ZEND_BEGIN_ARG_INFO(arginfo_imageantialias, 0)
  718. ZEND_ARG_INFO(0, im)
  719. ZEND_ARG_INFO(0, on)
  720. ZEND_END_ARG_INFO()
  721. #endif
  722. ZEND_BEGIN_ARG_INFO(arginfo_imagecrop, 0)
  723. ZEND_ARG_INFO(0, im)
  724. ZEND_ARG_INFO(0, rect)
  725. ZEND_END_ARG_INFO()
  726. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagecropauto, 0, 0, 1)
  727. ZEND_ARG_INFO(0, im)
  728. ZEND_ARG_INFO(0, mode)
  729. ZEND_ARG_INFO(0, threshold)
  730. ZEND_ARG_INFO(0, color)
  731. ZEND_END_ARG_INFO()
  732. ZEND_BEGIN_ARG_INFO_EX(arginfo_imagescale, 0, 0, 2)
  733. ZEND_ARG_INFO(0, im)
  734. ZEND_ARG_INFO(0, new_width)
  735. ZEND_ARG_INFO(0, new_height)
  736. ZEND_ARG_INFO(0, mode)
  737. ZEND_END_ARG_INFO()
  738. ZEND_BEGIN_ARG_INFO_EX(arginfo_imageaffine, 0, 0, 2)
  739. ZEND_ARG_INFO(0, im)
  740. ZEND_ARG_INFO(0, affine)
  741. ZEND_ARG_INFO(0, clip)
  742. ZEND_END_ARG_INFO()
  743. ZEND_BEGIN_ARG_INFO_EX(arginfo_imageaffinematrixget, 0, 0, 1)
  744. ZEND_ARG_INFO(0, type)
  745. ZEND_ARG_INFO(0, options)
  746. ZEND_END_ARG_INFO()
  747. ZEND_BEGIN_ARG_INFO(arginfo_imageaffinematrixconcat, 0)
  748. ZEND_ARG_INFO(0, m1)
  749. ZEND_ARG_INFO(0, m2)
  750. ZEND_END_ARG_INFO()
  751. ZEND_BEGIN_ARG_INFO(arginfo_imagesetinterpolation, 0)
  752. ZEND_ARG_INFO(0, im)
  753. ZEND_ARG_INFO(0, method)
  754. ZEND_END_ARG_INFO()
  755. /* }}} */
  756. /* {{{ gd_functions[]
  757. */
  758. const zend_function_entry gd_functions[] = {
  759. PHP_FE(gd_info, arginfo_gd_info)
  760. PHP_FE(imagearc, arginfo_imagearc)
  761. PHP_FE(imageellipse, arginfo_imageellipse)
  762. PHP_FE(imagechar, arginfo_imagechar)
  763. PHP_FE(imagecharup, arginfo_imagecharup)
  764. PHP_FE(imagecolorat, arginfo_imagecolorat)
  765. PHP_FE(imagecolorallocate, arginfo_imagecolorallocate)
  766. PHP_FE(imagepalettecopy, arginfo_imagepalettecopy)
  767. PHP_FE(imagecreatefromstring, arginfo_imagecreatefromstring)
  768. PHP_FE(imagecolorclosest, arginfo_imagecolorclosest)
  769. PHP_FE(imagecolorclosesthwb, arginfo_imagecolorclosesthwb)
  770. PHP_FE(imagecolordeallocate, arginfo_imagecolordeallocate)
  771. PHP_FE(imagecolorresolve, arginfo_imagecolorresolve)
  772. PHP_FE(imagecolorexact, arginfo_imagecolorexact)
  773. PHP_FE(imagecolorset, arginfo_imagecolorset)
  774. PHP_FE(imagecolortransparent, arginfo_imagecolortransparent)
  775. PHP_FE(imagecolorstotal, arginfo_imagecolorstotal)
  776. PHP_FE(imagecolorsforindex, arginfo_imagecolorsforindex)
  777. PHP_FE(imagecopy, arginfo_imagecopy)
  778. PHP_FE(imagecopymerge, arginfo_imagecopymerge)
  779. PHP_FE(imagecopymergegray, arginfo_imagecopymergegray)
  780. PHP_FE(imagecopyresized, arginfo_imagecopyresized)
  781. PHP_FE(imagecreate, arginfo_imagecreate)
  782. PHP_FE(imagecreatetruecolor, arginfo_imagecreatetruecolor)
  783. PHP_FE(imageistruecolor, arginfo_imageistruecolor)
  784. PHP_FE(imagetruecolortopalette, arginfo_imagetruecolortopalette)
  785. PHP_FE(imagepalettetotruecolor, arginfo_imagepalettetotruecolor)
  786. PHP_FE(imagesetthickness, arginfo_imagesetthickness)
  787. PHP_FE(imagefilledarc, arginfo_imagefilledarc)
  788. PHP_FE(imagefilledellipse, arginfo_imagefilledellipse)
  789. PHP_FE(imagealphablending, arginfo_imagealphablending)
  790. PHP_FE(imagesavealpha, arginfo_imagesavealpha)
  791. PHP_FE(imagecolorallocatealpha, arginfo_imagecolorallocatealpha)
  792. PHP_FE(imagecolorresolvealpha, arginfo_imagecolorresolvealpha)
  793. PHP_FE(imagecolorclosestalpha, arginfo_imagecolorclosestalpha)
  794. PHP_FE(imagecolorexactalpha, arginfo_imagecolorexactalpha)
  795. PHP_FE(imagecopyresampled, arginfo_imagecopyresampled)
  796. #ifdef PHP_WIN32
  797. PHP_FE(imagegrabwindow, arginfo_imagegrabwindow)
  798. PHP_FE(imagegrabscreen, arginfo_imagegrabscreen)
  799. #endif
  800. PHP_FE(imagerotate, arginfo_imagerotate)
  801. PHP_FE(imageflip, arginfo_imageflip)
  802. #ifdef HAVE_GD_BUNDLED
  803. PHP_FE(imageantialias, arginfo_imageantialias)
  804. #endif
  805. PHP_FE(imagecrop, arginfo_imagecrop)
  806. PHP_FE(imagecropauto, arginfo_imagecropauto)
  807. PHP_FE(imagescale, arginfo_imagescale)
  808. PHP_FE(imageaffine, arginfo_imageaffine)
  809. PHP_FE(imageaffinematrixconcat, arginfo_imageaffinematrixconcat)
  810. PHP_FE(imageaffinematrixget, arginfo_imageaffinematrixget)
  811. PHP_FE(imagesetinterpolation, arginfo_imagesetinterpolation)
  812. PHP_FE(imagesettile, arginfo_imagesettile)
  813. PHP_FE(imagesetbrush, arginfo_imagesetbrush)
  814. PHP_FE(imagesetstyle, arginfo_imagesetstyle)
  815. #ifdef HAVE_GD_PNG
  816. PHP_FE(imagecreatefrompng, arginfo_imagecreatefrompng)
  817. #endif
  818. #ifdef HAVE_GD_WEBP
  819. PHP_FE(imagecreatefromwebp, arginfo_imagecreatefromwebp)
  820. #endif
  821. PHP_FE(imagecreatefromgif, arginfo_imagecreatefromgif)
  822. #ifdef HAVE_GD_JPG
  823. PHP_FE(imagecreatefromjpeg, arginfo_imagecreatefromjpeg)
  824. #endif
  825. PHP_FE(imagecreatefromwbmp, arginfo_imagecreatefromwbmp)
  826. PHP_FE(imagecreatefromxbm, arginfo_imagecreatefromxbm)
  827. #if defined(HAVE_GD_XPM)
  828. PHP_FE(imagecreatefromxpm, arginfo_imagecreatefromxpm)
  829. #endif
  830. PHP_FE(imagecreatefromgd, arginfo_imagecreatefromgd)
  831. PHP_FE(imagecreatefromgd2, arginfo_imagecreatefromgd2)
  832. PHP_FE(imagecreatefromgd2part, arginfo_imagecreatefromgd2part)
  833. #ifdef HAVE_GD_PNG
  834. PHP_FE(imagepng, arginfo_imagepng)
  835. #endif
  836. #ifdef HAVE_GD_WEBP
  837. PHP_FE(imagewebp, arginfo_imagewebp)
  838. #endif
  839. PHP_FE(imagegif, arginfo_imagegif)
  840. #ifdef HAVE_GD_JPG
  841. PHP_FE(imagejpeg, arginfo_imagejpeg)
  842. #endif
  843. PHP_FE(imagewbmp, arginfo_imagewbmp)
  844. PHP_FE(imagegd, arginfo_imagegd)
  845. PHP_FE(imagegd2, arginfo_imagegd2)
  846. PHP_FE(imagedestroy, arginfo_imagedestroy)
  847. PHP_FE(imagegammacorrect, arginfo_imagegammacorrect)
  848. PHP_FE(imagefill, arginfo_imagefill)
  849. PHP_FE(imagefilledpolygon, arginfo_imagefilledpolygon)
  850. PHP_FE(imagefilledrectangle, arginfo_imagefilledrectangle)
  851. PHP_FE(imagefilltoborder, arginfo_imagefilltoborder)
  852. PHP_FE(imagefontwidth, arginfo_imagefontwidth)
  853. PHP_FE(imagefontheight, arginfo_imagefontheight)
  854. PHP_FE(imageinterlace, arginfo_imageinterlace)
  855. PHP_FE(imageline, arginfo_imageline)
  856. PHP_FE(imageloadfont, arginfo_imageloadfont)
  857. PHP_FE(imagepolygon, arginfo_imagepolygon)
  858. PHP_FE(imagerectangle, arginfo_imagerectangle)
  859. PHP_FE(imagesetpixel, arginfo_imagesetpixel)
  860. PHP_FE(imagestring, arginfo_imagestring)
  861. PHP_FE(imagestringup, arginfo_imagestringup)
  862. PHP_FE(imagesx, arginfo_imagesx)
  863. PHP_FE(imagesy, arginfo_imagesy)
  864. PHP_FE(imagedashedline, arginfo_imagedashedline)
  865. #ifdef ENABLE_GD_TTF
  866. PHP_FE(imagettfbbox, arginfo_imagettfbbox)
  867. PHP_FE(imagettftext, arginfo_imagettftext)
  868. #if HAVE_GD_FREETYPE && HAVE_LIBFREETYPE
  869. PHP_FE(imageftbbox, arginfo_imageftbbox)
  870. PHP_FE(imagefttext, arginfo_imagefttext)
  871. #endif
  872. #endif
  873. #ifdef HAVE_LIBT1
  874. PHP_FE(imagepsloadfont, arginfo_imagepsloadfont)
  875. /*
  876. PHP_FE(imagepscopyfont, arginfo_imagepscopyfont)
  877. */
  878. PHP_FE(imagepsfreefont, arginfo_imagepsfreefont)
  879. PHP_FE(imagepsencodefont, arginfo_imagepsencodefont)
  880. PHP_FE(imagepsextendfont, arginfo_imagepsextendfont)
  881. PHP_FE(imagepsslantfont, arginfo_imagepsslantfont)
  882. PHP_FE(imagepstext, arginfo_imagepstext)
  883. PHP_FE(imagepsbbox, arginfo_imagepsbbox)
  884. #endif
  885. PHP_FE(imagetypes, arginfo_imagetypes)
  886. #if defined(HAVE_GD_JPG)
  887. PHP_FE(jpeg2wbmp, arginfo_jpeg2wbmp)
  888. #endif
  889. #if defined(HAVE_GD_PNG)
  890. PHP_FE(png2wbmp, arginfo_png2wbmp)
  891. #endif
  892. PHP_FE(image2wbmp, arginfo_image2wbmp)
  893. PHP_FE(imagelayereffect, arginfo_imagelayereffect)
  894. PHP_FE(imagexbm, arginfo_imagexbm)
  895. PHP_FE(imagecolormatch, arginfo_imagecolormatch)
  896. /* gd filters */
  897. PHP_FE(imagefilter, arginfo_imagefilter)
  898. PHP_FE(imageconvolution, arginfo_imageconvolution)
  899. PHP_FE_END
  900. };
  901. /* }}} */
  902. zend_module_entry gd_module_entry = {
  903. STANDARD_MODULE_HEADER,
  904. "gd",
  905. gd_functions,
  906. PHP_MINIT(gd),
  907. #if HAVE_LIBT1
  908. PHP_MSHUTDOWN(gd),
  909. #else
  910. NULL,
  911. #endif
  912. NULL,
  913. #if HAVE_GD_FREETYPE && HAVE_LIBFREETYPE
  914. PHP_RSHUTDOWN(gd),
  915. #else
  916. NULL,
  917. #endif
  918. PHP_MINFO(gd),
  919. NO_VERSION_YET,
  920. STANDARD_MODULE_PROPERTIES
  921. };
  922. #ifdef COMPILE_DL_GD
  923. ZEND_GET_MODULE(gd)
  924. #endif
  925. /* {{{ PHP_INI_BEGIN */
  926. PHP_INI_BEGIN()
  927. PHP_INI_ENTRY("gd.jpeg_ignore_warning", "0", PHP_INI_ALL, NULL)
  928. PHP_INI_END()
  929. /* }}} */
  930. /* {{{ php_free_gd_image
  931. */
  932. static void php_free_gd_image(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  933. {
  934. gdImageDestroy((gdImagePtr) rsrc->ptr);
  935. }
  936. /* }}} */
  937. /* {{{ php_free_gd_font
  938. */
  939. static void php_free_gd_font(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  940. {
  941. gdFontPtr fp = (gdFontPtr) rsrc->ptr;
  942. if (fp->data) {
  943. efree(fp->data);
  944. }
  945. efree(fp);
  946. }
  947. /* }}} */
  948. #ifndef HAVE_GD_BUNDLED
  949. /* {{{ php_gd_error_method
  950. */
  951. void php_gd_error_method(int type, const char *format, va_list args)
  952. {
  953. TSRMLS_FETCH();
  954. switch (type) {
  955. case GD_DEBUG:
  956. case GD_INFO:
  957. case GD_NOTICE:
  958. type = E_NOTICE;
  959. break;
  960. case GD_WARNING:
  961. type = E_WARNING;
  962. break;
  963. default:
  964. type = E_ERROR;
  965. }
  966. php_verror(NULL, "", type, format, args TSRMLS_CC);
  967. }
  968. /* }}} */
  969. #endif
  970. /* {{{ PHP_MSHUTDOWN_FUNCTION
  971. */
  972. #if HAVE_LIBT1
  973. PHP_MSHUTDOWN_FUNCTION(gd)
  974. {
  975. T1_CloseLib();
  976. #if HAVE_GD_BUNDLED && HAVE_LIBFREETYPE
  977. gdFontCacheMutexShutdown();
  978. #endif
  979. UNREGISTER_INI_ENTRIES();
  980. return SUCCESS;
  981. }
  982. #endif
  983. /* }}} */
  984. /* {{{ PHP_MINIT_FUNCTION
  985. */
  986. PHP_MINIT_FUNCTION(gd)
  987. {
  988. le_gd = zend_register_list_destructors_ex(php_free_gd_image, NULL, "gd", module_number);
  989. le_gd_font = zend_register_list_destructors_ex(php_free_gd_font, NULL, "gd font", module_number);
  990. #if HAVE_GD_BUNDLED && HAVE_LIBFREETYPE
  991. gdFontCacheMutexSetup();
  992. #endif
  993. #if HAVE_LIBT1
  994. T1_SetBitmapPad(8);
  995. T1_InitLib(NO_LOGFILE | IGNORE_CONFIGFILE | IGNORE_FONTDATABASE);
  996. T1_SetLogLevel(T1LOG_DEBUG);
  997. le_ps_font = zend_register_list_destructors_ex(php_free_ps_font, NULL, "gd PS font", module_number);
  998. le_ps_enc = zend_register_list_destructors_ex(php_free_ps_enc, NULL, "gd PS encoding", module_number);
  999. #endif
  1000. #ifndef HAVE_GD_BUNDLED
  1001. gdSetErrorMethod(php_gd_error_method);
  1002. #endif
  1003. REGISTER_INI_ENTRIES();
  1004. REGISTER_LONG_CONSTANT("IMG_GIF", 1, CONST_CS | CONST_PERSISTENT);
  1005. REGISTER_LONG_CONSTANT("IMG_JPG", 2, CONST_CS | CONST_PERSISTENT);
  1006. REGISTER_LONG_CONSTANT("IMG_JPEG", 2, CONST_CS | CONST_PERSISTENT);
  1007. REGISTER_LONG_CONSTANT("IMG_PNG", 4, CONST_CS | CONST_PERSISTENT);
  1008. REGISTER_LONG_CONSTANT("IMG_WBMP", 8, CONST_CS | CONST_PERSISTENT);
  1009. REGISTER_LONG_CONSTANT("IMG_XPM", 16, CONST_CS | CONST_PERSISTENT);
  1010. REGISTER_LONG_CONSTANT("IMG_WEBP", 32, CONST_CS | CONST_PERSISTENT);
  1011. /* special colours for gd */
  1012. REGISTER_LONG_CONSTANT("IMG_COLOR_TILED", gdTiled, CONST_CS | CONST_PERSISTENT);
  1013. REGISTER_LONG_CONSTANT("IMG_COLOR_STYLED", gdStyled, CONST_CS | CONST_PERSISTENT);
  1014. REGISTER_LONG_CONSTANT("IMG_COLOR_BRUSHED", gdBrushed, CONST_CS | CONST_PERSISTENT);
  1015. REGISTER_LONG_CONSTANT("IMG_COLOR_STYLEDBRUSHED", gdStyledBrushed, CONST_CS | CONST_PERSISTENT);
  1016. REGISTER_LONG_CONSTANT("IMG_COLOR_TRANSPARENT", gdTransparent, CONST_CS | CONST_PERSISTENT);
  1017. /* for imagefilledarc */
  1018. REGISTER_LONG_CONSTANT("IMG_ARC_ROUNDED", gdArc, CONST_CS | CONST_PERSISTENT);
  1019. REGISTER_LONG_CONSTANT("IMG_ARC_PIE", gdPie, CONST_CS | CONST_PERSISTENT);
  1020. REGISTER_LONG_CONSTANT("IMG_ARC_CHORD", gdChord, CONST_CS | CONST_PERSISTENT);
  1021. REGISTER_LONG_CONSTANT("IMG_ARC_NOFILL", gdNoFill, CONST_CS | CONST_PERSISTENT);
  1022. REGISTER_LONG_CONSTANT("IMG_ARC_EDGED", gdEdged, CONST_CS | CONST_PERSISTENT);
  1023. /* GD2 image format types */
  1024. REGISTER_LONG_CONSTANT("IMG_GD2_RAW", GD2_FMT_RAW, CONST_CS | CONST_PERSISTENT);
  1025. REGISTER_LONG_CONSTANT("IMG_GD2_COMPRESSED", GD2_FMT_COMPRESSED, CONST_CS | CONST_PERSISTENT);
  1026. REGISTER_LONG_CONSTANT("IMG_FLIP_HORIZONTAL", GD_FLIP_HORINZONTAL, CONST_CS | CONST_PERSISTENT);
  1027. REGISTER_LONG_CONSTANT("IMG_FLIP_VERTICAL", GD_FLIP_VERTICAL, CONST_CS | CONST_PERSISTENT);
  1028. REGISTER_LONG_CONSTANT("IMG_FLIP_BOTH", GD_FLIP_BOTH, CONST_CS | CONST_PERSISTENT);
  1029. REGISTER_LONG_CONSTANT("IMG_EFFECT_REPLACE", gdEffectReplace, CONST_CS | CONST_PERSISTENT);
  1030. REGISTER_LONG_CONSTANT("IMG_EFFECT_ALPHABLEND", gdEffectAlphaBlend, CONST_CS | CONST_PERSISTENT);
  1031. REGISTER_LONG_CONSTANT("IMG_EFFECT_NORMAL", gdEffectNormal, CONST_CS | CONST_PERSISTENT);
  1032. REGISTER_LONG_CONSTANT("IMG_EFFECT_OVERLAY", gdEffectOverlay, CONST_CS | CONST_PERSISTENT);
  1033. REGISTER_LONG_CONSTANT("IMG_CROP_DEFAULT", GD_CROP_DEFAULT, CONST_CS | CONST_PERSISTENT);
  1034. REGISTER_LONG_CONSTANT("IMG_CROP_TRANSPARENT", GD_CROP_TRANSPARENT, CONST_CS | CONST_PERSISTENT);
  1035. REGISTER_LONG_CONSTANT("IMG_CROP_BLACK", GD_CROP_BLACK, CONST_CS | CONST_PERSISTENT);
  1036. REGISTER_LONG_CONSTANT("IMG_CROP_WHITE", GD_CROP_WHITE, CONST_CS | CONST_PERSISTENT);
  1037. REGISTER_LONG_CONSTANT("IMG_CROP_SIDES", GD_CROP_SIDES, CONST_CS | CONST_PERSISTENT);
  1038. REGISTER_LONG_CONSTANT("IMG_CROP_THRESHOLD", GD_CROP_THRESHOLD, CONST_CS | CONST_PERSISTENT);
  1039. REGISTER_LONG_CONSTANT("IMG_BELL", GD_BELL, CONST_CS | CONST_PERSISTENT);
  1040. REGISTER_LONG_CONSTANT("IMG_BESSEL", GD_BESSEL, CONST_CS | CONST_PERSISTENT);
  1041. REGISTER_LONG_CONSTANT("IMG_BILINEAR_FIXED", GD_BILINEAR_FIXED, CONST_CS | CONST_PERSISTENT);
  1042. REGISTER_LONG_CONSTANT("IMG_BICUBIC", GD_BICUBIC, CONST_CS | CONST_PERSISTENT);
  1043. REGISTER_LONG_CONSTANT("IMG_BICUBIC_FIXED", GD_BICUBIC_FIXED, CONST_CS | CONST_PERSISTENT);
  1044. REGISTER_LONG_CONSTANT("IMG_BLACKMAN", GD_BLACKMAN, CONST_CS | CONST_PERSISTENT);
  1045. REGISTER_LONG_CONSTANT("IMG_BOX", GD_BOX, CONST_CS | CONST_PERSISTENT);
  1046. REGISTER_LONG_CONSTANT("IMG_BSPLINE", GD_BSPLINE, CONST_CS | CONST_PERSISTENT);
  1047. REGISTER_LONG_CONSTANT("IMG_CATMULLROM", GD_CATMULLROM, CONST_CS | CONST_PERSISTENT);
  1048. REGISTER_LONG_CONSTANT("IMG_GAUSSIAN", GD_GAUSSIAN, CONST_CS | CONST_PERSISTENT);
  1049. REGISTER_LONG_CONSTANT("IMG_GENERALIZED_CUBIC", GD_GENERALIZED_CUBIC, CONST_CS | CONST_PERSISTENT);
  1050. REGISTER_LONG_CONSTANT("IMG_HERMITE", GD_HERMITE, CONST_CS | CONST_PERSISTENT);
  1051. REGISTER_LONG_CONSTANT("IMG_HAMMING", GD_HAMMING, CONST_CS | CONST_PERSISTENT);
  1052. REGISTER_LONG_CONSTANT("IMG_HANNING", GD_HANNING, CONST_CS | CONST_PERSISTENT);
  1053. REGISTER_LONG_CONSTANT("IMG_MITCHELL", GD_MITCHELL, CONST_CS | CONST_PERSISTENT);
  1054. REGISTER_LONG_CONSTANT("IMG_POWER", GD_POWER, CONST_CS | CONST_PERSISTENT);
  1055. REGISTER_LONG_CONSTANT("IMG_QUADRATIC", GD_QUADRATIC, CONST_CS | CONST_PERSISTENT);
  1056. REGISTER_LONG_CONSTANT("IMG_SINC", GD_SINC, CONST_CS | CONST_PERSISTENT);
  1057. REGISTER_LONG_CONSTANT("IMG_NEAREST_NEIGHBOUR", GD_NEAREST_NEIGHBOUR, CONST_CS | CONST_PERSISTENT);
  1058. REGISTER_LONG_CONSTANT("IMG_WEIGHTED4", GD_WEIGHTED4, CONST_CS | CONST_PERSISTENT);
  1059. REGISTER_LONG_CONSTANT("IMG_TRIANGLE", GD_TRIANGLE, CONST_CS | CONST_PERSISTENT);
  1060. REGISTER_LONG_CONSTANT("IMG_AFFINE_TRANSLATE", GD_AFFINE_TRANSLATE, CONST_CS | CONST_PERSISTENT);
  1061. REGISTER_LONG_CONSTANT("IMG_AFFINE_SCALE", GD_AFFINE_SCALE, CONST_CS | CONST_PERSISTENT);
  1062. REGISTER_LONG_CONSTANT("IMG_AFFINE_ROTATE", GD_AFFINE_ROTATE, CONST_CS | CONST_PERSISTENT);
  1063. REGISTER_LONG_CONSTANT("IMG_AFFINE_SHEAR_HORIZONTAL", GD_AFFINE_SHEAR_HORIZONTAL, CONST_CS | CONST_PERSISTENT);
  1064. REGISTER_LONG_CONSTANT("IMG_AFFINE_SHEAR_VERTICAL", GD_AFFINE_SHEAR_VERTICAL, CONST_CS | CONST_PERSISTENT);
  1065. #if defined(HAVE_GD_BUNDLED)
  1066. REGISTER_LONG_CONSTANT("GD_BUNDLED", 1, CONST_CS | CONST_PERSISTENT);
  1067. #else
  1068. REGISTER_LONG_CONSTANT("GD_BUNDLED", 0, CONST_CS | CONST_PERSISTENT);
  1069. #endif
  1070. /* Section Filters */
  1071. REGISTER_LONG_CONSTANT("IMG_FILTER_NEGATE", IMAGE_FILTER_NEGATE, CONST_CS | CONST_PERSISTENT);
  1072. REGISTER_LONG_CONSTANT("IMG_FILTER_GRAYSCALE", IMAGE_FILTER_GRAYSCALE, CONST_CS | CONST_PERSISTENT);
  1073. REGISTER_LONG_CONSTANT("IMG_FILTER_BRIGHTNESS", IMAGE_FILTER_BRIGHTNESS, CONST_CS | CONST_PERSISTENT);
  1074. REGISTER_LONG_CONSTANT("IMG_FILTER_CONTRAST", IMAGE_FILTER_CONTRAST, CONST_CS | CONST_PERSISTENT);
  1075. REGISTER_LONG_CONSTANT("IMG_FILTER_COLORIZE", IMAGE_FILTER_COLORIZE, CONST_CS | CONST_PERSISTENT);
  1076. REGISTER_LONG_CONSTANT("IMG_FILTER_EDGEDETECT", IMAGE_FILTER_EDGEDETECT, CONST_CS | CONST_PERSISTENT);
  1077. REGISTER_LONG_CONSTANT("IMG_FILTER_GAUSSIAN_BLUR", IMAGE_FILTER_GAUSSIAN_BLUR, CONST_CS | CONST_PERSISTENT);
  1078. REGISTER_LONG_CONSTANT("IMG_FILTER_SELECTIVE_BLUR", IMAGE_FILTER_SELECTIVE_BLUR, CONST_CS | CONST_PERSISTENT);
  1079. REGISTER_LONG_CONSTANT("IMG_FILTER_EMBOSS", IMAGE_FILTER_EMBOSS, CONST_CS | CONST_PERSISTENT);
  1080. REGISTER_LONG_CONSTANT("IMG_FILTER_MEAN_REMOVAL", IMAGE_FILTER_MEAN_REMOVAL, CONST_CS | CONST_PERSISTENT);
  1081. REGISTER_LONG_CONSTANT("IMG_FILTER_SMOOTH", IMAGE_FILTER_SMOOTH, CONST_CS | CONST_PERSISTENT);
  1082. REGISTER_LONG_CONSTANT("IMG_FILTER_PIXELATE", IMAGE_FILTER_PIXELATE, CONST_CS | CONST_PERSISTENT);
  1083. /* End Section Filters */
  1084. #ifdef GD_VERSION_STRING
  1085. REGISTER_STRING_CONSTANT("GD_VERSION", GD_VERSION_STRING, CONST_CS | CONST_PERSISTENT);
  1086. #endif
  1087. #if defined(GD_MAJOR_VERSION) && defined(GD_MINOR_VERSION) && defined(GD_RELEASE_VERSION) && defined(GD_EXTRA_VERSION)
  1088. REGISTER_LONG_CONSTANT("GD_MAJOR_VERSION", GD_MAJOR_VERSION, CONST_CS | CONST_PERSISTENT);
  1089. REGISTER_LONG_CONSTANT("GD_MINOR_VERSION", GD_MINOR_VERSION, CONST_CS | CONST_PERSISTENT);
  1090. REGISTER_LONG_CONSTANT("GD_RELEASE_VERSION", GD_RELEASE_VERSION, CONST_CS | CONST_PERSISTENT);
  1091. REGISTER_STRING_CONSTANT("GD_EXTRA_VERSION", GD_EXTRA_VERSION, CONST_CS | CONST_PERSISTENT);
  1092. #endif
  1093. #ifdef HAVE_GD_PNG
  1094. /*
  1095. * cannot include #include "png.h"
  1096. * /usr/include/pngconf.h:310:2: error: #error png.h already includes setjmp.h with some additional fixup.
  1097. * as error, use the values for now...
  1098. */
  1099. REGISTER_LONG_CONSTANT("PNG_NO_FILTER", 0x00, CONST_CS | CONST_PERSISTENT);
  1100. REGISTER_LONG_CONSTANT("PNG_FILTER_NONE", 0x08, CONST_CS | CONST_PERSISTENT);
  1101. REGISTER_LONG_CONSTANT("PNG_FILTER_SUB", 0x10, CONST_CS | CONST_PERSISTENT);
  1102. REGISTER_LONG_CONSTANT("PNG_FILTER_UP", 0x20, CONST_CS | CONST_PERSISTENT);
  1103. REGISTER_LONG_CONSTANT("PNG_FILTER_AVG", 0x40, CONST_CS | CONST_PERSISTENT);
  1104. REGISTER_LONG_CONSTANT("PNG_FILTER_PAETH", 0x80, CONST_CS | CONST_PERSISTENT);
  1105. REGISTER_LONG_CONSTANT("PNG_ALL_FILTERS", 0x08 | 0x10 | 0x20 | 0x40 | 0x80, CONST_CS | CONST_PERSISTENT);
  1106. #endif
  1107. return SUCCESS;
  1108. }
  1109. /* }}} */
  1110. /* {{{ PHP_RSHUTDOWN_FUNCTION
  1111. */
  1112. #if HAVE_GD_FREETYPE && HAVE_LIBFREETYPE
  1113. PHP_RSHUTDOWN_FUNCTION(gd)
  1114. {
  1115. gdFontCacheShutdown();
  1116. return SUCCESS;
  1117. }
  1118. #endif
  1119. /* }}} */
  1120. #if defined(HAVE_GD_BUNDLED)
  1121. #define PHP_GD_VERSION_STRING "bundled (2.1.0 compatible)"
  1122. #else
  1123. # define PHP_GD_VERSION_STRING GD_VERSION_STRING
  1124. #endif
  1125. /* {{{ PHP_MINFO_FUNCTION
  1126. */
  1127. PHP_MINFO_FUNCTION(gd)
  1128. {
  1129. php_info_print_table_start();
  1130. php_info_print_table_row(2, "GD Support", "enabled");
  1131. /* need to use a PHPAPI function here because it is external module in windows */
  1132. #if defined(HAVE_GD_BUNDLED)
  1133. php_info_print_table_row(2, "GD Version", PHP_GD_VERSION_STRING);
  1134. #else
  1135. php_info_print_table_row(2, "GD headers Version", PHP_GD_VERSION_STRING);
  1136. #if defined(HAVE_GD_LIBVERSION)
  1137. php_info_print_table_row(2, "GD library Version", gdVersionString());
  1138. #endif
  1139. #endif
  1140. #ifdef ENABLE_GD_TTF
  1141. php_info_print_table_row(2, "FreeType Support", "enabled");
  1142. #if HAVE_LIBFREETYPE
  1143. php_info_print_table_row(2, "FreeType Linkage", "with freetype");
  1144. {
  1145. char tmp[256];
  1146. #ifdef FREETYPE_PATCH
  1147. snprintf(tmp, sizeof(tmp), "%d.%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR, FREETYPE_PATCH);
  1148. #elif defined(FREETYPE_MAJOR)
  1149. snprintf(tmp, sizeof(tmp), "%d.%d", FREETYPE_MAJOR, FREETYPE_MINOR);
  1150. #else
  1151. snprintf(tmp, sizeof(tmp), "1.x");
  1152. #endif
  1153. php_info_print_table_row(2, "FreeType Version", tmp);
  1154. }
  1155. #else
  1156. php_info_print_table_row(2, "FreeType Linkage", "with unknown library");
  1157. #endif
  1158. #endif
  1159. #ifdef HAVE_LIBT1
  1160. php_info_print_table_row(2, "T1Lib Support", "enabled");
  1161. #endif
  1162. php_info_print_table_row(2, "GIF Read Support", "enabled");
  1163. php_info_print_table_row(2, "GIF Create Support", "enabled");
  1164. #ifdef HAVE_GD_JPG
  1165. {
  1166. php_info_print_table_row(2, "JPEG Support", "enabled");
  1167. php_info_print_table_row(2, "libJPEG Version", gdJpegGetVersionString());
  1168. }
  1169. #endif
  1170. #ifdef HAVE_GD_PNG
  1171. php_info_print_table_row(2, "PNG Support", "enabled");
  1172. php_info_print_table_row(2, "libPNG Version", gdPngGetVersionString());
  1173. #endif
  1174. php_info_print_table_row(2, "WBMP Support", "enabled");
  1175. #if defined(HAVE_GD_XPM)
  1176. php_info_print_table_row(2, "XPM Support", "enabled");
  1177. {
  1178. char tmp[12];
  1179. snprintf(tmp, sizeof(tmp), "%d", XpmLibraryVersion());
  1180. php_info_print_table_row(2, "libXpm Version", tmp);
  1181. }
  1182. #endif
  1183. php_info_print_table_row(2, "XBM Support", "enabled");
  1184. #if defined(USE_GD_JISX0208)
  1185. php_info_print_table_row(2, "JIS-mapped Japanese Font Support", "enabled");
  1186. #endif
  1187. #ifdef HAVE_GD_WEBP
  1188. php_info_print_table_row(2, "WebP Support", "enabled");
  1189. #endif
  1190. php_info_print_table_end();
  1191. DISPLAY_INI_ENTRIES();
  1192. }
  1193. /* }}} */
  1194. /* {{{ proto array gd_info()
  1195. */
  1196. PHP_FUNCTION(gd_info)
  1197. {
  1198. if (zend_parse_parameters_none() == FAILURE) {
  1199. RETURN_FALSE;
  1200. }
  1201. array_init(return_value);
  1202. add_assoc_string(return_value, "GD Version", PHP_GD_VERSION_STRING, 1);
  1203. #ifdef ENABLE_GD_TTF
  1204. add_assoc_bool(return_value, "FreeType Support", 1);
  1205. #if HAVE_LIBFREETYPE
  1206. add_assoc_string(return_value, "FreeType Linkage", "with freetype", 1);
  1207. #else
  1208. add_assoc_string(return_value, "FreeType Linkage", "with unknown library", 1);
  1209. #endif
  1210. #else
  1211. add_assoc_bool(return_value, "FreeType Support", 0);
  1212. #endif
  1213. #ifdef HAVE_LIBT1
  1214. add_assoc_bool(return_value, "T1Lib Support", 1);
  1215. #else
  1216. add_assoc_bool(return_value, "T1Lib Support", 0);
  1217. #endif
  1218. add_assoc_bool(return_value, "GIF Read Support", 1);
  1219. add_assoc_bool(return_value, "GIF Create Support", 1);
  1220. #ifdef HAVE_GD_JPG
  1221. add_assoc_bool(return_value, "JPEG Support", 1);
  1222. #else
  1223. add_assoc_bool(return_value, "JPEG Support", 0);
  1224. #endif
  1225. #ifdef HAVE_GD_PNG
  1226. add_assoc_bool(return_value, "PNG Support", 1);
  1227. #else
  1228. add_assoc_bool(return_value, "PNG Support", 0);
  1229. #endif
  1230. add_assoc_bool(return_value, "WBMP Support", 1);
  1231. #if defined(HAVE_GD_XPM)
  1232. add_assoc_bool(return_value, "XPM Support", 1);
  1233. #else
  1234. add_assoc_bool(return_value, "XPM Support", 0);
  1235. #endif
  1236. add_assoc_bool(return_value, "XBM Support", 1);
  1237. #ifdef HAVE_GD_WEBP
  1238. add_assoc_bool(return_value, "WebP Support", 1);
  1239. #else
  1240. add_assoc_bool(return_value, "WebP Support", 0);
  1241. #endif
  1242. #if defined(USE_GD_JISX0208)
  1243. add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 1);
  1244. #else
  1245. add_assoc_bool(return_value, "JIS-mapped Japanese Font Support", 0);
  1246. #endif
  1247. }
  1248. /* }}} */
  1249. /* Need this for cpdf. See also comment in file.c php3i_get_le_fp() */
  1250. PHP_GD_API int phpi_get_le_gd(void)
  1251. {
  1252. return le_gd;
  1253. }
  1254. /* }}} */
  1255. #define FLIPWORD(a) (((a & 0xff000000) >> 24) | ((a & 0x00ff0000) >> 8) | ((a & 0x0000ff00) << 8) | ((a & 0x000000ff) << 24))
  1256. /* {{{ proto int imageloadfont(string filename)
  1257. Load a new font */
  1258. PHP_FUNCTION(imageloadfont)
  1259. {
  1260. char *file;
  1261. int file_name, hdr_size = sizeof(gdFont) - sizeof(char *);
  1262. int ind, body_size, n = 0, b, i, body_size_check;
  1263. gdFontPtr font;
  1264. php_stream *stream;
  1265. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &file, &file_name) == FAILURE) {
  1266. return;
  1267. }
  1268. stream = php_stream_open_wrapper(file, "rb", IGNORE_PATH | IGNORE_URL_WIN | REPORT_ERRORS, NULL);
  1269. if (stream == NULL) {
  1270. RETURN_FALSE;
  1271. }
  1272. /* Only supports a architecture-dependent binary dump format
  1273. * at the moment.
  1274. * The file format is like this on machines with 32-byte integers:
  1275. *
  1276. * byte 0-3: (int) number of characters in the font
  1277. * byte 4-7: (int) value of first character in the font (often 32, space)
  1278. * byte 8-11: (int) pixel width of each character
  1279. * byte 12-15: (int) pixel height of each character
  1280. * bytes 16-: (char) array with character data, one byte per pixel
  1281. * in each character, for a total of
  1282. * (nchars*width*height) bytes.
  1283. */
  1284. font = (gdFontPtr) emalloc(sizeof(gdFont));
  1285. b = 0;
  1286. while (b < hdr_size && (n = php_stream_read(stream, (char*)&font[b], hdr_size - b))) {
  1287. b += n;
  1288. }
  1289. if (!n) {
  1290. efree(font);
  1291. if (php_stream_eof(stream)) {
  1292. php_error_docref(NULL TSRMLS_CC, E_WARNING, "End of file while reading header");
  1293. } else {
  1294. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading header");
  1295. }
  1296. php_stream_close(stream);
  1297. RETURN_FALSE;
  1298. }
  1299. i = php_stream_tell(stream);
  1300. php_stream_seek(stream, 0, SEEK_END);
  1301. body_size_check = php_stream_tell(stream) - hdr_size;
  1302. php_stream_seek(stream, i, SEEK_SET);
  1303. body_size = font->w * font->h * font->nchars;
  1304. if (body_size != body_size_check) {
  1305. font->w = FLIPWORD(font->w);
  1306. font->h = FLIPWORD(font->h);
  1307. font->nchars = FLIPWORD(font->nchars);
  1308. body_size = font->w * font->h * font->nchars;
  1309. }
  1310. if (overflow2(font->nchars, font->h) || overflow2(font->nchars * font->h, font->w )) {
  1311. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading font, invalid font header");
  1312. efree(font);
  1313. php_stream_close(stream);
  1314. RETURN_FALSE;
  1315. }
  1316. if (body_size != body_size_check) {
  1317. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading font");
  1318. efree(font);
  1319. php_stream_close(stream);
  1320. RETURN_FALSE;
  1321. }
  1322. font->data = emalloc(body_size);
  1323. b = 0;
  1324. while (b < body_size && (n = php_stream_read(stream, &font->data[b], body_size - b))) {
  1325. b += n;
  1326. }
  1327. if (!n) {
  1328. efree(font->data);
  1329. efree(font);
  1330. if (php_stream_eof(stream)) {
  1331. php_error_docref(NULL TSRMLS_CC, E_WARNING, "End of file while reading body");
  1332. } else {
  1333. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error while reading body");
  1334. }
  1335. php_stream_close(stream);
  1336. RETURN_FALSE;
  1337. }
  1338. php_stream_close(stream);
  1339. /* Adding 5 to the font index so we will never have font indices
  1340. * that overlap with the old fonts (with indices 1-5). The first
  1341. * list index given out is always 1.
  1342. */
  1343. ind = 5 + zend_list_insert(font, le_gd_font TSRMLS_CC);
  1344. RETURN_LONG(ind);
  1345. }
  1346. /* }}} */
  1347. /* {{{ proto bool imagesetstyle(resource im, array styles)
  1348. Set the line drawing styles for use with imageline and IMG_COLOR_STYLED. */
  1349. PHP_FUNCTION(imagesetstyle)
  1350. {
  1351. zval *IM, *styles;
  1352. gdImagePtr im;
  1353. int * stylearr;
  1354. int index;
  1355. HashPosition pos;
  1356. int num_styles;
  1357. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &IM, &styles) == FAILURE) {
  1358. return;
  1359. }
  1360. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1361. num_styles = zend_hash_num_elements(HASH_OF(styles));
  1362. if (num_styles == 0) {
  1363. php_error_docref(NULL TSRMLS_CC, E_WARNING, "styles array must not be empty");
  1364. RETURN_FALSE;
  1365. }
  1366. /* copy the style values in the stylearr */
  1367. stylearr = safe_emalloc(sizeof(int), num_styles, 0);
  1368. zend_hash_internal_pointer_reset_ex(HASH_OF(styles), &pos);
  1369. for (index = 0;; zend_hash_move_forward_ex(HASH_OF(styles), &pos)) {
  1370. zval ** item;
  1371. if (zend_hash_get_current_data_ex(HASH_OF(styles), (void **) &item, &pos) == FAILURE) {
  1372. break;
  1373. }
  1374. if (Z_TYPE_PP(item) != IS_LONG) {
  1375. zval lval;
  1376. lval = **item;
  1377. zval_copy_ctor(&lval);
  1378. convert_to_long(&lval);
  1379. stylearr[index++] = Z_LVAL(lval);
  1380. } else {
  1381. stylearr[index++] = Z_LVAL_PP(item);
  1382. }
  1383. }
  1384. gdImageSetStyle(im, stylearr, index);
  1385. efree(stylearr);
  1386. RETURN_TRUE;
  1387. }
  1388. /* }}} */
  1389. /* {{{ proto resource imagecreatetruecolor(int x_size, int y_size)
  1390. Create a new true color image */
  1391. PHP_FUNCTION(imagecreatetruecolor)
  1392. {
  1393. long x_size, y_size;
  1394. gdImagePtr im;
  1395. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &x_size, &y_size) == FAILURE) {
  1396. return;
  1397. }
  1398. if (x_size <= 0 || y_size <= 0 || x_size >= INT_MAX || y_size >= INT_MAX) {
  1399. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid image dimensions");
  1400. RETURN_FALSE;
  1401. }
  1402. im = gdImageCreateTrueColor(x_size, y_size);
  1403. if (!im) {
  1404. RETURN_FALSE;
  1405. }
  1406. ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
  1407. }
  1408. /* }}} */
  1409. /* {{{ proto bool imageistruecolor(resource im)
  1410. return true if the image uses truecolor */
  1411. PHP_FUNCTION(imageistruecolor)
  1412. {
  1413. zval *IM;
  1414. gdImagePtr im;
  1415. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &IM) == FAILURE) {
  1416. return;
  1417. }
  1418. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1419. RETURN_BOOL(im->trueColor);
  1420. }
  1421. /* }}} */
  1422. /* {{{ proto void imagetruecolortopalette(resource im, bool ditherFlag, int colorsWanted)
  1423. Convert a true color image to a palette based image with a number of colors, optionally using dithering. */
  1424. PHP_FUNCTION(imagetruecolortopalette)
  1425. {
  1426. zval *IM;
  1427. zend_bool dither;
  1428. long ncolors;
  1429. gdImagePtr im;
  1430. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rbl", &IM, &dither, &ncolors) == FAILURE) {
  1431. return;
  1432. }
  1433. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1434. if (ncolors <= 0 || ncolors > INT_MAX) {
  1435. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of colors has to be greater than zero and no more than %d", INT_MAX);
  1436. RETURN_FALSE;
  1437. }
  1438. gdImageTrueColorToPalette(im, dither, (int)ncolors);
  1439. RETURN_TRUE;
  1440. }
  1441. /* }}} */
  1442. /* {{{ proto void imagepalettetotruecolor(resource im)
  1443. Convert a palette based image to a true color image. */
  1444. PHP_FUNCTION(imagepalettetotruecolor)
  1445. {
  1446. zval *IM;
  1447. gdImagePtr im;
  1448. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &IM) == FAILURE) {
  1449. return;
  1450. }
  1451. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1452. if (gdImagePaletteToTrueColor(im) == 0) {
  1453. RETURN_FALSE;
  1454. }
  1455. RETURN_TRUE;
  1456. }
  1457. /* }}} */
  1458. /* {{{ proto bool imagecolormatch(resource im1, resource im2)
  1459. Makes the colors of the palette version of an image more closely match the true color version */
  1460. PHP_FUNCTION(imagecolormatch)
  1461. {
  1462. zval *IM1, *IM2;
  1463. gdImagePtr im1, im2;
  1464. int result;
  1465. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &IM1, &IM2) == FAILURE) {
  1466. return;
  1467. }
  1468. ZEND_FETCH_RESOURCE(im1, gdImagePtr, &IM1, -1, "Image", le_gd);
  1469. ZEND_FETCH_RESOURCE(im2, gdImagePtr, &IM2, -1, "Image", le_gd);
  1470. result = gdImageColorMatch(im1, im2);
  1471. switch (result) {
  1472. case -1:
  1473. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Image1 must be TrueColor" );
  1474. RETURN_FALSE;
  1475. break;
  1476. case -2:
  1477. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Image2 must be Palette" );
  1478. RETURN_FALSE;
  1479. break;
  1480. case -3:
  1481. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Image1 and Image2 must be the same size" );
  1482. RETURN_FALSE;
  1483. break;
  1484. case -4:
  1485. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Image2 must have at least one color" );
  1486. RETURN_FALSE;
  1487. break;
  1488. }
  1489. RETURN_TRUE;
  1490. }
  1491. /* }}} */
  1492. /* {{{ proto bool imagesetthickness(resource im, int thickness)
  1493. Set line thickness for drawing lines, ellipses, rectangles, polygons etc. */
  1494. PHP_FUNCTION(imagesetthickness)
  1495. {
  1496. zval *IM;
  1497. long thick;
  1498. gdImagePtr im;
  1499. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &IM, &thick) == FAILURE) {
  1500. return;
  1501. }
  1502. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1503. gdImageSetThickness(im, thick);
  1504. RETURN_TRUE;
  1505. }
  1506. /* }}} */
  1507. /* {{{ proto bool imagefilledellipse(resource im, int cx, int cy, int w, int h, int color)
  1508. Draw an ellipse */
  1509. PHP_FUNCTION(imagefilledellipse)
  1510. {
  1511. zval *IM;
  1512. long cx, cy, w, h, color;
  1513. gdImagePtr im;
  1514. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &IM, &cx, &cy, &w, &h, &color) == FAILURE) {
  1515. return;
  1516. }
  1517. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1518. gdImageFilledEllipse(im, cx, cy, w, h, color);
  1519. RETURN_TRUE;
  1520. }
  1521. /* }}} */
  1522. /* {{{ proto bool imagefilledarc(resource im, int cx, int cy, int w, int h, int s, int e, int col, int style)
  1523. Draw a filled partial ellipse */
  1524. PHP_FUNCTION(imagefilledarc)
  1525. {
  1526. zval *IM;
  1527. long cx, cy, w, h, ST, E, col, style;
  1528. gdImagePtr im;
  1529. int e, st;
  1530. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllllll", &IM, &cx, &cy, &w, &h, &ST, &E, &col, &style) == FAILURE) {
  1531. return;
  1532. }
  1533. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1534. e = E;
  1535. if (e < 0) {
  1536. e %= 360;
  1537. }
  1538. st = ST;
  1539. if (st < 0) {
  1540. st %= 360;
  1541. }
  1542. gdImageFilledArc(im, cx, cy, w, h, st, e, col, style);
  1543. RETURN_TRUE;
  1544. }
  1545. /* }}} */
  1546. /* {{{ proto bool imagealphablending(resource im, bool on)
  1547. Turn alpha blending mode on or off for the given image */
  1548. PHP_FUNCTION(imagealphablending)
  1549. {
  1550. zval *IM;
  1551. zend_bool blend;
  1552. gdImagePtr im;
  1553. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rb", &IM, &blend) == FAILURE) {
  1554. return;
  1555. }
  1556. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1557. gdImageAlphaBlending(im, blend);
  1558. RETURN_TRUE;
  1559. }
  1560. /* }}} */
  1561. /* {{{ proto bool imagesavealpha(resource im, bool on)
  1562. Include alpha channel to a saved image */
  1563. PHP_FUNCTION(imagesavealpha)
  1564. {
  1565. zval *IM;
  1566. zend_bool save;
  1567. gdImagePtr im;
  1568. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rb", &IM, &save) == FAILURE) {
  1569. return;
  1570. }
  1571. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1572. gdImageSaveAlpha(im, save);
  1573. RETURN_TRUE;
  1574. }
  1575. /* }}} */
  1576. /* {{{ proto bool imagelayereffect(resource im, int effect)
  1577. Set the alpha blending flag to use the bundled libgd layering effects */
  1578. PHP_FUNCTION(imagelayereffect)
  1579. {
  1580. zval *IM;
  1581. long effect;
  1582. gdImagePtr im;
  1583. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &IM, &effect) == FAILURE) {
  1584. return;
  1585. }
  1586. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1587. gdImageAlphaBlending(im, effect);
  1588. RETURN_TRUE;
  1589. }
  1590. /* }}} */
  1591. /* {{{ proto int imagecolorallocatealpha(resource im, int red, int green, int blue, int alpha)
  1592. Allocate a color with an alpha level. Works for true color and palette based images */
  1593. PHP_FUNCTION(imagecolorallocatealpha)
  1594. {
  1595. zval *IM;
  1596. long red, green, blue, alpha;
  1597. gdImagePtr im;
  1598. int ct = (-1);
  1599. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &IM, &red, &green, &blue, &alpha) == FAILURE) {
  1600. RETURN_FALSE;
  1601. }
  1602. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1603. ct = gdImageColorAllocateAlpha(im, red, green, blue, alpha);
  1604. if (ct < 0) {
  1605. RETURN_FALSE;
  1606. }
  1607. RETURN_LONG((long)ct);
  1608. }
  1609. /* }}} */
  1610. /* {{{ proto int imagecolorresolvealpha(resource im, int red, int green, int blue, int alpha)
  1611. Resolve/Allocate a colour with an alpha level. Works for true colour and palette based images */
  1612. PHP_FUNCTION(imagecolorresolvealpha)
  1613. {
  1614. zval *IM;
  1615. long red, green, blue, alpha;
  1616. gdImagePtr im;
  1617. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &IM, &red, &green, &blue, &alpha) == FAILURE) {
  1618. return;
  1619. }
  1620. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1621. RETURN_LONG(gdImageColorResolveAlpha(im, red, green, blue, alpha));
  1622. }
  1623. /* }}} */
  1624. /* {{{ proto int imagecolorclosestalpha(resource im, int red, int green, int blue, int alpha)
  1625. Find the closest matching colour with alpha transparency */
  1626. PHP_FUNCTION(imagecolorclosestalpha)
  1627. {
  1628. zval *IM;
  1629. long red, green, blue, alpha;
  1630. gdImagePtr im;
  1631. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &IM, &red, &green, &blue, &alpha) == FAILURE) {
  1632. return;
  1633. }
  1634. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1635. RETURN_LONG(gdImageColorClosestAlpha(im, red, green, blue, alpha));
  1636. }
  1637. /* }}} */
  1638. /* {{{ proto int imagecolorexactalpha(resource im, int red, int green, int blue, int alpha)
  1639. Find exact match for colour with transparency */
  1640. PHP_FUNCTION(imagecolorexactalpha)
  1641. {
  1642. zval *IM;
  1643. long red, green, blue, alpha;
  1644. gdImagePtr im;
  1645. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &IM, &red, &green, &blue, &alpha) == FAILURE) {
  1646. return;
  1647. }
  1648. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1649. RETURN_LONG(gdImageColorExactAlpha(im, red, green, blue, alpha));
  1650. }
  1651. /* }}} */
  1652. /* {{{ proto bool imagecopyresampled(resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h)
  1653. Copy and resize part of an image using resampling to help ensure clarity */
  1654. PHP_FUNCTION(imagecopyresampled)
  1655. {
  1656. zval *SIM, *DIM;
  1657. long SX, SY, SW, SH, DX, DY, DW, DH;
  1658. gdImagePtr im_dst, im_src;
  1659. int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
  1660. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrllllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) {
  1661. return;
  1662. }
  1663. ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, &DIM, -1, "Image", le_gd);
  1664. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  1665. srcX = SX;
  1666. srcY = SY;
  1667. srcH = SH;
  1668. srcW = SW;
  1669. dstX = DX;
  1670. dstY = DY;
  1671. dstH = DH;
  1672. dstW = DW;
  1673. gdImageCopyResampled(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
  1674. RETURN_TRUE;
  1675. }
  1676. /* }}} */
  1677. #ifdef PHP_WIN32
  1678. /* {{{ proto resource imagegrabwindow(int window_handle [, int client_area])
  1679. Grab a window or its client area using a windows handle (HWND property in COM instance) */
  1680. PHP_FUNCTION(imagegrabwindow)
  1681. {
  1682. HWND window;
  1683. long client_area = 0;
  1684. RECT rc = {0};
  1685. RECT rc_win = {0};
  1686. int Width, Height;
  1687. HDC hdc;
  1688. HDC memDC;
  1689. HBITMAP memBM;
  1690. HBITMAP hOld;
  1691. HINSTANCE handle;
  1692. long lwindow_handle;
  1693. typedef BOOL (WINAPI *tPrintWindow)(HWND, HDC,UINT);
  1694. tPrintWindow pPrintWindow = 0;
  1695. gdImagePtr im;
  1696. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &lwindow_handle, &client_area) == FAILURE) {
  1697. RETURN_FALSE;
  1698. }
  1699. window = (HWND) lwindow_handle;
  1700. if (!IsWindow(window)) {
  1701. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid window handle");
  1702. RETURN_FALSE;
  1703. }
  1704. hdc = GetDC(0);
  1705. if (client_area) {
  1706. GetClientRect(window, &rc);
  1707. Width = rc.right;
  1708. Height = rc.bottom;
  1709. } else {
  1710. GetWindowRect(window, &rc);
  1711. Width = rc.right - rc.left;
  1712. Height = rc.bottom - rc.top;
  1713. }
  1714. Width = (Width/4)*4;
  1715. memDC = CreateCompatibleDC(hdc);
  1716. memBM = CreateCompatibleBitmap(hdc, Width, Height);
  1717. hOld = (HBITMAP) SelectObject (memDC, memBM);
  1718. handle = LoadLibrary("User32.dll");
  1719. if ( handle == 0 ) {
  1720. goto clean;
  1721. }
  1722. pPrintWindow = (tPrintWindow) GetProcAddress(handle, "PrintWindow");
  1723. if ( pPrintWindow ) {
  1724. pPrintWindow(window, memDC, (UINT) client_area);
  1725. } else {
  1726. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Windows API too old");
  1727. goto clean;
  1728. }
  1729. FreeLibrary(handle);
  1730. im = gdImageCreateTrueColor(Width, Height);
  1731. if (im) {
  1732. int x,y;
  1733. for (y=0; y <= Height; y++) {
  1734. for (x=0; x <= Width; x++) {
  1735. int c = GetPixel(memDC, x,y);
  1736. gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
  1737. }
  1738. }
  1739. }
  1740. clean:
  1741. SelectObject(memDC,hOld);
  1742. DeleteObject(memBM);
  1743. DeleteDC(memDC);
  1744. ReleaseDC( 0, hdc );
  1745. if (!im) {
  1746. RETURN_FALSE;
  1747. } else {
  1748. ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
  1749. }
  1750. }
  1751. /* }}} */
  1752. /* {{{ proto resource imagegrabscreen()
  1753. Grab a screenshot */
  1754. PHP_FUNCTION(imagegrabscreen)
  1755. {
  1756. HWND window = GetDesktopWindow();
  1757. RECT rc = {0};
  1758. int Width, Height;
  1759. HDC hdc;
  1760. HDC memDC;
  1761. HBITMAP memBM;
  1762. HBITMAP hOld;
  1763. typedef BOOL (WINAPI *tPrintWindow)(HWND, HDC,UINT);
  1764. tPrintWindow pPrintWindow = 0;
  1765. gdImagePtr im;
  1766. hdc = GetDC(0);
  1767. if (zend_parse_parameters_none() == FAILURE) {
  1768. return;
  1769. }
  1770. if (!hdc) {
  1771. RETURN_FALSE;
  1772. }
  1773. GetWindowRect(window, &rc);
  1774. Width = rc.right - rc.left;
  1775. Height = rc.bottom - rc.top;
  1776. Width = (Width/4)*4;
  1777. memDC = CreateCompatibleDC(hdc);
  1778. memBM = CreateCompatibleBitmap(hdc, Width, Height);
  1779. hOld = (HBITMAP) SelectObject (memDC, memBM);
  1780. BitBlt( memDC, 0, 0, Width, Height , hdc, rc.left, rc.top , SRCCOPY );
  1781. im = gdImageCreateTrueColor(Width, Height);
  1782. if (im) {
  1783. int x,y;
  1784. for (y=0; y <= Height; y++) {
  1785. for (x=0; x <= Width; x++) {
  1786. int c = GetPixel(memDC, x,y);
  1787. gdImageSetPixel(im, x, y, gdTrueColor(GetRValue(c), GetGValue(c), GetBValue(c)));
  1788. }
  1789. }
  1790. }
  1791. SelectObject(memDC,hOld);
  1792. DeleteObject(memBM);
  1793. DeleteDC(memDC);
  1794. ReleaseDC( 0, hdc );
  1795. if (!im) {
  1796. RETURN_FALSE;
  1797. } else {
  1798. ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
  1799. }
  1800. }
  1801. /* }}} */
  1802. #endif /* PHP_WIN32 */
  1803. /* {{{ proto resource imagerotate(resource src_im, float angle, int bgdcolor [, int ignoretransparent])
  1804. Rotate an image using a custom angle */
  1805. PHP_FUNCTION(imagerotate)
  1806. {
  1807. zval *SIM;
  1808. gdImagePtr im_dst, im_src;
  1809. double degrees;
  1810. long color;
  1811. long ignoretransparent = 0;
  1812. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rdl|l", &SIM, &degrees, &color, &ignoretransparent) == FAILURE) {
  1813. RETURN_FALSE;
  1814. }
  1815. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  1816. im_dst = gdImageRotateInterpolated(im_src, (const float)degrees, color);
  1817. if (im_dst != NULL) {
  1818. ZEND_REGISTER_RESOURCE(return_value, im_dst, le_gd);
  1819. } else {
  1820. RETURN_FALSE;
  1821. }
  1822. }
  1823. /* }}} */
  1824. /* {{{ proto bool imagesettile(resource image, resource tile)
  1825. Set the tile image to $tile when filling $image with the "IMG_COLOR_TILED" color */
  1826. PHP_FUNCTION(imagesettile)
  1827. {
  1828. zval *IM, *TILE;
  1829. gdImagePtr im, tile;
  1830. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &IM, &TILE) == FAILURE) {
  1831. return;
  1832. }
  1833. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1834. ZEND_FETCH_RESOURCE(tile, gdImagePtr, &TILE, -1, "Image", le_gd);
  1835. gdImageSetTile(im, tile);
  1836. RETURN_TRUE;
  1837. }
  1838. /* }}} */
  1839. /* {{{ proto bool imagesetbrush(resource image, resource brush)
  1840. Set the brush image to $brush when filling $image with the "IMG_COLOR_BRUSHED" color */
  1841. PHP_FUNCTION(imagesetbrush)
  1842. {
  1843. zval *IM, *TILE;
  1844. gdImagePtr im, tile;
  1845. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &IM, &TILE) == FAILURE) {
  1846. return;
  1847. }
  1848. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  1849. ZEND_FETCH_RESOURCE(tile, gdImagePtr, &TILE, -1, "Image", le_gd);
  1850. gdImageSetBrush(im, tile);
  1851. RETURN_TRUE;
  1852. }
  1853. /* }}} */
  1854. /* {{{ proto resource imagecreate(int x_size, int y_size)
  1855. Create a new image */
  1856. PHP_FUNCTION(imagecreate)
  1857. {
  1858. long x_size, y_size;
  1859. gdImagePtr im;
  1860. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &x_size, &y_size) == FAILURE) {
  1861. return;
  1862. }
  1863. if (x_size <= 0 || y_size <= 0 || x_size >= INT_MAX || y_size >= INT_MAX) {
  1864. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid image dimensions");
  1865. RETURN_FALSE;
  1866. }
  1867. im = gdImageCreate(x_size, y_size);
  1868. if (!im) {
  1869. RETURN_FALSE;
  1870. }
  1871. ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
  1872. }
  1873. /* }}} */
  1874. /* {{{ proto int imagetypes(void)
  1875. Return the types of images supported in a bitfield - 1=GIF, 2=JPEG, 4=PNG, 8=WBMP, 16=XPM */
  1876. PHP_FUNCTION(imagetypes)
  1877. {
  1878. int ret=0;
  1879. ret = 1;
  1880. #ifdef HAVE_GD_JPG
  1881. ret |= 2;
  1882. #endif
  1883. #ifdef HAVE_GD_PNG
  1884. ret |= 4;
  1885. #endif
  1886. ret |= 8;
  1887. #if defined(HAVE_GD_XPM)
  1888. ret |= 16;
  1889. #endif
  1890. #ifdef HAVE_GD_WEBP
  1891. ret |= 32;
  1892. #endif
  1893. if (zend_parse_parameters_none() == FAILURE) {
  1894. return;
  1895. }
  1896. RETURN_LONG(ret);
  1897. }
  1898. /* }}} */
  1899. /* {{{ _php_ctx_getmbi
  1900. */
  1901. static int _php_ctx_getmbi(gdIOCtx *ctx)
  1902. {
  1903. int i, mbi = 0;
  1904. do {
  1905. i = (ctx->getC)(ctx);
  1906. if (i < 0) {
  1907. return -1;
  1908. }
  1909. mbi = (mbi << 7) | (i & 0x7f);
  1910. } while (i & 0x80);
  1911. return mbi;
  1912. }
  1913. /* }}} */
  1914. /* {{{ _php_image_type
  1915. */
  1916. static const char php_sig_gd2[3] = {'g', 'd', '2'};
  1917. static int _php_image_type (char data[8])
  1918. {
  1919. /* Based on ext/standard/image.c */
  1920. if (data == NULL) {
  1921. return -1;
  1922. }
  1923. if (!memcmp(data, php_sig_gd2, 3)) {
  1924. return PHP_GDIMG_TYPE_GD2;
  1925. } else if (!memcmp(data, php_sig_jpg, 3)) {
  1926. return PHP_GDIMG_TYPE_JPG;
  1927. } else if (!memcmp(data, php_sig_png, 3)) {
  1928. if (!memcmp(data, php_sig_png, 8)) {
  1929. return PHP_GDIMG_TYPE_PNG;
  1930. }
  1931. } else if (!memcmp(data, php_sig_gif, 3)) {
  1932. return PHP_GDIMG_TYPE_GIF;
  1933. }
  1934. else {
  1935. gdIOCtx *io_ctx;
  1936. io_ctx = gdNewDynamicCtxEx(8, data, 0);
  1937. if (io_ctx) {
  1938. if (_php_ctx_getmbi(io_ctx) == 0 && _php_ctx_getmbi(io_ctx) >= 0) {
  1939. io_ctx->gd_free(io_ctx);
  1940. return PHP_GDIMG_TYPE_WBM;
  1941. } else {
  1942. io_ctx->gd_free(io_ctx);
  1943. }
  1944. }
  1945. }
  1946. return -1;
  1947. }
  1948. /* }}} */
  1949. /* {{{ _php_image_create_from_string
  1950. */
  1951. gdImagePtr _php_image_create_from_string(zval **data, char *tn, gdImagePtr (*ioctx_func_p)() TSRMLS_DC)
  1952. {
  1953. gdImagePtr im;
  1954. gdIOCtx *io_ctx;
  1955. io_ctx = gdNewDynamicCtxEx(Z_STRLEN_PP(data), Z_STRVAL_PP(data), 0);
  1956. if (!io_ctx) {
  1957. return NULL;
  1958. }
  1959. im = (*ioctx_func_p)(io_ctx);
  1960. if (!im) {
  1961. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Passed data is not in '%s' format", tn);
  1962. io_ctx->gd_free(io_ctx);
  1963. return NULL;
  1964. }
  1965. io_ctx->gd_free(io_ctx);
  1966. return im;
  1967. }
  1968. /* }}} */
  1969. /* {{{ proto resource imagecreatefromstring(string image)
  1970. Create a new image from the image stream in the string */
  1971. PHP_FUNCTION(imagecreatefromstring)
  1972. {
  1973. zval **data;
  1974. gdImagePtr im;
  1975. int imtype;
  1976. char sig[8];
  1977. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &data) == FAILURE) {
  1978. return;
  1979. }
  1980. convert_to_string_ex(data);
  1981. if (Z_STRLEN_PP(data) < 8) {
  1982. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string or invalid image");
  1983. RETURN_FALSE;
  1984. }
  1985. memcpy(sig, Z_STRVAL_PP(data), 8);
  1986. imtype = _php_image_type(sig);
  1987. switch (imtype) {
  1988. case PHP_GDIMG_TYPE_JPG:
  1989. #ifdef HAVE_GD_JPG
  1990. im = _php_image_create_from_string(data, "JPEG", gdImageCreateFromJpegCtx TSRMLS_CC);
  1991. #else
  1992. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No JPEG support in this PHP build");
  1993. RETURN_FALSE;
  1994. #endif
  1995. break;
  1996. case PHP_GDIMG_TYPE_PNG:
  1997. #ifdef HAVE_GD_PNG
  1998. im = _php_image_create_from_string(data, "PNG", gdImageCreateFromPngCtx TSRMLS_CC);
  1999. #else
  2000. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No PNG support in this PHP build");
  2001. RETURN_FALSE;
  2002. #endif
  2003. break;
  2004. case PHP_GDIMG_TYPE_GIF:
  2005. im = _php_image_create_from_string(data, "GIF", gdImageCreateFromGifCtx TSRMLS_CC);
  2006. break;
  2007. case PHP_GDIMG_TYPE_WBM:
  2008. im = _php_image_create_from_string(data, "WBMP", gdImageCreateFromWBMPCtx TSRMLS_CC);
  2009. break;
  2010. case PHP_GDIMG_TYPE_GD2:
  2011. im = _php_image_create_from_string(data, "GD2", gdImageCreateFromGd2Ctx TSRMLS_CC);
  2012. break;
  2013. default:
  2014. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Data is not in a recognized format");
  2015. RETURN_FALSE;
  2016. }
  2017. if (!im) {
  2018. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't create GD Image Stream out of Data");
  2019. RETURN_FALSE;
  2020. }
  2021. ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
  2022. }
  2023. /* }}} */
  2024. /* {{{ _php_image_create_from
  2025. */
  2026. static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, gdImagePtr (*func_p)(), gdImagePtr (*ioctx_func_p)())
  2027. {
  2028. char *file;
  2029. int file_len;
  2030. long srcx, srcy, width, height;
  2031. gdImagePtr im = NULL;
  2032. php_stream *stream;
  2033. FILE * fp = NULL;
  2034. long ignore_warning;
  2035. if (image_type == PHP_GDIMG_TYPE_GD2PART) {
  2036. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "pllll", &file, &file_len, &srcx, &srcy, &width, &height) == FAILURE) {
  2037. return;
  2038. }
  2039. if (width < 1 || height < 1) {
  2040. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Zero width or height not allowed");
  2041. RETURN_FALSE;
  2042. }
  2043. } else {
  2044. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &file, &file_len) == FAILURE) {
  2045. return;
  2046. }
  2047. }
  2048. stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
  2049. if (stream == NULL) {
  2050. RETURN_FALSE;
  2051. }
  2052. /* try and avoid allocating a FILE* if the stream is not naturally a FILE* */
  2053. if (php_stream_is(stream, PHP_STREAM_IS_STDIO)) {
  2054. if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS)) {
  2055. goto out_err;
  2056. }
  2057. } else if (ioctx_func_p) {
  2058. /* we can create an io context */
  2059. gdIOCtx* io_ctx;
  2060. size_t buff_size;
  2061. char *buff;
  2062. /* needs to be malloc (persistent) - GD will free() it later */
  2063. buff_size = php_stream_copy_to_mem(stream, &buff, PHP_STREAM_COPY_ALL, 1);
  2064. if (!buff_size) {
  2065. php_error_docref(NULL TSRMLS_CC, E_WARNING,"Cannot read image data");
  2066. goto out_err;
  2067. }
  2068. io_ctx = gdNewDynamicCtxEx(buff_size, buff, 0);
  2069. if (!io_ctx) {
  2070. pefree(buff, 1);
  2071. php_error_docref(NULL TSRMLS_CC, E_WARNING,"Cannot allocate GD IO context");
  2072. goto out_err;
  2073. }
  2074. if (image_type == PHP_GDIMG_TYPE_GD2PART) {
  2075. im = (*ioctx_func_p)(io_ctx, srcx, srcy, width, height);
  2076. } else {
  2077. im = (*ioctx_func_p)(io_ctx);
  2078. }
  2079. io_ctx->gd_free(io_ctx);
  2080. pefree(buff, 1);
  2081. }
  2082. else if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO)) {
  2083. /* try and force the stream to be FILE* */
  2084. if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_TRY_HARD, (void **) &fp, REPORT_ERRORS)) {
  2085. goto out_err;
  2086. }
  2087. }
  2088. if (!im && fp) {
  2089. switch (image_type) {
  2090. case PHP_GDIMG_TYPE_GD2PART:
  2091. im = (*func_p)(fp, srcx, srcy, width, height);
  2092. break;
  2093. #if defined(HAVE_GD_XPM)
  2094. case PHP_GDIMG_TYPE_XPM:
  2095. im = gdImageCreateFromXpm(file);
  2096. break;
  2097. #endif
  2098. #ifdef HAVE_GD_JPG
  2099. case PHP_GDIMG_TYPE_JPG:
  2100. ignore_warning = INI_INT("gd.jpeg_ignore_warning");
  2101. im = gdImageCreateFromJpegEx(fp, ignore_warning);
  2102. break;
  2103. #endif
  2104. default:
  2105. im = (*func_p)(fp);
  2106. break;
  2107. }
  2108. fflush(fp);
  2109. }
  2110. /* register_im: */
  2111. if (im) {
  2112. ZEND_REGISTER_RESOURCE(return_value, im, le_gd);
  2113. php_stream_close(stream);
  2114. return;
  2115. }
  2116. php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid %s file", file, tn);
  2117. out_err:
  2118. php_stream_close(stream);
  2119. RETURN_FALSE;
  2120. }
  2121. /* }}} */
  2122. /* {{{ proto resource imagecreatefromgif(string filename)
  2123. Create a new image from GIF file or URL */
  2124. PHP_FUNCTION(imagecreatefromgif)
  2125. {
  2126. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageCreateFromGif, gdImageCreateFromGifCtx);
  2127. }
  2128. /* }}} */
  2129. #ifdef HAVE_GD_JPG
  2130. /* {{{ proto resource imagecreatefromjpeg(string filename)
  2131. Create a new image from JPEG file or URL */
  2132. PHP_FUNCTION(imagecreatefromjpeg)
  2133. {
  2134. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageCreateFromJpeg, gdImageCreateFromJpegCtx);
  2135. }
  2136. /* }}} */
  2137. #endif /* HAVE_GD_JPG */
  2138. #ifdef HAVE_GD_PNG
  2139. /* {{{ proto resource imagecreatefrompng(string filename)
  2140. Create a new image from PNG file or URL */
  2141. PHP_FUNCTION(imagecreatefrompng)
  2142. {
  2143. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImageCreateFromPng, gdImageCreateFromPngCtx);
  2144. }
  2145. /* }}} */
  2146. #endif /* HAVE_GD_PNG */
  2147. #ifdef HAVE_GD_WEBP
  2148. /* {{{ proto resource imagecreatefromwebp(string filename)
  2149. Create a new image from WEBP file or URL */
  2150. PHP_FUNCTION(imagecreatefromwebp)
  2151. {
  2152. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WEBP, "WEBP", gdImageCreateFromWebp, gdImageCreateFromWebpCtx);
  2153. }
  2154. /* }}} */
  2155. #endif /* HAVE_GD_VPX */
  2156. /* {{{ proto resource imagecreatefromxbm(string filename)
  2157. Create a new image from XBM file or URL */
  2158. PHP_FUNCTION(imagecreatefromxbm)
  2159. {
  2160. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageCreateFromXbm, NULL);
  2161. }
  2162. /* }}} */
  2163. #if defined(HAVE_GD_XPM)
  2164. /* {{{ proto resource imagecreatefromxpm(string filename)
  2165. Create a new image from XPM file or URL */
  2166. PHP_FUNCTION(imagecreatefromxpm)
  2167. {
  2168. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XPM, "XPM", gdImageCreateFromXpm, NULL);
  2169. }
  2170. /* }}} */
  2171. #endif
  2172. /* {{{ proto resource imagecreatefromwbmp(string filename)
  2173. Create a new image from WBMP file or URL */
  2174. PHP_FUNCTION(imagecreatefromwbmp)
  2175. {
  2176. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageCreateFromWBMP, gdImageCreateFromWBMPCtx);
  2177. }
  2178. /* }}} */
  2179. /* {{{ proto resource imagecreatefromgd(string filename)
  2180. Create a new image from GD file or URL */
  2181. PHP_FUNCTION(imagecreatefromgd)
  2182. {
  2183. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageCreateFromGd, gdImageCreateFromGdCtx);
  2184. }
  2185. /* }}} */
  2186. /* {{{ proto resource imagecreatefromgd2(string filename)
  2187. Create a new image from GD2 file or URL */
  2188. PHP_FUNCTION(imagecreatefromgd2)
  2189. {
  2190. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageCreateFromGd2, gdImageCreateFromGd2Ctx);
  2191. }
  2192. /* }}} */
  2193. /* {{{ proto resource imagecreatefromgd2part(string filename, int srcX, int srcY, int width, int height)
  2194. Create a new image from a given part of GD2 file or URL */
  2195. PHP_FUNCTION(imagecreatefromgd2part)
  2196. {
  2197. _php_image_create_from(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2PART, "GD2", gdImageCreateFromGd2Part, gdImageCreateFromGd2PartCtx);
  2198. }
  2199. /* }}} */
  2200. /* {{{ _php_image_output
  2201. */
  2202. static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char *tn, void (*func_p)())
  2203. {
  2204. zval *imgind;
  2205. char *file = NULL;
  2206. long quality = 0, type = 0;
  2207. gdImagePtr im;
  2208. char *fn = NULL;
  2209. FILE *fp;
  2210. int file_len = 0, argc = ZEND_NUM_ARGS();
  2211. int q = -1, i, t = 1;
  2212. /* The quality parameter for Wbmp stands for the threshold when called from image2wbmp() */
  2213. /* When called from imagewbmp() the quality parameter stands for the foreground color. Default: black. */
  2214. /* The quality parameter for gd2 stands for chunk size */
  2215. if (zend_parse_parameters(argc TSRMLS_CC, "r|pll", &imgind, &file, &file_len, &quality, &type) == FAILURE) {
  2216. return;
  2217. }
  2218. ZEND_FETCH_RESOURCE(im, gdImagePtr, &imgind, -1, "Image", le_gd);
  2219. if (argc > 1) {
  2220. fn = file;
  2221. if (argc >= 3) {
  2222. q = quality;
  2223. if (argc == 4) {
  2224. t = type;
  2225. }
  2226. }
  2227. }
  2228. if (argc >= 2 && file_len) {
  2229. PHP_GD_CHECK_OPEN_BASEDIR(fn, "Invalid filename");
  2230. fp = VCWD_FOPEN(fn, "wb");
  2231. if (!fp) {
  2232. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing", fn);
  2233. RETURN_FALSE;
  2234. }
  2235. switch (image_type) {
  2236. case PHP_GDIMG_CONVERT_WBM:
  2237. if (q == -1) {
  2238. q = 0;
  2239. } else if (q < 0 || q > 255) {
  2240. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
  2241. q = 0;
  2242. }
  2243. gdImageWBMP(im, q, fp);
  2244. break;
  2245. case PHP_GDIMG_TYPE_JPG:
  2246. (*func_p)(im, fp, q);
  2247. break;
  2248. case PHP_GDIMG_TYPE_WBM:
  2249. for (i = 0; i < gdImageColorsTotal(im); i++) {
  2250. if (gdImageRed(im, i) == 0) break;
  2251. }
  2252. (*func_p)(im, i, fp);
  2253. break;
  2254. case PHP_GDIMG_TYPE_GD:
  2255. if (im->trueColor){
  2256. gdImageTrueColorToPalette(im,1,256);
  2257. }
  2258. (*func_p)(im, fp);
  2259. break;
  2260. case PHP_GDIMG_TYPE_GD2:
  2261. if (q == -1) {
  2262. q = 128;
  2263. }
  2264. (*func_p)(im, fp, q, t);
  2265. break;
  2266. default:
  2267. if (q == -1) {
  2268. q = 128;
  2269. }
  2270. (*func_p)(im, fp, q, t);
  2271. break;
  2272. }
  2273. fflush(fp);
  2274. fclose(fp);
  2275. } else {
  2276. int b;
  2277. FILE *tmp;
  2278. char buf[4096];
  2279. char *path;
  2280. tmp = php_open_temporary_file(NULL, NULL, &path TSRMLS_CC);
  2281. if (tmp == NULL) {
  2282. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open temporary file");
  2283. RETURN_FALSE;
  2284. }
  2285. switch (image_type) {
  2286. case PHP_GDIMG_CONVERT_WBM:
  2287. if (q == -1) {
  2288. q = 0;
  2289. } else if (q < 0 || q > 255) {
  2290. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid threshold value '%d'. It must be between 0 and 255", q);
  2291. q = 0;
  2292. }
  2293. gdImageWBMP(im, q, tmp);
  2294. break;
  2295. case PHP_GDIMG_TYPE_JPG:
  2296. (*func_p)(im, tmp, q);
  2297. break;
  2298. case PHP_GDIMG_TYPE_WBM:
  2299. for (i = 0; i < gdImageColorsTotal(im); i++) {
  2300. if (gdImageRed(im, i) == 0) {
  2301. break;
  2302. }
  2303. }
  2304. (*func_p)(im, q, tmp);
  2305. break;
  2306. case PHP_GDIMG_TYPE_GD:
  2307. if (im->trueColor) {
  2308. gdImageTrueColorToPalette(im,1,256);
  2309. }
  2310. (*func_p)(im, tmp);
  2311. break;
  2312. case PHP_GDIMG_TYPE_GD2:
  2313. if (q == -1) {
  2314. q = 128;
  2315. }
  2316. (*func_p)(im, tmp, q, t);
  2317. break;
  2318. default:
  2319. (*func_p)(im, tmp);
  2320. break;
  2321. }
  2322. fseek(tmp, 0, SEEK_SET);
  2323. #if APACHE && defined(CHARSET_EBCDIC)
  2324. /* XXX this is unlikely to work any more thies@thieso.net */
  2325. /* This is a binary file already: avoid EBCDIC->ASCII conversion */
  2326. ap_bsetflag(php3_rqst->connection->client, B_EBCDIC2ASCII, 0);
  2327. #endif
  2328. while ((b = fread(buf, 1, sizeof(buf), tmp)) > 0) {
  2329. php_write(buf, b TSRMLS_CC);
  2330. }
  2331. fclose(tmp);
  2332. VCWD_UNLINK((const char *)path); /* make sure that the temporary file is removed */
  2333. efree(path);
  2334. }
  2335. RETURN_TRUE;
  2336. }
  2337. /* }}} */
  2338. /* {{{ proto int imagexbm(int im, string filename [, int foreground])
  2339. Output XBM image to browser or file */
  2340. PHP_FUNCTION(imagexbm)
  2341. {
  2342. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_XBM, "XBM", gdImageXbmCtx);
  2343. }
  2344. /* }}} */
  2345. /* {{{ proto bool imagegif(resource im [, string filename])
  2346. Output GIF image to browser or file */
  2347. PHP_FUNCTION(imagegif)
  2348. {
  2349. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GIF, "GIF", gdImageGifCtx);
  2350. }
  2351. /* }}} */
  2352. #ifdef HAVE_GD_PNG
  2353. /* {{{ proto bool imagepng(resource im [, string filename])
  2354. Output PNG image to browser or file */
  2355. PHP_FUNCTION(imagepng)
  2356. {
  2357. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG, "PNG", gdImagePngCtxEx);
  2358. }
  2359. /* }}} */
  2360. #endif /* HAVE_GD_PNG */
  2361. #ifdef HAVE_GD_WEBP
  2362. /* {{{ proto bool imagewebp(resource im [, string filename[, quality]] )
  2363. Output WEBP image to browser or file */
  2364. PHP_FUNCTION(imagewebp)
  2365. {
  2366. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WEBP, "WEBP", gdImageWebpCtx);
  2367. }
  2368. /* }}} */
  2369. #endif /* HAVE_GD_WEBP */
  2370. #ifdef HAVE_GD_JPG
  2371. /* {{{ proto bool imagejpeg(resource im [, string filename [, int quality]])
  2372. Output JPEG image to browser or file */
  2373. PHP_FUNCTION(imagejpeg)
  2374. {
  2375. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG, "JPEG", gdImageJpegCtx);
  2376. }
  2377. /* }}} */
  2378. #endif /* HAVE_GD_JPG */
  2379. /* {{{ proto bool imagewbmp(resource im [, string filename, [, int foreground]])
  2380. Output WBMP image to browser or file */
  2381. PHP_FUNCTION(imagewbmp)
  2382. {
  2383. _php_image_output_ctx(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_WBM, "WBMP", gdImageWBMPCtx);
  2384. }
  2385. /* }}} */
  2386. /* {{{ proto bool imagegd(resource im [, string filename])
  2387. Output GD image to browser or file */
  2388. PHP_FUNCTION(imagegd)
  2389. {
  2390. _php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD, "GD", gdImageGd);
  2391. }
  2392. /* }}} */
  2393. /* {{{ proto bool imagegd2(resource im [, string filename, [, int chunk_size, [, int type]]])
  2394. Output GD2 image to browser or file */
  2395. PHP_FUNCTION(imagegd2)
  2396. {
  2397. _php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_GD2, "GD2", gdImageGd2);
  2398. }
  2399. /* }}} */
  2400. /* {{{ proto bool imagedestroy(resource im)
  2401. Destroy an image */
  2402. PHP_FUNCTION(imagedestroy)
  2403. {
  2404. zval *IM;
  2405. gdImagePtr im;
  2406. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &IM) == FAILURE) {
  2407. return;
  2408. }
  2409. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2410. zend_list_delete(Z_LVAL_P(IM));
  2411. RETURN_TRUE;
  2412. }
  2413. /* }}} */
  2414. /* {{{ proto int imagecolorallocate(resource im, int red, int green, int blue)
  2415. Allocate a color for an image */
  2416. PHP_FUNCTION(imagecolorallocate)
  2417. {
  2418. zval *IM;
  2419. long red, green, blue;
  2420. gdImagePtr im;
  2421. int ct = (-1);
  2422. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &IM, &red, &green, &blue) == FAILURE) {
  2423. return;
  2424. }
  2425. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2426. ct = gdImageColorAllocate(im, red, green, blue);
  2427. if (ct < 0) {
  2428. RETURN_FALSE;
  2429. }
  2430. RETURN_LONG(ct);
  2431. }
  2432. /* }}} */
  2433. /* {{{ proto void imagepalettecopy(resource dst, resource src)
  2434. Copy the palette from the src image onto the dst image */
  2435. PHP_FUNCTION(imagepalettecopy)
  2436. {
  2437. zval *dstim, *srcim;
  2438. gdImagePtr dst, src;
  2439. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &dstim, &srcim) == FAILURE) {
  2440. return;
  2441. }
  2442. ZEND_FETCH_RESOURCE(dst, gdImagePtr, &dstim, -1, "Image", le_gd);
  2443. ZEND_FETCH_RESOURCE(src, gdImagePtr, &srcim, -1, "Image", le_gd);
  2444. gdImagePaletteCopy(dst, src);
  2445. }
  2446. /* }}} */
  2447. /* {{{ proto int imagecolorat(resource im, int x, int y)
  2448. Get the index of the color of a pixel */
  2449. PHP_FUNCTION(imagecolorat)
  2450. {
  2451. zval *IM;
  2452. long x, y;
  2453. gdImagePtr im;
  2454. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &IM, &x, &y) == FAILURE) {
  2455. return;
  2456. }
  2457. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2458. if (gdImageTrueColor(im)) {
  2459. if (im->tpixels && gdImageBoundsSafe(im, x, y)) {
  2460. RETURN_LONG(gdImageTrueColorPixel(im, x, y));
  2461. } else {
  2462. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%ld,%ld is out of bounds", x, y);
  2463. RETURN_FALSE;
  2464. }
  2465. } else {
  2466. if (im->pixels && gdImageBoundsSafe(im, x, y)) {
  2467. RETURN_LONG(im->pixels[y][x]);
  2468. } else {
  2469. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%ld,%ld is out of bounds", x, y);
  2470. RETURN_FALSE;
  2471. }
  2472. }
  2473. }
  2474. /* }}} */
  2475. /* {{{ proto int imagecolorclosest(resource im, int red, int green, int blue)
  2476. Get the index of the closest color to the specified color */
  2477. PHP_FUNCTION(imagecolorclosest)
  2478. {
  2479. zval *IM;
  2480. long red, green, blue;
  2481. gdImagePtr im;
  2482. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &IM, &red, &green, &blue) == FAILURE) {
  2483. return;
  2484. }
  2485. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2486. RETURN_LONG(gdImageColorClosest(im, red, green, blue));
  2487. }
  2488. /* }}} */
  2489. /* {{{ proto int imagecolorclosesthwb(resource im, int red, int green, int blue)
  2490. Get the index of the color which has the hue, white and blackness nearest to the given color */
  2491. PHP_FUNCTION(imagecolorclosesthwb)
  2492. {
  2493. zval *IM;
  2494. long red, green, blue;
  2495. gdImagePtr im;
  2496. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &IM, &red, &green, &blue) == FAILURE) {
  2497. return;
  2498. }
  2499. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2500. RETURN_LONG(gdImageColorClosestHWB(im, red, green, blue));
  2501. }
  2502. /* }}} */
  2503. /* {{{ proto bool imagecolordeallocate(resource im, int index)
  2504. De-allocate a color for an image */
  2505. PHP_FUNCTION(imagecolordeallocate)
  2506. {
  2507. zval *IM;
  2508. long index;
  2509. int col;
  2510. gdImagePtr im;
  2511. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &IM, &index) == FAILURE) {
  2512. return;
  2513. }
  2514. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2515. /* We can return right away for a truecolor image as deallocating colours is meaningless here */
  2516. if (gdImageTrueColor(im)) {
  2517. RETURN_TRUE;
  2518. }
  2519. col = index;
  2520. if (col >= 0 && col < gdImageColorsTotal(im)) {
  2521. gdImageColorDeallocate(im, col);
  2522. RETURN_TRUE;
  2523. } else {
  2524. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Color index %d out of range", col);
  2525. RETURN_FALSE;
  2526. }
  2527. }
  2528. /* }}} */
  2529. /* {{{ proto int imagecolorresolve(resource im, int red, int green, int blue)
  2530. Get the index of the specified color or its closest possible alternative */
  2531. PHP_FUNCTION(imagecolorresolve)
  2532. {
  2533. zval *IM;
  2534. long red, green, blue;
  2535. gdImagePtr im;
  2536. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &IM, &red, &green, &blue) == FAILURE) {
  2537. return;
  2538. }
  2539. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2540. RETURN_LONG(gdImageColorResolve(im, red, green, blue));
  2541. }
  2542. /* }}} */
  2543. /* {{{ proto int imagecolorexact(resource im, int red, int green, int blue)
  2544. Get the index of the specified color */
  2545. PHP_FUNCTION(imagecolorexact)
  2546. {
  2547. zval *IM;
  2548. long red, green, blue;
  2549. gdImagePtr im;
  2550. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &IM, &red, &green, &blue) == FAILURE) {
  2551. return;
  2552. }
  2553. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2554. RETURN_LONG(gdImageColorExact(im, red, green, blue));
  2555. }
  2556. /* }}} */
  2557. /* {{{ proto void imagecolorset(resource im, int col, int red, int green, int blue)
  2558. Set the color for the specified palette index */
  2559. PHP_FUNCTION(imagecolorset)
  2560. {
  2561. zval *IM;
  2562. long color, red, green, blue, alpha = 0;
  2563. int col;
  2564. gdImagePtr im;
  2565. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll|l", &IM, &color, &red, &green, &blue, &alpha) == FAILURE) {
  2566. return;
  2567. }
  2568. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2569. col = color;
  2570. if (col >= 0 && col < gdImageColorsTotal(im)) {
  2571. im->red[col] = red;
  2572. im->green[col] = green;
  2573. im->blue[col] = blue;
  2574. im->alpha[col] = alpha;
  2575. } else {
  2576. RETURN_FALSE;
  2577. }
  2578. }
  2579. /* }}} */
  2580. /* {{{ proto array imagecolorsforindex(resource im, int col)
  2581. Get the colors for an index */
  2582. PHP_FUNCTION(imagecolorsforindex)
  2583. {
  2584. zval *IM;
  2585. long index;
  2586. int col;
  2587. gdImagePtr im;
  2588. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &IM, &index) == FAILURE) {
  2589. return;
  2590. }
  2591. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2592. col = index;
  2593. if ((col >= 0 && gdImageTrueColor(im)) || (!gdImageTrueColor(im) && col >= 0 && col < gdImageColorsTotal(im))) {
  2594. array_init(return_value);
  2595. add_assoc_long(return_value,"red", gdImageRed(im,col));
  2596. add_assoc_long(return_value,"green", gdImageGreen(im,col));
  2597. add_assoc_long(return_value,"blue", gdImageBlue(im,col));
  2598. add_assoc_long(return_value,"alpha", gdImageAlpha(im,col));
  2599. } else {
  2600. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Color index %d out of range", col);
  2601. RETURN_FALSE;
  2602. }
  2603. }
  2604. /* }}} */
  2605. /* {{{ proto bool imagegammacorrect(resource im, float inputgamma, float outputgamma)
  2606. Apply a gamma correction to a GD image */
  2607. PHP_FUNCTION(imagegammacorrect)
  2608. {
  2609. zval *IM;
  2610. gdImagePtr im;
  2611. int i;
  2612. double input, output;
  2613. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rdd", &IM, &input, &output) == FAILURE) {
  2614. return;
  2615. }
  2616. if ( input <= 0.0 || output <= 0.0 ) {
  2617. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Gamma values should be positive");
  2618. RETURN_FALSE;
  2619. }
  2620. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2621. if (gdImageTrueColor(im)) {
  2622. int x, y, c;
  2623. for (y = 0; y < gdImageSY(im); y++) {
  2624. for (x = 0; x < gdImageSX(im); x++) {
  2625. c = gdImageGetPixel(im, x, y);
  2626. gdImageSetPixel(im, x, y,
  2627. gdTrueColorAlpha(
  2628. (int) ((pow((pow((gdTrueColorGetRed(c) / 255.0), input)), 1.0 / output) * 255) + .5),
  2629. (int) ((pow((pow((gdTrueColorGetGreen(c) / 255.0), input)), 1.0 / output) * 255) + .5),
  2630. (int) ((pow((pow((gdTrueColorGetBlue(c) / 255.0), input)), 1.0 / output) * 255) + .5),
  2631. gdTrueColorGetAlpha(c)
  2632. )
  2633. );
  2634. }
  2635. }
  2636. RETURN_TRUE;
  2637. }
  2638. for (i = 0; i < gdImageColorsTotal(im); i++) {
  2639. im->red[i] = (int)((pow((pow((im->red[i] / 255.0), input)), 1.0 / output) * 255) + .5);
  2640. im->green[i] = (int)((pow((pow((im->green[i] / 255.0), input)), 1.0 / output) * 255) + .5);
  2641. im->blue[i] = (int)((pow((pow((im->blue[i] / 255.0), input)), 1.0 / output) * 255) + .5);
  2642. }
  2643. RETURN_TRUE;
  2644. }
  2645. /* }}} */
  2646. /* {{{ proto bool imagesetpixel(resource im, int x, int y, int col)
  2647. Set a single pixel */
  2648. PHP_FUNCTION(imagesetpixel)
  2649. {
  2650. zval *IM;
  2651. long x, y, col;
  2652. gdImagePtr im;
  2653. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &IM, &x, &y, &col) == FAILURE) {
  2654. return;
  2655. }
  2656. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2657. gdImageSetPixel(im, x, y, col);
  2658. RETURN_TRUE;
  2659. }
  2660. /* }}} */
  2661. /* {{{ proto bool imageline(resource im, int x1, int y1, int x2, int y2, int col)
  2662. Draw a line */
  2663. PHP_FUNCTION(imageline)
  2664. {
  2665. zval *IM;
  2666. long x1, y1, x2, y2, col;
  2667. gdImagePtr im;
  2668. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
  2669. return;
  2670. }
  2671. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2672. #ifdef HAVE_GD_BUNDLED
  2673. if (im->antialias) {
  2674. gdImageAALine(im, x1, y1, x2, y2, col);
  2675. } else
  2676. #endif
  2677. {
  2678. gdImageLine(im, x1, y1, x2, y2, col);
  2679. }
  2680. RETURN_TRUE;
  2681. }
  2682. /* }}} */
  2683. /* {{{ proto bool imagedashedline(resource im, int x1, int y1, int x2, int y2, int col)
  2684. Draw a dashed line */
  2685. PHP_FUNCTION(imagedashedline)
  2686. {
  2687. zval *IM;
  2688. long x1, y1, x2, y2, col;
  2689. gdImagePtr im;
  2690. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
  2691. return;
  2692. }
  2693. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2694. gdImageDashedLine(im, x1, y1, x2, y2, col);
  2695. RETURN_TRUE;
  2696. }
  2697. /* }}} */
  2698. /* {{{ proto bool imagerectangle(resource im, int x1, int y1, int x2, int y2, int col)
  2699. Draw a rectangle */
  2700. PHP_FUNCTION(imagerectangle)
  2701. {
  2702. zval *IM;
  2703. long x1, y1, x2, y2, col;
  2704. gdImagePtr im;
  2705. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
  2706. return;
  2707. }
  2708. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2709. gdImageRectangle(im, x1, y1, x2, y2, col);
  2710. RETURN_TRUE;
  2711. }
  2712. /* }}} */
  2713. /* {{{ proto bool imagefilledrectangle(resource im, int x1, int y1, int x2, int y2, int col)
  2714. Draw a filled rectangle */
  2715. PHP_FUNCTION(imagefilledrectangle)
  2716. {
  2717. zval *IM;
  2718. long x1, y1, x2, y2, col;
  2719. gdImagePtr im;
  2720. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &IM, &x1, &y1, &x2, &y2, &col) == FAILURE) {
  2721. return;
  2722. }
  2723. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2724. gdImageFilledRectangle(im, x1, y1, x2, y2, col);
  2725. RETURN_TRUE;
  2726. }
  2727. /* }}} */
  2728. /* {{{ proto bool imagearc(resource im, int cx, int cy, int w, int h, int s, int e, int col)
  2729. Draw a partial ellipse */
  2730. PHP_FUNCTION(imagearc)
  2731. {
  2732. zval *IM;
  2733. long cx, cy, w, h, ST, E, col;
  2734. gdImagePtr im;
  2735. int e, st;
  2736. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllllll", &IM, &cx, &cy, &w, &h, &ST, &E, &col) == FAILURE) {
  2737. return;
  2738. }
  2739. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2740. e = E;
  2741. if (e < 0) {
  2742. e %= 360;
  2743. }
  2744. st = ST;
  2745. if (st < 0) {
  2746. st %= 360;
  2747. }
  2748. gdImageArc(im, cx, cy, w, h, st, e, col);
  2749. RETURN_TRUE;
  2750. }
  2751. /* }}} */
  2752. /* {{{ proto bool imageellipse(resource im, int cx, int cy, int w, int h, int color)
  2753. Draw an ellipse */
  2754. PHP_FUNCTION(imageellipse)
  2755. {
  2756. zval *IM;
  2757. long cx, cy, w, h, color;
  2758. gdImagePtr im;
  2759. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllll", &IM, &cx, &cy, &w, &h, &color) == FAILURE) {
  2760. return;
  2761. }
  2762. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2763. gdImageEllipse(im, cx, cy, w, h, color);
  2764. RETURN_TRUE;
  2765. }
  2766. /* }}} */
  2767. /* {{{ proto bool imagefilltoborder(resource im, int x, int y, int border, int col)
  2768. Flood fill to specific color */
  2769. PHP_FUNCTION(imagefilltoborder)
  2770. {
  2771. zval *IM;
  2772. long x, y, border, col;
  2773. gdImagePtr im;
  2774. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &IM, &x, &y, &border, &col) == FAILURE) {
  2775. return;
  2776. }
  2777. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2778. gdImageFillToBorder(im, x, y, border, col);
  2779. RETURN_TRUE;
  2780. }
  2781. /* }}} */
  2782. /* {{{ proto bool imagefill(resource im, int x, int y, int col)
  2783. Flood fill */
  2784. PHP_FUNCTION(imagefill)
  2785. {
  2786. zval *IM;
  2787. long x, y, col;
  2788. gdImagePtr im;
  2789. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &IM, &x, &y, &col) == FAILURE) {
  2790. return;
  2791. }
  2792. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2793. gdImageFill(im, x, y, col);
  2794. RETURN_TRUE;
  2795. }
  2796. /* }}} */
  2797. /* {{{ proto int imagecolorstotal(resource im)
  2798. Find out the number of colors in an image's palette */
  2799. PHP_FUNCTION(imagecolorstotal)
  2800. {
  2801. zval *IM;
  2802. gdImagePtr im;
  2803. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &IM) == FAILURE) {
  2804. return;
  2805. }
  2806. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2807. RETURN_LONG(gdImageColorsTotal(im));
  2808. }
  2809. /* }}} */
  2810. /* {{{ proto int imagecolortransparent(resource im [, int col])
  2811. Define a color as transparent */
  2812. PHP_FUNCTION(imagecolortransparent)
  2813. {
  2814. zval *IM;
  2815. long COL = 0;
  2816. gdImagePtr im;
  2817. int argc = ZEND_NUM_ARGS();
  2818. if (zend_parse_parameters(argc TSRMLS_CC, "r|l", &IM, &COL) == FAILURE) {
  2819. return;
  2820. }
  2821. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2822. if (argc > 1) {
  2823. gdImageColorTransparent(im, COL);
  2824. }
  2825. RETURN_LONG(gdImageGetTransparent(im));
  2826. }
  2827. /* }}} */
  2828. /* {{{ proto int imageinterlace(resource im [, int interlace])
  2829. Enable or disable interlace */
  2830. PHP_FUNCTION(imageinterlace)
  2831. {
  2832. zval *IM;
  2833. int argc = ZEND_NUM_ARGS();
  2834. long INT = 0;
  2835. gdImagePtr im;
  2836. if (zend_parse_parameters(argc TSRMLS_CC, "r|l", &IM, &INT) == FAILURE) {
  2837. return;
  2838. }
  2839. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2840. if (argc > 1) {
  2841. gdImageInterlace(im, INT);
  2842. }
  2843. RETURN_LONG(gdImageGetInterlaced(im));
  2844. }
  2845. /* }}} */
  2846. /* {{{ php_imagepolygon
  2847. arg = 0 normal polygon
  2848. arg = 1 filled polygon */
  2849. /* im, points, num_points, col */
  2850. static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
  2851. {
  2852. zval *IM, *POINTS;
  2853. long NPOINTS, COL;
  2854. zval **var = NULL;
  2855. gdImagePtr im;
  2856. gdPointPtr points;
  2857. int npoints, col, nelem, i;
  2858. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rall", &IM, &POINTS, &NPOINTS, &COL) == FAILURE) {
  2859. return;
  2860. }
  2861. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  2862. npoints = NPOINTS;
  2863. col = COL;
  2864. nelem = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
  2865. if (nelem < 6) {
  2866. php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must have at least 3 points in your array");
  2867. RETURN_FALSE;
  2868. }
  2869. if (npoints <= 0) {
  2870. php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must give a positive number of points");
  2871. RETURN_FALSE;
  2872. }
  2873. if (nelem < npoints * 2) {
  2874. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Trying to use %d points in array with only %d points", npoints, nelem/2);
  2875. RETURN_FALSE;
  2876. }
  2877. points = (gdPointPtr) safe_emalloc(npoints, sizeof(gdPoint), 0);
  2878. for (i = 0; i < npoints; i++) {
  2879. if (zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2), (void **) &var) == SUCCESS) {
  2880. if (Z_TYPE_PP(var) != IS_LONG) {
  2881. zval lval;
  2882. lval = **var;
  2883. zval_copy_ctor(&lval);
  2884. convert_to_long(&lval);
  2885. points[i].x = Z_LVAL(lval);
  2886. } else {
  2887. points[i].x = Z_LVAL_PP(var);
  2888. }
  2889. }
  2890. if (zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2) + 1, (void **) &var) == SUCCESS) {
  2891. if (Z_TYPE_PP(var) != IS_LONG) {
  2892. zval lval;
  2893. lval = **var;
  2894. zval_copy_ctor(&lval);
  2895. convert_to_long(&lval);
  2896. points[i].y = Z_LVAL(lval);
  2897. } else {
  2898. points[i].y = Z_LVAL_PP(var);
  2899. }
  2900. }
  2901. }
  2902. if (filled) {
  2903. gdImageFilledPolygon(im, points, npoints, col);
  2904. } else {
  2905. gdImagePolygon(im, points, npoints, col);
  2906. }
  2907. efree(points);
  2908. RETURN_TRUE;
  2909. }
  2910. /* }}} */
  2911. /* {{{ proto bool imagepolygon(resource im, array point, int num_points, int col)
  2912. Draw a polygon */
  2913. PHP_FUNCTION(imagepolygon)
  2914. {
  2915. php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2916. }
  2917. /* }}} */
  2918. /* {{{ proto bool imagefilledpolygon(resource im, array point, int num_points, int col)
  2919. Draw a filled polygon */
  2920. PHP_FUNCTION(imagefilledpolygon)
  2921. {
  2922. php_imagepolygon(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2923. }
  2924. /* }}} */
  2925. /* {{{ php_find_gd_font
  2926. */
  2927. static gdFontPtr php_find_gd_font(int size TSRMLS_DC)
  2928. {
  2929. gdFontPtr font;
  2930. int ind_type;
  2931. switch (size) {
  2932. case 1:
  2933. font = gdFontTiny;
  2934. break;
  2935. case 2:
  2936. font = gdFontSmall;
  2937. break;
  2938. case 3:
  2939. font = gdFontMediumBold;
  2940. break;
  2941. case 4:
  2942. font = gdFontLarge;
  2943. break;
  2944. case 5:
  2945. font = gdFontGiant;
  2946. break;
  2947. default:
  2948. font = zend_list_find(size - 5, &ind_type);
  2949. if (!font || ind_type != le_gd_font) {
  2950. if (size < 1) {
  2951. font = gdFontTiny;
  2952. } else {
  2953. font = gdFontGiant;
  2954. }
  2955. }
  2956. break;
  2957. }
  2958. return font;
  2959. }
  2960. /* }}} */
  2961. /* {{{ php_imagefontsize
  2962. * arg = 0 ImageFontWidth
  2963. * arg = 1 ImageFontHeight
  2964. */
  2965. static void php_imagefontsize(INTERNAL_FUNCTION_PARAMETERS, int arg)
  2966. {
  2967. long SIZE;
  2968. gdFontPtr font;
  2969. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &SIZE) == FAILURE) {
  2970. return;
  2971. }
  2972. font = php_find_gd_font(SIZE TSRMLS_CC);
  2973. RETURN_LONG(arg ? font->h : font->w);
  2974. }
  2975. /* }}} */
  2976. /* {{{ proto int imagefontwidth(int font)
  2977. Get font width */
  2978. PHP_FUNCTION(imagefontwidth)
  2979. {
  2980. php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2981. }
  2982. /* }}} */
  2983. /* {{{ proto int imagefontheight(int font)
  2984. Get font height */
  2985. PHP_FUNCTION(imagefontheight)
  2986. {
  2987. php_imagefontsize(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2988. }
  2989. /* }}} */
  2990. /* {{{ php_gdimagecharup
  2991. * workaround for a bug in gd 1.2 */
  2992. static void php_gdimagecharup(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color)
  2993. {
  2994. int cx, cy, px, py, fline;
  2995. cx = 0;
  2996. cy = 0;
  2997. if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
  2998. return;
  2999. }
  3000. fline = (c - f->offset) * f->h * f->w;
  3001. for (py = y; (py > (y - f->w)); py--) {
  3002. for (px = x; (px < (x + f->h)); px++) {
  3003. if (f->data[fline + cy * f->w + cx]) {
  3004. gdImageSetPixel(im, px, py, color);
  3005. }
  3006. cy++;
  3007. }
  3008. cy = 0;
  3009. cx++;
  3010. }
  3011. }
  3012. /* }}} */
  3013. /* {{{ php_imagechar
  3014. * arg = 0 ImageChar
  3015. * arg = 1 ImageCharUp
  3016. * arg = 2 ImageString
  3017. * arg = 3 ImageStringUp
  3018. */
  3019. static void php_imagechar(INTERNAL_FUNCTION_PARAMETERS, int mode)
  3020. {
  3021. zval *IM;
  3022. long SIZE, X, Y, COL;
  3023. char *C;
  3024. int C_len;
  3025. gdImagePtr im;
  3026. int ch = 0, col, x, y, size, i, l = 0;
  3027. unsigned char *str = NULL;
  3028. gdFontPtr font;
  3029. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllsl", &IM, &SIZE, &X, &Y, &C, &C_len, &COL) == FAILURE) {
  3030. return;
  3031. }
  3032. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  3033. col = COL;
  3034. if (mode < 2) {
  3035. ch = (int)((unsigned char)*C);
  3036. } else {
  3037. str = (unsigned char *) estrndup(C, C_len);
  3038. l = strlen((char *)str);
  3039. }
  3040. y = Y;
  3041. x = X;
  3042. size = SIZE;
  3043. font = php_find_gd_font(size TSRMLS_CC);
  3044. switch (mode) {
  3045. case 0:
  3046. gdImageChar(im, font, x, y, ch, col);
  3047. break;
  3048. case 1:
  3049. php_gdimagecharup(im, font, x, y, ch, col);
  3050. break;
  3051. case 2:
  3052. for (i = 0; (i < l); i++) {
  3053. gdImageChar(im, font, x, y, (int) ((unsigned char) str[i]), col);
  3054. x += font->w;
  3055. }
  3056. break;
  3057. case 3: {
  3058. for (i = 0; (i < l); i++) {
  3059. /* php_gdimagecharup(im, font, x, y, (int) str[i], col); */
  3060. gdImageCharUp(im, font, x, y, (int) str[i], col);
  3061. y -= font->w;
  3062. }
  3063. break;
  3064. }
  3065. }
  3066. if (str) {
  3067. efree(str);
  3068. }
  3069. RETURN_TRUE;
  3070. }
  3071. /* }}} */
  3072. /* {{{ proto bool imagechar(resource im, int font, int x, int y, string c, int col)
  3073. Draw a character */
  3074. PHP_FUNCTION(imagechar)
  3075. {
  3076. php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  3077. }
  3078. /* }}} */
  3079. /* {{{ proto bool imagecharup(resource im, int font, int x, int y, string c, int col)
  3080. Draw a character rotated 90 degrees counter-clockwise */
  3081. PHP_FUNCTION(imagecharup)
  3082. {
  3083. php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  3084. }
  3085. /* }}} */
  3086. /* {{{ proto bool imagestring(resource im, int font, int x, int y, string str, int col)
  3087. Draw a string horizontally */
  3088. PHP_FUNCTION(imagestring)
  3089. {
  3090. php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
  3091. }
  3092. /* }}} */
  3093. /* {{{ proto bool imagestringup(resource im, int font, int x, int y, string str, int col)
  3094. Draw a string vertically - rotated 90 degrees counter-clockwise */
  3095. PHP_FUNCTION(imagestringup)
  3096. {
  3097. php_imagechar(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
  3098. }
  3099. /* }}} */
  3100. /* {{{ proto bool imagecopy(resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h)
  3101. Copy part of an image */
  3102. PHP_FUNCTION(imagecopy)
  3103. {
  3104. zval *SIM, *DIM;
  3105. long SX, SY, SW, SH, DX, DY;
  3106. gdImagePtr im_dst, im_src;
  3107. int srcH, srcW, srcY, srcX, dstY, dstX;
  3108. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &SW, &SH) == FAILURE) {
  3109. return;
  3110. }
  3111. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  3112. ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, &DIM, -1, "Image", le_gd);
  3113. srcX = SX;
  3114. srcY = SY;
  3115. srcH = SH;
  3116. srcW = SW;
  3117. dstX = DX;
  3118. dstY = DY;
  3119. gdImageCopy(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH);
  3120. RETURN_TRUE;
  3121. }
  3122. /* }}} */
  3123. /* {{{ proto bool imagecopymerge(resource src_im, resource dst_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct)
  3124. Merge one part of an image with another */
  3125. PHP_FUNCTION(imagecopymerge)
  3126. {
  3127. zval *SIM, *DIM;
  3128. long SX, SY, SW, SH, DX, DY, PCT;
  3129. gdImagePtr im_dst, im_src;
  3130. int srcH, srcW, srcY, srcX, dstY, dstX, pct;
  3131. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrlllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
  3132. return;
  3133. }
  3134. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  3135. ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, &DIM, -1, "Image", le_gd);
  3136. srcX = SX;
  3137. srcY = SY;
  3138. srcH = SH;
  3139. srcW = SW;
  3140. dstX = DX;
  3141. dstY = DY;
  3142. pct = PCT;
  3143. gdImageCopyMerge(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
  3144. RETURN_TRUE;
  3145. }
  3146. /* }}} */
  3147. /* {{{ proto bool imagecopymergegray(resource src_im, resource dst_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, int pct)
  3148. Merge one part of an image with another */
  3149. PHP_FUNCTION(imagecopymergegray)
  3150. {
  3151. zval *SIM, *DIM;
  3152. long SX, SY, SW, SH, DX, DY, PCT;
  3153. gdImagePtr im_dst, im_src;
  3154. int srcH, srcW, srcY, srcX, dstY, dstX, pct;
  3155. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrlllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &SW, &SH, &PCT) == FAILURE) {
  3156. return;
  3157. }
  3158. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  3159. ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, &DIM, -1, "Image", le_gd);
  3160. srcX = SX;
  3161. srcY = SY;
  3162. srcH = SH;
  3163. srcW = SW;
  3164. dstX = DX;
  3165. dstY = DY;
  3166. pct = PCT;
  3167. gdImageCopyMergeGray(im_dst, im_src, dstX, dstY, srcX, srcY, srcW, srcH, pct);
  3168. RETURN_TRUE;
  3169. }
  3170. /* }}} */
  3171. /* {{{ proto bool imagecopyresized(resource dst_im, resource src_im, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h)
  3172. Copy and resize part of an image */
  3173. PHP_FUNCTION(imagecopyresized)
  3174. {
  3175. zval *SIM, *DIM;
  3176. long SX, SY, SW, SH, DX, DY, DW, DH;
  3177. gdImagePtr im_dst, im_src;
  3178. int srcH, srcW, dstH, dstW, srcY, srcX, dstY, dstX;
  3179. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrllllllll", &DIM, &SIM, &DX, &DY, &SX, &SY, &DW, &DH, &SW, &SH) == FAILURE) {
  3180. return;
  3181. }
  3182. ZEND_FETCH_RESOURCE(im_dst, gdImagePtr, &DIM, -1, "Image", le_gd);
  3183. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  3184. srcX = SX;
  3185. srcY = SY;
  3186. srcH = SH;
  3187. srcW = SW;
  3188. dstX = DX;
  3189. dstY = DY;
  3190. dstH = DH;
  3191. dstW = DW;
  3192. if (dstW <= 0 || dstH <= 0 || srcW <= 0 || srcH <= 0) {
  3193. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid image dimensions");
  3194. RETURN_FALSE;
  3195. }
  3196. gdImageCopyResized(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH);
  3197. RETURN_TRUE;
  3198. }
  3199. /* }}} */
  3200. /* {{{ proto int imagesx(resource im)
  3201. Get image width */
  3202. PHP_FUNCTION(imagesx)
  3203. {
  3204. zval *IM;
  3205. gdImagePtr im;
  3206. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &IM) == FAILURE) {
  3207. return;
  3208. }
  3209. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  3210. RETURN_LONG(gdImageSX(im));
  3211. }
  3212. /* }}} */
  3213. /* {{{ proto int imagesy(resource im)
  3214. Get image height */
  3215. PHP_FUNCTION(imagesy)
  3216. {
  3217. zval *IM;
  3218. gdImagePtr im;
  3219. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &IM) == FAILURE) {
  3220. return;
  3221. }
  3222. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  3223. RETURN_LONG(gdImageSY(im));
  3224. }
  3225. /* }}} */
  3226. #ifdef ENABLE_GD_TTF
  3227. #define TTFTEXT_DRAW 0
  3228. #define TTFTEXT_BBOX 1
  3229. #endif
  3230. #ifdef ENABLE_GD_TTF
  3231. #if HAVE_GD_FREETYPE && HAVE_LIBFREETYPE
  3232. /* {{{ proto array imageftbbox(float size, float angle, string font_file, string text [, array extrainfo])
  3233. Give the bounding box of a text using fonts via freetype2 */
  3234. PHP_FUNCTION(imageftbbox)
  3235. {
  3236. php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_BBOX, 1);
  3237. }
  3238. /* }}} */
  3239. /* {{{ proto array imagefttext(resource im, float size, float angle, int x, int y, int col, string font_file, string text [, array extrainfo])
  3240. Write text to the image using fonts via freetype2 */
  3241. PHP_FUNCTION(imagefttext)
  3242. {
  3243. php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_DRAW, 1);
  3244. }
  3245. /* }}} */
  3246. #endif /* HAVE_GD_FREETYPE && HAVE_LIBFREETYPE */
  3247. /* {{{ proto array imagettfbbox(float size, float angle, string font_file, string text)
  3248. Give the bounding box of a text using TrueType fonts */
  3249. PHP_FUNCTION(imagettfbbox)
  3250. {
  3251. php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_BBOX, 0);
  3252. }
  3253. /* }}} */
  3254. /* {{{ proto array imagettftext(resource im, float size, float angle, int x, int y, int col, string font_file, string text)
  3255. Write text to the image using a TrueType font */
  3256. PHP_FUNCTION(imagettftext)
  3257. {
  3258. php_imagettftext_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, TTFTEXT_DRAW, 0);
  3259. }
  3260. /* }}} */
  3261. /* {{{ php_imagettftext_common
  3262. */
  3263. static void php_imagettftext_common(INTERNAL_FUNCTION_PARAMETERS, int mode, int extended)
  3264. {
  3265. zval *IM, *EXT = NULL;
  3266. gdImagePtr im=NULL;
  3267. long col = -1, x = 0, y = 0;
  3268. int str_len, fontname_len, i, brect[8];
  3269. double ptsize, angle;
  3270. char *str = NULL, *fontname = NULL;
  3271. char *error = NULL;
  3272. int argc = ZEND_NUM_ARGS();
  3273. gdFTStringExtra strex = {0};
  3274. if (mode == TTFTEXT_BBOX) {
  3275. if (argc < 4 || argc > ((extended) ? 5 : 4)) {
  3276. ZEND_WRONG_PARAM_COUNT();
  3277. } else if (zend_parse_parameters(argc TSRMLS_CC, "ddss|a", &ptsize, &angle, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
  3278. RETURN_FALSE;
  3279. }
  3280. } else {
  3281. if (argc < 8 || argc > ((extended) ? 9 : 8)) {
  3282. ZEND_WRONG_PARAM_COUNT();
  3283. } else if (zend_parse_parameters(argc TSRMLS_CC, "rddlllss|a", &IM, &ptsize, &angle, &x, &y, &col, &fontname, &fontname_len, &str, &str_len, &EXT) == FAILURE) {
  3284. RETURN_FALSE;
  3285. }
  3286. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  3287. }
  3288. /* convert angle to radians */
  3289. angle = angle * (M_PI/180);
  3290. if (extended && EXT) { /* parse extended info */
  3291. HashPosition pos;
  3292. /* walk the assoc array */
  3293. zend_hash_internal_pointer_reset_ex(HASH_OF(EXT), &pos);
  3294. do {
  3295. zval ** item;
  3296. char * key;
  3297. ulong num_key;
  3298. if (zend_hash_get_current_key_ex(HASH_OF(EXT), &key, NULL, &num_key, 0, &pos) != HASH_KEY_IS_STRING) {
  3299. continue;
  3300. }
  3301. if (zend_hash_get_current_data_ex(HASH_OF(EXT), (void **) &item, &pos) == FAILURE) {
  3302. continue;
  3303. }
  3304. if (strcmp("linespacing", key) == 0) {
  3305. convert_to_double_ex(item);
  3306. strex.flags |= gdFTEX_LINESPACE;
  3307. strex.linespacing = Z_DVAL_PP(item);
  3308. }
  3309. } while (zend_hash_move_forward_ex(HASH_OF(EXT), &pos) == SUCCESS);
  3310. }
  3311. #ifdef VIRTUAL_DIR
  3312. {
  3313. char tmp_font_path[MAXPATHLEN];
  3314. if (!VCWD_REALPATH(fontname, tmp_font_path)) {
  3315. fontname = NULL;
  3316. }
  3317. }
  3318. #endif /* VIRTUAL_DIR */
  3319. PHP_GD_CHECK_OPEN_BASEDIR(fontname, "Invalid font filename");
  3320. #ifdef HAVE_GD_FREETYPE
  3321. if (extended) {
  3322. error = gdImageStringFTEx(im, brect, col, fontname, ptsize, angle, x, y, str, &strex);
  3323. }
  3324. else
  3325. error = gdImageStringFT(im, brect, col, fontname, ptsize, angle, x, y, str);
  3326. #endif /* HAVE_GD_FREETYPE */
  3327. if (error) {
  3328. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", error);
  3329. RETURN_FALSE;
  3330. }
  3331. array_init(return_value);
  3332. /* return array with the text's bounding box */
  3333. for (i = 0; i < 8; i++) {
  3334. add_next_index_long(return_value, brect[i]);
  3335. }
  3336. }
  3337. /* }}} */
  3338. #endif /* ENABLE_GD_TTF */
  3339. #if HAVE_LIBT1
  3340. /* {{{ php_free_ps_font
  3341. */
  3342. static void php_free_ps_font(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  3343. {
  3344. int *font = (int *) rsrc->ptr;
  3345. T1_DeleteFont(*font);
  3346. efree(font);
  3347. }
  3348. /* }}} */
  3349. /* {{{ php_free_ps_enc
  3350. */
  3351. static void php_free_ps_enc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  3352. {
  3353. char **enc = (char **) rsrc->ptr;
  3354. T1_DeleteEncoding(enc);
  3355. }
  3356. /* }}} */
  3357. /* {{{ proto resource imagepsloadfont(string pathname)
  3358. Load a new font from specified file */
  3359. PHP_FUNCTION(imagepsloadfont)
  3360. {
  3361. char *file;
  3362. int file_len, f_ind, *font;
  3363. #ifdef PHP_WIN32
  3364. struct stat st;
  3365. #endif
  3366. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &file, &file_len) == FAILURE) {
  3367. return;
  3368. }
  3369. #ifdef PHP_WIN32
  3370. if (VCWD_STAT(file, &st) < 0) {
  3371. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Font file not found (%s)", file);
  3372. RETURN_FALSE;
  3373. }
  3374. #endif
  3375. f_ind = T1_AddFont(file);
  3376. if (f_ind < 0) {
  3377. php_error_docref(NULL TSRMLS_CC, E_WARNING, "T1Lib Error (%i): %s", f_ind, T1_StrError(f_ind));
  3378. RETURN_FALSE;
  3379. }
  3380. if (T1_LoadFont(f_ind)) {
  3381. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't load the font");
  3382. RETURN_FALSE;
  3383. }
  3384. font = (int *) emalloc(sizeof(int));
  3385. *font = f_ind;
  3386. ZEND_REGISTER_RESOURCE(return_value, font, le_ps_font);
  3387. }
  3388. /* }}} */
  3389. /* {{{ proto int imagepscopyfont(int font_index)
  3390. Make a copy of a font for purposes like extending or reenconding */
  3391. /* The function in t1lib which this function uses seem to be buggy...
  3392. PHP_FUNCTION(imagepscopyfont)
  3393. {
  3394. int l_ind, type;
  3395. gd_ps_font *nf_ind, *of_ind;
  3396. long fnt;
  3397. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &fnt) == FAILURE) {
  3398. return;
  3399. }
  3400. of_ind = zend_list_find(fnt, &type);
  3401. if (type != le_ps_font) {
  3402. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%ld is not a Type 1 font index", fnt);
  3403. RETURN_FALSE;
  3404. }
  3405. nf_ind = emalloc(sizeof(gd_ps_font));
  3406. nf_ind->font_id = T1_CopyFont(of_ind->font_id);
  3407. if (nf_ind->font_id < 0) {
  3408. l_ind = nf_ind->font_id;
  3409. efree(nf_ind);
  3410. switch (l_ind) {
  3411. case -1:
  3412. php_error_docref(NULL TSRMLS_CC, E_WARNING, "FontID %d is not loaded in memory", l_ind);
  3413. RETURN_FALSE;
  3414. break;
  3415. case -2:
  3416. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Tried to copy a logical font");
  3417. RETURN_FALSE;
  3418. break;
  3419. case -3:
  3420. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Memory allocation fault in t1lib");
  3421. RETURN_FALSE;
  3422. break;
  3423. default:
  3424. php_error_docref(NULL TSRMLS_CC, E_WARNING, "An unknown error occurred in t1lib");
  3425. RETURN_FALSE;
  3426. break;
  3427. }
  3428. }
  3429. nf_ind->extend = 1;
  3430. l_ind = zend_list_insert(nf_ind, le_ps_font TSRMLS_CC);
  3431. RETURN_LONG(l_ind);
  3432. }
  3433. */
  3434. /* }}} */
  3435. /* {{{ proto bool imagepsfreefont(resource font_index)
  3436. Free memory used by a font */
  3437. PHP_FUNCTION(imagepsfreefont)
  3438. {
  3439. zval *fnt;
  3440. int *f_ind;
  3441. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &fnt) == FAILURE) {
  3442. return;
  3443. }
  3444. ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
  3445. zend_list_delete(Z_LVAL_P(fnt));
  3446. RETURN_TRUE;
  3447. }
  3448. /* }}} */
  3449. /* {{{ proto bool imagepsencodefont(resource font_index, string filename)
  3450. To change a fonts character encoding vector */
  3451. PHP_FUNCTION(imagepsencodefont)
  3452. {
  3453. zval *fnt;
  3454. char *enc, **enc_vector;
  3455. int enc_len, *f_ind;
  3456. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rp", &fnt, &enc, &enc_len) == FAILURE) {
  3457. return;
  3458. }
  3459. ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
  3460. if ((enc_vector = T1_LoadEncoding(enc)) == NULL) {
  3461. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't load encoding vector from %s", enc);
  3462. RETURN_FALSE;
  3463. }
  3464. T1_DeleteAllSizes(*f_ind);
  3465. if (T1_ReencodeFont(*f_ind, enc_vector)) {
  3466. T1_DeleteEncoding(enc_vector);
  3467. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't re-encode font");
  3468. RETURN_FALSE;
  3469. }
  3470. zend_list_insert(enc_vector, le_ps_enc TSRMLS_CC);
  3471. RETURN_TRUE;
  3472. }
  3473. /* }}} */
  3474. /* {{{ proto bool imagepsextendfont(resource font_index, float extend)
  3475. Extend or or condense (if extend < 1) a font */
  3476. PHP_FUNCTION(imagepsextendfont)
  3477. {
  3478. zval *fnt;
  3479. double ext;
  3480. int *f_ind;
  3481. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &fnt, &ext) == FAILURE) {
  3482. return;
  3483. }
  3484. ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
  3485. T1_DeleteAllSizes(*f_ind);
  3486. if (ext <= 0) {
  3487. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Second parameter %F out of range (must be > 0)", ext);
  3488. RETURN_FALSE;
  3489. }
  3490. if (T1_ExtendFont(*f_ind, ext) != 0) {
  3491. RETURN_FALSE;
  3492. }
  3493. RETURN_TRUE;
  3494. }
  3495. /* }}} */
  3496. /* {{{ proto bool imagepsslantfont(resource font_index, float slant)
  3497. Slant a font */
  3498. PHP_FUNCTION(imagepsslantfont)
  3499. {
  3500. zval *fnt;
  3501. double slt;
  3502. int *f_ind;
  3503. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rd", &fnt, &slt) == FAILURE) {
  3504. return;
  3505. }
  3506. ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
  3507. if (T1_SlantFont(*f_ind, slt) != 0) {
  3508. RETURN_FALSE;
  3509. }
  3510. RETURN_TRUE;
  3511. }
  3512. /* }}} */
  3513. /* {{{ proto array imagepstext(resource image, string text, resource font, int size, int foreground, int background, int xcoord, int ycoord [, int space [, int tightness [, float angle [, int antialias])
  3514. Rasterize a string over an image */
  3515. PHP_FUNCTION(imagepstext)
  3516. {
  3517. zval *img, *fnt;
  3518. int i, j;
  3519. long _fg, _bg, x, y, size, space = 0, aa_steps = 4, width = 0;
  3520. int *f_ind;
  3521. int h_lines, v_lines, c_ind;
  3522. int rd, gr, bl, fg_rd, fg_gr, fg_bl, bg_rd, bg_gr, bg_bl;
  3523. int fg_al, bg_al, al;
  3524. int aa[16];
  3525. int amount_kern, add_width;
  3526. double angle = 0.0, extend;
  3527. unsigned long aa_greys[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  3528. gdImagePtr bg_img;
  3529. GLYPH *str_img;
  3530. T1_OUTLINE *char_path, *str_path;
  3531. T1_TMATRIX *transform = NULL;
  3532. char *str;
  3533. int str_len;
  3534. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsrlllll|lldl", &img, &str, &str_len, &fnt, &size, &_fg, &_bg, &x, &y, &space, &width, &angle, &aa_steps) == FAILURE) {
  3535. return;
  3536. }
  3537. if (aa_steps != 4 && aa_steps != 16) {
  3538. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Antialias steps must be 4 or 16");
  3539. RETURN_FALSE;
  3540. }
  3541. ZEND_FETCH_RESOURCE(bg_img, gdImagePtr, &img, -1, "Image", le_gd);
  3542. ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
  3543. /* Ensure that the provided colors are valid */
  3544. if (_fg < 0 || (!gdImageTrueColor(bg_img) && _fg > gdImageColorsTotal(bg_img))) {
  3545. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Foreground color index %ld out of range", _fg);
  3546. RETURN_FALSE;
  3547. }
  3548. if (_bg < 0 || (!gdImageTrueColor(bg_img) && _fg > gdImageColorsTotal(bg_img))) {
  3549. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Background color index %ld out of range", _bg);
  3550. RETURN_FALSE;
  3551. }
  3552. fg_rd = gdImageRed (bg_img, _fg);
  3553. fg_gr = gdImageGreen(bg_img, _fg);
  3554. fg_bl = gdImageBlue (bg_img, _fg);
  3555. fg_al = gdImageAlpha(bg_img, _fg);
  3556. bg_rd = gdImageRed (bg_img, _bg);
  3557. bg_gr = gdImageGreen(bg_img, _bg);
  3558. bg_bl = gdImageBlue (bg_img, _bg);
  3559. bg_al = gdImageAlpha(bg_img, _bg);
  3560. for (i = 0; i < aa_steps; i++) {
  3561. rd = bg_rd + (double) (fg_rd - bg_rd) / aa_steps * (i + 1);
  3562. gr = bg_gr + (double) (fg_gr - bg_gr) / aa_steps * (i + 1);
  3563. bl = bg_bl + (double) (fg_bl - bg_bl) / aa_steps * (i + 1);
  3564. al = bg_al + (double) (fg_al - bg_al) / aa_steps * (i + 1);
  3565. aa[i] = gdImageColorResolveAlpha(bg_img, rd, gr, bl, al);
  3566. }
  3567. T1_AASetBitsPerPixel(8);
  3568. switch (aa_steps) {
  3569. case 4:
  3570. T1_AASetGrayValues(0, 1, 2, 3, 4);
  3571. T1_AASetLevel(T1_AA_LOW);
  3572. break;
  3573. case 16:
  3574. T1_AAHSetGrayValues(aa_greys);
  3575. T1_AASetLevel(T1_AA_HIGH);
  3576. break;
  3577. default:
  3578. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid value %ld as number of steps for antialiasing", aa_steps);
  3579. RETURN_FALSE;
  3580. }
  3581. if (angle) {
  3582. transform = T1_RotateMatrix(NULL, angle);
  3583. }
  3584. if (width) {
  3585. extend = T1_GetExtend(*f_ind);
  3586. str_path = T1_GetCharOutline(*f_ind, str[0], size, transform);
  3587. if (!str_path) {
  3588. if (T1_errno) {
  3589. php_error_docref(NULL TSRMLS_CC, E_WARNING, "T1Lib Error: %s", T1_StrError(T1_errno));
  3590. }
  3591. RETURN_FALSE;
  3592. }
  3593. for (i = 1; i < str_len; i++) {
  3594. amount_kern = (int) T1_GetKerning(*f_ind, str[i - 1], str[i]);
  3595. amount_kern += str[i - 1] == ' ' ? space : 0;
  3596. add_width = (int) (amount_kern + width) / extend;
  3597. char_path = T1_GetMoveOutline(*f_ind, add_width, 0, 0, size, transform);
  3598. str_path = T1_ConcatOutlines(str_path, char_path);
  3599. char_path = T1_GetCharOutline(*f_ind, str[i], size, transform);
  3600. str_path = T1_ConcatOutlines(str_path, char_path);
  3601. }
  3602. str_img = T1_AAFillOutline(str_path, 0);
  3603. } else {
  3604. str_img = T1_AASetString(*f_ind, str, str_len, space, T1_KERNING, size, transform);
  3605. }
  3606. if (T1_errno) {
  3607. php_error_docref(NULL TSRMLS_CC, E_WARNING, "T1Lib Error: %s", T1_StrError(T1_errno));
  3608. RETURN_FALSE;
  3609. }
  3610. h_lines = str_img->metrics.ascent - str_img->metrics.descent;
  3611. v_lines = str_img->metrics.rightSideBearing - str_img->metrics.leftSideBearing;
  3612. for (i = 0; i < v_lines; i++) {
  3613. for (j = 0; j < h_lines; j++) {
  3614. switch (str_img->bits[j * v_lines + i]) {
  3615. case 0:
  3616. break;
  3617. default:
  3618. c_ind = aa[str_img->bits[j * v_lines + i] - 1];
  3619. gdImageSetPixel(bg_img, x + str_img->metrics.leftSideBearing + i, y - str_img->metrics.ascent + j, c_ind);
  3620. break;
  3621. }
  3622. }
  3623. }
  3624. array_init(return_value);
  3625. add_next_index_long(return_value, str_img->metrics.leftSideBearing);
  3626. add_next_index_long(return_value, str_img->metrics.descent);
  3627. add_next_index_long(return_value, str_img->metrics.rightSideBearing);
  3628. add_next_index_long(return_value, str_img->metrics.ascent);
  3629. }
  3630. /* }}} */
  3631. /* {{{ proto array imagepsbbox(string text, resource font, int size [, int space, int tightness, float angle])
  3632. Return the bounding box needed by a string if rasterized */
  3633. PHP_FUNCTION(imagepsbbox)
  3634. {
  3635. zval *fnt;
  3636. long sz = 0, sp = 0, wd = 0;
  3637. char *str;
  3638. int i, space = 0, add_width = 0, char_width, amount_kern;
  3639. int cur_x, cur_y, dx, dy;
  3640. int x1, y1, x2, y2, x3, y3, x4, y4;
  3641. int *f_ind;
  3642. int str_len, per_char = 0;
  3643. int argc = ZEND_NUM_ARGS();
  3644. double angle = 0, sin_a = 0, cos_a = 0;
  3645. BBox char_bbox, str_bbox = {0, 0, 0, 0};
  3646. if (argc != 3 && argc != 6) {
  3647. ZEND_WRONG_PARAM_COUNT();
  3648. }
  3649. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "srl|lld", &str, &str_len, &fnt, &sz, &sp, &wd, &angle) == FAILURE) {
  3650. return;
  3651. }
  3652. if (argc == 6) {
  3653. space = sp;
  3654. add_width = wd;
  3655. angle = angle * M_PI / 180;
  3656. sin_a = sin(angle);
  3657. cos_a = cos(angle);
  3658. per_char = add_width || angle ? 1 : 0;
  3659. }
  3660. ZEND_FETCH_RESOURCE(f_ind, int *, &fnt, -1, "Type 1 font", le_ps_font);
  3661. #define max(a, b) (a > b ? a : b)
  3662. #define min(a, b) (a < b ? a : b)
  3663. #define new_x(a, b) (int) ((a) * cos_a - (b) * sin_a)
  3664. #define new_y(a, b) (int) ((a) * sin_a + (b) * cos_a)
  3665. if (per_char) {
  3666. space += T1_GetCharWidth(*f_ind, ' ');
  3667. cur_x = cur_y = 0;
  3668. for (i = 0; i < str_len; i++) {
  3669. if (str[i] == ' ') {
  3670. char_bbox.llx = char_bbox.lly = char_bbox.ury = 0;
  3671. char_bbox.urx = char_width = space;
  3672. } else {
  3673. char_bbox = T1_GetCharBBox(*f_ind, str[i]);
  3674. char_width = T1_GetCharWidth(*f_ind, str[i]);
  3675. }
  3676. amount_kern = i ? T1_GetKerning(*f_ind, str[i - 1], str[i]) : 0;
  3677. /* Transfer character bounding box to right place */
  3678. x1 = new_x(char_bbox.llx, char_bbox.lly) + cur_x;
  3679. y1 = new_y(char_bbox.llx, char_bbox.lly) + cur_y;
  3680. x2 = new_x(char_bbox.llx, char_bbox.ury) + cur_x;
  3681. y2 = new_y(char_bbox.llx, char_bbox.ury) + cur_y;
  3682. x3 = new_x(char_bbox.urx, char_bbox.ury) + cur_x;
  3683. y3 = new_y(char_bbox.urx, char_bbox.ury) + cur_y;
  3684. x4 = new_x(char_bbox.urx, char_bbox.lly) + cur_x;
  3685. y4 = new_y(char_bbox.urx, char_bbox.lly) + cur_y;
  3686. /* Find min & max values and compare them with current bounding box */
  3687. str_bbox.llx = min(str_bbox.llx, min(x1, min(x2, min(x3, x4))));
  3688. str_bbox.lly = min(str_bbox.lly, min(y1, min(y2, min(y3, y4))));
  3689. str_bbox.urx = max(str_bbox.urx, max(x1, max(x2, max(x3, x4))));
  3690. str_bbox.ury = max(str_bbox.ury, max(y1, max(y2, max(y3, y4))));
  3691. /* Move to the next base point */
  3692. dx = new_x(char_width + add_width + amount_kern, 0);
  3693. dy = new_y(char_width + add_width + amount_kern, 0);
  3694. cur_x += dx;
  3695. cur_y += dy;
  3696. /*
  3697. printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", x1, y1, x2, y2, x3, y3, x4, y4, char_bbox.llx, char_bbox.lly, char_bbox.urx, char_bbox.ury, char_width, amount_kern, cur_x, cur_y, dx, dy);
  3698. */
  3699. }
  3700. } else {
  3701. str_bbox = T1_GetStringBBox(*f_ind, str, str_len, space, T1_KERNING);
  3702. }
  3703. if (T1_errno) {
  3704. RETURN_FALSE;
  3705. }
  3706. array_init(return_value);
  3707. /*
  3708. printf("%d %d %d %d\n", str_bbox.llx, str_bbox.lly, str_bbox.urx, str_bbox.ury);
  3709. */
  3710. add_next_index_long(return_value, (int) ceil(((double) str_bbox.llx)*sz/1000));
  3711. add_next_index_long(return_value, (int) ceil(((double) str_bbox.lly)*sz/1000));
  3712. add_next_index_long(return_value, (int) ceil(((double) str_bbox.urx)*sz/1000));
  3713. add_next_index_long(return_value, (int) ceil(((double) str_bbox.ury)*sz/1000));
  3714. }
  3715. /* }}} */
  3716. #endif
  3717. /* {{{ proto bool image2wbmp(resource im [, string filename [, int threshold]])
  3718. Output WBMP image to browser or file */
  3719. PHP_FUNCTION(image2wbmp)
  3720. {
  3721. _php_image_output(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_CONVERT_WBM, "WBMP", _php_image_bw_convert);
  3722. }
  3723. /* }}} */
  3724. #if defined(HAVE_GD_JPG)
  3725. /* {{{ proto bool jpeg2wbmp (string f_org, string f_dest, int d_height, int d_width, int threshold)
  3726. Convert JPEG image to WBMP image */
  3727. PHP_FUNCTION(jpeg2wbmp)
  3728. {
  3729. _php_image_convert(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_JPG);
  3730. }
  3731. /* }}} */
  3732. #endif
  3733. #if defined(HAVE_GD_PNG)
  3734. /* {{{ proto bool png2wbmp (string f_org, string f_dest, int d_height, int d_width, int threshold)
  3735. Convert PNG image to WBMP image */
  3736. PHP_FUNCTION(png2wbmp)
  3737. {
  3738. _php_image_convert(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_GDIMG_TYPE_PNG);
  3739. }
  3740. /* }}} */
  3741. #endif
  3742. /* {{{ _php_image_bw_convert
  3743. * It converts a gd Image to bw using a threshold value */
  3744. static void _php_image_bw_convert(gdImagePtr im_org, gdIOCtx *out, int threshold)
  3745. {
  3746. gdImagePtr im_dest;
  3747. int white, black;
  3748. int color, color_org, median;
  3749. int dest_height = gdImageSY(im_org);
  3750. int dest_width = gdImageSX(im_org);
  3751. int x, y;
  3752. TSRMLS_FETCH();
  3753. im_dest = gdImageCreate(dest_width, dest_height);
  3754. if (im_dest == NULL) {
  3755. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate temporary buffer");
  3756. return;
  3757. }
  3758. white = gdImageColorAllocate(im_dest, 255, 255, 255);
  3759. if (white == -1) {
  3760. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate the colors for the destination buffer");
  3761. return;
  3762. }
  3763. black = gdImageColorAllocate(im_dest, 0, 0, 0);
  3764. if (black == -1) {
  3765. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate the colors for the destination buffer");
  3766. return;
  3767. }
  3768. if (im_org->trueColor) {
  3769. gdImageTrueColorToPalette(im_org, 1, 256);
  3770. }
  3771. for (y = 0; y < dest_height; y++) {
  3772. for (x = 0; x < dest_width; x++) {
  3773. color_org = gdImageGetPixel(im_org, x, y);
  3774. median = (im_org->red[color_org] + im_org->green[color_org] + im_org->blue[color_org]) / 3;
  3775. if (median < threshold) {
  3776. color = black;
  3777. } else {
  3778. color = white;
  3779. }
  3780. gdImageSetPixel (im_dest, x, y, color);
  3781. }
  3782. }
  3783. gdImageWBMPCtx (im_dest, black, out);
  3784. }
  3785. /* }}} */
  3786. /* {{{ _php_image_convert
  3787. * _php_image_convert converts jpeg/png images to wbmp and resizes them as needed */
  3788. static void _php_image_convert(INTERNAL_FUNCTION_PARAMETERS, int image_type )
  3789. {
  3790. char *f_org, *f_dest;
  3791. int f_org_len, f_dest_len;
  3792. long height, width, threshold;
  3793. gdImagePtr im_org, im_dest, im_tmp;
  3794. char *fn_org = NULL;
  3795. char *fn_dest = NULL;
  3796. FILE *org, *dest;
  3797. int dest_height = -1;
  3798. int dest_width = -1;
  3799. int org_height, org_width;
  3800. int white, black;
  3801. int color, color_org, median;
  3802. int int_threshold;
  3803. int x, y;
  3804. float x_ratio, y_ratio;
  3805. long ignore_warning;
  3806. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "pplll", &f_org, &f_org_len, &f_dest, &f_dest_len, &height, &width, &threshold) == FAILURE) {
  3807. return;
  3808. }
  3809. fn_org = f_org;
  3810. fn_dest = f_dest;
  3811. dest_height = height;
  3812. dest_width = width;
  3813. int_threshold = threshold;
  3814. /* Check threshold value */
  3815. if (int_threshold < 0 || int_threshold > 8) {
  3816. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid threshold value '%d'", int_threshold);
  3817. RETURN_FALSE;
  3818. }
  3819. /* Check origin file */
  3820. PHP_GD_CHECK_OPEN_BASEDIR(fn_org, "Invalid origin filename");
  3821. /* Check destination file */
  3822. PHP_GD_CHECK_OPEN_BASEDIR(fn_dest, "Invalid destination filename");
  3823. /* Open origin file */
  3824. org = VCWD_FOPEN(fn_org, "rb");
  3825. if (!org) {
  3826. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for reading", fn_org);
  3827. RETURN_FALSE;
  3828. }
  3829. /* Open destination file */
  3830. dest = VCWD_FOPEN(fn_dest, "wb");
  3831. if (!dest) {
  3832. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' for writing", fn_dest);
  3833. fclose(org);
  3834. RETURN_FALSE;
  3835. }
  3836. switch (image_type) {
  3837. case PHP_GDIMG_TYPE_GIF:
  3838. im_org = gdImageCreateFromGif(org);
  3839. if (im_org == NULL) {
  3840. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' Not a valid GIF file", fn_dest);
  3841. fclose(org);
  3842. fclose(dest);
  3843. RETURN_FALSE;
  3844. }
  3845. break;
  3846. #ifdef HAVE_GD_JPG
  3847. case PHP_GDIMG_TYPE_JPG:
  3848. ignore_warning = INI_INT("gd.jpeg_ignore_warning");
  3849. im_org = gdImageCreateFromJpegEx(org, ignore_warning);
  3850. if (im_org == NULL) {
  3851. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' Not a valid JPEG file", fn_dest);
  3852. fclose(org);
  3853. fclose(dest);
  3854. RETURN_FALSE;
  3855. }
  3856. break;
  3857. #endif /* HAVE_GD_JPG */
  3858. #ifdef HAVE_GD_PNG
  3859. case PHP_GDIMG_TYPE_PNG:
  3860. im_org = gdImageCreateFromPng(org);
  3861. if (im_org == NULL) {
  3862. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open '%s' Not a valid PNG file", fn_dest);
  3863. fclose(org);
  3864. fclose(dest);
  3865. RETURN_FALSE;
  3866. }
  3867. break;
  3868. #endif /* HAVE_GD_PNG */
  3869. default:
  3870. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Format not supported");
  3871. fclose(org);
  3872. fclose(dest);
  3873. RETURN_FALSE;
  3874. break;
  3875. }
  3876. fclose(org);
  3877. org_width = gdImageSX (im_org);
  3878. org_height = gdImageSY (im_org);
  3879. x_ratio = (float) org_width / (float) dest_width;
  3880. y_ratio = (float) org_height / (float) dest_height;
  3881. if (x_ratio > 1 && y_ratio > 1) {
  3882. if (y_ratio > x_ratio) {
  3883. x_ratio = y_ratio;
  3884. } else {
  3885. y_ratio = x_ratio;
  3886. }
  3887. dest_width = (int) (org_width / x_ratio);
  3888. dest_height = (int) (org_height / y_ratio);
  3889. } else {
  3890. x_ratio = (float) dest_width / (float) org_width;
  3891. y_ratio = (float) dest_height / (float) org_height;
  3892. if (y_ratio < x_ratio) {
  3893. x_ratio = y_ratio;
  3894. } else {
  3895. y_ratio = x_ratio;
  3896. }
  3897. dest_width = (int) (org_width * x_ratio);
  3898. dest_height = (int) (org_height * y_ratio);
  3899. }
  3900. im_tmp = gdImageCreate (dest_width, dest_height);
  3901. if (im_tmp == NULL ) {
  3902. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate temporary buffer");
  3903. fclose(dest);
  3904. gdImageDestroy(im_org);
  3905. RETURN_FALSE;
  3906. }
  3907. gdImageCopyResized (im_tmp, im_org, 0, 0, 0, 0, dest_width, dest_height, org_width, org_height);
  3908. gdImageDestroy(im_org);
  3909. im_dest = gdImageCreate(dest_width, dest_height);
  3910. if (im_dest == NULL) {
  3911. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate destination buffer");
  3912. fclose(dest);
  3913. gdImageDestroy(im_tmp);
  3914. RETURN_FALSE;
  3915. }
  3916. white = gdImageColorAllocate(im_dest, 255, 255, 255);
  3917. if (white == -1) {
  3918. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate the colors for the destination buffer");
  3919. fclose(dest);
  3920. gdImageDestroy(im_tmp);
  3921. gdImageDestroy(im_dest);
  3922. RETURN_FALSE;
  3923. }
  3924. black = gdImageColorAllocate(im_dest, 0, 0, 0);
  3925. if (black == -1) {
  3926. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate the colors for the destination buffer");
  3927. fclose(dest);
  3928. gdImageDestroy(im_tmp);
  3929. gdImageDestroy(im_dest);
  3930. RETURN_FALSE;
  3931. }
  3932. int_threshold = int_threshold * 32;
  3933. for (y = 0; y < dest_height; y++) {
  3934. for (x = 0; x < dest_width; x++) {
  3935. color_org = gdImageGetPixel (im_tmp, x, y);
  3936. median = (im_tmp->red[color_org] + im_tmp->green[color_org] + im_tmp->blue[color_org]) / 3;
  3937. if (median < int_threshold) {
  3938. color = black;
  3939. } else {
  3940. color = white;
  3941. }
  3942. gdImageSetPixel (im_dest, x, y, color);
  3943. }
  3944. }
  3945. gdImageDestroy (im_tmp );
  3946. gdImageWBMP(im_dest, black , dest);
  3947. fflush(dest);
  3948. fclose(dest);
  3949. gdImageDestroy(im_dest);
  3950. RETURN_TRUE;
  3951. }
  3952. /* }}} */
  3953. /* Section Filters */
  3954. #define PHP_GD_SINGLE_RES \
  3955. zval *SIM; \
  3956. gdImagePtr im_src; \
  3957. if (zend_parse_parameters(1 TSRMLS_CC, "r", &SIM) == FAILURE) { \
  3958. RETURN_FALSE; \
  3959. } \
  3960. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd); \
  3961. if (im_src == NULL) { \
  3962. RETURN_FALSE; \
  3963. }
  3964. static void php_image_filter_negate(INTERNAL_FUNCTION_PARAMETERS)
  3965. {
  3966. PHP_GD_SINGLE_RES
  3967. if (gdImageNegate(im_src) == 1) {
  3968. RETURN_TRUE;
  3969. }
  3970. RETURN_FALSE;
  3971. }
  3972. static void php_image_filter_grayscale(INTERNAL_FUNCTION_PARAMETERS)
  3973. {
  3974. PHP_GD_SINGLE_RES
  3975. if (gdImageGrayScale(im_src) == 1) {
  3976. RETURN_TRUE;
  3977. }
  3978. RETURN_FALSE;
  3979. }
  3980. static void php_image_filter_brightness(INTERNAL_FUNCTION_PARAMETERS)
  3981. {
  3982. zval *SIM;
  3983. gdImagePtr im_src;
  3984. long brightness, tmp;
  3985. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zll", &SIM, &tmp, &brightness) == FAILURE) {
  3986. RETURN_FALSE;
  3987. }
  3988. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  3989. if (im_src == NULL) {
  3990. RETURN_FALSE;
  3991. }
  3992. if (gdImageBrightness(im_src, (int)brightness) == 1) {
  3993. RETURN_TRUE;
  3994. }
  3995. RETURN_FALSE;
  3996. }
  3997. static void php_image_filter_contrast(INTERNAL_FUNCTION_PARAMETERS)
  3998. {
  3999. zval *SIM;
  4000. gdImagePtr im_src;
  4001. long contrast, tmp;
  4002. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &SIM, &tmp, &contrast) == FAILURE) {
  4003. RETURN_FALSE;
  4004. }
  4005. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  4006. if (im_src == NULL) {
  4007. RETURN_FALSE;
  4008. }
  4009. if (gdImageContrast(im_src, (int)contrast) == 1) {
  4010. RETURN_TRUE;
  4011. }
  4012. RETURN_FALSE;
  4013. }
  4014. static void php_image_filter_colorize(INTERNAL_FUNCTION_PARAMETERS)
  4015. {
  4016. zval *SIM;
  4017. gdImagePtr im_src;
  4018. long r,g,b,tmp;
  4019. long a = 0;
  4020. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll|l", &SIM, &tmp, &r, &g, &b, &a) == FAILURE) {
  4021. RETURN_FALSE;
  4022. }
  4023. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  4024. if (im_src == NULL) {
  4025. RETURN_FALSE;
  4026. }
  4027. if (gdImageColor(im_src, (int) r, (int) g, (int) b, (int) a) == 1) {
  4028. RETURN_TRUE;
  4029. }
  4030. RETURN_FALSE;
  4031. }
  4032. static void php_image_filter_edgedetect(INTERNAL_FUNCTION_PARAMETERS)
  4033. {
  4034. PHP_GD_SINGLE_RES
  4035. if (gdImageEdgeDetectQuick(im_src) == 1) {
  4036. RETURN_TRUE;
  4037. }
  4038. RETURN_FALSE;
  4039. }
  4040. static void php_image_filter_emboss(INTERNAL_FUNCTION_PARAMETERS)
  4041. {
  4042. PHP_GD_SINGLE_RES
  4043. if (gdImageEmboss(im_src) == 1) {
  4044. RETURN_TRUE;
  4045. }
  4046. RETURN_FALSE;
  4047. }
  4048. static void php_image_filter_gaussian_blur(INTERNAL_FUNCTION_PARAMETERS)
  4049. {
  4050. PHP_GD_SINGLE_RES
  4051. if (gdImageGaussianBlur(im_src) == 1) {
  4052. RETURN_TRUE;
  4053. }
  4054. RETURN_FALSE;
  4055. }
  4056. static void php_image_filter_selective_blur(INTERNAL_FUNCTION_PARAMETERS)
  4057. {
  4058. PHP_GD_SINGLE_RES
  4059. if (gdImageSelectiveBlur(im_src) == 1) {
  4060. RETURN_TRUE;
  4061. }
  4062. RETURN_FALSE;
  4063. }
  4064. static void php_image_filter_mean_removal(INTERNAL_FUNCTION_PARAMETERS)
  4065. {
  4066. PHP_GD_SINGLE_RES
  4067. if (gdImageMeanRemoval(im_src) == 1) {
  4068. RETURN_TRUE;
  4069. }
  4070. RETURN_FALSE;
  4071. }
  4072. static void php_image_filter_smooth(INTERNAL_FUNCTION_PARAMETERS)
  4073. {
  4074. zval *SIM;
  4075. long tmp;
  4076. gdImagePtr im_src;
  4077. double weight;
  4078. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rld", &SIM, &tmp, &weight) == FAILURE) {
  4079. RETURN_FALSE;
  4080. }
  4081. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  4082. if (im_src == NULL) {
  4083. RETURN_FALSE;
  4084. }
  4085. if (gdImageSmooth(im_src, (float)weight)==1) {
  4086. RETURN_TRUE;
  4087. }
  4088. RETURN_FALSE;
  4089. }
  4090. static void php_image_filter_pixelate(INTERNAL_FUNCTION_PARAMETERS)
  4091. {
  4092. zval *IM;
  4093. gdImagePtr im;
  4094. long tmp, blocksize;
  4095. zend_bool mode = 0;
  4096. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll|b", &IM, &tmp, &blocksize, &mode) == FAILURE) {
  4097. RETURN_FALSE;
  4098. }
  4099. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  4100. if (im == NULL) {
  4101. RETURN_FALSE;
  4102. }
  4103. if (gdImagePixelate(im, (int) blocksize, (const unsigned int) mode)) {
  4104. RETURN_TRUE;
  4105. }
  4106. RETURN_FALSE;
  4107. }
  4108. /* {{{ proto bool imagefilter(resource src_im, int filtertype, [args] )
  4109. Applies Filter an image using a custom angle */
  4110. PHP_FUNCTION(imagefilter)
  4111. {
  4112. zval *tmp;
  4113. typedef void (*image_filter)(INTERNAL_FUNCTION_PARAMETERS);
  4114. long filtertype;
  4115. image_filter filters[] =
  4116. {
  4117. php_image_filter_negate ,
  4118. php_image_filter_grayscale,
  4119. php_image_filter_brightness,
  4120. php_image_filter_contrast,
  4121. php_image_filter_colorize,
  4122. php_image_filter_edgedetect,
  4123. php_image_filter_emboss,
  4124. php_image_filter_gaussian_blur,
  4125. php_image_filter_selective_blur,
  4126. php_image_filter_mean_removal,
  4127. php_image_filter_smooth,
  4128. php_image_filter_pixelate
  4129. };
  4130. if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > IMAGE_FILTER_MAX_ARGS) {
  4131. WRONG_PARAM_COUNT;
  4132. } else if (zend_parse_parameters(2 TSRMLS_CC, "rl", &tmp, &filtertype) == FAILURE) {
  4133. return;
  4134. }
  4135. if (filtertype >= 0 && filtertype <= IMAGE_FILTER_MAX) {
  4136. filters[filtertype](INTERNAL_FUNCTION_PARAM_PASSTHRU);
  4137. }
  4138. }
  4139. /* }}} */
  4140. /* {{{ proto resource imageconvolution(resource src_im, array matrix3x3, double div, double offset)
  4141. Apply a 3x3 convolution matrix, using coefficient div and offset */
  4142. PHP_FUNCTION(imageconvolution)
  4143. {
  4144. zval *SIM, *hash_matrix;
  4145. zval **var = NULL, **var2 = NULL;
  4146. gdImagePtr im_src = NULL;
  4147. double div, offset;
  4148. int nelem, i, j, res;
  4149. float matrix[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};
  4150. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "radd", &SIM, &hash_matrix, &div, &offset) == FAILURE) {
  4151. RETURN_FALSE;
  4152. }
  4153. ZEND_FETCH_RESOURCE(im_src, gdImagePtr, &SIM, -1, "Image", le_gd);
  4154. nelem = zend_hash_num_elements(Z_ARRVAL_P(hash_matrix));
  4155. if (nelem != 3) {
  4156. php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must have 3x3 array");
  4157. RETURN_FALSE;
  4158. }
  4159. for (i=0; i<3; i++) {
  4160. if (zend_hash_index_find(Z_ARRVAL_P(hash_matrix), (i), (void **) &var) == SUCCESS && Z_TYPE_PP(var) == IS_ARRAY) {
  4161. if (Z_TYPE_PP(var) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_PP(var)) != 3 ) {
  4162. php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must have 3x3 array");
  4163. RETURN_FALSE;
  4164. }
  4165. for (j=0; j<3; j++) {
  4166. if (zend_hash_index_find(Z_ARRVAL_PP(var), (j), (void **) &var2) == SUCCESS) {
  4167. if (Z_TYPE_PP(var2) != IS_DOUBLE) {
  4168. zval dval;
  4169. dval = **var2;
  4170. zval_copy_ctor(&dval);
  4171. convert_to_double(&dval);
  4172. matrix[i][j] = (float)Z_DVAL(dval);
  4173. } else {
  4174. matrix[i][j] = (float)Z_DVAL_PP(var2);
  4175. }
  4176. } else {
  4177. php_error_docref(NULL TSRMLS_CC, E_WARNING, "You must have a 3x3 matrix");
  4178. RETURN_FALSE;
  4179. }
  4180. }
  4181. }
  4182. }
  4183. res = gdImageConvolution(im_src, matrix, (float)div, (float)offset);
  4184. if (res) {
  4185. RETURN_TRUE;
  4186. } else {
  4187. RETURN_FALSE;
  4188. }
  4189. }
  4190. /* }}} */
  4191. /* End section: Filters */
  4192. /* {{{ proto void imageflip(resource im, int mode)
  4193. Flip an image (in place) horizontally, vertically or both directions. */
  4194. PHP_FUNCTION(imageflip)
  4195. {
  4196. zval *IM;
  4197. long mode;
  4198. gdImagePtr im;
  4199. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &IM, &mode) == FAILURE) {
  4200. return;
  4201. }
  4202. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  4203. switch (mode) {
  4204. case GD_FLIP_VERTICAL:
  4205. gdImageFlipVertical(im);
  4206. break;
  4207. case GD_FLIP_HORINZONTAL:
  4208. gdImageFlipHorizontal(im);
  4209. break;
  4210. case GD_FLIP_BOTH:
  4211. gdImageFlipBoth(im);
  4212. break;
  4213. default:
  4214. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown flip mode");
  4215. RETURN_FALSE;
  4216. }
  4217. RETURN_TRUE;
  4218. }
  4219. /* }}} */
  4220. #ifdef HAVE_GD_BUNDLED
  4221. /* {{{ proto bool imageantialias(resource im, bool on)
  4222. Should antialiased functions used or not*/
  4223. PHP_FUNCTION(imageantialias)
  4224. {
  4225. zval *IM;
  4226. zend_bool alias;
  4227. gdImagePtr im;
  4228. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rb", &IM, &alias) == FAILURE) {
  4229. return;
  4230. }
  4231. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  4232. gdImageAntialias(im, alias);
  4233. RETURN_TRUE;
  4234. }
  4235. /* }}} */
  4236. #endif
  4237. /* {{{ proto void imagecrop(resource im, array rect)
  4238. Crop an image using the given coordinates and size, x, y, width and height. */
  4239. PHP_FUNCTION(imagecrop)
  4240. {
  4241. zval *IM;
  4242. gdImagePtr im;
  4243. gdImagePtr im_crop;
  4244. gdRect rect;
  4245. zval *z_rect;
  4246. zval **tmp;
  4247. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &IM, &z_rect) == FAILURE) {
  4248. return;
  4249. }
  4250. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  4251. if (zend_hash_find(HASH_OF(z_rect), "x", sizeof("x"), (void **)&tmp) != FAILURE) {
  4252. if (Z_TYPE_PP(tmp) != IS_LONG) {
  4253. zval lval;
  4254. lval = **tmp;
  4255. zval_copy_ctor(&lval);
  4256. convert_to_long(&lval);
  4257. rect.x = Z_LVAL(lval);
  4258. } else {
  4259. rect.x = Z_LVAL_PP(tmp);
  4260. }
  4261. } else {
  4262. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing x position");
  4263. RETURN_FALSE;
  4264. }
  4265. if (zend_hash_find(HASH_OF(z_rect), "y", sizeof("x"), (void **)&tmp) != FAILURE) {
  4266. if (Z_TYPE_PP(tmp) != IS_LONG) {
  4267. zval lval;
  4268. lval = **tmp;
  4269. zval_copy_ctor(&lval);
  4270. convert_to_long(&lval);
  4271. rect.y = Z_LVAL(lval);
  4272. } else {
  4273. rect.y = Z_LVAL_PP(tmp);
  4274. }
  4275. } else {
  4276. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing y position");
  4277. RETURN_FALSE;
  4278. }
  4279. if (zend_hash_find(HASH_OF(z_rect), "width", sizeof("width"), (void **)&tmp) != FAILURE) {
  4280. if (Z_TYPE_PP(tmp) != IS_LONG) {
  4281. zval lval;
  4282. lval = **tmp;
  4283. zval_copy_ctor(&lval);
  4284. convert_to_long(&lval);
  4285. rect.width = Z_LVAL(lval);
  4286. } else {
  4287. rect.width = Z_LVAL_PP(tmp);
  4288. }
  4289. } else {
  4290. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing width");
  4291. RETURN_FALSE;
  4292. }
  4293. if (zend_hash_find(HASH_OF(z_rect), "height", sizeof("height"), (void **)&tmp) != FAILURE) {
  4294. if (Z_TYPE_PP(tmp) != IS_LONG) {
  4295. zval lval;
  4296. lval = **tmp;
  4297. zval_copy_ctor(&lval);
  4298. convert_to_long(&lval);
  4299. rect.height = Z_LVAL(lval);
  4300. } else {
  4301. rect.height = Z_LVAL_PP(tmp);
  4302. }
  4303. } else {
  4304. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing height");
  4305. RETURN_FALSE;
  4306. }
  4307. im_crop = gdImageCrop(im, &rect);
  4308. if (im_crop == NULL) {
  4309. RETURN_FALSE;
  4310. } else {
  4311. ZEND_REGISTER_RESOURCE(return_value, im_crop, le_gd);
  4312. }
  4313. }
  4314. /* }}} */
  4315. /* {{{ proto void imagecropauto(resource im [, int mode [, threshold [, color]]])
  4316. Crop an image automatically using one of the available modes. */
  4317. PHP_FUNCTION(imagecropauto)
  4318. {
  4319. zval *IM;
  4320. long mode = -1;
  4321. long color = -1;
  4322. double threshold = 0.5f;
  4323. gdImagePtr im;
  4324. gdImagePtr im_crop;
  4325. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ldl", &IM, &mode, &threshold, &color) == FAILURE) {
  4326. return;
  4327. }
  4328. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  4329. switch (mode) {
  4330. case -1:
  4331. mode = GD_CROP_DEFAULT;
  4332. case GD_CROP_DEFAULT:
  4333. case GD_CROP_TRANSPARENT:
  4334. case GD_CROP_BLACK:
  4335. case GD_CROP_WHITE:
  4336. case GD_CROP_SIDES:
  4337. im_crop = gdImageCropAuto(im, mode);
  4338. break;
  4339. case GD_CROP_THRESHOLD:
  4340. if (color < 0 || (!gdImageTrueColor(im) && color >= gdImageColorsTotal(im))) {
  4341. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Color argument missing with threshold mode");
  4342. RETURN_FALSE;
  4343. }
  4344. im_crop = gdImageCropThreshold(im, color, (float) threshold);
  4345. break;
  4346. default:
  4347. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown crop mode");
  4348. RETURN_FALSE;
  4349. }
  4350. if (im_crop == NULL) {
  4351. RETURN_FALSE;
  4352. } else {
  4353. ZEND_REGISTER_RESOURCE(return_value, im_crop, le_gd);
  4354. }
  4355. }
  4356. /* }}} */
  4357. /* {{{ proto resource imagescale(resource im, new_width[, new_height[, method]])
  4358. Scale an image using the given new width and height. */
  4359. PHP_FUNCTION(imagescale)
  4360. {
  4361. zval *IM;
  4362. gdImagePtr im;
  4363. gdImagePtr im_scaled = NULL;
  4364. int new_width, new_height;
  4365. long tmp_w, tmp_h=-1, tmp_m = GD_BILINEAR_FIXED;
  4366. gdInterpolationMethod method, old_method;
  4367. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|ll", &IM, &tmp_w, &tmp_h, &tmp_m) == FAILURE) {
  4368. return;
  4369. }
  4370. method = tmp_m;
  4371. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  4372. if (tmp_h < 0) {
  4373. /* preserve ratio */
  4374. long src_x, src_y;
  4375. src_x = gdImageSX(im);
  4376. src_y = gdImageSY(im);
  4377. if (src_x) {
  4378. tmp_h = tmp_w * src_y / src_x;
  4379. }
  4380. }
  4381. if (tmp_h <= 0 || tmp_w <= 0) {
  4382. RETURN_FALSE;
  4383. }
  4384. new_width = tmp_w;
  4385. new_height = tmp_h;
  4386. /* gdImageGetInterpolationMethod() is only available as of GD 2.1.1 */
  4387. old_method = im->interpolation_id;
  4388. if (gdImageSetInterpolationMethod(im, method)) {
  4389. im_scaled = gdImageScale(im, new_width, new_height);
  4390. }
  4391. gdImageSetInterpolationMethod(im, old_method);
  4392. if (im_scaled == NULL) {
  4393. RETURN_FALSE;
  4394. } else {
  4395. ZEND_REGISTER_RESOURCE(return_value, im_scaled, le_gd);
  4396. }
  4397. }
  4398. /* }}} */
  4399. /* {{{ proto resource imageaffine(resource src, array affine[, array clip])
  4400. Return an image containing the affine tramsformed src image, using an optional clipping area */
  4401. PHP_FUNCTION(imageaffine)
  4402. {
  4403. zval *IM;
  4404. gdImagePtr src;
  4405. gdImagePtr dst;
  4406. gdRect rect;
  4407. gdRectPtr pRect = NULL;
  4408. zval *z_rect = NULL;
  4409. zval *z_affine;
  4410. zval **tmp;
  4411. double affine[6];
  4412. int i, nelems;
  4413. zval **zval_affine_elem = NULL;
  4414. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra|a", &IM, &z_affine, &z_rect) == FAILURE) {
  4415. return;
  4416. }
  4417. ZEND_FETCH_RESOURCE(src, gdImagePtr, &IM, -1, "Image", le_gd);
  4418. if ((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_affine))) != 6) {
  4419. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Affine array must have six elements");
  4420. RETURN_FALSE;
  4421. }
  4422. for (i = 0; i < nelems; i++) {
  4423. if (zend_hash_index_find(Z_ARRVAL_P(z_affine), i, (void **) &zval_affine_elem) == SUCCESS) {
  4424. switch (Z_TYPE_PP(zval_affine_elem)) {
  4425. case IS_LONG:
  4426. affine[i] = Z_LVAL_PP(zval_affine_elem);
  4427. break;
  4428. case IS_DOUBLE:
  4429. affine[i] = Z_DVAL_PP(zval_affine_elem);
  4430. break;
  4431. case IS_STRING:
  4432. {
  4433. zval dval;
  4434. dval = **zval_affine_elem;
  4435. zval_copy_ctor(&dval);
  4436. convert_to_double(&dval);
  4437. affine[i] = Z_DVAL(dval);
  4438. }
  4439. break;
  4440. default:
  4441. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid type for element %i", i);
  4442. RETURN_FALSE;
  4443. }
  4444. }
  4445. }
  4446. if (z_rect != NULL) {
  4447. if (zend_hash_find(HASH_OF(z_rect), "x", sizeof("x"), (void **)&tmp) != FAILURE) {
  4448. if (Z_TYPE_PP(tmp) != IS_LONG) {
  4449. zval lval;
  4450. lval = **tmp;
  4451. zval_copy_ctor(&lval);
  4452. convert_to_long(&lval);
  4453. rect.x = Z_LVAL(lval);
  4454. } else {
  4455. rect.x = Z_LVAL_PP(tmp);
  4456. }
  4457. } else {
  4458. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing x position");
  4459. RETURN_FALSE;
  4460. }
  4461. if (zend_hash_find(HASH_OF(z_rect), "y", sizeof("x"), (void **)&tmp) != FAILURE) {
  4462. if (Z_TYPE_PP(tmp) != IS_LONG) {
  4463. zval lval;
  4464. lval = **tmp;
  4465. zval_copy_ctor(&lval);
  4466. convert_to_long(&lval);
  4467. rect.y = Z_LVAL(lval);
  4468. } else {
  4469. rect.y = Z_LVAL_PP(tmp);
  4470. }
  4471. } else {
  4472. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing y position");
  4473. RETURN_FALSE;
  4474. }
  4475. if (zend_hash_find(HASH_OF(z_rect), "width", sizeof("width"), (void **)&tmp) != FAILURE) {
  4476. if (Z_TYPE_PP(tmp) != IS_LONG) {
  4477. zval lval;
  4478. lval = **tmp;
  4479. zval_copy_ctor(&lval);
  4480. convert_to_long(&lval);
  4481. rect.width = Z_LVAL(lval);
  4482. } else {
  4483. rect.width = Z_LVAL_PP(tmp);
  4484. }
  4485. } else {
  4486. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing width");
  4487. RETURN_FALSE;
  4488. }
  4489. if (zend_hash_find(HASH_OF(z_rect), "height", sizeof("height"), (void **)&tmp) != FAILURE) {
  4490. if (Z_TYPE_PP(tmp) != IS_LONG) {
  4491. zval lval;
  4492. lval = **tmp;
  4493. zval_copy_ctor(&lval);
  4494. convert_to_long(&lval);
  4495. rect.height = Z_LVAL(lval);
  4496. } else {
  4497. rect.height = Z_LVAL_PP(tmp);
  4498. }
  4499. } else {
  4500. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing height");
  4501. RETURN_FALSE;
  4502. }
  4503. pRect = &rect;
  4504. } else {
  4505. rect.x = -1;
  4506. rect.y = -1;
  4507. rect.width = gdImageSX(src);
  4508. rect.height = gdImageSY(src);
  4509. pRect = NULL;
  4510. }
  4511. if (gdTransformAffineGetImage(&dst, src, pRect, affine) != GD_TRUE) {
  4512. RETURN_FALSE;
  4513. }
  4514. if (dst == NULL) {
  4515. RETURN_FALSE;
  4516. } else {
  4517. ZEND_REGISTER_RESOURCE(return_value, dst, le_gd);
  4518. }
  4519. }
  4520. /* }}} */
  4521. /* {{{ proto array imageaffinematrixget(type[, options])
  4522. Return an image containing the affine tramsformed src image, using an optional clipping area */
  4523. PHP_FUNCTION(imageaffinematrixget)
  4524. {
  4525. double affine[6];
  4526. long type;
  4527. zval *options = NULL;
  4528. zval **tmp;
  4529. int res = GD_FALSE, i;
  4530. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|z", &type, &options) == FAILURE) {
  4531. return;
  4532. }
  4533. switch((gdAffineStandardMatrix)type) {
  4534. case GD_AFFINE_TRANSLATE:
  4535. case GD_AFFINE_SCALE: {
  4536. double x, y;
  4537. if (!options || Z_TYPE_P(options) != IS_ARRAY) {
  4538. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array expected as options");
  4539. RETURN_FALSE;
  4540. }
  4541. if (zend_hash_find(HASH_OF(options), "x", sizeof("x"), (void **)&tmp) != FAILURE) {
  4542. if (Z_TYPE_PP(tmp) != IS_DOUBLE) {
  4543. zval dval;
  4544. dval = **tmp;
  4545. zval_copy_ctor(&dval);
  4546. convert_to_double(&dval);
  4547. x = Z_DVAL(dval);
  4548. } else {
  4549. x = Z_DVAL_PP(tmp);
  4550. }
  4551. } else {
  4552. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing x position");
  4553. RETURN_FALSE;
  4554. }
  4555. if (zend_hash_find(HASH_OF(options), "y", sizeof("y"), (void **)&tmp) != FAILURE) {
  4556. if (Z_TYPE_PP(tmp) != IS_DOUBLE) {
  4557. zval dval;
  4558. dval = **tmp;
  4559. zval_copy_ctor(&dval);
  4560. convert_to_double(&dval);
  4561. y = Z_DVAL(dval);
  4562. } else {
  4563. y = Z_DVAL_PP(tmp);
  4564. }
  4565. } else {
  4566. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing y position");
  4567. RETURN_FALSE;
  4568. }
  4569. if (type == GD_AFFINE_TRANSLATE) {
  4570. res = gdAffineTranslate(affine, x, y);
  4571. } else {
  4572. res = gdAffineScale(affine, x, y);
  4573. }
  4574. break;
  4575. }
  4576. case GD_AFFINE_ROTATE:
  4577. case GD_AFFINE_SHEAR_HORIZONTAL:
  4578. case GD_AFFINE_SHEAR_VERTICAL: {
  4579. double angle;
  4580. if (!options) {
  4581. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number is expected as option");
  4582. RETURN_FALSE;
  4583. }
  4584. if(Z_TYPE_P(options) != IS_DOUBLE) {
  4585. zval dval;
  4586. dval = *options;
  4587. zval_copy_ctor(&dval);
  4588. convert_to_double(&dval);
  4589. angle = Z_DVAL(dval);
  4590. } else {
  4591. angle = Z_DVAL_P(options);
  4592. }
  4593. if (type == GD_AFFINE_SHEAR_HORIZONTAL) {
  4594. res = gdAffineShearHorizontal(affine, angle);
  4595. } else if (type == GD_AFFINE_SHEAR_VERTICAL) {
  4596. res = gdAffineShearVertical(affine, angle);
  4597. } else {
  4598. res = gdAffineRotate(affine, angle);
  4599. }
  4600. break;
  4601. }
  4602. default:
  4603. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid type for element %li", type);
  4604. RETURN_FALSE;
  4605. }
  4606. if (res == GD_FALSE) {
  4607. RETURN_FALSE;
  4608. } else {
  4609. array_init(return_value);
  4610. for (i = 0; i < 6; i++) {
  4611. add_index_double(return_value, i, affine[i]);
  4612. }
  4613. }
  4614. }
  4615. /* {{{ proto array imageaffineconcat(array m1, array m2)
  4616. Concat two matrices (as in doing many ops in one go) */
  4617. PHP_FUNCTION(imageaffinematrixconcat)
  4618. {
  4619. double m1[6];
  4620. double m2[6];
  4621. double mr[6];
  4622. zval **tmp;
  4623. zval *z_m1;
  4624. zval *z_m2;
  4625. int i, nelems;
  4626. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aa", &z_m1, &z_m2) == FAILURE) {
  4627. return;
  4628. }
  4629. if (((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_m1))) != 6) || (nelems = zend_hash_num_elements(Z_ARRVAL_P(z_m2))) != 6) {
  4630. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Affine arrays must have six elements");
  4631. RETURN_FALSE;
  4632. }
  4633. for (i = 0; i < 6; i++) {
  4634. if (zend_hash_index_find(Z_ARRVAL_P(z_m1), i, (void **) &tmp) == SUCCESS) {
  4635. switch (Z_TYPE_PP(tmp)) {
  4636. case IS_LONG:
  4637. m1[i] = Z_LVAL_PP(tmp);
  4638. break;
  4639. case IS_DOUBLE:
  4640. m1[i] = Z_DVAL_PP(tmp);
  4641. break;
  4642. case IS_STRING:
  4643. {
  4644. zval dval;
  4645. dval = **tmp;
  4646. zval_copy_ctor(&dval);
  4647. convert_to_double(&dval);
  4648. m1[i] = Z_DVAL(dval);
  4649. }
  4650. break;
  4651. default:
  4652. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid type for element %i", i);
  4653. RETURN_FALSE;
  4654. }
  4655. }
  4656. if (zend_hash_index_find(Z_ARRVAL_P(z_m2), i, (void **) &tmp) == SUCCESS) {
  4657. switch (Z_TYPE_PP(tmp)) {
  4658. case IS_LONG:
  4659. m2[i] = Z_LVAL_PP(tmp);
  4660. break;
  4661. case IS_DOUBLE:
  4662. m2[i] = Z_DVAL_PP(tmp);
  4663. break;
  4664. case IS_STRING:
  4665. {
  4666. zval dval;
  4667. dval = **tmp;
  4668. zval_copy_ctor(&dval);
  4669. convert_to_double(&dval);
  4670. m2[i] = Z_DVAL(dval);
  4671. }
  4672. break;
  4673. default:
  4674. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid type for element %i", i);
  4675. RETURN_FALSE;
  4676. }
  4677. }
  4678. }
  4679. if (gdAffineConcat (mr, m1, m2) != GD_TRUE) {
  4680. RETURN_FALSE;
  4681. }
  4682. array_init(return_value);
  4683. for (i = 0; i < 6; i++) {
  4684. add_index_double(return_value, i, mr[i]);
  4685. }
  4686. }
  4687. /* {{{ proto resource imagesetinterpolation(resource im, [, method]])
  4688. Set the default interpolation method, passing -1 or 0 sets it to the libgd default (bilinear). */
  4689. PHP_FUNCTION(imagesetinterpolation)
  4690. {
  4691. zval *IM;
  4692. gdImagePtr im;
  4693. long method = GD_BILINEAR_FIXED;
  4694. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &IM, &method) == FAILURE) {
  4695. return;
  4696. }
  4697. ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
  4698. if (method == -1) {
  4699. method = GD_BILINEAR_FIXED;
  4700. }
  4701. RETURN_BOOL(gdImageSetInterpolationMethod(im, (gdInterpolationMethod) method));
  4702. }
  4703. /* }}} */
  4704. /*
  4705. * Local variables:
  4706. * tab-width: 4
  4707. * c-basic-offset: 4
  4708. * End:
  4709. * vim600: sw=4 ts=4 fdm=marker
  4710. * vim<600: sw=4 ts=4
  4711. */