gd.c 129 KB

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