class.upload.php 267 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302
  1. <?php
  2. // +------------------------------------------------------------------------+
  3. // | class.upload.php |
  4. // +------------------------------------------------------------------------+
  5. // | Copyright (c) Colin Verot 2003-2010. All rights reserved. |
  6. // | Version 0.32 |
  7. // | Last modified 15/01/2013 |
  8. // | Email colin@verot.net |
  9. // | Web http://www.verot.net |
  10. // +------------------------------------------------------------------------+
  11. // | This program is free software; you can redistribute it and/or modify |
  12. // | it under the terms of the GNU General Public License version 2 as |
  13. // | published by the Free Software Foundation. |
  14. // | |
  15. // | This program is distributed in the hope that it will be useful, |
  16. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. // | GNU General Public License for more details. |
  19. // | |
  20. // | You should have received a copy of the GNU General Public License |
  21. // | along with this program; if not, write to the |
  22. // | Boston, MA 02111-1307 USA |
  23. // | |
  24. // | Please give credit on sites that use class.upload and submit changes |
  25. // | of the script so other people can use them as well. |
  26. // | This script is free to use, don't abuse. |
  27. // +------------------------------------------------------------------------+
  28. //
  29. /**
  30. * Class upload
  31. *
  32. * @version 0.32
  33. * @author Colin Verot <colin@verot.net>
  34. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  35. * @copyright Colin Verot
  36. * @package cmf
  37. * @subpackage external
  38. */
  39. /**
  40. * Class upload
  41. *
  42. * <b>What does it do?</b>
  43. *
  44. * It manages file uploads for you. In short, it manages the uploaded file,
  45. * and allows you to do whatever you want with the file, especially if it
  46. * is an image, and as many times as you want.
  47. *
  48. * It is the ideal class to quickly integrate file upload in your site.
  49. * If the file is an image, you can convert, resize, crop it in many ways.
  50. * You can also apply filters, add borders, text, watermarks, etc...
  51. * That's all you need for a gallery script for instance. Supported formats
  52. * are PNG, JPG, GIF and BMP.
  53. *
  54. * You can also use the class to work on local files, which is especially
  55. * useful to use the image manipulation features. The class also supports
  56. * Flash uploaders.
  57. *
  58. * The class works with PHP 4 and 5, and its error messages can
  59. * be localized at will.
  60. *
  61. * <b>How does it work?</b>
  62. *
  63. * You instanciate the class with the $_FILES['my_field'] array
  64. * where my_field is the field name from your upload form.
  65. * The class will check if the original file has been uploaded
  66. * to its temporary location (alternatively, you can instanciate
  67. * the class with a local filename).
  68. *
  69. * You can then set a number of processing variables to act on the file.
  70. * For instance, you can rename the file, and if it is an image,
  71. * convert and resize it in many ways.
  72. * You can also set what will the class do if the file already exists.
  73. *
  74. * Then you call the function {@link process} to actually perform the actions
  75. * according to the processing parameters you set above.
  76. * It will create new instances of the original file,
  77. * so the original file remains the same between each process.
  78. * The file will be manipulated, and copied to the given location.
  79. * The processing variables will be reset once it is done.
  80. *
  81. * You can repeat setting up a new set of processing variables,
  82. * and calling {@link process} again as many times as you want.
  83. * When you have finished, you can call {@link clean} to delete
  84. * the original uploaded file.
  85. *
  86. * If you don't set any processing parameters and call {@link process}
  87. * just after instanciating the class. The uploaded file will be simply
  88. * copied to the given location without any alteration or checks.
  89. *
  90. * Don't forget to add <i>enctype="multipart/form-data"</i> in your form
  91. * tag <form> if you want your form to upload the file.
  92. *
  93. * <b>How to use it?</b><br>
  94. * Create a simple HTML file, with a form such as:
  95. * <pre>
  96. * <form enctype="multipart/form-data" method="post" action="upload.php">
  97. * <input type="file" size="32" name="image_field" value="">
  98. * <input type="submit" name="Submit" value="upload">
  99. * </form>
  100. * </pre>
  101. * Create a file called upload.php:
  102. * <pre>
  103. * $handle = new upload($_FILES['image_field']);
  104. * if ($handle->uploaded) {
  105. * $handle->file_new_name_body = 'image_resized';
  106. * $handle->image_resize = true;
  107. * $handle->image_x = 100;
  108. * $handle->image_ratio_y = true;
  109. * $handle->process('/home/user/files/');
  110. * if ($handle->processed) {
  111. * echo 'image resized';
  112. * $handle->clean();
  113. * } else {
  114. * echo 'error : ' . $handle->error;
  115. * }
  116. * }
  117. * </pre>
  118. *
  119. * <b>How to process a file uploaded via XMLHttpRequest?</b><br>
  120. * Use the class as following, the rest being the same as above:
  121. * <pre>
  122. * $handle = new upload('php:'.$_SERVER['HTTP_X_FILE_NAME']);
  123. * </pre>
  124. * Prefixing the argument with "php:" tells the class to retrieve the uploaded data
  125. * in php://input, and the rest is the stream's filename, which is generally in
  126. * $_SERVER['HTTP_X_FILE_NAME']. But you can use any other name you see fit:
  127. * <pre>
  128. * $handle = new upload('php:mycustomname.ext');
  129. * </pre>
  130. *
  131. * <b>How to process local files?</b><br>
  132. * Use the class as following, the rest being the same as above:
  133. * <pre>
  134. * $handle = new upload('/home/user/myfile.jpg');
  135. * </pre>
  136. *
  137. * <b>How to set the language?</b><br>
  138. * Instantiate the class with a second argument being the language code:
  139. * <pre>
  140. * $handle = new upload($_FILES['image_field'], 'fr_FR');
  141. * $handle = new upload('/home/user/myfile.jpg', 'fr_FR');
  142. * </pre>
  143. *
  144. * <b>How to output the resulting file or picture directly to the browser?</b><br>
  145. * Simply call {@link process}() without an argument (or with null as first argument):
  146. * <pre>
  147. * $handle = new upload($_FILES['image_field']);
  148. * header('Content-type: ' . $handle->file_src_mime);
  149. * echo $handle->Process();
  150. * die();
  151. * </pre>
  152. * Or if you want to force the download of the file:
  153. * <pre>
  154. * $handle = new upload($_FILES['image_field']);
  155. * header('Content-type: ' . $handle->file_src_mime);
  156. * header("Content-Disposition: attachment; filename=".rawurlencode($handle->file_src_name).";");
  157. * echo $handle->Process();
  158. * die();
  159. * </pre>
  160. *
  161. * <b>Processing parameters</b> (reset after each process)
  162. * <ul>
  163. * <li><b>{@link file_new_name_body}</b> replaces the name body (default: null)<br>
  164. * <pre>$handle->file_new_name_body = 'new name';</pre></li>
  165. * <li><b>{@link file_name_body_add}</b> appends to the name body (default: null)<br>
  166. * <pre>$handle->file_name_body_add = '_uploaded';</pre></li>
  167. * <li><b>{@link file_name_body_pre}</b> prepends to the name body (default: null)<br>
  168. * <pre>$handle->file_name_body_pre = 'thumb_';</pre></li>
  169. * <li><b>{@link file_new_name_ext}</b> replaces the file extension (default: null)<br>
  170. * <pre>$handle->file_new_name_ext = 'txt';</pre></li>
  171. * <li><b>{@link file_safe_name}</b> formats the filename (spaces changed to _) (default: true)<br>
  172. * <pre>$handle->file_safe_name = true;</pre></li>
  173. * <li><b>{@link file_force_extension}</b> forces an extension if there is't any (default: true)<br>
  174. * <pre>$handle->file_force_extension = true;</pre></li>
  175. * <li><b>{@link file_overwrite}</b> sets behaviour if file already exists (default: false)<br>
  176. * <pre>$handle->file_overwrite = true;</pre></li>
  177. * <li><b>{@link file_auto_rename}</b> automatically renames file if it already exists (default: true)<br>
  178. * <pre>$handle->file_auto_rename = true;</pre></li>
  179. * <li><b>{@link dir_auto_create}</b> automatically creates destination directory if missing (default: true)<br>
  180. * <pre>$handle->auto_create_dir = true;</pre></li>
  181. * <li><b>{@link dir_auto_chmod}</b> automatically attempts to chmod the destination directory if not writeable (default: true)<br>
  182. * <pre>$handle->dir_auto_chmod = true;</pre></li>
  183. * <li><b>{@link dir_chmod}</b> chmod used when creating directory or if directory not writeable (default: 0777)<br>
  184. * <pre>$handle->dir_chmod = 0777;</pre></li>
  185. * <li><b>{@link file_max_size}</b> sets maximum upload size (default: upload_max_filesize from php.ini)<br>
  186. * <pre>$handle->file_max_size = '1024'; // 1KB</pre></li>
  187. * <li><b>{@link mime_check}</b> sets if the class check the MIME against the {@link allowed} list (default: true)<br>
  188. * <pre>$handle->mime_check = true;</pre></li>
  189. * <li><b>{@link no_script}</b> sets if the class turns scripts into text files (default: true)<br>
  190. * <pre>$handle->no_script = false;</pre></li>
  191. * <li><b>{@link allowed}</b> array of allowed mime-types (or one string). wildcard accepted, as in image/* (default: check {@link Init})<br>
  192. * <pre>$handle->allowed = array('application/pdf','application/msword', 'image/*');</pre></li>
  193. * <li><b>{@link forbidden}</b> array of forbidden mime-types (or one string). wildcard accepted, as in image/* (default: check {@link Init})<br>
  194. * <pre>$handle->forbidden = array('application/*');</pre></li>
  195. * </ul>
  196. * <ul>
  197. * <li><b>{@link image_convert}</b> if set, image will be converted (possible values : ''|'png'|'jpeg'|'gif'|'bmp'; default: '')<br>
  198. * <pre>$handle->image_convert = 'jpg';</pre></li>
  199. * <li><b>{@link image_background_color}</b> if set, will forcibly fill transparent areas with the color, in hexadecimal (default: null)<br>
  200. * <pre>$handle->image_background_color = '#FF00FF';</pre></li>
  201. * <li><b>{@link image_default_color}</b> fallback color background color for non alpha-transparent output formats, such as JPEG or BMP, in hexadecimal (default: #FFFFFF)<br>
  202. * <pre>$handle->image_default_color = '#FF00FF';</pre></li>
  203. * <li><b>{@link png_compression}</b> sets the compression level for PNG images, between 1 (fast but large files) and 9 (slow but smaller files) (default: null (Zlib default))<br>
  204. * <pre>$handle->png_compression = 9;</pre></li>
  205. * <li><b>{@link jpeg_quality}</b> sets the compression quality for JPEG images (default: 85)<br>
  206. * <pre>$handle->jpeg_quality = 50;</pre></li>
  207. * <li><b>{@link jpeg_size}</b> if set to a size in bytes, will approximate {@link jpeg_quality} so the output image fits within the size (default: null)<br>
  208. * <pre>$handle->jpeg_size = 3072;</pre></li>
  209. * <li><b>{@link image_interlace}</b> if set to true, the image will be saved interlaced (default: false)<br>
  210. * <pre>$handle->image_interlace = true;</pre></li>
  211. * </ul>
  212. * The following eight settings can be used to invalidate an upload if the file is an image (note that <i>open_basedir</i> restrictions prevent the use of these settings)
  213. * <ul>
  214. * <li><b>{@link image_max_width}</b> if set to a dimension in pixels, the upload will be invalid if the image width is greater (default: null)<br>
  215. * <pre>$handle->image_max_width = 200;</pre></li>
  216. * <li><b>{@link image_max_height}</b> if set to a dimension in pixels, the upload will be invalid if the image height is greater (default: null)<br>
  217. * <pre>$handle->image_max_height = 100;</pre></li>
  218. * <li><b>{@link image_max_pixels}</b> if set to a number of pixels, the upload will be invalid if the image number of pixels is greater (default: null)<br>
  219. * <pre>$handle->image_max_pixels = 50000;</pre></li>
  220. * <li><b>{@link image_max_ratio}</b> if set to a aspect ratio (width/height), the upload will be invalid if the image apect ratio is greater (default: null)<br>
  221. * <pre>$handle->image_max_ratio = 1.5;</pre></li>
  222. * <li><b>{@link image_min_width}</b> if set to a dimension in pixels, the upload will be invalid if the image width is lower (default: null)<br>
  223. * <pre>$handle->image_min_width = 100;</pre></li>
  224. * <li><b>{@link image_min_height}</b> if set to a dimension in pixels, the upload will be invalid if the image height is lower (default: null)<br>
  225. * <pre>$handle->image_min_height = 500;</pre></li>
  226. * <li><b>{@link image_min_pixels}</b> if set to a number of pixels, the upload will be invalid if the image number of pixels is lower (default: null)<br>
  227. * <pre>$handle->image_min_pixels = 20000;</pre></li>
  228. * <li><b>{@link image_min_ratio}</b> if set to a aspect ratio (width/height), the upload will be invalid if the image apect ratio is lower (default: null)<br>
  229. * <pre>$handle->image_min_ratio = 0.5;</pre></li>
  230. * </ul>
  231. * <ul>
  232. * <li><b>{@link image_resize}</b> determines is an image will be resized (default: false)<br>
  233. * <pre>$handle->image_resize = true;</pre></li>
  234. * </ul>
  235. * The following variables are used only if {@link image_resize} == true
  236. * <ul>
  237. * <li><b>{@link image_x}</b> destination image width (default: 150)<br>
  238. * <pre>$handle->image_x = 100;</pre></li>
  239. * <li><b>{@link image_y}</b> destination image height (default: 150)<br>
  240. * <pre>$handle->image_y = 200;</pre></li>
  241. * </ul>
  242. * Use either one of the following
  243. * <ul>
  244. * <li><b>{@link image_ratio}</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes if true (default: false)<br>
  245. * <pre>$handle->image_ratio = true;</pre></li>
  246. * <li><b>{@link image_ratio_crop}</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes, and cropping excedent to fill the space. setting can also be a string, with one or more from 'TBLR', indicating which side of the image will be kept while cropping (default: false)<br>
  247. * <pre>$handle->image_ratio_crop = true;</pre></li>
  248. * <li><b>{@link image_ratio_fill}</b> if true, resize image conserving the original sizes ratio, using {@link image_x} AND {@link image_y} as max sizes, fitting the image in the space and coloring the remaining space. setting can also be a string, with one or more from 'TBLR', indicating which side of the space the image will be in (default: false)<br>
  249. * <pre>$handle->image_ratio_fill = true;</pre></li>
  250. * <li><b>{@link image_ratio_no_zoom_in}</b> same as {@link image_ratio}, but won't resize if the source image is smaller than {@link image_x} x {@link image_y} (default: false)<br>
  251. * <pre>$handle->image_ratio_no_zoom_in = true;</pre></li>
  252. * <li><b>{@link image_ratio_no_zoom_out}</b> same as {@link image_ratio}, but won't resize if the source image is bigger than {@link image_x} x {@link image_y} (default: false)<br>
  253. * <pre>$handle->image_ratio_no_zoom_out = true;</pre></li>
  254. * <li><b>{@link image_ratio_x}</b> if true, resize image, calculating {@link image_x} from {@link image_y} and conserving the original sizes ratio (default: false)<br>
  255. * <pre>$handle->image_ratio_x = true;</pre></li>
  256. * <li><b>{@link image_ratio_y}</b> if true, resize image, calculating {@link image_y} from {@link image_x} and conserving the original sizes ratio (default: false)<br>
  257. * <pre>$handle->image_ratio_y = true;</pre></li>
  258. * <li><b>{@link image_ratio_pixels}</b> if set to a long integer, resize image, calculating {@link image_y} and {@link image_x} to match a the number of pixels (default: false)<br>
  259. * <pre>$handle->image_ratio_pixels = 25000;</pre></li>
  260. * </ul>
  261. * The following image manipulations require GD2+
  262. * <ul>
  263. * <li><b>{@link image_brightness}</b> if set, corrects the brightness. value between -127 and 127 (default: null)<br>
  264. * <pre>$handle->image_brightness = 40;</pre></li>
  265. * <li><b>{@link image_contrast}</b> if set, corrects the contrast. value between -127 and 127 (default: null)<br>
  266. * <pre>$handle->image_contrast = 50;</pre></li>
  267. * <li><b>{@link image_opacity}</b> if set, changes the image opacity. value between 0 and 100 (default: null)<br>
  268. * <pre>$handle->image_opacity = 50;</pre></li>
  269. * <li><b>{@link image_tint_color}</b> if set, will tint the image with a color, value as hexadecimal #FFFFFF (default: null)<br>
  270. * <pre>$handle->image_tint_color = '#FF0000';</pre></li>
  271. * <li><b>{@link image_overlay_color}</b> if set, will add a colored overlay, value as hexadecimal #FFFFFF (default: null)<br>
  272. * <pre>$handle->image_overlay_color = '#FF0000';</pre></li>
  273. * <li><b>{@link image_overlay_opacity}</b> used when {@link image_overlay_color} is set, determines the opacity (default: 50)<br>
  274. * <pre>$handle->image_overlay_opacity = 20;</pre></li>
  275. * <li><b>{@link image_negative}</b> inverts the colors in the image (default: false)<br>
  276. * <pre>$handle->image_negative = true;</pre></li>
  277. * <li><b>{@link image_greyscale}</b> transforms an image into greyscale (default: false)<br>
  278. * <pre>$handle->image_greyscale = true;</pre></li>
  279. * <li><b>{@link image_threshold}</b> applies a threshold filter. value between -127 and 127 (default: null)<br>
  280. * <pre>$handle->image_threshold = 20;</pre></li>
  281. * <li><b>{@link image_pixelate}</b> pixelate an image, value is block size (default: null)<br>
  282. * <pre>$handle->image_pixelate = 10;</pre></li>
  283. * <li><b>{@link image_unsharp}</b> applies an unsharp mask, with alpha transparency support (default: false)<br>
  284. * <pre>$handle->image_unsharp = true;</pre></li>
  285. * <li><b>{@link image_unsharp_amount}</b> unsharp mask amount, typically 50 - 200 (default: 80)<br>
  286. * <pre>$handle->image_unsharp_amount = 120;</pre></li>
  287. * <li><b>{@link image_unsharp_radius}</b> unsharp mask radius, typically 0.5 - 1 (default: 0.5)<br>
  288. * <pre>$handle->image_unsharp_radius = 1;</pre></li>
  289. * <li><b>{@link image_unsharp_threshold}</b> unsharp mask threshold, typically 0 - 5 (default: 1)<br>
  290. * <pre>$handle->image_unsharp_threshold = 0;</pre></li>
  291. * </ul>
  292. * <ul>
  293. * <li><b>{@link image_text}</b> creates a text label on the image, value is a string, with eventual replacement tokens (default: null)<br>
  294. * <pre>$handle->image_text = 'test';</pre></li>
  295. * <li><b>{@link image_text_direction}</b> text label direction, either 'h' horizontal or 'v' vertical (default: 'h')<br>
  296. * <pre>$handle->image_text_direction = 'v';</pre></li>
  297. * <li><b>{@link image_text_color}</b> text color for the text label, in hexadecimal (default: #FFFFFF)<br>
  298. * <pre>$handle->image_text_color = '#FF0000';</pre></li>
  299. * <li><b>{@link image_text_opacity}</b> text opacity on the text label, integer between 0 and 100 (default: 100)<br>
  300. * <pre>$handle->image_text_opacity = 50;</pre></li>
  301. * <li><b>{@link image_text_background}</b> text label background color, in hexadecimal (default: null)<br>
  302. * <pre>$handle->image_text_background = '#FFFFFF';</pre></li>
  303. * <li><b>{@link image_text_background_opacity}</b> text label background opacity, integer between 0 and 100 (default: 100)<br>
  304. * <pre>$handle->image_text_background_opacity = 50;</pre></li>
  305. * <li><b>{@link image_text_font}</b> built-in font for the text label, from 1 to 5. 1 is the smallest (default: 5)<br>
  306. * <pre>$handle->image_text_font = 4;</pre></li>
  307. * <li><b>{@link image_text_x}</b> absolute text label position, in pixels from the left border. can be negative (default: null)<br>
  308. * <pre>$handle->image_text_x = 5;</pre></li>
  309. * <li><b>{@link image_text_y}</b> absolute text label position, in pixels from the top border. can be negative (default: null)<br>
  310. * <pre>$handle->image_text_y = 5;</pre></li>
  311. * <li><b>{@link image_text_position}</b> text label position withing the image, a combination of one or two from 'TBLR': top, bottom, left, right (default: null)<br>
  312. * <pre>$handle->image_text_position = 'LR';</pre></li>
  313. * <li><b>{@link image_text_padding}</b> text label padding, in pixels. can be overridden by {@link image_text_padding_x} and {@link image_text_padding_y} (default: 0)<br>
  314. * <pre>$handle->image_text_padding = 5;</pre></li>
  315. * <li><b>{@link image_text_padding_x}</b> text label horizontal padding (default: null)<br>
  316. * <pre>$handle->image_text_padding_x = 2;</pre></li>
  317. * <li><b>{@link image_text_padding_y}</b> text label vertical padding (default: null)<br>
  318. * <pre>$handle->image_text_padding_y = 10;</pre></li>
  319. * <li><b>{@link image_text_alignment}</b> text alignment when text has multiple lines, either 'L', 'C' or 'R' (default: 'C')<br>
  320. * <pre>$handle->image_text_alignment = 'R';</pre></li>
  321. * <li><b>{@link image_text_line_spacing}</b> space between lines in pixels, when text has multiple lines (default: 0)<br>
  322. * <pre>$handle->image_text_line_spacing = 3;</pre></li>
  323. * </ul>
  324. * <ul>
  325. * <li><b>{@link image_flip}</b> flips image, wither 'h' horizontal or 'v' vertical (default: null)<br>
  326. * <pre>$handle->image_flip = 'h';</pre></li>
  327. * <li><b>{@link image_rotate}</b> rotates image. possible values are 90, 180 and 270 (default: null)<br>
  328. * <pre>$handle->image_rotate = 90;</pre></li>
  329. * <li><b>{@link image_crop}</b> crops image. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: null)<br>
  330. * <pre>$handle->image_crop = array(50,40,30,20); OR '-20 20%'...</pre></li>
  331. * <li><b>{@link image_precrop}</b> crops image, before an eventual resizing. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: null)<br>
  332. * <pre>$handle->image_precrop = array(50,40,30,20); OR '-20 20%'...</pre></li>
  333. * </ul>
  334. * <ul>
  335. * <li><b>{@link image_bevel}</b> adds a bevel border to the image. value is thickness in pixels (default: null)<br>
  336. * <pre>$handle->image_bevel = 20;</pre></li>
  337. * <li><b>{@link image_bevel_color1}</b> top and left bevel color, in hexadecimal (default: #FFFFFF)<br>
  338. * <pre>$handle->image_bevel_color1 = '#FFFFFF';</pre></li>
  339. * <li><b>{@link image_bevel_color2}</b> bottom and right bevel color, in hexadecimal (default: #000000)<br>
  340. * <pre>$handle->image_bevel_color2 = '#000000';</pre></li>
  341. * <li><b>{@link image_border}</b> adds a unicolor border to the image. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: null)<br>
  342. * <pre>$handle->image_border = '3px'; OR '-20 20%' OR array(3,2)...</pre></li>
  343. * <li><b>{@link image_border_color}</b> border color, in hexadecimal (default: #FFFFFF)<br>
  344. * <pre>$handle->image_border_color = '#FFFFFF';</pre></li>
  345. * <li><b>{@link image_border_opacity}</b> border opacity, integer between 0 and 100 (default: 100)<br>
  346. * <pre>$handle->image_border_opacity = 50;</pre></li>
  347. * <li><b>{@link image_border_transparent}</b> adds a fading-to-transparent border to the image. accepts 4, 2 or 1 values as 'T R B L' or 'TB LR' or 'TBLR'. dimension can be 20, or 20px or 20% (default: null)<br>
  348. * <pre>$handle->image_border_transparent = '3px'; OR '-20 20%' OR array(3,2)...</pre></li>
  349. * <li><b>{@link image_frame}</b> type of frame: 1=flat 2=crossed (default: null)<br>
  350. * <pre>$handle->image_frame = 2;</pre></li>
  351. * <li><b>{@link image_frame_colors}</b> list of hex colors, in an array or a space separated string (default: '#FFFFFF #999999 #666666 #000000')<br>
  352. * <pre>$handle->image_frame_colors = array('#999999', '#FF0000', '#666666', '#333333', '#000000');</pre></li>
  353. * <li><b>{@link image_frame_opacity}</b> frame opacity, integer between 0 and 100 (default: 100)<br>
  354. * <pre>$handle->image_frame_opacity = 50;</pre></li>
  355. * </ul>
  356. * <ul>
  357. * <li><b>{@link image_watermark}</b> adds a watermark on the image, value is a local filename. accepted files are GIF, JPG, BMP, PNG and PNG alpha (default: null)<br>
  358. * <pre>$handle->image_watermark = 'watermark.png';</pre></li>
  359. * <li><b>{@link image_watermark_x}</b> absolute watermark position, in pixels from the left border. can be negative (default: null)<br>
  360. * <pre>$handle->image_watermark_x = 5;</pre></li>
  361. * <li><b>{@link image_watermark_y}</b> absolute watermark position, in pixels from the top border. can be negative (default: null)<br>
  362. * <pre>$handle->image_watermark_y = 5;</pre></li>
  363. * <li><b>{@link image_watermark_position}</b> watermark position withing the image, a combination of one or two from 'TBLR': top, bottom, left, right (default: null)<br>
  364. * <pre>$handle->image_watermark_position = 'LR';</pre></li>
  365. * <li><b>{@link image_watermark_no_zoom_in}</b> prevents the watermark to be resized up if it is smaller than the image (default: true)<br>
  366. * <pre>$handle->image_watermark_no_zoom_in = false;</pre></li>
  367. * <li><b>{@link image_watermark_no_zoom_out}</b> prevents the watermark to be resized down if it is bigger than the image (default: false)<br>
  368. * <pre>$handle->image_watermark_no_zoom_out = true;</pre></li>
  369. * </ul>
  370. * <ul>
  371. * <li><b>{@link image_reflection_height}</b> if set, a reflection will be added. Format is either in pixels or percentage, such as 40, '40', '40px' or '40%' (default: null)<br>
  372. * <pre>$handle->image_reflection_height = '25%';</pre></li>
  373. * <li><b>{@link image_reflection_space}</b> space in pixels between the source image and the reflection, can be negative (default: null)<br>
  374. * <pre>$handle->image_reflection_space = 3;</pre></li>
  375. * <li><b>{@link image_reflection_color}</b> reflection background color, in hexadecimal. Now deprecated in favor of {@link image_default_color} (default: #FFFFFF)<br>
  376. * <pre>$handle->image_default_color = '#000000';</pre></li>
  377. * <li><b>{@link image_reflection_opacity}</b> opacity level at which the reflection starts, integer between 0 and 100 (default: 60)<br>
  378. * <pre>$handle->image_reflection_opacity = 60;</pre></li>
  379. * </ul>
  380. *
  381. * <b>Values that can be read before calling {@link process}()</b>
  382. * <ul>
  383. * <li><b>{@link file_src_name}</b> Source file name</li>
  384. * <li><b>{@link file_src_name_body}</b> Source file name body</li>
  385. * <li><b>{@link file_src_name_ext}</b> Source file extension</li>
  386. * <li><b>{@link file_src_pathname}</b> Source file complete path and name</li>
  387. * <li><b>{@link file_src_mime}</b> Source file mime type</li>
  388. * <li><b>{@link file_src_size}</b> Source file size in bytes</li>
  389. * <li><b>{@link file_src_error}</b> Upload error code</li>
  390. * <li><b>{@link file_is_image}</b> Boolean flag, true if the file is a supported image type</li>
  391. * </ul>
  392. * If the file is a supported image type (and <i>open_basedir</i> restrictions allow it)
  393. * <ul>
  394. * <li><b>{@link image_src_x}</b> Source file width in pixels</li>
  395. * <li><b>{@link image_src_y}</b> Source file height in pixels</li>
  396. * <li><b>{@link image_src_pixels}</b> Source file number of pixels</li>
  397. * <li><b>{@link image_src_type}</b> Source file type (png, jpg, gif or bmp)</li>
  398. * <li><b>{@link image_src_bits}</b> Source file color depth</li>
  399. * </ul>
  400. *
  401. * <b>Values that can be read after calling {@link process}()</b>
  402. * <ul>
  403. * <li><b>{@link file_dst_path}</b> Destination file path</li>
  404. * <li><b>{@link file_dst_name_body}</b> Destination file name body</li>
  405. * <li><b>{@link file_dst_name_ext}</b> Destination file extension</li>
  406. * <li><b>{@link file_dst_name}</b> Destination file name</li>
  407. * <li><b>{@link file_dst_pathname}</b> Destination file complete path and name</li>
  408. * </ul>
  409. * If the file is a supported image type
  410. * <ul>
  411. * <li><b>{@link image_dst_x}</b> Destination file width</li>
  412. * <li><b>{@link image_dst_y}</b> Destination file height</li>
  413. * <li><b>{@link image_convert}</b> Destination file format</li>
  414. * </ul>
  415. *
  416. * <b>Requirements</b>
  417. *
  418. * Most of the image operations require GD. GD2 is greatly recommended
  419. *
  420. * The class requires PHP 4.3+, and is compatible with PHP5
  421. *
  422. * <b>Changelog</b>
  423. * <ul>
  424. * <li><b>v 0.32</b> 15/01/2013<br>
  425. * - add support for XMLHttpRequest uploads<br>
  426. * - added {@link image_pixelate}<br>
  427. * - added {@link image_interlace}<br>
  428. * - added {@link png_compression} to change PNG compressoin level<br>
  429. * - deactivate exec() if Suhosin is enabled<br>
  430. * - add more extension to dangerous scripts detection<br>
  431. * - imagejpeg takes null as second argument since PHP 5.4<br>
  432. * - default PECL Fileinfo MAGIC path to null<br>
  433. * - set gd.jpeg_ignore_warning to true by default<br>
  434. * - fixed file name normalization</li>
  435. * <li><b>v 0.31</b> 11/04/2011<br>
  436. * - added application/x-rar MIME type<br>
  437. * - make sure exec() and ini_get_all()function are not disabled if we want to use them<br>
  438. * - make sure that we don't divide by zero when calculating JPEG size<br>
  439. * - {@link allowed} and {@link forbidden} can now accept strings<br>
  440. * - try to guess the file extension from the MIME type if there is no file extension<br>
  441. * - better class properties when changing the file extension<br>
  442. * - added {@link file_force_extension} to allow extension-less files if needed<br>
  443. * - better file safe conversion of the filename<br>
  444. * - allow shorthand byte values, such as 1K, 2M, 3G for {@link file_max_size} and {@link jpeg_size}<br>
  445. * - added {@link image_opacity} to change picture opacity<br>
  446. * - added {@link image_border_opacity} to allow semi-transparent borders<br>
  447. * - added {@link image_frame_opacity} to allow semi-transparent frames<br>
  448. * - added {@link image_border_transparent} to allow borders fading to transparent<br>
  449. * - duplicated {@link image_overlay_percent} into {@link image_overlay_opacity}<br>
  450. * - duplicated {@link image_text_percent} into {@link image_text_opacity}<br>
  451. * - duplicated {@link image_text_background_percent} into {@link image_text_background_opacity}</li>
  452. * <li><b>v 0.30</b> 05/09/2010<br>
  453. * - implemented an unsharp mask, with alpha transparency support, activated if {@link image_unsharp} is true. added {@link image_unsharp_amount}, {@link image_unsharp_radius}, and {@link image_unsharp_threshold}<br>
  454. * - added text/rtf MIME type, and no_script exception<br>
  455. * - corrected bug when {@link no_script} is activated and several process() are called<br>
  456. * - better error handling for finfo<br>
  457. * - display upload_max_filesize information from php.ini in the log<br>
  458. * - automatic extension for extension-less images<br>
  459. * - fixed {@link image_ratio_fill} top and left filling<br>
  460. * - fixed alphablending issue when applying a transparent PNG watermark on a transparent PNG<br>
  461. * - added {@link image_watermark_no_zoom_in} and {@link image_watermark_no_zoom_out} to allow the watermark to be resized down (or up) to fit in the image. By default, the watermark may be resized down, but not up.</li>
  462. * <li><b>v 0.29</b> 03/02/2010<br>
  463. * - added protection against malicious images<br>
  464. * - added zip and torrent MIME type<br>
  465. * - replaced split() with explode()<br>
  466. * - initialise image_dst_x/y with image_src_x/y<br>
  467. * - removed {@link mime_fileinfo}, {@link mime_file}, {@link mime_magic} and {@link mime_getimagesize} from the docs since they are used before {@link process}<br>
  468. * - added more extensions and MIME types<br>
  469. * - improved MIME type validation<br>
  470. * - improved logging</li>
  471. * <li><b>v 0.28</b> 10/08/2009<br>
  472. * - replaced ereg functions to be compatible with PHP 5.3<br>
  473. * - added flv MIME type<br>
  474. * - improved MIME type detection<br>
  475. * - added {@link file_name_body_pre} to prepend a string to the file name<br>
  476. * - added {@link mime_fileinfo}, {@link mime_file}, {@link mime_magic} and {@link mime_getimagesize} so that it is possible to deactivate some MIME type checking method<br>
  477. * - use exec() rather than shell_exec(), to play better with safe mode <br>
  478. * - added some error messages<br>
  479. * - fix bug when checking on conditions, {@link processed} wasn't propagated properly</li>
  480. * <li><b>v 0.27</b> 14/05/2009<br>
  481. * - look for the language files directory from __FILE__<br>
  482. * - deactivate {@link file_auto_rename} if {@link file_overwrite} is set<br>
  483. * - improved transparency replacement for true color images<br>
  484. * - fixed calls to newer version of UNIX file utility<br>
  485. * - fixed error when using PECL Fileinfo extension in SAFE MODE, and when using the finfo class<br>
  486. * - added {@link image_precrop} to crop the image before an eventual resizing</li>
  487. * <li><b>v 0.26</b> 13/11/2008<br>
  488. * - rewrote conversion from palette to true color to handle transparency better<br>
  489. * - fixed imagecopymergealpha() when the overlayed image is of wrong dimensions<br>
  490. * - fixed imagecreatenew() when the image to create have less than 1 pixels width or height<br>
  491. * - rewrote MIME type detection to be more secure and not rely on browser information; now using Fileinfo PECL extension, UNIX file() command, MIME magic, and getimagesize(), in that order<br>
  492. * - added support for Flash uploaders<br>
  493. * - some bug fixing and error handling</li>
  494. * <li><b>v 0.25</b> 17/11/2007<br>
  495. * - added translation files and mechanism to instantiate the class with a language different from English<br>
  496. * - added {@link forbidden} to set an array of forbidden MIME types<br>
  497. * - implemented support for simple wildcards in {@link allowed} and {@link forbidden}, such as image/*<br>
  498. * - preset the file extension to the desired conversion format when converting an image<br>
  499. * - added read and write support for BMP images<br>
  500. * - added a flag {@link file_is_image} to determine if the file is a supported image type<br>
  501. * - the class now provides some information about the image, before calling {@link process}(). Available are {@link image_src_x}, {@link image_src_y} and the newly introduced {@link image_src_bits}, {@link image_src_pixels} and {@link image_src_type}. Note that this will not work if <i>open_basedir</i> restrictions are in place<br>
  502. * - improved logging; now provides useful system information<br>
  503. * - added some more pre-processing checks for files that are images: {@link image_max_width}, {@link image_max_height}, {@link image_max_pixels}, {@link image_max_ratio}, {@link image_min_width}, {@link image_min_height}, {@link image_min_pixels} and {@link image_min_ratio}<br>
  504. * - added {@link image_ratio_pixels} to resize an image to a number of pixels, keeping aspect ratio<br>
  505. * - added {@link image_is_palette} and {@link image_is_transparent} and {@link image_transparent_color} for GIF images<br>
  506. * - added {@link image_default_color} to define a fallback color for non alpha-transparent output formats, such as JPEG or BMP<br>
  507. * - changed {@link image_background_color}, which now forces transparent areas to be painted<br>
  508. * - improved reflections and color overlays so that it works with alpha transparent images<br>
  509. * - {@link image_reflection_color} is now deprecated in favour of {@link image_default_color}<br />
  510. * - transparent PNGs are now processed in true color, and fully preserving the alpha channel when doing merges<br>
  511. * - transparent GIFs are now automatically detected. {@link preserve_transparency} is deprecated<br>
  512. * - transparent true color images can be saved as GIF while retaining transparency, semi transparent areas being merged with {@link image_default_color}<br>
  513. * - transparent true color images can be saved as JPG/BMP with the semi transparent areas being merged with {@link image_default_color}<br>
  514. * - fixed conversion of images to true color<br>
  515. * - the class can now output the uploaded files content as the return value of process() if the function is called with an empty or null argumenti, or no argument</li>
  516. * <li><b>v 0.24</b> 25/05/2007<br>
  517. * - added {@link image_background_color}, to set the default background color of an image<br>
  518. * - added possibility of using replacement tokens in text labels<br>
  519. * - changed default JPEG quality to 85<br>
  520. * - fixed a small bug when using greyscale filter and associated filters<br>
  521. * - added {@link image_ratio_fill} in order to fit an image within some dimensions and color the remaining space. Very similar to {@link image_ratio_crop}<br>
  522. * - improved the recursive creation of directories<br>
  523. * - the class now converts palette based images to true colors before doing graphic manipulations</li>
  524. * <li><b>v 0.23</b> 23/12/2006<br>
  525. * - fixed a bug when processing more than once the same uploaded file. If there is an open_basedir restriction, the class now creates a temporary file for the first call to process(). This file will be used for subsequent processes, and will be deleted upon calling clean()</li>
  526. * <li><b>v 0.22</b> 16/12/2006<br>
  527. * - added automatic creation of a temporary file if the upload directory is not within open_basedir<br>
  528. * - fixed a bug which was preventing to work on a local file by overwriting it with its processed copy<br>
  529. * - added MIME types video/x-ms-wmv and image/x-png and fixed PNG support for IE weird MIME types<br>
  530. * - modified {@link image_ratio_crop} so it can accept one or more from string 'TBLR', determining which side of the image is kept while cropping<br>
  531. * - added support for multiple lines in the text, using "\n" as a line break<br>
  532. * - added {@link image_text_line_spacing} which allow to set the space between several lines of text<br>
  533. * - added {@link image_text_alignment} which allow to set the alignment when text has several lines<br>
  534. * - {@link image_text_font} can now be set to the path of a GDF font to load external fonts<br>
  535. * - added {@link image_reflection_height} to create a reflection of the source image, which height is in pixels or percentage<br>
  536. * - added {@link image_reflection_space} to set the space in pixels between the source image and the reflection<br>
  537. * - added {@link image_reflection_color} to set the reflection background color<br>
  538. * - added {@link image_reflection_opacity} to set the initial level of opacity of the reflection</li>
  539. * <li><b>v 0.21</b> 30/09/2006<br>
  540. * - added {@link image_ratio_crop} which resizes within {@link image_x} and {@link image_y}, keeping ratio, but filling the space by cropping excedent of image<br>
  541. * - added {@link mime_check}, which default is true, to set checks against {@link allowed} MIME list<br>
  542. * - if MIME is empty, the class now triggers an error<br>
  543. * - color #000000 is OK for {@link image_text_color}, and related text transparency bug fixed<br>
  544. * - {@link gd_version}() now uses gd_info(), or else phpinfo()<br>
  545. * - fixed path issue when the destination path has no trailing slash on Windows systems <br>
  546. * - removed inline functions to be fully PHP5 compatible </li>
  547. * <li><b>v 0.20</b> 11/08/2006<br>
  548. * - added some more error checking and messages (GD presence, permissions...)<br>
  549. * - fix when uploading files without extension<br>
  550. * - changed values for {@link image_brightness} and {@link image_contrast} to be between -127 and 127<br>
  551. * - added {@link dir_auto_create} to automatically and recursively create destination directory if missing.<br>
  552. * - added {@link dir_auto_chmod} to automatically chmod the destination directory if not writeable.<br>
  553. * - added {@link dir_chmod} to set the default chmod to use.<br>
  554. * - added {@link image_crop} to crop images<br>
  555. * - added {@link image_negative} to invert the colors on the image<br>
  556. * - added {@link image_greyscale} to turn the image into greyscale<br>
  557. * - added {@link image_threshold} to apply a threshold filter on the image<br>
  558. * - added {@link image_bevel}, {@link image_bevel_color1} and {@link image_bevel_color2} to add a bevel border<br>
  559. * - added {@link image_border} and {@link image_border_color} to add a single color border<br>
  560. * - added {@link image_frame} and {@link image_frame_colors} to add a multicolored frame</li>
  561. * <li><b>v 0.19</b> 29/03/2006<br>
  562. * - class is now compatible i18n (thanks Sylwester).<br>
  563. * - the class can mow manipulate local files, not only uploaded files (instanciate the class with a local filename).<br>
  564. * - {@link file_safe_name} has been improved a bit.<br>
  565. * - added {@link image_brightness}, {@link image_contrast}, {@link image_tint_color}, {@link image_overlay_color} and {@link image_overlay_percent} to do color manipulation on the images.<br>
  566. * - added {@link image_text} and all derivated settings to add a text label on the image.<br>
  567. * - added {@link image_watermark} and all derivated settings to add a watermark image on the image.<br>
  568. * - added {@link image_flip} and {@link image_rotate} for more image manipulations<br>
  569. * - added {@link jpeg_size} to calculate the JPG compression quality in order to fit within one filesize.</li>
  570. * <li><b>v 0.18</b> 02/02/2006<br>
  571. * - added {@link no_script} to turn dangerous scripts into text files.<br>
  572. * - added {@link mime_magic_check} to set the class to use mime_magic.<br>
  573. * - added {@link preserve_transparency} *experimental*. Thanks Gregor.<br>
  574. * - fixed size and mime checking, wasn't working :/ Thanks Willem.<br>
  575. * - fixed memory leak when resizing images.<br>
  576. * - when resizing, it is not necessary anymore to set {@link image_convert}.<br>
  577. * - il is now possible to simply convert an image, with no resizing.<br>
  578. * - sets the default {@link file_max_size} to upload_max_filesize from php.ini. Thanks Edward</li>
  579. * <li><b>v 0.17</b> 28/05/2005<br>
  580. * - the class can be used with any version of GD.<br>
  581. * - added security check on the file with a list of mime-types.<br>
  582. * - changed the license to GPL v2 only</li>
  583. * <li><b>v 0.16</b> 19/05/2005<br>
  584. * - added {@link file_auto_rename} automatic file renaming if the same filename already exists.<br>
  585. * - added {@link file_safe_name} safe formatting of the filename (spaces to _underscores so far).<br>
  586. * - added some more error reporting to avoid crash if GD is not present</li>
  587. * <li><b>v 0.15</b> 16/04/2005<br>
  588. * - added JPEG compression quality setting. Thanks Vad</li>
  589. * <li><b>v 0.14</b> 14/03/2005<br>
  590. * - reworked the class file to allow parsing with phpDocumentor</li>
  591. * <li><b>v 0.13</b> 07/03/2005<br>
  592. * - fixed a bug with {@link image_ratio}. Thanks Justin.<br>
  593. * - added {@link image_ratio_no_zoom_in} and {@link image_ratio_no_zoom_out} </li>
  594. * <li><b>v 0.12</b> 21/01/2005<br>
  595. * - added {@link image_ratio} to resize within max values, keeping image ratio</li>
  596. * <li><b>v 0.11</b> 22/08/2003<br>
  597. * - update for GD2 (changed imageresized() into imagecopyresampled() and imagecreate() into imagecreatetruecolor())</li>
  598. * </ul>
  599. *
  600. * @package cmf
  601. * @subpackage external
  602. */
  603. class Upload {
  604. /**
  605. * Class version
  606. *
  607. * @access public
  608. * @var string
  609. */
  610. var $version;
  611. /**
  612. * Uploaded file name
  613. *
  614. * @access public
  615. * @var string
  616. */
  617. var $file_src_name;
  618. /**
  619. * Uploaded file name body (i.e. without extension)
  620. *
  621. * @access public
  622. * @var string
  623. */
  624. var $file_src_name_body;
  625. /**
  626. * Uploaded file name extension
  627. *
  628. * @access public
  629. * @var string
  630. */
  631. var $file_src_name_ext;
  632. /**
  633. * Uploaded file MIME type
  634. *
  635. * @access public
  636. * @var string
  637. */
  638. var $file_src_mime;
  639. /**
  640. * Uploaded file size, in bytes
  641. *
  642. * @access public
  643. * @var double
  644. */
  645. var $file_src_size;
  646. /**
  647. * Holds eventual PHP error code from $_FILES
  648. *
  649. * @access public
  650. * @var string
  651. */
  652. var $file_src_error;
  653. /**
  654. * Uloaded file name, including server path
  655. *
  656. * @access public
  657. * @var string
  658. */
  659. var $file_src_pathname;
  660. /**
  661. * Uloaded file name temporary copy
  662. *
  663. * @access private
  664. * @var string
  665. */
  666. var $file_src_temp;
  667. /**
  668. * Destination file name
  669. *
  670. * @access public
  671. * @var string
  672. */
  673. var $file_dst_path;
  674. /**
  675. * Destination file name
  676. *
  677. * @access public
  678. * @var string
  679. */
  680. var $file_dst_name;
  681. /**
  682. * Destination file name body (i.e. without extension)
  683. *
  684. * @access public
  685. * @var string
  686. */
  687. var $file_dst_name_body;
  688. /**
  689. * Destination file extension
  690. *
  691. * @access public
  692. * @var string
  693. */
  694. var $file_dst_name_ext;
  695. /**
  696. * Destination file name, including path
  697. *
  698. * @access public
  699. * @var string
  700. */
  701. var $file_dst_pathname;
  702. /**
  703. * Source image width
  704. *
  705. * @access public
  706. * @var integer
  707. */
  708. var $image_src_x;
  709. /**
  710. * Source image height
  711. *
  712. * @access public
  713. * @var integer
  714. */
  715. var $image_src_y;
  716. /**
  717. * Source image color depth
  718. *
  719. * @access public
  720. * @var integer
  721. */
  722. var $image_src_bits;
  723. /**
  724. * Number of pixels
  725. *
  726. * @access public
  727. * @var long
  728. */
  729. var $image_src_pixels;
  730. /**
  731. * Type of image (png, gif, jpg or bmp)
  732. *
  733. * @access public
  734. * @var string
  735. */
  736. var $image_src_type;
  737. /**
  738. * Destination image width
  739. *
  740. * @access public
  741. * @var integer
  742. */
  743. var $image_dst_x;
  744. /**
  745. * Destination image height
  746. *
  747. * @access public
  748. * @var integer
  749. */
  750. var $image_dst_y;
  751. /**
  752. * Supported image formats
  753. *
  754. * @access private
  755. * @var array
  756. */
  757. var $image_supported;
  758. /**
  759. * Flag to determine if the source file is an image
  760. *
  761. * @access public
  762. * @var boolean
  763. */
  764. var $file_is_image;
  765. /**
  766. * Flag set after instanciating the class
  767. *
  768. * Indicates if the file has been uploaded properly
  769. *
  770. * @access public
  771. * @var bool
  772. */
  773. var $uploaded;
  774. /**
  775. * Flag stopping PHP upload checks
  776. *
  777. * Indicates whether we instanciated the class with a filename, in which case
  778. * we will not check on the validity of the PHP *upload*
  779. *
  780. * This flag is automatically set to true when working on a local file
  781. *
  782. * Warning: for uploads, this flag MUST be set to false for security reason
  783. *
  784. * @access public
  785. * @var bool
  786. */
  787. var $no_upload_check;
  788. /**
  789. * Flag set after calling a process
  790. *
  791. * Indicates if the processing, and copy of the resulting file went OK
  792. *
  793. * @access public
  794. * @var bool
  795. */
  796. var $processed;
  797. /**
  798. * Holds eventual error message in plain english
  799. *
  800. * @access public
  801. * @var string
  802. */
  803. var $error;
  804. /**
  805. * Holds an HTML formatted log
  806. *
  807. * @access public
  808. * @var string
  809. */
  810. var $log;
  811. // overiddable processing variables
  812. /**
  813. * Set this variable to replace the name body (i.e. without extension)
  814. *
  815. * @access public
  816. * @var string
  817. */
  818. var $file_new_name_body;
  819. /**
  820. * Set this variable to append a string to the file name body
  821. *
  822. * @access public
  823. * @var string
  824. */
  825. var $file_name_body_add;
  826. /**
  827. * Set this variable to prepend a string to the file name body
  828. *
  829. * @access public
  830. * @var string
  831. */
  832. var $file_name_body_pre;
  833. /**
  834. * Set this variable to change the file extension
  835. *
  836. * @access public
  837. * @var string
  838. */
  839. var $file_new_name_ext;
  840. /**
  841. * Set this variable to format the filename (spaces changed to _)
  842. *
  843. * @access public
  844. * @var boolean
  845. */
  846. var $file_safe_name;
  847. /**
  848. * Forces an extension if the source file doesn't have one
  849. *
  850. * If the file is an image, then the correct extension will be added
  851. * Otherwise, a .txt extension will be chosen
  852. *
  853. * @access public
  854. * @var boolean
  855. */
  856. var $file_force_extension;
  857. /**
  858. * Set this variable to false if you don't want to check the MIME against the allowed list
  859. *
  860. * This variable is set to true by default for security reason
  861. *
  862. * @access public
  863. * @var boolean
  864. */
  865. var $mime_check;
  866. /**
  867. * Set this variable to false in the init() function if you don't want to check the MIME
  868. * with Fileinfo PECL extension. On some systems, Fileinfo is known to be buggy, and you
  869. * may want to deactivate it in the class code directly.
  870. *
  871. * You can also set it with the path of the magic database file.
  872. * If set to true, the class will try to read the MAGIC environment variable
  873. * and if it is empty, will default to the system's default
  874. * If set to an empty string, it will call finfo_open without the path argument
  875. *
  876. * This variable is set to true by default for security reason
  877. *
  878. * @access public
  879. * @var boolean
  880. */
  881. var $mime_fileinfo;
  882. /**
  883. * Set this variable to false in the init() function if you don't want to check the MIME
  884. * with UNIX file() command
  885. *
  886. * This variable is set to true by default for security reason
  887. *
  888. * @access public
  889. * @var boolean
  890. */
  891. var $mime_file;
  892. /**
  893. * Set this variable to false in the init() function if you don't want to check the MIME
  894. * with the magic.mime file
  895. *
  896. * The function mime_content_type() will be deprecated,
  897. * and this variable will be set to false in a future release
  898. *
  899. * This variable is set to true by default for security reason
  900. *
  901. * @access public
  902. * @var boolean
  903. */
  904. var $mime_magic;
  905. /**
  906. * Set this variable to false in the init() function if you don't want to check the MIME
  907. * with getimagesize()
  908. *
  909. * The class tries to get a MIME type from getimagesize()
  910. * If no MIME is returned, it tries to guess the MIME type from the file type
  911. *
  912. * This variable is set to true by default for security reason
  913. *
  914. * @access public
  915. * @var boolean
  916. */
  917. var $mime_getimagesize;
  918. /**
  919. * Set this variable to false if you don't want to turn dangerous scripts into simple text files
  920. *
  921. * @access public
  922. * @var boolean
  923. */
  924. var $no_script;
  925. /**
  926. * Set this variable to true to allow automatic renaming of the file
  927. * if the file already exists
  928. *
  929. * Default value is true
  930. *
  931. * For instance, on uploading foo.ext,<br>
  932. * if foo.ext already exists, upload will be renamed foo_1.ext<br>
  933. * and if foo_1.ext already exists, upload will be renamed foo_2.ext<br>
  934. *
  935. * Note that this option doesn't have any effect if {@link file_overwrite} is true
  936. *
  937. * @access public
  938. * @var bool
  939. */
  940. var $file_auto_rename;
  941. /**
  942. * Set this variable to true to allow automatic creation of the destination
  943. * directory if it is missing (works recursively)
  944. *
  945. * Default value is true
  946. *
  947. * @access public
  948. * @var bool
  949. */
  950. var $dir_auto_create;
  951. /**
  952. * Set this variable to true to allow automatic chmod of the destination
  953. * directory if it is not writeable
  954. *
  955. * Default value is true
  956. *
  957. * @access public
  958. * @var bool
  959. */
  960. var $dir_auto_chmod;
  961. /**
  962. * Set this variable to the default chmod you want the class to use
  963. * when creating directories, or attempting to write in a directory
  964. *
  965. * Default value is 0777 (without quotes)
  966. *
  967. * @access public
  968. * @var bool
  969. */
  970. var $dir_chmod;
  971. /**
  972. * Set this variable tu true to allow overwriting of an existing file
  973. *
  974. * Default value is false, so no files will be overwritten
  975. *
  976. * @access public
  977. * @var bool
  978. */
  979. var $file_overwrite;
  980. /**
  981. * Set this variable to change the maximum size in bytes for an uploaded file
  982. *
  983. * Default value is the value <i>upload_max_filesize</i> from php.ini
  984. *
  985. * Value in bytes (integer) or shorthand byte values (string) is allowed.
  986. * The available options are K (for Kilobytes), M (for Megabytes) and G (for Gigabytes)
  987. *
  988. * @access public
  989. * @var double
  990. */
  991. var $file_max_size;
  992. /**
  993. * Set this variable to true to resize the file if it is an image
  994. *
  995. * You will probably want to set {@link image_x} and {@link image_y}, and maybe one of the ratio variables
  996. *
  997. * Default value is false (no resizing)
  998. *
  999. * @access public
  1000. * @var bool
  1001. */
  1002. var $image_resize;
  1003. /**
  1004. * Set this variable to convert the file if it is an image
  1005. *
  1006. * Possibles values are : ''; 'png'; 'jpeg'; 'gif'; 'bmp'
  1007. *
  1008. * Default value is '' (no conversion)<br>
  1009. * If {@link resize} is true, {@link convert} will be set to the source file extension
  1010. *
  1011. * @access public
  1012. * @var string
  1013. */
  1014. var $image_convert;
  1015. /**
  1016. * Set this variable to the wanted (or maximum/minimum) width for the processed image, in pixels
  1017. *
  1018. * Default value is 150
  1019. *
  1020. * @access public
  1021. * @var integer
  1022. */
  1023. var $image_x;
  1024. /**
  1025. * Set this variable to the wanted (or maximum/minimum) height for the processed image, in pixels
  1026. *
  1027. * Default value is 150
  1028. *
  1029. * @access public
  1030. * @var integer
  1031. */
  1032. var $image_y;
  1033. /**
  1034. * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}
  1035. *
  1036. * Default value is false
  1037. *
  1038. * @access public
  1039. * @var bool
  1040. */
  1041. var $image_ratio;
  1042. /**
  1043. * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}
  1044. *
  1045. * The image will be resized as to fill the whole space, and excedent will be cropped
  1046. *
  1047. * Value can also be a string, one or more character from 'TBLR' (top, bottom, left and right)
  1048. * If set as a string, it determines which side of the image is kept while cropping.
  1049. * By default, the part of the image kept is in the center, i.e. it crops equally on both sides
  1050. *
  1051. * Default value is false
  1052. *
  1053. * @access public
  1054. * @var mixed
  1055. */
  1056. var $image_ratio_crop;
  1057. /**
  1058. * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y}
  1059. *
  1060. * The image will be resized to fit entirely in the space, and the rest will be colored.
  1061. * The default color is white, but can be set with {@link image_default_color}
  1062. *
  1063. * Value can also be a string, one or more character from 'TBLR' (top, bottom, left and right)
  1064. * If set as a string, it determines in which side of the space the image is displayed.
  1065. * By default, the image is displayed in the center, i.e. it fills the remaining space equally on both sides
  1066. *
  1067. * Default value is false
  1068. *
  1069. * @access public
  1070. * @var mixed
  1071. */
  1072. var $image_ratio_fill;
  1073. /**
  1074. * Set this variable to a number of pixels so that {@link image_x} and {@link image_y} are the best match possible
  1075. *
  1076. * The image will be resized to have approximatively the number of pixels
  1077. * The aspect ratio wil be conserved
  1078. *
  1079. * Default value is false
  1080. *
  1081. * @access public
  1082. * @var mixed
  1083. */
  1084. var $image_ratio_pixels;
  1085. /**
  1086. * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y},
  1087. * but only if original image is bigger
  1088. *
  1089. * Default value is false
  1090. *
  1091. * @access public
  1092. * @var bool
  1093. */
  1094. var $image_ratio_no_zoom_in;
  1095. /**
  1096. * Set this variable to keep the original size ratio to fit within {@link image_x} x {@link image_y},
  1097. * but only if original image is smaller
  1098. *
  1099. * Default value is false
  1100. *
  1101. * @access public
  1102. * @var bool
  1103. */
  1104. var $image_ratio_no_zoom_out;
  1105. /**
  1106. * Set this variable to calculate {@link image_x} automatically , using {@link image_y} and conserving ratio
  1107. *
  1108. * Default value is false
  1109. *
  1110. * @access public
  1111. * @var bool
  1112. */
  1113. var $image_ratio_x;
  1114. /**
  1115. * Set this variable to calculate {@link image_y} automatically , using {@link image_x} and conserving ratio
  1116. *
  1117. * Default value is false
  1118. *
  1119. * @access public
  1120. * @var bool
  1121. */
  1122. var $image_ratio_y;
  1123. /**
  1124. * Set this variable to set a maximum image width, above which the upload will be invalid
  1125. *
  1126. * Default value is null
  1127. *
  1128. * @access public
  1129. * @var integer
  1130. */
  1131. var $image_max_width;
  1132. /**
  1133. * Set this variable to set a maximum image height, above which the upload will be invalid
  1134. *
  1135. * Default value is null
  1136. *
  1137. * @access public
  1138. * @var integer
  1139. */
  1140. var $image_max_height;
  1141. /**
  1142. * Set this variable to set a maximum number of pixels for an image, above which the upload will be invalid
  1143. *
  1144. * Default value is null
  1145. *
  1146. * @access public
  1147. * @var long
  1148. */
  1149. var $image_max_pixels;
  1150. /**
  1151. * Set this variable to set a maximum image aspect ratio, above which the upload will be invalid
  1152. *
  1153. * Note that ratio = width / height
  1154. *
  1155. * Default value is null
  1156. *
  1157. * @access public
  1158. * @var float
  1159. */
  1160. var $image_max_ratio;
  1161. /**
  1162. * Set this variable to set a minimum image width, below which the upload will be invalid
  1163. *
  1164. * Default value is null
  1165. *
  1166. * @access public
  1167. * @var integer
  1168. */
  1169. var $image_min_width;
  1170. /**
  1171. * Set this variable to set a minimum image height, below which the upload will be invalid
  1172. *
  1173. * Default value is null
  1174. *
  1175. * @access public
  1176. * @var integer
  1177. */
  1178. var $image_min_height;
  1179. /**
  1180. * Set this variable to set a minimum number of pixels for an image, below which the upload will be invalid
  1181. *
  1182. * Default value is null
  1183. *
  1184. * @access public
  1185. * @var long
  1186. */
  1187. var $image_min_pixels;
  1188. /**
  1189. * Set this variable to set a minimum image aspect ratio, below which the upload will be invalid
  1190. *
  1191. * Note that ratio = width / height
  1192. *
  1193. * Default value is null
  1194. *
  1195. * @access public
  1196. * @var float
  1197. */
  1198. var $image_min_ratio;
  1199. /**
  1200. * Compression level for PNG images
  1201. *
  1202. * Between 1 (fast but large files) and 9 (slow but smaller files)
  1203. *
  1204. * Default value is null (Zlib default)
  1205. *
  1206. * @access public
  1207. * @var integer
  1208. */
  1209. var $png_compression;
  1210. /**
  1211. * Quality of JPEG created/converted destination image
  1212. *
  1213. * Default value is 85
  1214. *
  1215. * @access public
  1216. * @var integer
  1217. */
  1218. var $jpeg_quality;
  1219. /**
  1220. * Determines the quality of the JPG image to fit a desired file size
  1221. *
  1222. * The JPG quality will be set between 1 and 100%
  1223. * The calculations are approximations.
  1224. *
  1225. * Value in bytes (integer) or shorthand byte values (string) is allowed.
  1226. * The available options are K (for Kilobytes), M (for Megabytes) and G (for Gigabytes)
  1227. *
  1228. * Default value is null (no calculations)
  1229. *
  1230. * @access public
  1231. * @var integer
  1232. */
  1233. var $jpeg_size;
  1234. /**
  1235. * Turns the interlace bit on
  1236. *
  1237. * This is actually used only for JPEG images, and defaults to false
  1238. *
  1239. * @access public
  1240. * @var boolean
  1241. */
  1242. var $image_interlace;
  1243. /**
  1244. * Preserve transparency when resizing or converting an image (deprecated)
  1245. *
  1246. * Default value is automatically set to true for transparent GIFs
  1247. * This setting is now deprecated
  1248. *
  1249. * @access public
  1250. * @var integer
  1251. */
  1252. var $preserve_transparency;
  1253. /**
  1254. * Flag set to true when the image is transparent
  1255. *
  1256. * This is actually used only for transparent GIFs
  1257. *
  1258. * @access public
  1259. * @var boolean
  1260. */
  1261. var $image_is_transparent;
  1262. /**
  1263. * Transparent color in a palette
  1264. *
  1265. * This is actually used only for transparent GIFs
  1266. *
  1267. * @access public
  1268. * @var boolean
  1269. */
  1270. var $image_transparent_color;
  1271. /**
  1272. * Background color, used to paint transparent areas with
  1273. *
  1274. * If set, it will forcibly remove transparency by painting transparent areas with the color
  1275. * This setting will fill in all transparent areas in PNG and GIF, as opposed to {@link image_default_color}
  1276. * which will do so only in BMP, JPEG, and alpha transparent areas in transparent GIFs
  1277. * This setting overrides {@link image_default_color}
  1278. *
  1279. * Default value is null
  1280. *
  1281. * @access public
  1282. * @var string
  1283. */
  1284. var $image_background_color;
  1285. /**
  1286. * Default color for non alpha-transparent images
  1287. *
  1288. * This setting is to be used to define a background color for semi transparent areas
  1289. * of an alpha transparent when the output format doesn't support alpha transparency
  1290. * This is useful when, from an alpha transparent PNG image, or an image with alpha transparent features
  1291. * if you want to output it as a transparent GIFs for instance, you can set a blending color for transparent areas
  1292. * If you output in JPEG or BMP, this color will be used to fill in the previously transparent areas
  1293. *
  1294. * The default color white
  1295. *
  1296. * @access public
  1297. * @var boolean
  1298. */
  1299. var $image_default_color;
  1300. /**
  1301. * Flag set to true when the image is not true color
  1302. *
  1303. * @access public
  1304. * @var boolean
  1305. */
  1306. var $image_is_palette;
  1307. /**
  1308. * Corrects the image brightness
  1309. *
  1310. * Value can range between -127 and 127
  1311. *
  1312. * Default value is null
  1313. *
  1314. * @access public
  1315. * @var integer
  1316. */
  1317. var $image_brightness;
  1318. /**
  1319. * Corrects the image contrast
  1320. *
  1321. * Value can range between -127 and 127
  1322. *
  1323. * Default value is null
  1324. *
  1325. * @access public
  1326. * @var integer
  1327. */
  1328. var $image_contrast;
  1329. /**
  1330. * Changes the image opacity
  1331. *
  1332. * Value can range between 0 and 100
  1333. *
  1334. * Default value is null
  1335. *
  1336. * @access public
  1337. * @var integer
  1338. */
  1339. var $image_opacity;
  1340. /**
  1341. * Applies threshold filter
  1342. *
  1343. * Value can range between -127 and 127
  1344. *
  1345. * Default value is null
  1346. *
  1347. * @access public
  1348. * @var integer
  1349. */
  1350. var $image_threshold;
  1351. /**
  1352. * Applies a tint on the image
  1353. *
  1354. * Value is an hexadecimal color, such as #FFFFFF
  1355. *
  1356. * Default value is null
  1357. *
  1358. * @access public
  1359. * @var string;
  1360. */
  1361. var $image_tint_color;
  1362. /**
  1363. * Applies a colored overlay on the image
  1364. *
  1365. * Value is an hexadecimal color, such as #FFFFFF
  1366. *
  1367. * To use with {@link image_overlay_opacity}
  1368. *
  1369. * Default value is null
  1370. *
  1371. * @access public
  1372. * @var string;
  1373. */
  1374. var $image_overlay_color;
  1375. /**
  1376. * Sets the opacity for the colored overlay
  1377. *
  1378. * Value is a percentage, as an integer between 0 (transparent) and 100 (opaque)
  1379. *
  1380. * Unless used with {@link image_overlay_color}, this setting has no effect
  1381. *
  1382. * Default value is 50
  1383. *
  1384. * @access public
  1385. * @var integer
  1386. */
  1387. var $image_overlay_opacity;
  1388. /**
  1389. * Soon to be deprecated old form of {@link image_overlay_opacity}
  1390. *
  1391. * @access public
  1392. * @var integer
  1393. */
  1394. var $image_overlay_percent;
  1395. /**
  1396. * Inverts the color of an image
  1397. *
  1398. * Default value is FALSE
  1399. *
  1400. * @access public
  1401. * @var boolean;
  1402. */
  1403. var $image_negative;
  1404. /**
  1405. * Turns the image into greyscale
  1406. *
  1407. * Default value is FALSE
  1408. *
  1409. * @access public
  1410. * @var boolean;
  1411. */
  1412. var $image_greyscale;
  1413. /**
  1414. * Pixelate an image
  1415. *
  1416. * Value is integer, represents the block size
  1417. *
  1418. * Default value is null
  1419. *
  1420. * @access public
  1421. * @var integer;
  1422. */
  1423. var $image_pixelate;
  1424. /**
  1425. * Applies an unsharp mask, with alpha transparency support
  1426. *
  1427. * Beware that this unsharp mask is quite resource-intensive
  1428. *
  1429. * Default value is FALSE
  1430. *
  1431. * @access public
  1432. * @var boolean;
  1433. */
  1434. var $image_unsharp;
  1435. /**
  1436. * Sets the unsharp mask amount
  1437. *
  1438. * Value is an integer between 0 and 500, typically between 50 and 200
  1439. *
  1440. * Unless used with {@link image_unsharp}, this setting has no effect
  1441. *
  1442. * Default value is 80
  1443. *
  1444. * @access public
  1445. * @var integer
  1446. */
  1447. var $image_unsharp_amount;
  1448. /**
  1449. * Sets the unsharp mask radius
  1450. *
  1451. * Value is an integer between 0 and 50, typically between 0.5 and 1
  1452. * It is not recommended to change it, the default works best
  1453. *
  1454. * Unless used with {@link image_unsharp}, this setting has no effect
  1455. *
  1456. * From PHP 5.1, imageconvolution is used, and this setting has no effect
  1457. *
  1458. * Default value is 0.5
  1459. *
  1460. * @access public
  1461. * @var integer
  1462. */
  1463. var $image_unsharp_radius;
  1464. /**
  1465. * Sets the unsharp mask threshold
  1466. *
  1467. * Value is an integer between 0 and 255, typically between 0 and 5
  1468. *
  1469. * Unless used with {@link image_unsharp}, this setting has no effect
  1470. *
  1471. * Default value is 1
  1472. *
  1473. * @access public
  1474. * @var integer
  1475. */
  1476. var $image_unsharp_threshold;
  1477. /**
  1478. * Adds a text label on the image
  1479. *
  1480. * Value is a string, any text. Text will not word-wrap, although you can use breaklines in your text "\n"
  1481. *
  1482. * If set, this setting allow the use of all other settings starting with image_text_
  1483. *
  1484. * Replacement tokens can be used in the string:
  1485. * <pre>
  1486. * gd_version src_name src_name_body src_name_ext
  1487. * src_pathname src_mime src_x src_y
  1488. * src_type src_bits src_pixels
  1489. * src_size src_size_kb src_size_mb src_size_human
  1490. * dst_path dst_name_body dst_pathname
  1491. * dst_name dst_name_ext dst_x dst_y
  1492. * date time host server ip
  1493. * </pre>
  1494. * The tokens must be enclosed in square brackets: [dst_x] will be replaced by the width of the picture
  1495. *
  1496. * Default value is null
  1497. *
  1498. * @access public
  1499. * @var string;
  1500. */
  1501. var $image_text;
  1502. /**
  1503. * Sets the text direction for the text label
  1504. *
  1505. * Value is either 'h' or 'v', as in horizontal and vertical
  1506. *
  1507. * Default value is h (horizontal)
  1508. *
  1509. * @access public
  1510. * @var string;
  1511. */
  1512. var $image_text_direction;
  1513. /**
  1514. * Sets the text color for the text label
  1515. *
  1516. * Value is an hexadecimal color, such as #FFFFFF
  1517. *
  1518. * Default value is #FFFFFF (white)
  1519. *
  1520. * @access public
  1521. * @var string;
  1522. */
  1523. var $image_text_color;
  1524. /**
  1525. * Sets the text opacity in the text label
  1526. *
  1527. * Value is a percentage, as an integer between 0 (transparent) and 100 (opaque)
  1528. *
  1529. * Default value is 100
  1530. *
  1531. * @access public
  1532. * @var integer
  1533. */
  1534. var $image_text_opacity;
  1535. /**
  1536. * Soon to be deprecated old form of {@link image_text_opacity}
  1537. *
  1538. * @access public
  1539. * @var integer
  1540. */
  1541. var $image_text_percent;
  1542. /**
  1543. * Sets the text background color for the text label
  1544. *
  1545. * Value is an hexadecimal color, such as #FFFFFF
  1546. *
  1547. * Default value is null (no background)
  1548. *
  1549. * @access public
  1550. * @var string;
  1551. */
  1552. var $image_text_background;
  1553. /**
  1554. * Sets the text background opacity in the text label
  1555. *
  1556. * Value is a percentage, as an integer between 0 (transparent) and 100 (opaque)
  1557. *
  1558. * Default value is 100
  1559. *
  1560. * @access public
  1561. * @var integer
  1562. */
  1563. var $image_text_background_opacity;
  1564. /**
  1565. * Soon to be deprecated old form of {@link image_text_background_opacity}
  1566. *
  1567. * @access public
  1568. * @var integer
  1569. */
  1570. var $image_text_background_percent;
  1571. /**
  1572. * Sets the text font in the text label
  1573. *
  1574. * Value is a an integer between 1 and 5 for GD built-in fonts. 1 is the smallest font, 5 the biggest
  1575. * Value can also be a string, which represents the path to a GDF font. The font will be loaded into GD, and used as a built-in font.
  1576. *
  1577. * Default value is 5
  1578. *
  1579. * @access public
  1580. * @var mixed;
  1581. */
  1582. var $image_text_font;
  1583. /**
  1584. * Sets the text label position within the image
  1585. *
  1586. * Value is one or two out of 'TBLR' (top, bottom, left, right)
  1587. *
  1588. * The positions are as following:
  1589. * <pre>
  1590. * TL T TR
  1591. * L R
  1592. * BL B BR
  1593. * </pre>
  1594. *
  1595. * Default value is null (centered, horizontal and vertical)
  1596. *
  1597. * Note that is {@link image_text_x} and {@link image_text_y} are used, this setting has no effect
  1598. *
  1599. * @access public
  1600. * @var string;
  1601. */
  1602. var $image_text_position;
  1603. /**
  1604. * Sets the text label absolute X position within the image
  1605. *
  1606. * Value is in pixels, representing the distance between the left of the image and the label
  1607. * If a negative value is used, it will represent the distance between the right of the image and the label
  1608. *
  1609. * Default value is null (so {@link image_text_position} is used)
  1610. *
  1611. * @access public
  1612. * @var integer
  1613. */
  1614. var $image_text_x;
  1615. /**
  1616. * Sets the text label absolute Y position within the image
  1617. *
  1618. * Value is in pixels, representing the distance between the top of the image and the label
  1619. * If a negative value is used, it will represent the distance between the bottom of the image and the label
  1620. *
  1621. * Default value is null (so {@link image_text_position} is used)
  1622. *
  1623. * @access public
  1624. * @var integer
  1625. */
  1626. var $image_text_y;
  1627. /**
  1628. * Sets the text label padding
  1629. *
  1630. * Value is in pixels, representing the distance between the text and the label background border
  1631. *
  1632. * Default value is 0
  1633. *
  1634. * This setting can be overriden by {@link image_text_padding_x} and {@link image_text_padding_y}
  1635. *
  1636. * @access public
  1637. * @var integer
  1638. */
  1639. var $image_text_padding;
  1640. /**
  1641. * Sets the text label horizontal padding
  1642. *
  1643. * Value is in pixels, representing the distance between the text and the left and right label background borders
  1644. *
  1645. * Default value is null
  1646. *
  1647. * If set, this setting overrides the horizontal part of {@link image_text_padding}
  1648. *
  1649. * @access public
  1650. * @var integer
  1651. */
  1652. var $image_text_padding_x;
  1653. /**
  1654. * Sets the text label vertical padding
  1655. *
  1656. * Value is in pixels, representing the distance between the text and the top and bottom label background borders
  1657. *
  1658. * Default value is null
  1659. *
  1660. * If set, his setting overrides the vertical part of {@link image_text_padding}
  1661. *
  1662. * @access public
  1663. * @var integer
  1664. */
  1665. var $image_text_padding_y;
  1666. /**
  1667. * Sets the text alignment
  1668. *
  1669. * Value is a string, which can be either 'L', 'C' or 'R'
  1670. *
  1671. * Default value is 'C'
  1672. *
  1673. * This setting is relevant only if the text has several lines.
  1674. *
  1675. * @access public
  1676. * @var string;
  1677. */
  1678. var $image_text_alignment;
  1679. /**
  1680. * Sets the text line spacing
  1681. *
  1682. * Value is an integer, in pixels
  1683. *
  1684. * Default value is 0
  1685. *
  1686. * This setting is relevant only if the text has several lines.
  1687. *
  1688. * @access public
  1689. * @var integer
  1690. */
  1691. var $image_text_line_spacing;
  1692. /**
  1693. * Sets the height of the reflection
  1694. *
  1695. * Value is an integer in pixels, or a string which format can be in pixels or percentage.
  1696. * For instance, values can be : 40, '40', '40px' or '40%'
  1697. *
  1698. * Default value is null, no reflection
  1699. *
  1700. * @access public
  1701. * @var mixed;
  1702. */
  1703. var $image_reflection_height;
  1704. /**
  1705. * Sets the space between the source image and its relection
  1706. *
  1707. * Value is an integer in pixels, which can be negative
  1708. *
  1709. * Default value is 2
  1710. *
  1711. * This setting is relevant only if {@link image_reflection_height} is set
  1712. *
  1713. * @access public
  1714. * @var integer
  1715. */
  1716. var $image_reflection_space;
  1717. /**
  1718. * Sets the color of the reflection background (deprecated)
  1719. *
  1720. * Value is an hexadecimal color, such as #FFFFFF
  1721. *
  1722. * Default value is #FFFFFF
  1723. *
  1724. * This setting is relevant only if {@link image_reflection_height} is set
  1725. *
  1726. * This setting is now deprecated in favor of {@link image_default_color}
  1727. *
  1728. * @access public
  1729. * @var string;
  1730. */
  1731. var $image_reflection_color;
  1732. /**
  1733. * Sets the initial opacity of the reflection
  1734. *
  1735. * Value is an integer between 0 (no opacity) and 100 (full opacity).
  1736. * The reflection will start from {@link image_reflection_opacity} and end up at 0
  1737. *
  1738. * Default value is 60
  1739. *
  1740. * This setting is relevant only if {@link image_reflection_height} is set
  1741. *
  1742. * @access public
  1743. * @var integer
  1744. */
  1745. var $image_reflection_opacity;
  1746. /**
  1747. * Flips the image vertically or horizontally
  1748. *
  1749. * Value is either 'h' or 'v', as in horizontal and vertical
  1750. *
  1751. * Default value is null (no flip)
  1752. *
  1753. * @access public
  1754. * @var string;
  1755. */
  1756. var $image_flip;
  1757. /**
  1758. * Rotates the image by increments of 45 degrees
  1759. *
  1760. * Value is either 90, 180 or 270
  1761. *
  1762. * Default value is null (no rotation)
  1763. *
  1764. * @access public
  1765. * @var string;
  1766. */
  1767. var $image_rotate;
  1768. /**
  1769. * Crops an image
  1770. *
  1771. * Values are four dimensions, or two, or one (CSS style)
  1772. * They represent the amount cropped top, right, bottom and left.
  1773. * These values can either be in an array, or a space separated string.
  1774. * Each value can be in pixels (with or without 'px'), or percentage (of the source image)
  1775. *
  1776. * For instance, are valid:
  1777. * <pre>
  1778. * $foo->image_crop = 20 OR array(20);
  1779. * $foo->image_crop = '20px' OR array('20px');
  1780. * $foo->image_crop = '20 40' OR array('20', 40);
  1781. * $foo->image_crop = '-20 25%' OR array(-20, '25%');
  1782. * $foo->image_crop = '20px 25%' OR array('20px', '25%');
  1783. * $foo->image_crop = '20% 25%' OR array('20%', '25%');
  1784. * $foo->image_crop = '20% 25% 10% 30%' OR array('20%', '25%', '10%', '30%');
  1785. * $foo->image_crop = '20px 25px 2px 2px' OR array('20px', '25%px', '2px', '2px');
  1786. * $foo->image_crop = '20 25% 40px 10%' OR array(20, '25%', '40px', '10%');
  1787. * </pre>
  1788. *
  1789. * If a value is negative, the image will be expanded, and the extra parts will be filled with black
  1790. *
  1791. * Default value is null (no cropping)
  1792. *
  1793. * @access public
  1794. * @var string OR array;
  1795. */
  1796. var $image_crop;
  1797. /**
  1798. * Crops an image, before an eventual resizing
  1799. *
  1800. * See {@link image_crop} for valid formats
  1801. *
  1802. * Default value is null (no cropping)
  1803. *
  1804. * @access public
  1805. * @var string OR array;
  1806. */
  1807. var $image_precrop;
  1808. /**
  1809. * Adds a bevel border on the image
  1810. *
  1811. * Value is a positive integer, representing the thickness of the bevel
  1812. *
  1813. * If the bevel colors are the same as the background, it makes a fade out effect
  1814. *
  1815. * Default value is null (no bevel)
  1816. *
  1817. * @access public
  1818. * @var integer
  1819. */
  1820. var $image_bevel;
  1821. /**
  1822. * Top and left bevel color
  1823. *
  1824. * Value is a color, in hexadecimal format
  1825. * This setting is used only if {@link image_bevel} is set
  1826. *
  1827. * Default value is #FFFFFF
  1828. *
  1829. * @access public
  1830. * @var string;
  1831. */
  1832. var $image_bevel_color1;
  1833. /**
  1834. * Right and bottom bevel color
  1835. *
  1836. * Value is a color, in hexadecimal format
  1837. * This setting is used only if {@link image_bevel} is set
  1838. *
  1839. * Default value is #000000
  1840. *
  1841. * @access public
  1842. * @var string;
  1843. */
  1844. var $image_bevel_color2;
  1845. /**
  1846. * Adds a single-color border on the outer of the image
  1847. *
  1848. * Values are four dimensions, or two, or one (CSS style)
  1849. * They represent the border thickness top, right, bottom and left.
  1850. * These values can either be in an array, or a space separated string.
  1851. * Each value can be in pixels (with or without 'px'), or percentage (of the source image)
  1852. *
  1853. * See {@link image_crop} for valid formats
  1854. *
  1855. * If a value is negative, the image will be cropped.
  1856. * Note that the dimensions of the picture will be increased by the borders' thickness
  1857. *
  1858. * Default value is null (no border)
  1859. *
  1860. * @access public
  1861. * @var integer
  1862. */
  1863. var $image_border;
  1864. /**
  1865. * Border color
  1866. *
  1867. * Value is a color, in hexadecimal format.
  1868. * This setting is used only if {@link image_border} is set
  1869. *
  1870. * Default value is #FFFFFF
  1871. *
  1872. * @access public
  1873. * @var string;
  1874. */
  1875. var $image_border_color;
  1876. /**
  1877. * Sets the opacity for the borders
  1878. *
  1879. * Value is a percentage, as an integer between 0 (transparent) and 100 (opaque)
  1880. *
  1881. * Unless used with {@link image_border}, this setting has no effect
  1882. *
  1883. * Default value is 100
  1884. *
  1885. * @access public
  1886. * @var integer
  1887. */
  1888. var $image_border_opacity;
  1889. /**
  1890. * Adds a fading-to-transparent border on the image
  1891. *
  1892. * Values are four dimensions, or two, or one (CSS style)
  1893. * They represent the border thickness top, right, bottom and left.
  1894. * These values can either be in an array, or a space separated string.
  1895. * Each value can be in pixels (with or without 'px'), or percentage (of the source image)
  1896. *
  1897. * See {@link image_crop} for valid formats
  1898. *
  1899. * Note that the dimensions of the picture will not be increased by the borders' thickness
  1900. *
  1901. * Default value is null (no border)
  1902. *
  1903. * @access public
  1904. * @var integer
  1905. */
  1906. var $image_border_transparent;
  1907. /**
  1908. * Adds a multi-color frame on the outer of the image
  1909. *
  1910. * Value is an integer. Two values are possible for now:
  1911. * 1 for flat border, meaning that the frame is mirrored horizontally and vertically
  1912. * 2 for crossed border, meaning that the frame will be inversed, as in a bevel effect
  1913. *
  1914. * The frame will be composed of colored lines set in {@link image_frame_colors}
  1915. *
  1916. * Note that the dimensions of the picture will be increased by the borders' thickness
  1917. *
  1918. * Default value is null (no frame)
  1919. *
  1920. * @access public
  1921. * @var integer
  1922. */
  1923. var $image_frame;
  1924. /**
  1925. * Sets the colors used to draw a frame
  1926. *
  1927. * Values is a list of n colors in hexadecimal format.
  1928. * These values can either be in an array, or a space separated string.
  1929. *
  1930. * The colors are listed in the following order: from the outset of the image to its center
  1931. *
  1932. * For instance, are valid:
  1933. * <pre>
  1934. * $foo->image_frame_colors = '#FFFFFF #999999 #666666 #000000';
  1935. * $foo->image_frame_colors = array('#FFFFFF', '#999999', '#666666', '#000000');
  1936. * </pre>
  1937. *
  1938. * This setting is used only if {@link image_frame} is set
  1939. *
  1940. * Default value is '#FFFFFF #999999 #666666 #000000'
  1941. *
  1942. * @access public
  1943. * @var string OR array;
  1944. */
  1945. var $image_frame_colors;
  1946. /**
  1947. * Sets the opacity for the frame
  1948. *
  1949. * Value is a percentage, as an integer between 0 (transparent) and 100 (opaque)
  1950. *
  1951. * Unless used with {@link image_frame}, this setting has no effect
  1952. *
  1953. * Default value is 100
  1954. *
  1955. * @access public
  1956. * @var integer
  1957. */
  1958. var $image_frame_opacity;
  1959. /**
  1960. * Adds a watermark on the image
  1961. *
  1962. * Value is a local image filename, relative or absolute. GIF, JPG, BMP and PNG are supported, as well as PNG alpha.
  1963. *
  1964. * If set, this setting allow the use of all other settings starting with image_watermark_
  1965. *
  1966. * Default value is null
  1967. *
  1968. * @access public
  1969. * @var string;
  1970. */
  1971. var $image_watermark;
  1972. /**
  1973. * Sets the watermarkposition within the image
  1974. *
  1975. * Value is one or two out of 'TBLR' (top, bottom, left, right)
  1976. *
  1977. * The positions are as following: TL T TR
  1978. * L R
  1979. * BL B BR
  1980. *
  1981. * Default value is null (centered, horizontal and vertical)
  1982. *
  1983. * Note that is {@link image_watermark_x} and {@link image_watermark_y} are used, this setting has no effect
  1984. *
  1985. * @access public
  1986. * @var string;
  1987. */
  1988. var $image_watermark_position;
  1989. /**
  1990. * Sets the watermark absolute X position within the image
  1991. *
  1992. * Value is in pixels, representing the distance between the top of the image and the watermark
  1993. * If a negative value is used, it will represent the distance between the bottom of the image and the watermark
  1994. *
  1995. * Default value is null (so {@link image_watermark_position} is used)
  1996. *
  1997. * @access public
  1998. * @var integer
  1999. */
  2000. var $image_watermark_x;
  2001. /**
  2002. * Sets the twatermark absolute Y position within the image
  2003. *
  2004. * Value is in pixels, representing the distance between the left of the image and the watermark
  2005. * If a negative value is used, it will represent the distance between the right of the image and the watermark
  2006. *
  2007. * Default value is null (so {@link image_watermark_position} is used)
  2008. *
  2009. * @access public
  2010. * @var integer
  2011. */
  2012. var $image_watermark_y;
  2013. /**
  2014. * Prevents the watermark to be resized up if it is smaller than the image
  2015. *
  2016. * If the watermark if smaller than the destination image, taking in account the desired watermark position
  2017. * then it will be resized up to fill in the image (minus the {@link image_watermark_x} or {@link image_watermark_y} values)
  2018. *
  2019. * If you don't want your watermark to be resized in any way, then
  2020. * set {@link image_watermark_no_zoom_in} and {@link image_watermark_no_zoom_out} to true
  2021. * If you want your watermark to be resized up or doan to fill in the image better, then
  2022. * set {@link image_watermark_no_zoom_in} and {@link image_watermark_no_zoom_out} to false
  2023. *
  2024. * Default value is true (so the watermark will not be resized up, which is the behaviour most people expect)
  2025. *
  2026. * @access public
  2027. * @var integer
  2028. */
  2029. var $image_watermark_no_zoom_in;
  2030. /**
  2031. * Prevents the watermark to be resized down if it is bigger than the image
  2032. *
  2033. * If the watermark if bigger than the destination image, taking in account the desired watermark position
  2034. * then it will be resized down to fit in the image (minus the {@link image_watermark_x} or {@link image_watermark_y} values)
  2035. *
  2036. * If you don't want your watermark to be resized in any way, then
  2037. * set {@link image_watermark_no_zoom_in} and {@link image_watermark_no_zoom_out} to true
  2038. * If you want your watermark to be resized up or doan to fill in the image better, then
  2039. * set {@link image_watermark_no_zoom_in} and {@link image_watermark_no_zoom_out} to false
  2040. *
  2041. * Default value is false (so the watermark may be shrinked to fit in the image)
  2042. *
  2043. * @access public
  2044. * @var integer
  2045. */
  2046. var $image_watermark_no_zoom_out;
  2047. /**
  2048. * List of MIME types per extension
  2049. *
  2050. * @access private
  2051. * @var array
  2052. */
  2053. var $mime_types;
  2054. /**
  2055. * Allowed MIME types
  2056. *
  2057. * Default is a selection of safe mime-types, but you might want to change it
  2058. *
  2059. * Simple wildcards are allowed, such as image/* or application/*
  2060. * If there is only one MIME type allowed, then it can be a string instead of an array
  2061. *
  2062. * @access public
  2063. * @var array OR string
  2064. */
  2065. var $allowed;
  2066. /**
  2067. * Forbidden MIME types
  2068. *
  2069. * Default is a selection of safe mime-types, but you might want to change it
  2070. * To only check for forbidden MIME types, and allow everything else, set {@link allowed} to array('* / *') without the spaces
  2071. *
  2072. * Simple wildcards are allowed, such as image/* or application/*
  2073. * If there is only one MIME type forbidden, then it can be a string instead of an array
  2074. *
  2075. * @access public
  2076. * @var array OR string
  2077. */
  2078. var $forbidden;
  2079. /**
  2080. * Array of translated error messages
  2081. *
  2082. * By default, the language is english (en_GB)
  2083. * Translations can be in separate files, in a lang/ subdirectory
  2084. *
  2085. * @access public
  2086. * @var array
  2087. */
  2088. var $translation;
  2089. /**
  2090. * Language selected for the translations
  2091. *
  2092. * By default, the language is english ("en_GB")
  2093. *
  2094. * @access public
  2095. * @var array
  2096. */
  2097. var $language;
  2098. function __construct() {
  2099. }
  2100. /**
  2101. * Init or re-init all the processing variables to their default values
  2102. *
  2103. * This function is called in the constructor, and after each call of {@link process}
  2104. *
  2105. * @access private
  2106. */
  2107. function init() {
  2108. // overiddable variables
  2109. $this->file_new_name_body = null; // replace the name body
  2110. $this->file_name_body_add = null; // append to the name body
  2111. $this->file_name_body_pre = null; // prepend to the name body
  2112. $this->file_new_name_ext = null; // replace the file extension
  2113. $this->file_safe_name = true; // format safely the filename
  2114. $this->file_force_extension = true; // forces extension if there isn't one
  2115. $this->file_overwrite = false; // allows overwritting if the file already exists
  2116. $this->file_auto_rename = true; // auto-rename if the file already exists
  2117. $this->dir_auto_create = true; // auto-creates directory if missing
  2118. $this->dir_auto_chmod = true; // auto-chmod directory if not writeable
  2119. $this->dir_chmod = 0777; // default chmod to use
  2120. $this->no_script = true; // turns scripts into test files
  2121. $this->mime_check = true; // checks the mime type against the allowed list
  2122. // these are the different MIME detection methods. if one of these method doesn't work on your
  2123. // system, you can deactivate it here; just set it to false
  2124. $this->mime_fileinfo = true; // MIME detection with Fileinfo PECL extension
  2125. $this->mime_file = true; // MIME detection with UNIX file() command
  2126. $this->mime_magic = true; // MIME detection with mime_magic (mime_content_type())
  2127. $this->mime_getimagesize = true; // MIME detection with getimagesize()
  2128. // get the default max size from php.ini
  2129. $this->file_max_size_raw = trim(ini_get('upload_max_filesize'));
  2130. $this->file_max_size = $this->getsize($this->file_max_size_raw);
  2131. $this->image_resize = false; // resize the image
  2132. $this->image_convert = ''; // convert. values :''; 'png'; 'jpeg'; 'gif'; 'bmp'
  2133. $this->image_x = 150;
  2134. $this->image_y = 150;
  2135. $this->image_ratio = false; // keeps aspect ratio with x and y dimensions
  2136. $this->image_ratio_crop = false; // keeps aspect ratio with x and y dimensions, filling the space
  2137. $this->image_ratio_fill = false; // keeps aspect ratio with x and y dimensions, fitting the image in the space, and coloring the rest
  2138. $this->image_ratio_pixels = false; // keeps aspect ratio, calculating x and y so that the image is approx the set number of pixels
  2139. $this->image_ratio_no_zoom_in = false;
  2140. $this->image_ratio_no_zoom_out = false;
  2141. $this->image_ratio_x = false; // calculate the $image_x if true
  2142. $this->image_ratio_y = false; // calculate the $image_y if true
  2143. $this->png_compression = null;
  2144. $this->jpeg_quality = 85;
  2145. $this->jpeg_size = null;
  2146. $this->image_interlace = false;
  2147. $this->preserve_transparency = false;
  2148. $this->image_is_transparent = false;
  2149. $this->image_transparent_color = null;
  2150. $this->image_background_color = null;
  2151. $this->image_default_color = '#ffffff';
  2152. $this->image_is_palette = false;
  2153. $this->image_max_width = null;
  2154. $this->image_max_height = null;
  2155. $this->image_max_pixels = null;
  2156. $this->image_max_ratio = null;
  2157. $this->image_min_width = null;
  2158. $this->image_min_height = null;
  2159. $this->image_min_pixels = null;
  2160. $this->image_min_ratio = null;
  2161. $this->image_brightness = null;
  2162. $this->image_contrast = null;
  2163. $this->image_opacity = null;
  2164. $this->image_threshold = null;
  2165. $this->image_tint_color = null;
  2166. $this->image_overlay_color = null;
  2167. $this->image_overlay_opacity = null;
  2168. $this->image_overlay_percent = null;
  2169. $this->image_negative = false;
  2170. $this->image_greyscale = false;
  2171. $this->image_pixelate = null;
  2172. $this->image_unsharp = false;
  2173. $this->image_unsharp_amount = 80;
  2174. $this->image_unsharp_radius = 0.5;
  2175. $this->image_unsharp_threshold = 1;
  2176. $this->image_text = null;
  2177. $this->image_text_direction = null;
  2178. $this->image_text_color = '#FFFFFF';
  2179. $this->image_text_opacity = 100;
  2180. $this->image_text_percent = 100;
  2181. $this->image_text_background = null;
  2182. $this->image_text_background_opacity = 100;
  2183. $this->image_text_background_percent = 100;
  2184. $this->image_text_font = 5;
  2185. $this->image_text_x = null;
  2186. $this->image_text_y = null;
  2187. $this->image_text_position = null;
  2188. $this->image_text_padding = 0;
  2189. $this->image_text_padding_x = null;
  2190. $this->image_text_padding_y = null;
  2191. $this->image_text_alignment = 'C';
  2192. $this->image_text_line_spacing = 0;
  2193. $this->image_reflection_height = null;
  2194. $this->image_reflection_space = 2;
  2195. $this->image_reflection_color = '#ffffff';
  2196. $this->image_reflection_opacity = 60;
  2197. $this->image_watermark = null;
  2198. $this->image_watermark_x = null;
  2199. $this->image_watermark_y = null;
  2200. $this->image_watermark_position = null;
  2201. $this->image_watermark_no_zoom_in = true;
  2202. $this->image_watermark_no_zoom_out = false;
  2203. $this->image_flip = null;
  2204. $this->image_rotate = null;
  2205. $this->image_crop = null;
  2206. $this->image_precrop = null;
  2207. $this->image_bevel = null;
  2208. $this->image_bevel_color1 = '#FFFFFF';
  2209. $this->image_bevel_color2 = '#000000';
  2210. $this->image_border = null;
  2211. $this->image_border_color = '#FFFFFF';
  2212. $this->image_border_opacity = 100;
  2213. $this->image_border_transparent = null;
  2214. $this->image_frame = null;
  2215. $this->image_frame_colors = '#FFFFFF #999999 #666666 #000000';
  2216. $this->image_frame_opacity = 100;
  2217. $this->forbidden = array();
  2218. $this->allowed = array(
  2219. 'application/haansofthwp',
  2220. 'application/arj',
  2221. 'application/excel',
  2222. 'application/gnutar',
  2223. 'application/mspowerpoint',
  2224. 'application/msword',
  2225. 'application/octet-stream',
  2226. 'application/onenote',
  2227. 'application/pdf',
  2228. 'application/plain',
  2229. 'application/postscript',
  2230. 'application/powerpoint',
  2231. 'application/rar',
  2232. 'application/rtf',
  2233. 'application/vnd.ms-excel',
  2234. 'application/vnd.ms-excel.addin.macroEnabled.12',
  2235. 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  2236. 'application/vnd.ms-excel.sheet.macroEnabled.12',
  2237. 'application/vnd.ms-excel.template.macroEnabled.12',
  2238. 'application/vnd.ms-office',
  2239. 'application/vnd.ms-officetheme',
  2240. 'application/vnd.ms-powerpoint',
  2241. 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  2242. 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  2243. 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  2244. 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  2245. 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  2246. 'application/vnd.ms-word',
  2247. 'application/vnd.ms-word.document.macroEnabled.12',
  2248. 'application/vnd.ms-word.template.macroEnabled.12',
  2249. 'application/vnd.oasis.opendocument.chart',
  2250. 'application/vnd.oasis.opendocument.database',
  2251. 'application/vnd.oasis.opendocument.formula',
  2252. 'application/vnd.oasis.opendocument.graphics',
  2253. 'application/vnd.oasis.opendocument.graphics-template',
  2254. 'application/vnd.oasis.opendocument.image',
  2255. 'application/vnd.oasis.opendocument.presentation',
  2256. 'application/vnd.oasis.opendocument.presentation-template',
  2257. 'application/vnd.oasis.opendocument.spreadsheet',
  2258. 'application/vnd.oasis.opendocument.spreadsheet-template',
  2259. 'application/vnd.oasis.opendocument.text',
  2260. 'application/vnd.oasis.opendocument.text-master',
  2261. 'application/vnd.oasis.opendocument.text-template',
  2262. 'application/vnd.oasis.opendocument.text-web',
  2263. 'application/vnd.openofficeorg.extension',
  2264. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  2265. 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  2266. 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  2267. 'application/vnd.openxmlformats-officedocument.presentationml.template',
  2268. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  2269. 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  2270. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  2271. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  2272. 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  2273. 'application/vocaltec-media-file',
  2274. 'application/wordperfect',
  2275. 'application/x-bittorrent',
  2276. 'application/x-bzip',
  2277. 'application/x-bzip2',
  2278. 'application/x-compressed',
  2279. 'application/x-excel',
  2280. 'application/x-gzip',
  2281. 'application/x-latex',
  2282. 'application/x-midi',
  2283. 'application/xml',
  2284. 'application/x-msexcel',
  2285. 'application/x-rar',
  2286. 'application/x-rar-compressed',
  2287. 'application/x-rtf',
  2288. 'application/x-shockwave-flash',
  2289. 'application/x-sit',
  2290. 'application/x-stuffit',
  2291. 'application/x-troff-msvideo',
  2292. 'application/x-zip',
  2293. 'application/x-zip-compressed',
  2294. 'application/zip',
  2295. 'audio/*',
  2296. 'image/*',
  2297. 'multipart/x-gzip',
  2298. 'multipart/x-zip',
  2299. 'text/plain',
  2300. 'text/rtf',
  2301. 'text/richtext',
  2302. 'text/xml',
  2303. 'video/*'
  2304. );
  2305. $this->mime_types = array(
  2306. 'hwp' => 'application/haansofthwp',
  2307. 'jpg' => 'image/jpeg',
  2308. 'jpeg' => 'image/jpeg',
  2309. 'jpe' => 'image/jpeg',
  2310. 'gif' => 'image/gif',
  2311. 'png' => 'image/png',
  2312. 'bmp' => 'image/bmp',
  2313. 'flv' => 'video/x-flv',
  2314. 'js' => 'application/x-javascript',
  2315. 'json' => 'application/json',
  2316. 'tiff' => 'image/tiff',
  2317. 'css' => 'text/css',
  2318. 'xml' => 'application/xml',
  2319. 'doc' => 'application/msword',
  2320. 'docx' => 'application/msword',
  2321. 'xls' => 'application/vnd.ms-excel',
  2322. 'xlt' => 'application/vnd.ms-excel',
  2323. 'xlm' => 'application/vnd.ms-excel',
  2324. 'xld' => 'application/vnd.ms-excel',
  2325. 'xla' => 'application/vnd.ms-excel',
  2326. 'xlc' => 'application/vnd.ms-excel',
  2327. 'xlw' => 'application/vnd.ms-excel',
  2328. 'xll' => 'application/vnd.ms-excel',
  2329. 'ppt' => 'application/vnd.ms-powerpoint',
  2330. 'pps' => 'application/vnd.ms-powerpoint',
  2331. 'rtf' => 'application/rtf',
  2332. 'pdf' => 'application/pdf',
  2333. 'html' => 'text/html',
  2334. 'htm' => 'text/html',
  2335. 'php' => 'text/html',
  2336. 'txt' => 'text/plain',
  2337. 'mpeg' => 'video/mpeg',
  2338. 'mpg' => 'video/mpeg',
  2339. 'mpe' => 'video/mpeg',
  2340. 'mp3' => 'audio/mpeg3',
  2341. 'wav' => 'audio/wav',
  2342. 'aiff' => 'audio/aiff',
  2343. 'aif' => 'audio/aiff',
  2344. 'avi' => 'video/msvideo',
  2345. 'wmv' => 'video/x-ms-wmv',
  2346. 'mov' => 'video/quicktime',
  2347. 'zip' => 'application/zip',
  2348. 'tar' => 'application/x-tar',
  2349. 'swf' => 'application/x-shockwave-flash',
  2350. 'odt' => 'application/vnd.oasis.opendocument.text',
  2351. 'ott' => 'application/vnd.oasis.opendocument.text-template',
  2352. 'oth' => 'application/vnd.oasis.opendocument.text-web',
  2353. 'odm' => 'application/vnd.oasis.opendocument.text-master',
  2354. 'odg' => 'application/vnd.oasis.opendocument.graphics',
  2355. 'otg' => 'application/vnd.oasis.opendocument.graphics-template',
  2356. 'odp' => 'application/vnd.oasis.opendocument.presentation',
  2357. 'otp' => 'application/vnd.oasis.opendocument.presentation-template',
  2358. 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
  2359. 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
  2360. 'odc' => 'application/vnd.oasis.opendocument.chart',
  2361. 'odf' => 'application/vnd.oasis.opendocument.formula',
  2362. 'odb' => 'application/vnd.oasis.opendocument.database',
  2363. 'odi' => 'application/vnd.oasis.opendocument.image',
  2364. 'oxt' => 'application/vnd.openofficeorg.extension',
  2365. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  2366. 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
  2367. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  2368. 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
  2369. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  2370. 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  2371. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  2372. 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
  2373. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  2374. 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  2375. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  2376. 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  2377. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  2378. 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  2379. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  2380. 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  2381. 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  2382. 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
  2383. 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
  2384. 'thmx' => 'application/vnd.ms-officetheme',
  2385. 'onetoc' => 'application/onenote',
  2386. 'onetoc2' => 'application/onenote',
  2387. 'onetmp' => 'application/onenote',
  2388. 'onepkg' => 'application/onenote',
  2389. );
  2390. }
  2391. /**
  2392. * Constructor. Checks if the file has been uploaded
  2393. *
  2394. * The constructor takes $_FILES['form_field'] array as argument
  2395. * where form_field is the form field name
  2396. *
  2397. * The constructor will check if the file has been uploaded in its temporary location, and
  2398. * accordingly will set {@link uploaded} (and {@link error} is an error occurred)
  2399. *
  2400. * If the file has been uploaded, the constructor will populate all the variables holding the upload
  2401. * information (none of the processing class variables are used here).
  2402. * You can have access to information about the file (name, size, MIME type...).
  2403. *
  2404. *
  2405. * Alternatively, you can set the first argument to be a local filename (string)
  2406. * This allows processing of a local file, as if the file was uploaded
  2407. *
  2408. * The optional second argument allows you to set the language for the error messages
  2409. *
  2410. * @access private
  2411. * @param array $file $_FILES['form_field']
  2412. * or string $file Local filename
  2413. * @param string $lang Optional language code
  2414. */
  2415. function upload($file, $lang = 'en_GB') {
  2416. $this->version = '0.32';
  2417. $this->file_src_name = '';
  2418. $this->file_src_name_body = '';
  2419. $this->file_src_name_ext = '';
  2420. $this->file_src_mime = '';
  2421. $this->file_src_size = '';
  2422. $this->file_src_error = '';
  2423. $this->file_src_pathname = '';
  2424. $this->file_src_temp = '';
  2425. $this->file_dst_path = '';
  2426. $this->file_dst_name = '';
  2427. $this->file_dst_name_body = '';
  2428. $this->file_dst_name_ext = '';
  2429. $this->file_dst_pathname = '';
  2430. $this->image_src_x = null;
  2431. $this->image_src_y = null;
  2432. $this->image_src_bits = null;
  2433. $this->image_src_type = null;
  2434. $this->image_src_pixels = null;
  2435. $this->image_dst_x = 0;
  2436. $this->image_dst_y = 0;
  2437. $this->uploaded = true;
  2438. $this->no_upload_check = false;
  2439. $this->processed = true;
  2440. $this->error = '';
  2441. $this->log = '';
  2442. $this->allowed = array();
  2443. $this->forbidden = array();
  2444. $this->file_is_image = false;
  2445. $this->init();
  2446. $info = null;
  2447. $mime_from_browser = null;
  2448. // sets default language
  2449. $this->translation = array();
  2450. $this->translation['file_error'] = 'File error. Please try again.';
  2451. $this->translation['local_file_missing'] = 'Local file doesn\'t exist.';
  2452. $this->translation['local_file_not_readable'] = 'Local file is not readable.';
  2453. $this->translation['uploaded_too_big_ini'] = 'File upload error (the uploaded file exceeds the upload_max_filesize directive in php.ini).';
  2454. $this->translation['uploaded_too_big_html'] = 'File upload error (the uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form).';
  2455. $this->translation['uploaded_partial'] = 'File upload error (the uploaded file was only partially uploaded).';
  2456. $this->translation['uploaded_missing'] = 'File upload error (no file was uploaded).';
  2457. $this->translation['uploaded_no_tmp_dir'] = 'File upload error (missing a temporary folder).';
  2458. $this->translation['uploaded_cant_write'] = 'File upload error (failed to write file to disk).';
  2459. $this->translation['uploaded_err_extension'] = 'File upload error (file upload stopped by extension).';
  2460. $this->translation['uploaded_unknown'] = 'File upload error (unknown error code).';
  2461. $this->translation['try_again'] = 'File upload error. Please try again.';
  2462. $this->translation['file_too_big'] = 'File too big.';
  2463. $this->translation['no_mime'] = 'MIME type can\'t be detected.';
  2464. $this->translation['incorrect_file'] = 'Incorrect type of file.';
  2465. $this->translation['image_too_wide'] = 'Image too wide.';
  2466. $this->translation['image_too_narrow'] = 'Image too narrow.';
  2467. $this->translation['image_too_high'] = 'Image too tall.';
  2468. $this->translation['image_too_short'] = 'Image too short.';
  2469. $this->translation['ratio_too_high'] = 'Image ratio too high (image too wide).';
  2470. $this->translation['ratio_too_low'] = 'Image ratio too low (image too high).';
  2471. $this->translation['too_many_pixels'] = 'Image has too many pixels.';
  2472. $this->translation['not_enough_pixels'] = 'Image has not enough pixels.';
  2473. $this->translation['file_not_uploaded'] = 'File not uploaded. Can\'t carry on a process.';
  2474. $this->translation['already_exists'] = '%s already exists. Please change the file name.';
  2475. $this->translation['temp_file_missing'] = 'No correct temp source file. Can\'t carry on a process.';
  2476. $this->translation['source_missing'] = 'No correct uploaded source file. Can\'t carry on a process.';
  2477. $this->translation['destination_dir'] = 'Destination directory can\'t be created. Can\'t carry on a process.';
  2478. $this->translation['destination_dir_missing'] = 'Destination directory doesn\'t exist. Can\'t carry on a process.';
  2479. $this->translation['destination_path_not_dir'] = 'Destination path is not a directory. Can\'t carry on a process.';
  2480. $this->translation['destination_dir_write'] = 'Destination directory can\'t be made writeable. Can\'t carry on a process.';
  2481. $this->translation['destination_path_write'] = 'Destination path is not a writeable. Can\'t carry on a process.';
  2482. $this->translation['temp_file'] = 'Can\'t create the temporary file. Can\'t carry on a process.';
  2483. $this->translation['source_not_readable'] = 'Source file is not readable. Can\'t carry on a process.';
  2484. $this->translation['no_create_support'] = 'No create from %s support.';
  2485. $this->translation['create_error'] = 'Error in creating %s image from source.';
  2486. $this->translation['source_invalid'] = 'Can\'t read image source. Not an image?.';
  2487. $this->translation['gd_missing'] = 'GD doesn\'t seem to be present.';
  2488. $this->translation['watermark_no_create_support'] = 'No create from %s support, can\'t read watermark.';
  2489. $this->translation['watermark_create_error'] = 'No %s read support, can\'t create watermark.';
  2490. $this->translation['watermark_invalid'] = 'Unknown image format, can\'t read watermark.';
  2491. $this->translation['file_create'] = 'No %s create support.';
  2492. $this->translation['no_conversion_type'] = 'No conversion type defined.';
  2493. $this->translation['copy_failed'] = 'Error copying file on the server. copy() failed.';
  2494. $this->translation['reading_failed'] = 'Error reading the file.';
  2495. // determines the language
  2496. $this->lang = $lang;
  2497. if ($this->lang != 'en_GB' && file_exists(dirname(__FILE__).'/lang') && file_exists(dirname(__FILE__).'/lang/class.upload.' . $lang . '.php')) {
  2498. $translation = null;
  2499. include(dirname(__FILE__).'/lang/class.upload.' . $lang . '.php');
  2500. if (is_array($translation)) {
  2501. $this->translation = array_merge($this->translation, $translation);
  2502. } else {
  2503. $this->lang = 'en_GB';
  2504. }
  2505. }
  2506. // determines the supported MIME types, and matching image format
  2507. $this->image_supported = array();
  2508. if ($this->gdversion()) {
  2509. if (imagetypes() & IMG_GIF) {
  2510. $this->image_supported['image/gif'] = 'gif';
  2511. }
  2512. if (imagetypes() & IMG_JPG) {
  2513. $this->image_supported['image/jpg'] = 'jpg';
  2514. $this->image_supported['image/jpeg'] = 'jpg';
  2515. $this->image_supported['image/pjpeg'] = 'jpg';
  2516. }
  2517. if (imagetypes() & IMG_PNG) {
  2518. $this->image_supported['image/png'] = 'png';
  2519. $this->image_supported['image/x-png'] = 'png';
  2520. }
  2521. if (imagetypes() & IMG_WBMP) {
  2522. $this->image_supported['image/bmp'] = 'bmp';
  2523. $this->image_supported['image/x-ms-bmp'] = 'bmp';
  2524. $this->image_supported['image/x-windows-bmp'] = 'bmp';
  2525. }
  2526. }
  2527. // display some system information
  2528. if (empty($this->log)) {
  2529. $this->log .= '<b>system information</b><br />';
  2530. if (function_exists('ini_get_all')) {
  2531. $inis = ini_get_all();
  2532. $open_basedir = (array_key_exists('open_basedir', $inis) && array_key_exists('local_value', $inis['open_basedir']) && !empty($inis['open_basedir']['local_value'])) ? $inis['open_basedir']['local_value'] : false;
  2533. } else {
  2534. $open_basedir = false;
  2535. }
  2536. $gd = $this->gdversion() ? $this->gdversion(true) : 'GD not present';
  2537. $supported = trim((in_array('png', $this->image_supported) ? 'png' : '') . ' ' . (in_array('jpg', $this->image_supported) ? 'jpg' : '') . ' ' . (in_array('gif', $this->image_supported) ? 'gif' : '') . ' ' . (in_array('bmp', $this->image_supported) ? 'bmp' : ''));
  2538. $this->log .= '-&nbsp;class version : ' . $this->version . '<br />';
  2539. $this->log .= '-&nbsp;operating system : ' . PHP_OS . '<br />';
  2540. $this->log .= '-&nbsp;PHP version : ' . PHP_VERSION . '<br />';
  2541. $this->log .= '-&nbsp;GD version : ' . $gd . '<br />';
  2542. $this->log .= '-&nbsp;supported image types : ' . (!empty($supported) ? $supported : 'none') . '<br />';
  2543. $this->log .= '-&nbsp;open_basedir : ' . (!empty($open_basedir) ? $open_basedir : 'no restriction') . '<br />';
  2544. $this->log .= '-&nbsp;upload_max_filesize : ' . $this->file_max_size_raw . ' (' . $this->file_max_size . ' bytes)<br />';
  2545. $this->log .= '-&nbsp;language : ' . $this->lang . '<br />';
  2546. }
  2547. if (!$file) {
  2548. $this->uploaded = false;
  2549. $this->error = $this->translate('file_error');
  2550. }
  2551. // check if we sent a local filename or a PHP stream rather than a $_FILE element
  2552. if (!is_array($file)) {
  2553. if (empty($file)) {
  2554. $this->uploaded = false;
  2555. $this->error = $this->translate('file_error');
  2556. } else {
  2557. if (substr($file, 0, 4) == 'php:') {
  2558. // this is a local filename, i.e.not uploaded
  2559. $file = preg_replace('/^php:(.*)/i', '$1', $file);
  2560. if (!$file) $file = $_SERVER['HTTP_X_FILE_NAME'];
  2561. if (!$file) $file = 'unknown';
  2562. $this->log .= '<b>' . $this->translate("source is a PHP stream") . ' ' . $file . '</b><br />';
  2563. $this->no_upload_check = TRUE;
  2564. $this->log .= '- this is a PHP stream, requires a temp file ... ';
  2565. $hash = $this->temp_dir() . md5($file . rand(1, 1000));
  2566. if (file_put_contents($hash, file_get_contents('php://input'))) {
  2567. $this->file_src_pathname = $hash;
  2568. $this->log .= ' file created<br />';
  2569. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;temp file is: ' . $this->file_src_pathname . '<br />';
  2570. } else {
  2571. $this->log .= ' failed<br />';
  2572. $this->uploaded = false;
  2573. $this->error = $this->translate('temp_file');
  2574. }
  2575. if ($this->uploaded) {
  2576. $this->file_src_name = $file;
  2577. $this->log .= '- local file OK<br />';
  2578. preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);
  2579. if (is_array($extension) && sizeof($extension) > 0) {
  2580. $this->file_src_name_ext = strtolower($extension[1]);
  2581. $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1);
  2582. } else {
  2583. $this->file_src_name_ext = '';
  2584. $this->file_src_name_body = $this->file_src_name;
  2585. }
  2586. $this->file_src_size = (file_exists($file) ? filesize($file) : 0);
  2587. }
  2588. $this->file_src_error = 0;
  2589. } else {
  2590. // this is a local filename, i.e.not uploaded
  2591. $this->log .= '<b>' . $this->translate("source is a local file") . ' ' . $file . '</b><br />';
  2592. $this->no_upload_check = TRUE;
  2593. if ($this->uploaded && !file_exists($file)) {
  2594. $this->uploaded = false;
  2595. $this->error = $this->translate('local_file_missing');
  2596. }
  2597. if ($this->uploaded && !is_readable($file)) {
  2598. $this->uploaded = false;
  2599. $this->error = $this->translate('local_file_not_readable');
  2600. }
  2601. if ($this->uploaded) {
  2602. $this->file_src_pathname = $file;
  2603. $this->file_src_name = basename($file);
  2604. $this->log .= '- local file OK<br />';
  2605. preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);
  2606. if (is_array($extension) && sizeof($extension) > 0) {
  2607. $this->file_src_name_ext = strtolower($extension[1]);
  2608. $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1);
  2609. } else {
  2610. $this->file_src_name_ext = '';
  2611. $this->file_src_name_body = $this->file_src_name;
  2612. }
  2613. $this->file_src_size = (file_exists($file) ? filesize($file) : 0);
  2614. }
  2615. $this->file_src_error = 0;
  2616. }
  2617. }
  2618. } else {
  2619. // this is an element from $_FILE, i.e. an uploaded file
  2620. $this->log .= '<b>source is an uploaded file</b><br />';
  2621. if ($this->uploaded) {
  2622. $this->file_src_error = trim($file['error']);
  2623. switch($this->file_src_error) {
  2624. case UPLOAD_ERR_OK:
  2625. // all is OK
  2626. $this->log .= '- upload OK<br />';
  2627. break;
  2628. case UPLOAD_ERR_INI_SIZE:
  2629. $this->uploaded = false;
  2630. $this->error = $this->translate('uploaded_too_big_ini');
  2631. break;
  2632. case UPLOAD_ERR_FORM_SIZE:
  2633. $this->uploaded = false;
  2634. $this->error = $this->translate('uploaded_too_big_html');
  2635. break;
  2636. case UPLOAD_ERR_PARTIAL:
  2637. $this->uploaded = false;
  2638. $this->error = $this->translate('uploaded_partial');
  2639. break;
  2640. case UPLOAD_ERR_NO_FILE:
  2641. $this->uploaded = false;
  2642. $this->error = $this->translate('uploaded_missing');
  2643. break;
  2644. case @UPLOAD_ERR_NO_TMP_DIR:
  2645. $this->uploaded = false;
  2646. $this->error = $this->translate('uploaded_no_tmp_dir');
  2647. break;
  2648. case @UPLOAD_ERR_CANT_WRITE:
  2649. $this->uploaded = false;
  2650. $this->error = $this->translate('uploaded_cant_write');
  2651. break;
  2652. case @UPLOAD_ERR_EXTENSION:
  2653. $this->uploaded = false;
  2654. $this->error = $this->translate('uploaded_err_extension');
  2655. break;
  2656. default:
  2657. $this->uploaded = false;
  2658. $this->error = $this->translate('uploaded_unknown') . ' ('.$this->file_src_error.')';
  2659. }
  2660. }
  2661. if ($this->uploaded) {
  2662. $this->file_src_pathname = $file['tmp_name'];
  2663. $this->file_src_name = $file['name'];
  2664. if ($this->file_src_name == '') {
  2665. $this->uploaded = false;
  2666. $this->error = $this->translate('try_again');
  2667. }
  2668. }
  2669. if ($this->uploaded) {
  2670. $this->log .= '- file name OK<br />';
  2671. preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);
  2672. if (is_array($extension) && sizeof($extension) > 0) {
  2673. $this->file_src_name_ext = strtolower($extension[1]);
  2674. $this->file_src_name_body = substr($this->file_src_name, 0, ((strlen($this->file_src_name) - strlen($this->file_src_name_ext)))-1);
  2675. } else {
  2676. $this->file_src_name_ext = '';
  2677. $this->file_src_name_body = $this->file_src_name;
  2678. }
  2679. $this->file_src_size = $file['size'];
  2680. $mime_from_browser = $file['type'];
  2681. }
  2682. }
  2683. if ($this->uploaded) {
  2684. $this->log .= '<b>determining MIME type</b><br />';
  2685. $this->file_src_mime = null;
  2686. // checks MIME type with Fileinfo PECL extension
  2687. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2688. if ($this->mime_fileinfo) {
  2689. $this->log .= '- Checking MIME type with Fileinfo PECL extension<br />';
  2690. if (function_exists('finfo_open')) {
  2691. $path = null;
  2692. if ($this->mime_fileinfo !== '') {
  2693. if ($this->mime_fileinfo === true) {
  2694. if (getenv('MAGIC') === FALSE) {
  2695. if (substr(PHP_OS, 0, 3) == 'WIN') {
  2696. $path = realpath(ini_get('extension_dir') . '/../') . 'extras/magic';
  2697. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path defaults to ' . $path . '<br />';
  2698. }
  2699. } else {
  2700. $path = getenv('MAGIC');
  2701. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path is set to ' . $path . ' from MAGIC variable<br />';
  2702. }
  2703. } else {
  2704. $path = $this->mime_fileinfo;
  2705. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path is set to ' . $path . '<br />';
  2706. }
  2707. }
  2708. if ($path) {
  2709. $f = @finfo_open(FILEINFO_MIME, $path);
  2710. } else {
  2711. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MAGIC path will not be used<br />';
  2712. $f = @finfo_open(FILEINFO_MIME);
  2713. }
  2714. if (is_resource($f)) {
  2715. $mime = finfo_file($f, realpath($this->file_src_pathname));
  2716. finfo_close($f);
  2717. $this->file_src_mime = $mime;
  2718. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by Fileinfo PECL extension<br />';
  2719. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2720. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2721. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2722. } else {
  2723. $this->file_src_mime = null;
  2724. }
  2725. } else {
  2726. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension failed (finfo_open)<br />';
  2727. }
  2728. } elseif (@class_exists('finfo')) {
  2729. $f = new finfo( FILEINFO_MIME );
  2730. if ($f) {
  2731. $this->file_src_mime = $f->file(realpath($this->file_src_pathname));
  2732. $this->log .= '- MIME type detected as ' . $this->file_src_mime . ' by Fileinfo PECL extension<br />';
  2733. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2734. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2735. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2736. } else {
  2737. $this->file_src_mime = null;
  2738. }
  2739. } else {
  2740. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension failed (finfo)<br />';
  2741. }
  2742. } else {
  2743. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;Fileinfo PECL extension not available<br />';
  2744. }
  2745. } else {
  2746. $this->log .= '- Fileinfo PECL extension deactivated<br />';
  2747. }
  2748. }
  2749. // checks MIME type with shell if unix access is authorized
  2750. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2751. if ($this->mime_file) {
  2752. $this->log .= '- Checking MIME type with UNIX file() command<br />';
  2753. if (substr(PHP_OS, 0, 3) != 'WIN') {
  2754. if (function_exists('exec') && function_exists('escapeshellarg') && !extension_loaded('suhosin')) {
  2755. if (strlen($mime = @exec("file -bi ".escapeshellarg($this->file_src_pathname))) != 0) {
  2756. $this->file_src_mime = trim($mime);
  2757. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by UNIX file() command<br />';
  2758. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2759. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2760. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2761. } else {
  2762. $this->file_src_mime = null;
  2763. }
  2764. } else {
  2765. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;UNIX file() command failed<br />';
  2766. }
  2767. } else {
  2768. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;PHP exec() function is disabled<br />';
  2769. }
  2770. } else {
  2771. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;UNIX file() command not availabled<br />';
  2772. }
  2773. } else {
  2774. $this->log .= '- UNIX file() command is deactivated<br />';
  2775. }
  2776. }
  2777. // checks MIME type with mime_magic
  2778. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2779. if ($this->mime_magic) {
  2780. $this->log .= '- Checking MIME type with mime.magic file (mime_content_type())<br />';
  2781. if (function_exists('mime_content_type')) {
  2782. $this->file_src_mime = mime_content_type($this->file_src_pathname);
  2783. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by mime_content_type()<br />';
  2784. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2785. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2786. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2787. } else {
  2788. $this->file_src_mime = null;
  2789. }
  2790. } else {
  2791. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;mime_content_type() is not available<br />';
  2792. }
  2793. } else {
  2794. $this->log .= '- mime.magic file (mime_content_type()) is deactivated<br />';
  2795. }
  2796. }
  2797. // checks MIME type with getimagesize()
  2798. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2799. if ($this->mime_getimagesize) {
  2800. $this->log .= '- Checking MIME type with getimagesize()<br />';
  2801. $info = getimagesize($this->file_src_pathname);
  2802. if (is_array($info) && array_key_exists('mime', $info)) {
  2803. $this->file_src_mime = trim($info['mime']);
  2804. if (empty($this->file_src_mime)) {
  2805. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME empty, guessing from type<br />';
  2806. $mime = (is_array($info) && array_key_exists(2, $info) ? $info[2] : null); // 1 = GIF, 2 = JPG, 3 = PNG
  2807. $this->file_src_mime = ($mime==IMAGETYPE_GIF ? 'image/gif' : ($mime==IMAGETYPE_JPEG ? 'image/jpeg' : ($mime==IMAGETYPE_PNG ? 'image/png' : ($mime==IMAGETYPE_BMP ? 'image/bmp' : null))));
  2808. }
  2809. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;MIME type detected as ' . $this->file_src_mime . ' by PHP getimagesize() function<br />';
  2810. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2811. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2812. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2813. } else {
  2814. $this->file_src_mime = null;
  2815. }
  2816. } else {
  2817. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;getimagesize() failed<br />';
  2818. }
  2819. } else {
  2820. $this->log .= '- getimagesize() is deactivated<br />';
  2821. }
  2822. }
  2823. // default to MIME from browser (or Flash)
  2824. if (!empty($mime_from_browser) && !$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime)) {
  2825. $this->file_src_mime =$mime_from_browser;
  2826. $this->log .= '- MIME type detected as ' . $this->file_src_mime . ' by browser<br />';
  2827. if (preg_match("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", $this->file_src_mime)) {
  2828. $this->file_src_mime = preg_replace("/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $this->file_src_mime);
  2829. $this->log .= '-&nbsp;MIME validated as ' . $this->file_src_mime . '<br />';
  2830. } else {
  2831. $this->file_src_mime = null;
  2832. }
  2833. }
  2834. // we need to work some magic if we upload via Flash
  2835. if ($this->file_src_mime == 'application/octet-stream' || !$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2836. if ($this->file_src_mime == 'application/octet-stream') $this->log .= '- Flash may be rewriting MIME as application/octet-stream<br />';
  2837. $this->log .= '- Try to guess MIME type from file extension (' . $this->file_src_name_ext . '): ';
  2838. if (array_key_exists($this->file_src_name_ext, $this->mime_types)) $this->file_src_mime = $this->mime_types[$this->file_src_name_ext];
  2839. if ($this->file_src_mime == 'application/octet-stream') {
  2840. $this->log .= 'doesn\'t look like anything known<br />';
  2841. } else {
  2842. $this->log .= 'MIME type set to ' . $this->file_src_mime . '<br />';
  2843. }
  2844. }
  2845. if (!$this->file_src_mime || !is_string($this->file_src_mime) || empty($this->file_src_mime) || strpos($this->file_src_mime, '/') === FALSE) {
  2846. $this->log .= '- MIME type couldn\'t be detected! (' . (string) $this->file_src_mime . ')<br />';
  2847. }
  2848. // determine whether the file is an image
  2849. if ($this->file_src_mime && is_string($this->file_src_mime) && !empty($this->file_src_mime) && array_key_exists($this->file_src_mime, $this->image_supported)) {
  2850. $this->file_is_image = true;
  2851. $this->image_src_type = $this->image_supported[$this->file_src_mime];
  2852. }
  2853. // if the file is an image, we gather some useful data
  2854. if ($this->file_is_image) {
  2855. if ($h = fopen($this->file_src_pathname, 'r')) {
  2856. fclose($h);
  2857. $info = getimagesize($this->file_src_pathname);
  2858. if (is_array($info)) {
  2859. $this->image_src_x = $info[0];
  2860. $this->image_src_y = $info[1];
  2861. $this->image_dst_x = $this->image_src_x;
  2862. $this->image_dst_y = $this->image_src_y;
  2863. $this->image_src_pixels = $this->image_src_x * $this->image_src_y;
  2864. $this->image_src_bits = array_key_exists('bits', $info) ? $info['bits'] : null;
  2865. } else {
  2866. $this->file_is_image = false;
  2867. $this->uploaded = false;
  2868. $this->log .= '- can\'t retrieve image information, image may have been tampered with<br />';
  2869. $this->error = $this->translate('source_invalid');
  2870. }
  2871. } else {
  2872. $this->log .= '- can\'t read source file directly. open_basedir restriction in place?<br />';
  2873. }
  2874. }
  2875. $this->log .= '<b>source variables</b><br />';
  2876. $this->log .= '- You can use all these before calling process()<br />';
  2877. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name : ' . $this->file_src_name . '<br />';
  2878. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name_body : ' . $this->file_src_name_body . '<br />';
  2879. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_name_ext : ' . $this->file_src_name_ext . '<br />';
  2880. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_pathname : ' . $this->file_src_pathname . '<br />';
  2881. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_mime : ' . $this->file_src_mime . '<br />';
  2882. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_size : ' . $this->file_src_size . ' (max= ' . $this->file_max_size . ')<br />';
  2883. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_src_error : ' . $this->file_src_error . '<br />';
  2884. if ($this->file_is_image) {
  2885. $this->log .= '- source file is an image<br />';
  2886. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_x : ' . $this->image_src_x . '<br />';
  2887. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_y : ' . $this->image_src_y . '<br />';
  2888. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_pixels : ' . $this->image_src_pixels . '<br />';
  2889. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_type : ' . $this->image_src_type . '<br />';
  2890. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_bits : ' . $this->image_src_bits . '<br />';
  2891. }
  2892. }
  2893. }
  2894. /**
  2895. * Returns the version of GD
  2896. *
  2897. * @access public
  2898. * @param boolean $full Optional flag to get precise version
  2899. * @return float GD version
  2900. */
  2901. function gdversion($full = false) {
  2902. static $gd_version = null;
  2903. static $gd_full_version = null;
  2904. if ($gd_version === null) {
  2905. if (function_exists('gd_info')) {
  2906. $gd = gd_info();
  2907. $gd = $gd["GD Version"];
  2908. $regex = "/([\d\.]+)/i";
  2909. } else {
  2910. ob_start();
  2911. phpinfo(8);
  2912. $gd = ob_get_contents();
  2913. ob_end_clean();
  2914. $regex = "/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i";
  2915. }
  2916. if (preg_match($regex, $gd, $m)) {
  2917. $gd_full_version = (string) $m[1];
  2918. $gd_version = (float) $m[1];
  2919. } else {
  2920. $gd_full_version = 'none';
  2921. $gd_version = 0;
  2922. }
  2923. }
  2924. if ($full) {
  2925. return $gd_full_version;
  2926. } else {
  2927. return $gd_version;
  2928. }
  2929. }
  2930. /**
  2931. * Creates directories recursively
  2932. *
  2933. * @access private
  2934. * @param string $path Path to create
  2935. * @param integer $mode Optional permissions
  2936. * @return boolean Success
  2937. */
  2938. function rmkdir($path, $mode = 0777) {
  2939. return is_dir($path) || ( $this->rmkdir(dirname($path), $mode) && $this->_mkdir($path, $mode) );
  2940. }
  2941. /**
  2942. * Creates directory
  2943. *
  2944. * @access private
  2945. * @param string $path Path to create
  2946. * @param integer $mode Optional permissions
  2947. * @return boolean Success
  2948. */
  2949. function _mkdir($path, $mode = 0777) {
  2950. $old = umask(0);
  2951. $res = @mkdir($path, $mode);
  2952. umask($old);
  2953. return $res;
  2954. }
  2955. /**
  2956. * Translate error messages
  2957. *
  2958. * @access private
  2959. * @param string $str Message to translate
  2960. * @param array $tokens Optional token values
  2961. * @return string Translated string
  2962. */
  2963. function translate($str, $tokens = array()) {
  2964. if (array_key_exists($str, $this->translation)) $str = $this->translation[$str];
  2965. if (is_array($tokens) && sizeof($tokens) > 0) $str = vsprintf($str, $tokens);
  2966. return $str;
  2967. }
  2968. /**
  2969. * Returns the temp directory
  2970. *
  2971. * @access private
  2972. * @return string Temp directory string
  2973. */
  2974. function temp_dir() {
  2975. $dir = '';
  2976. if (function_exists('sys_get_temp_dir')) $dir = sys_get_temp_dir();
  2977. if (!$dir && $tmp=getenv('TMP')) $dir = $tmp;
  2978. if (!$dir && $tmp=getenv('TEMP')) $dir = $tmp;
  2979. if (!$dir && $tmp=getenv('TMPDIR')) $dir = $tmp;
  2980. if (!$dir) {
  2981. $tmp = tempnam(__FILE__,'');
  2982. if (file_exists($tmp)) {
  2983. unlink($tmp);
  2984. $dir = dirname($tmp);
  2985. }
  2986. }
  2987. if (!$dir) return '';
  2988. $slash = (strtolower(substr(PHP_OS, 0, 3)) === 'win' ? '\\' : '/');
  2989. if (substr($dir, -1) != $slash) $dir = $dir . $slash;
  2990. return $dir;
  2991. }
  2992. /**
  2993. * Decodes colors
  2994. *
  2995. * @access private
  2996. * @param string $color Color string
  2997. * @return array RGB colors
  2998. */
  2999. function getcolors($color) {
  3000. $color = str_replace('#', '', $color);
  3001. if (strlen($color) == 3) $color = str_repeat(substr($color, 0, 1), 2) . str_repeat(substr($color, 1, 1), 2) . str_repeat(substr($color, 2, 1), 2);
  3002. $r = sscanf($color, "%2x%2x%2x");
  3003. $red = (is_array($r) && array_key_exists(0, $r) && is_numeric($r[0]) ? $r[0] : 0);
  3004. $green = (is_array($r) && array_key_exists(1, $r) && is_numeric($r[1]) ? $r[1] : 0);
  3005. $blue = (is_array($r) && array_key_exists(2, $r) && is_numeric($r[2]) ? $r[2] : 0);
  3006. return array($red, $green, $blue);
  3007. }
  3008. /**
  3009. * Decodes sizes
  3010. *
  3011. * @access private
  3012. * @param string $size Size in bytes, or shorthand byte options
  3013. * @return integer Size in bytes
  3014. */
  3015. function getsize($size) {
  3016. $last = strtolower($size[strlen($size)-1]);
  3017. switch($last) {
  3018. case 'g':
  3019. $size *= 1024;
  3020. case 'm':
  3021. $size *= 1024;
  3022. case 'k':
  3023. $size *= 1024;
  3024. }
  3025. return $size;
  3026. }
  3027. /**
  3028. * Decodes offsets
  3029. *
  3030. * @access private
  3031. * @param misc $offsets Offsets, as an integer, a string or an array
  3032. * @param integer $x Reference picture width
  3033. * @param integer $y Reference picture height
  3034. * @param boolean $round Round offsets before returning them
  3035. * @param boolean $negative Allow negative offsets to be returned
  3036. * @return array Array of four offsets (TRBL)
  3037. */
  3038. function getoffsets($offsets, $x, $y, $round = true, $negative = true) {
  3039. if (!is_array($offsets)) $offsets = explode(' ', $offsets);
  3040. if (sizeof($offsets) == 4) {
  3041. $ct = $offsets[0]; $cr = $offsets[1]; $cb = $offsets[2]; $cl = $offsets[3];
  3042. } else if (sizeof($offsets) == 2) {
  3043. $ct = $offsets[0]; $cr = $offsets[1]; $cb = $offsets[0]; $cl = $offsets[1];
  3044. } else {
  3045. $ct = $offsets[0]; $cr = $offsets[0]; $cb = $offsets[0]; $cl = $offsets[0];
  3046. }
  3047. if (strpos($ct, '%')>0) $ct = $y * (str_replace('%','',$ct) / 100);
  3048. if (strpos($cr, '%')>0) $cr = $x * (str_replace('%','',$cr) / 100);
  3049. if (strpos($cb, '%')>0) $cb = $y * (str_replace('%','',$cb) / 100);
  3050. if (strpos($cl, '%')>0) $cl = $x * (str_replace('%','',$cl) / 100);
  3051. if (strpos($ct, 'px')>0) $ct = str_replace('px','',$ct);
  3052. if (strpos($cr, 'px')>0) $cr = str_replace('px','',$cr);
  3053. if (strpos($cb, 'px')>0) $cb = str_replace('px','',$cb);
  3054. if (strpos($cl, 'px')>0) $cl = str_replace('px','',$cl);
  3055. $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl;
  3056. if ($round) {
  3057. $ct = round($ct);
  3058. $cr = round($cr);
  3059. $cb = round($cb);
  3060. $cl = round($cl);
  3061. }
  3062. if (!$negative) {
  3063. if ($ct < 0) $ct = 0;
  3064. if ($cr < 0) $cr = 0;
  3065. if ($cb < 0) $cb = 0;
  3066. if ($cl < 0) $cl = 0;
  3067. }
  3068. return array($ct, $cr, $cb, $cl);
  3069. }
  3070. /**
  3071. * Creates a container image
  3072. *
  3073. * @access private
  3074. * @param integer $x Width
  3075. * @param integer $y Height
  3076. * @param boolean $fill Optional flag to draw the background color or not
  3077. * @param boolean $trsp Optional flag to set the background to be transparent
  3078. * @return resource Container image
  3079. */
  3080. function imagecreatenew($x, $y, $fill = true, $trsp = false) {
  3081. if ($x < 1) $x = 1; if ($y < 1) $y = 1;
  3082. if ($this->gdversion() >= 2 && !$this->image_is_palette) {
  3083. // create a true color image
  3084. $dst_im = imagecreatetruecolor($x, $y);
  3085. // this preserves transparency in PNGs, in true color
  3086. if (empty($this->image_background_color) || $trsp) {
  3087. imagealphablending($dst_im, false );
  3088. imagefilledrectangle($dst_im, 0, 0, $x, $y, imagecolorallocatealpha($dst_im, 0, 0, 0, 127));
  3089. }
  3090. } else {
  3091. // creates a palette image
  3092. $dst_im = imagecreate($x, $y);
  3093. // preserves transparency for palette images, if the original image has transparency
  3094. if (($fill && $this->image_is_transparent && empty($this->image_background_color)) || $trsp) {
  3095. imagefilledrectangle($dst_im, 0, 0, $x, $y, $this->image_transparent_color);
  3096. imagecolortransparent($dst_im, $this->image_transparent_color);
  3097. }
  3098. }
  3099. // fills with background color if any is set
  3100. if ($fill && !empty($this->image_background_color) && !$trsp) {
  3101. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  3102. $background_color = imagecolorallocate($dst_im, $red, $green, $blue);
  3103. imagefilledrectangle($dst_im, 0, 0, $x, $y, $background_color);
  3104. }
  3105. return $dst_im;
  3106. }
  3107. /**
  3108. * Transfers an image from the container to the destination image
  3109. *
  3110. * @access private
  3111. * @param resource $src_im Container image
  3112. * @param resource $dst_im Destination image
  3113. * @return resource Destination image
  3114. */
  3115. function imagetransfer($src_im, $dst_im) {
  3116. if (is_resource($dst_im)) imagedestroy($dst_im);
  3117. $dst_im = & $src_im;
  3118. return $dst_im;
  3119. }
  3120. /**
  3121. * Merges two images
  3122. *
  3123. * If the output format is PNG, then we do it pixel per pixel to retain the alpha channel
  3124. *
  3125. * @access private
  3126. * @param resource $dst_img Destination image
  3127. * @param resource $src_img Overlay image
  3128. * @param int $dst_x x-coordinate of destination point
  3129. * @param int $dst_y y-coordinate of destination point
  3130. * @param int $src_x x-coordinate of source point
  3131. * @param int $src_y y-coordinate of source point
  3132. * @param int $src_w Source width
  3133. * @param int $src_h Source height
  3134. * @param int $pct Optional percentage of the overlay, between 0 and 100 (default: 100)
  3135. * @return resource Destination image
  3136. */
  3137. function imagecopymergealpha(&$dst_im, &$src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct = 0) {
  3138. $dst_x = (int) $dst_x;
  3139. $dst_y = (int) $dst_y;
  3140. $src_x = (int) $src_x;
  3141. $src_y = (int) $src_y;
  3142. $src_w = (int) $src_w;
  3143. $src_h = (int) $src_h;
  3144. $pct = (int) $pct;
  3145. $dst_w = imagesx($dst_im);
  3146. $dst_h = imagesy($dst_im);
  3147. for ($y = $src_y; $y < $src_h; $y++) {
  3148. for ($x = $src_x; $x < $src_w; $x++) {
  3149. if ($x + $dst_x >= 0 && $x + $dst_x < $dst_w && $x + $src_x >= 0 && $x + $src_x < $src_w
  3150. && $y + $dst_y >= 0 && $y + $dst_y < $dst_h && $y + $src_y >= 0 && $y + $src_y < $src_h) {
  3151. $dst_pixel = imagecolorsforindex($dst_im, imagecolorat($dst_im, $x + $dst_x, $y + $dst_y));
  3152. $src_pixel = imagecolorsforindex($src_im, imagecolorat($src_im, $x + $src_x, $y + $src_y));
  3153. $src_alpha = 1 - ($src_pixel['alpha'] / 127);
  3154. $dst_alpha = 1 - ($dst_pixel['alpha'] / 127);
  3155. $opacity = $src_alpha * $pct / 100;
  3156. if ($dst_alpha >= $opacity) $alpha = $dst_alpha;
  3157. if ($dst_alpha < $opacity) $alpha = $opacity;
  3158. if ($alpha > 1) $alpha = 1;
  3159. if ($opacity > 0) {
  3160. $dst_red = round(( ($dst_pixel['red'] * $dst_alpha * (1 - $opacity)) ) );
  3161. $dst_green = round(( ($dst_pixel['green'] * $dst_alpha * (1 - $opacity)) ) );
  3162. $dst_blue = round(( ($dst_pixel['blue'] * $dst_alpha * (1 - $opacity)) ) );
  3163. $src_red = round((($src_pixel['red'] * $opacity)) );
  3164. $src_green = round((($src_pixel['green'] * $opacity)) );
  3165. $src_blue = round((($src_pixel['blue'] * $opacity)) );
  3166. $red = round(($dst_red + $src_red ) / ($dst_alpha * (1 - $opacity) + $opacity));
  3167. $green = round(($dst_green + $src_green) / ($dst_alpha * (1 - $opacity) + $opacity));
  3168. $blue = round(($dst_blue + $src_blue ) / ($dst_alpha * (1 - $opacity) + $opacity));
  3169. if ($red > 255) $red = 255;
  3170. if ($green > 255) $green = 255;
  3171. if ($blue > 255) $blue = 255;
  3172. $alpha = round((1 - $alpha) * 127);
  3173. $color = imagecolorallocatealpha($dst_im, $red, $green, $blue, $alpha);
  3174. imagesetpixel($dst_im, $x + $dst_x, $y + $dst_y, $color);
  3175. }
  3176. }
  3177. }
  3178. }
  3179. return true;
  3180. }
  3181. /**
  3182. * Actually uploads the file, and act on it according to the set processing class variables
  3183. *
  3184. * This function copies the uploaded file to the given location, eventually performing actions on it.
  3185. * Typically, you can call {@link process} several times for the same file,
  3186. * for instance to create a resized image and a thumbnail of the same file.
  3187. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times.
  3188. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls.
  3189. *
  3190. * According to the processing class variables set in the calling file, the file can be renamed,
  3191. * and if it is an image, can be resized or converted.
  3192. *
  3193. * When the processing is completed, and the file copied to its new location, the
  3194. * processing class variables will be reset to their default value.
  3195. * This allows you to set new properties, and perform another {@link process} on the same uploaded file
  3196. *
  3197. * If the function is called with a null or empty argument, then it will return the content of the picture
  3198. *
  3199. * It will set {@link processed} (and {@link error} is an error occurred)
  3200. *
  3201. * @access public
  3202. * @param string $server_path Optional path location of the uploaded file, with an ending slash
  3203. * @return string Optional content of the image
  3204. */
  3205. function process($server_path = null) {
  3206. $this->error = '';
  3207. $this->processed = true;
  3208. $return_mode = false;
  3209. $return_content = null;
  3210. // clean up dst variables
  3211. $this->file_dst_path = '';
  3212. $this->file_dst_pathname = '';
  3213. $this->file_dst_name = '';
  3214. $this->file_dst_name_body = '';
  3215. $this->file_dst_name_ext = '';
  3216. // clean up some parameters
  3217. $this->file_max_size = $this->getsize($this->file_max_size);
  3218. $this->jpeg_size = $this->getsize($this->jpeg_size);
  3219. // some parameters are being deprecated, and replaced with others
  3220. if (is_null($this->image_overlay_opacity)) $this->image_overlay_opacity = $this->image_overlay_percent;
  3221. if ($this->image_text_opacity == 100) $this->image_text_opacity = $this->image_text_percent;
  3222. if ($this->image_text_background_opacity == 100) $this->image_text_background_opacity = $this->image_text_background_percent;
  3223. // copy some variables as we need to keep them clean
  3224. $file_src_name = $this->file_src_name;
  3225. $file_src_name_body = $this->file_src_name_body;
  3226. $file_src_name_ext = $this->file_src_name_ext;
  3227. if (!$this->uploaded) {
  3228. $this->error = $this->translate('file_not_uploaded');
  3229. $this->processed = false;
  3230. }
  3231. if ($this->processed) {
  3232. if (empty($server_path) || is_null($server_path)) {
  3233. $this->log .= '<b>process file and return the content</b><br />';
  3234. $return_mode = true;
  3235. } else {
  3236. if(strtolower(substr(PHP_OS, 0, 3)) === 'win') {
  3237. if (substr($server_path, -1, 1) != '\\') $server_path = $server_path . '\\';
  3238. } else {
  3239. if (substr($server_path, -1, 1) != '/') $server_path = $server_path . '/';
  3240. }
  3241. $this->log .= '<b>process file to ' . $server_path . '</b><br />';
  3242. }
  3243. }
  3244. if ($this->processed) {
  3245. // checks file max size
  3246. if ($this->file_src_size > $this->file_max_size) {
  3247. $this->processed = false;
  3248. $this->error = $this->translate('file_too_big');
  3249. } else {
  3250. $this->log .= '- file size OK<br />';
  3251. }
  3252. }
  3253. if ($this->processed) {
  3254. // if we have an image without extension, set it
  3255. if ($this->file_force_extension && $this->file_is_image && !$this->file_src_name_ext) $file_src_name_ext = $this->image_src_type;
  3256. // turn dangerous scripts into text files
  3257. if ($this->no_script) {
  3258. // if the file has no extension, we try to guess it from the MIME type
  3259. if ($this->file_force_extension && empty($file_src_name_ext)) {
  3260. if ($key = array_search($this->file_src_mime, $this->mime_types)) {
  3261. $file_src_name_ext = $key;
  3262. $file_src_name = $file_src_name_body . '.' . $file_src_name_ext;
  3263. $this->log .= '- file renamed as ' . $file_src_name_body . '.' . $file_src_name_ext . '!<br />';
  3264. }
  3265. }
  3266. // if the file is text based, or has a dangerous extension, we rename it as .txt
  3267. if ((((substr($this->file_src_mime, 0, 5) == 'text/' && $this->file_src_mime != 'text/rtf') || strpos($this->file_src_mime, 'javascript') !== false) && (substr($file_src_name, -4) != '.txt'))
  3268. || preg_match('/\.(php|php5|php4|php3|phtml|pl|py|cgi|asp|js)$/i', $this->file_src_name)
  3269. || $this->file_force_extension && empty($file_src_name_ext)) {
  3270. $this->file_src_mime = 'text/plain';
  3271. if ($this->file_src_name_ext) $file_src_name_body = $file_src_name_body . '.' . $this->file_src_name_ext;
  3272. $file_src_name_ext = 'txt';
  3273. $file_src_name = $file_src_name_body . '.' . $file_src_name_ext;
  3274. $this->log .= '- script renamed as ' . $file_src_name_body . '.' . $file_src_name_ext . '!<br />';
  3275. }
  3276. }
  3277. if ($this->mime_check && empty($this->file_src_mime)) {
  3278. $this->processed = false;
  3279. $this->error = $this->translate('no_mime');
  3280. } else if ($this->mime_check && !empty($this->file_src_mime) && strpos($this->file_src_mime, '/') !== false) {
  3281. list($m1, $m2) = explode('/', $this->file_src_mime);
  3282. $allowed = false;
  3283. // check wether the mime type is allowed
  3284. if (!is_array($this->allowed)) $this->allowed = array($this->allowed);
  3285. foreach($this->allowed as $k => $v) {
  3286. list($v1, $v2) = explode('/', $v);
  3287. if (($v1 == '*' && $v2 == '*') || ($v1 == $m1 && ($v2 == $m2 || $v2 == '*'))) {
  3288. $allowed = true;
  3289. break;
  3290. }
  3291. }
  3292. // check wether the mime type is forbidden
  3293. if (!is_array($this->forbidden)) $this->forbidden = array($this->forbidden);
  3294. foreach($this->forbidden as $k => $v) {
  3295. list($v1, $v2) = explode('/', $v);
  3296. if (($v1 == '*' && $v2 == '*') || ($v1 == $m1 && ($v2 == $m2 || $v2 == '*'))) {
  3297. $allowed = false;
  3298. break;
  3299. }
  3300. }
  3301. if (!$allowed) {
  3302. $this->processed = false;
  3303. $this->error = $this->translate('incorrect_file');
  3304. } else {
  3305. $this->log .= '- file mime OK : ' . $this->file_src_mime . '<br />';
  3306. }
  3307. } else {
  3308. $this->log .= '- file mime (not checked) : ' . $this->file_src_mime . '<br />';
  3309. }
  3310. // if the file is an image, we can check on its dimensions
  3311. // these checks are not available if open_basedir restrictions are in place
  3312. if ($this->file_is_image) {
  3313. if (is_numeric($this->image_src_x) && is_numeric($this->image_src_y)) {
  3314. $ratio = $this->image_src_x / $this->image_src_y;
  3315. if (!is_null($this->image_max_width) && $this->image_src_x > $this->image_max_width) {
  3316. $this->processed = false;
  3317. $this->error = $this->translate('image_too_wide');
  3318. }
  3319. if (!is_null($this->image_min_width) && $this->image_src_x < $this->image_min_width) {
  3320. $this->processed = false;
  3321. $this->error = $this->translate('image_too_narrow');
  3322. }
  3323. if (!is_null($this->image_max_height) && $this->image_src_y > $this->image_max_height) {
  3324. $this->processed = false;
  3325. $this->error = $this->translate('image_too_high');
  3326. }
  3327. if (!is_null($this->image_min_height) && $this->image_src_y < $this->image_min_height) {
  3328. $this->processed = false;
  3329. $this->error = $this->translate('image_too_short');
  3330. }
  3331. if (!is_null($this->image_max_ratio) && $ratio > $this->image_max_ratio) {
  3332. $this->processed = false;
  3333. $this->error = $this->translate('ratio_too_high');
  3334. }
  3335. if (!is_null($this->image_min_ratio) && $ratio < $this->image_min_ratio) {
  3336. $this->processed = false;
  3337. $this->error = $this->translate('ratio_too_low');
  3338. }
  3339. if (!is_null($this->image_max_pixels) && $this->image_src_pixels > $this->image_max_pixels) {
  3340. $this->processed = false;
  3341. $this->error = $this->translate('too_many_pixels');
  3342. }
  3343. if (!is_null($this->image_min_pixels) && $this->image_src_pixels < $this->image_min_pixels) {
  3344. $this->processed = false;
  3345. $this->error = $this->translate('not_enough_pixels');
  3346. }
  3347. } else {
  3348. $this->log .= '- no image properties available, can\'t enforce dimension checks : ' . $this->file_src_mime . '<br />';
  3349. }
  3350. }
  3351. }
  3352. if ($this->processed) {
  3353. $this->file_dst_path = $server_path;
  3354. // repopulate dst variables from src
  3355. $this->file_dst_name = $file_src_name;
  3356. $this->file_dst_name_body = $file_src_name_body;
  3357. $this->file_dst_name_ext = $file_src_name_ext;
  3358. if ($this->file_overwrite) $this->file_auto_rename = false;
  3359. if ($this->image_convert && $this->file_is_image) { // if we convert as an image
  3360. if ($this->file_src_name_ext) $this->file_dst_name_ext = $this->image_convert;
  3361. $this->log .= '- new file name ext : ' . $this->image_convert . '<br />';
  3362. }
  3363. if (!is_null($this->file_new_name_body)) { // rename file body
  3364. $this->file_dst_name_body = $this->file_new_name_body;
  3365. $this->log .= '- new file name body : ' . $this->file_new_name_body . '<br />';
  3366. }
  3367. if (!is_null($this->file_new_name_ext)) { // rename file ext
  3368. $this->file_dst_name_ext = $this->file_new_name_ext;
  3369. $this->log .= '- new file name ext : ' . $this->file_new_name_ext . '<br />';
  3370. }
  3371. if (!is_null($this->file_name_body_add)) { // append a string to the name
  3372. $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add;
  3373. $this->log .= '- file name body append : ' . $this->file_name_body_add . '<br />';
  3374. }
  3375. if (!is_null($this->file_name_body_pre)) { // prepend a string to the name
  3376. $this->file_dst_name_body = $this->file_name_body_pre . $this->file_dst_name_body;
  3377. $this->log .= '- file name body prepend : ' . $this->file_name_body_pre . '<br />';
  3378. }
  3379. if ($this->file_safe_name) { // formats the name
  3380. $this->file_dst_name_body = utf8_encode(strtr(utf8_decode($this->file_dst_name_body), utf8_decode('ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ'), 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy'));
  3381. $this->file_dst_name_body = strtr($this->file_dst_name_body, array('Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u'));
  3382. $this->file_dst_name_body = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $this->file_dst_name_body);
  3383. $this->log .= '- file name safe format<br />';
  3384. }
  3385. $this->log .= '- destination variables<br />';
  3386. if (empty($this->file_dst_path) || is_null($this->file_dst_path)) {
  3387. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_path : n/a<br />';
  3388. } else {
  3389. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_path : ' . $this->file_dst_path . '<br />';
  3390. }
  3391. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name_body : ' . $this->file_dst_name_body . '<br />';
  3392. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name_ext : ' . $this->file_dst_name_ext . '<br />';
  3393. // do we do some image manipulation?
  3394. $image_manipulation = ($this->file_is_image && (
  3395. $this->image_resize
  3396. || $this->image_convert != ''
  3397. || is_numeric($this->image_brightness)
  3398. || is_numeric($this->image_contrast)
  3399. || is_numeric($this->image_opacity)
  3400. || is_numeric($this->image_threshold)
  3401. || !empty($this->image_tint_color)
  3402. || !empty($this->image_overlay_color)
  3403. || $this->image_pixelate
  3404. || $this->image_unsharp
  3405. || !empty($this->image_text)
  3406. || $this->image_greyscale
  3407. || $this->image_negative
  3408. || !empty($this->image_watermark)
  3409. || is_numeric($this->image_rotate)
  3410. || is_numeric($this->jpeg_size)
  3411. || !empty($this->image_flip)
  3412. || !empty($this->image_crop)
  3413. || !empty($this->image_precrop)
  3414. || !empty($this->image_border)
  3415. || !empty($this->image_border_transparent)
  3416. || $this->image_frame > 0
  3417. || $this->image_bevel > 0
  3418. || $this->image_reflection_height));
  3419. // set the destination file name
  3420. $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
  3421. if (!$return_mode) {
  3422. if (!$this->file_auto_rename) {
  3423. $this->log .= '- no auto_rename if same filename exists<br />';
  3424. $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
  3425. } else {
  3426. $this->log .= '- checking for auto_rename<br />';
  3427. $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
  3428. $body = $this->file_dst_name_body;
  3429. $ext = '';
  3430. // if we have changed the extension, then we add our increment before
  3431. if ($file_src_name_ext != $this->file_src_name_ext) {
  3432. if (substr($this->file_dst_name_body, -1 - strlen($this->file_src_name_ext)) == '.' . $this->file_src_name_ext) {
  3433. $body = substr($this->file_dst_name_body, 0, strlen($this->file_dst_name_body) - 1 - strlen($this->file_src_name_ext));
  3434. $ext = '.' . $this->file_src_name_ext;
  3435. }
  3436. }
  3437. $cpt = 1;
  3438. while (@file_exists($this->file_dst_pathname)) {
  3439. $this->file_dst_name_body = $body . '_' . $cpt . $ext;
  3440. $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
  3441. $cpt++;
  3442. $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name;
  3443. }
  3444. if ($cpt>1) $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;auto_rename to ' . $this->file_dst_name . '<br />';
  3445. }
  3446. $this->log .= '- destination file details<br />';
  3447. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_name : ' . $this->file_dst_name . '<br />';
  3448. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;file_dst_pathname : ' . $this->file_dst_pathname . '<br />';
  3449. if ($this->file_overwrite) {
  3450. $this->log .= '- no overwrite checking<br />';
  3451. } else {
  3452. if (@file_exists($this->file_dst_pathname)) {
  3453. $this->processed = false;
  3454. $this->error = $this->translate('already_exists', array($this->file_dst_name));
  3455. } else {
  3456. $this->log .= '- ' . $this->file_dst_name . ' doesn\'t exist already<br />';
  3457. }
  3458. }
  3459. }
  3460. }
  3461. if ($this->processed) {
  3462. // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists
  3463. if (!empty($this->file_src_temp)) {
  3464. $this->log .= '- use the temp file instead of the original file since it is a second process<br />';
  3465. $this->file_src_pathname = $this->file_src_temp;
  3466. if (!file_exists($this->file_src_pathname)) {
  3467. $this->processed = false;
  3468. $this->error = $this->translate('temp_file_missing');
  3469. }
  3470. // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file()
  3471. } else if (!$this->no_upload_check) {
  3472. if (!is_uploaded_file($this->file_src_pathname)) {
  3473. $this->processed = false;
  3474. $this->error = $this->translate('source_missing');
  3475. }
  3476. // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists()
  3477. } else {
  3478. if (!file_exists($this->file_src_pathname)) {
  3479. $this->processed = false;
  3480. $this->error = $this->translate('source_missing');
  3481. }
  3482. }
  3483. // checks if the destination directory exists, and attempt to create it
  3484. if (!$return_mode) {
  3485. if ($this->processed && !file_exists($this->file_dst_path)) {
  3486. if ($this->dir_auto_create) {
  3487. $this->log .= '- ' . $this->file_dst_path . ' doesn\'t exist. Attempting creation:';
  3488. if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) {
  3489. $this->log .= ' failed<br />';
  3490. $this->processed = false;
  3491. $this->error = $this->translate('destination_dir');
  3492. } else {
  3493. $this->log .= ' success<br />';
  3494. }
  3495. } else {
  3496. $this->error = $this->translate('destination_dir_missing');
  3497. }
  3498. }
  3499. if ($this->processed && !is_dir($this->file_dst_path)) {
  3500. $this->processed = false;
  3501. $this->error = $this->translate('destination_path_not_dir');
  3502. }
  3503. // checks if the destination directory is writeable, and attempt to make it writeable
  3504. $hash = md5($this->file_dst_name_body . rand(1, 1000));
  3505. if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''), 'a+'))) {
  3506. if ($this->dir_auto_chmod) {
  3507. $this->log .= '- ' . $this->file_dst_path . ' is not writeable. Attempting chmod:';
  3508. if (!@chmod($this->file_dst_path, $this->dir_chmod)) {
  3509. $this->log .= ' failed<br />';
  3510. $this->processed = false;
  3511. $this->error = $this->translate('destination_dir_write');
  3512. } else {
  3513. $this->log .= ' success<br />';
  3514. if (!($f = @fopen($this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''), 'a+'))) { // we re-check
  3515. $this->processed = false;
  3516. $this->error = $this->translate('destination_dir_write');
  3517. } else {
  3518. @fclose($f);
  3519. }
  3520. }
  3521. } else {
  3522. $this->processed = false;
  3523. $this->error = $this->translate('destination_path_write');
  3524. }
  3525. } else {
  3526. if ($this->processed) @fclose($f);
  3527. @unlink($this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''));
  3528. }
  3529. // if we have an uploaded file, and if it is the first process, and if we can't access the file directly (open_basedir restriction)
  3530. // then we create a temp file that will be used as the source file in subsequent processes
  3531. // the third condition is there to check if the file is not accessible *directly* (it already has positively gone through is_uploaded_file(), so it exists)
  3532. if (!$this->no_upload_check && empty($this->file_src_temp) && !@file_exists($this->file_src_pathname)) {
  3533. $this->log .= '- attempting to use a temp file:';
  3534. $hash = md5($this->file_dst_name_body . rand(1, 1000));
  3535. if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''))) {
  3536. $this->file_src_pathname = $this->file_dst_path . $hash . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : '');
  3537. $this->file_src_temp = $this->file_src_pathname;
  3538. $this->log .= ' file created<br />';
  3539. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;temp file is: ' . $this->file_src_temp . '<br />';
  3540. } else {
  3541. $this->log .= ' failed<br />';
  3542. $this->processed = false;
  3543. $this->error = $this->translate('temp_file');
  3544. }
  3545. }
  3546. }
  3547. }
  3548. if ($this->processed) {
  3549. // we do a quick check to ensure the file is really an image
  3550. // we can do this only now, as it would have failed before in case of open_basedir
  3551. if ($image_manipulation && !@getimagesize($this->file_src_pathname)) {
  3552. $this->log .= '- the file is not an image!<br />';
  3553. $image_manipulation = false;
  3554. }
  3555. if ($image_manipulation) {
  3556. // make sure GD doesn't complain too much
  3557. ini_set("gd.jpeg_ignore_warning", 1);
  3558. // checks if the source file is readable
  3559. if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) {
  3560. $this->processed = false;
  3561. $this->error = $this->translate('source_not_readable');
  3562. } else {
  3563. @fclose($f);
  3564. }
  3565. // we now do all the image manipulations
  3566. $this->log .= '- image resizing or conversion wanted<br />';
  3567. if ($this->gdversion()) {
  3568. switch($this->image_src_type) {
  3569. case 'jpg':
  3570. if (!function_exists('imagecreatefromjpeg')) {
  3571. $this->processed = false;
  3572. $this->error = $this->translate('no_create_support', array('JPEG'));
  3573. } else {
  3574. $image_src = @imagecreatefromjpeg($this->file_src_pathname);
  3575. if (!$image_src) {
  3576. $this->processed = false;
  3577. $this->error = $this->translate('create_error', array('JPEG'));
  3578. } else {
  3579. $this->log .= '- source image is JPEG<br />';
  3580. }
  3581. }
  3582. break;
  3583. case 'png':
  3584. if (!function_exists('imagecreatefrompng')) {
  3585. $this->processed = false;
  3586. $this->error = $this->translate('no_create_support', array('PNG'));
  3587. } else {
  3588. $image_src = @imagecreatefrompng($this->file_src_pathname);
  3589. if (!$image_src) {
  3590. $this->processed = false;
  3591. $this->error = $this->translate('create_error', array('PNG'));
  3592. } else {
  3593. $this->log .= '- source image is PNG<br />';
  3594. }
  3595. }
  3596. break;
  3597. case 'gif':
  3598. if (!function_exists('imagecreatefromgif')) {
  3599. $this->processed = false;
  3600. $this->error = $this->translate('no_create_support', array('GIF'));
  3601. } else {
  3602. $image_src = @imagecreatefromgif($this->file_src_pathname);
  3603. if (!$image_src) {
  3604. $this->processed = false;
  3605. $this->error = $this->translate('create_error', array('GIF'));
  3606. } else {
  3607. $this->log .= '- source image is GIF<br />';
  3608. }
  3609. }
  3610. break;
  3611. case 'bmp':
  3612. if (!method_exists($this, 'imagecreatefrombmp')) {
  3613. $this->processed = false;
  3614. $this->error = $this->translate('no_create_support', array('BMP'));
  3615. } else {
  3616. $image_src = @$this->imagecreatefrombmp($this->file_src_pathname);
  3617. if (!$image_src) {
  3618. $this->processed = false;
  3619. $this->error = $this->translate('create_error', array('BMP'));
  3620. } else {
  3621. $this->log .= '- source image is BMP<br />';
  3622. }
  3623. }
  3624. break;
  3625. default:
  3626. $this->processed = false;
  3627. $this->error = $this->translate('source_invalid');
  3628. }
  3629. } else {
  3630. $this->processed = false;
  3631. $this->error = $this->translate('gd_missing');
  3632. }
  3633. if ($this->processed && $image_src) {
  3634. // we have to set image_convert if it is not already
  3635. if (empty($this->image_convert)) {
  3636. $this->log .= '- setting destination file type to ' . $this->image_src_type . '<br />';
  3637. $this->image_convert = $this->image_src_type;
  3638. }
  3639. if (!in_array($this->image_convert, $this->image_supported)) {
  3640. $this->image_convert = 'jpg';
  3641. }
  3642. // we set the default color to be the background color if we don't output in a transparent format
  3643. if ($this->image_convert != 'png' && $this->image_convert != 'gif' && !empty($this->image_default_color) && empty($this->image_background_color)) $this->image_background_color = $this->image_default_color;
  3644. if (!empty($this->image_background_color)) $this->image_default_color = $this->image_background_color;
  3645. if (empty($this->image_default_color)) $this->image_default_color = '#FFFFFF';
  3646. $this->image_src_x = imagesx($image_src);
  3647. $this->image_src_y = imagesy($image_src);
  3648. $gd_version = $this->gdversion();
  3649. $ratio_crop = null;
  3650. if (!imageistruecolor($image_src)) { // $this->image_src_type == 'gif'
  3651. $this->log .= '- image is detected as having a palette<br />';
  3652. $this->image_is_palette = true;
  3653. $this->image_transparent_color = imagecolortransparent($image_src);
  3654. if ($this->image_transparent_color >= 0 && imagecolorstotal($image_src) > $this->image_transparent_color) {
  3655. $this->image_is_transparent = true;
  3656. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;palette image is detected as transparent<br />';
  3657. }
  3658. // if the image has a palette (GIF), we convert it to true color, preserving transparency
  3659. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;convert palette image to true color<br />';
  3660. $true_color = imagecreatetruecolor($this->image_src_x, $this->image_src_y);
  3661. imagealphablending($true_color, false);
  3662. imagesavealpha($true_color, true);
  3663. for ($x = 0; $x < $this->image_src_x; $x++) {
  3664. for ($y = 0; $y < $this->image_src_y; $y++) {
  3665. if ($this->image_transparent_color >= 0 && imagecolorat($image_src, $x, $y) == $this->image_transparent_color) {
  3666. imagesetpixel($true_color, $x, $y, 127 << 24);
  3667. } else {
  3668. $rgb = imagecolorsforindex($image_src, imagecolorat($image_src, $x, $y));
  3669. imagesetpixel($true_color, $x, $y, ($rgb['alpha'] << 24) | ($rgb['red'] << 16) | ($rgb['green'] << 8) | $rgb['blue']);
  3670. }
  3671. }
  3672. }
  3673. $image_src = $this->imagetransfer($true_color, $image_src);
  3674. imagealphablending($image_src, false);
  3675. imagesavealpha($image_src, true);
  3676. $this->image_is_palette = false;
  3677. }
  3678. $image_dst = & $image_src;
  3679. // pre-crop image, before resizing
  3680. if ((!empty($this->image_precrop))) {
  3681. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_precrop, $this->image_src_x, $this->image_src_y, true, true);
  3682. $this->log .= '- pre-crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />';
  3683. $this->image_src_x = $this->image_src_x - $cl - $cr;
  3684. $this->image_src_y = $this->image_src_y - $ct - $cb;
  3685. if ($this->image_src_x < 1) $this->image_src_x = 1;
  3686. if ($this->image_src_y < 1) $this->image_src_y = 1;
  3687. $tmp = $this->imagecreatenew($this->image_src_x, $this->image_src_y);
  3688. // we copy the image into the recieving image
  3689. imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_src_x, $this->image_src_y);
  3690. // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
  3691. if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0 ) {
  3692. // use the background color if present
  3693. if (!empty($this->image_background_color)) {
  3694. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  3695. $fill = imagecolorallocate($tmp, $red, $green, $blue);
  3696. } else {
  3697. $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
  3698. }
  3699. // fills eventual negative margins
  3700. if ($ct < 0) imagefilledrectangle($tmp, 0, 0, $this->image_src_x, -$ct, $fill);
  3701. if ($cr < 0) imagefilledrectangle($tmp, $this->image_src_x + $cr, 0, $this->image_src_x, $this->image_src_y, $fill);
  3702. if ($cb < 0) imagefilledrectangle($tmp, 0, $this->image_src_y + $cb, $this->image_src_x, $this->image_src_y, $fill);
  3703. if ($cl < 0) imagefilledrectangle($tmp, 0, 0, -$cl, $this->image_src_y, $fill);
  3704. }
  3705. // we transfert tmp into image_dst
  3706. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3707. }
  3708. // resize image (and move image_src_x, image_src_y dimensions into image_dst_x, image_dst_y)
  3709. if ($this->image_resize) {
  3710. $this->log .= '- resizing...<br />';
  3711. if ($this->image_ratio_x) {
  3712. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate x size<br />';
  3713. $this->image_dst_x = round(($this->image_src_x * $this->image_y) / $this->image_src_y);
  3714. $this->image_dst_y = $this->image_y;
  3715. } else if ($this->image_ratio_y) {
  3716. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate y size<br />';
  3717. $this->image_dst_x = $this->image_x;
  3718. $this->image_dst_y = round(($this->image_src_y * $this->image_x) / $this->image_src_x);
  3719. } else if (is_numeric($this->image_ratio_pixels)) {
  3720. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;calculate x/y size to match a number of pixels<br />';
  3721. $pixels = $this->image_src_y * $this->image_src_x;
  3722. $diff = sqrt($this->image_ratio_pixels / $pixels);
  3723. $this->image_dst_x = round($this->image_src_x * $diff);
  3724. $this->image_dst_y = round($this->image_src_y * $diff);
  3725. } else if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_fill || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out) {
  3726. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;check x/y sizes<br />';
  3727. if ((!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out)
  3728. || ($this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y))
  3729. || ($this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y)) {
  3730. $this->image_dst_x = $this->image_x;
  3731. $this->image_dst_y = $this->image_y;
  3732. if ($this->image_ratio_crop) {
  3733. if (!is_string($this->image_ratio_crop)) $this->image_ratio_crop = '';
  3734. $this->image_ratio_crop = strtolower($this->image_ratio_crop);
  3735. if (($this->image_src_x/$this->image_x) > ($this->image_src_y/$this->image_y)) {
  3736. $this->image_dst_y = $this->image_y;
  3737. $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y));
  3738. $ratio_crop = array();
  3739. $ratio_crop['x'] = $this->image_dst_x - $this->image_x;
  3740. if (strpos($this->image_ratio_crop, 'l') !== false) {
  3741. $ratio_crop['l'] = 0;
  3742. $ratio_crop['r'] = $ratio_crop['x'];
  3743. } else if (strpos($this->image_ratio_crop, 'r') !== false) {
  3744. $ratio_crop['l'] = $ratio_crop['x'];
  3745. $ratio_crop['r'] = 0;
  3746. } else {
  3747. $ratio_crop['l'] = round($ratio_crop['x']/2);
  3748. $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l'];
  3749. }
  3750. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_crop_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />';
  3751. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  3752. } else {
  3753. $this->image_dst_x = $this->image_x;
  3754. $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x));
  3755. $ratio_crop = array();
  3756. $ratio_crop['y'] = $this->image_dst_y - $this->image_y;
  3757. if (strpos($this->image_ratio_crop, 't') !== false) {
  3758. $ratio_crop['t'] = 0;
  3759. $ratio_crop['b'] = $ratio_crop['y'];
  3760. } else if (strpos($this->image_ratio_crop, 'b') !== false) {
  3761. $ratio_crop['t'] = $ratio_crop['y'];
  3762. $ratio_crop['b'] = 0;
  3763. } else {
  3764. $ratio_crop['t'] = round($ratio_crop['y']/2);
  3765. $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t'];
  3766. }
  3767. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_crop_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />';
  3768. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  3769. }
  3770. } else if ($this->image_ratio_fill) {
  3771. if (!is_string($this->image_ratio_fill)) $this->image_ratio_fill = '';
  3772. $this->image_ratio_fill = strtolower($this->image_ratio_fill);
  3773. if (($this->image_src_x/$this->image_x) < ($this->image_src_y/$this->image_y)) {
  3774. $this->image_dst_y = $this->image_y;
  3775. $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y));
  3776. $ratio_crop = array();
  3777. $ratio_crop['x'] = $this->image_dst_x - $this->image_x;
  3778. if (strpos($this->image_ratio_fill, 'l') !== false) {
  3779. $ratio_crop['l'] = 0;
  3780. $ratio_crop['r'] = $ratio_crop['x'];
  3781. } else if (strpos($this->image_ratio_fill, 'r') !== false) {
  3782. $ratio_crop['l'] = $ratio_crop['x'];
  3783. $ratio_crop['r'] = 0;
  3784. } else {
  3785. $ratio_crop['l'] = round($ratio_crop['x']/2);
  3786. $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l'];
  3787. }
  3788. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_fill_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />';
  3789. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  3790. } else {
  3791. $this->image_dst_x = $this->image_x;
  3792. $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x));
  3793. $ratio_crop = array();
  3794. $ratio_crop['y'] = $this->image_dst_y - $this->image_y;
  3795. if (strpos($this->image_ratio_fill, 't') !== false) {
  3796. $ratio_crop['t'] = 0;
  3797. $ratio_crop['b'] = $ratio_crop['y'];
  3798. } else if (strpos($this->image_ratio_fill, 'b') !== false) {
  3799. $ratio_crop['t'] = $ratio_crop['y'];
  3800. $ratio_crop['b'] = 0;
  3801. } else {
  3802. $ratio_crop['t'] = round($ratio_crop['y']/2);
  3803. $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t'];
  3804. }
  3805. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;ratio_fill_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />';
  3806. if (is_null($this->image_crop)) $this->image_crop = array(0, 0, 0, 0);
  3807. }
  3808. } else {
  3809. if (($this->image_src_x/$this->image_x) > ($this->image_src_y/$this->image_y)) {
  3810. $this->image_dst_x = $this->image_x;
  3811. $this->image_dst_y = intval($this->image_src_y*($this->image_x / $this->image_src_x));
  3812. } else {
  3813. $this->image_dst_y = $this->image_y;
  3814. $this->image_dst_x = intval($this->image_src_x*($this->image_y / $this->image_src_y));
  3815. }
  3816. }
  3817. } else {
  3818. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;doesn\'t calculate x/y sizes<br />';
  3819. $this->image_dst_x = $this->image_src_x;
  3820. $this->image_dst_y = $this->image_src_y;
  3821. }
  3822. } else {
  3823. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;use plain sizes<br />';
  3824. $this->image_dst_x = $this->image_x;
  3825. $this->image_dst_y = $this->image_y;
  3826. }
  3827. if ($this->image_dst_x < 1) $this->image_dst_x = 1;
  3828. if ($this->image_dst_y < 1) $this->image_dst_y = 1;
  3829. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3830. if ($gd_version >= 2) {
  3831. $res = imagecopyresampled($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y);
  3832. } else {
  3833. $res = imagecopyresized($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y);
  3834. }
  3835. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;resized image object created<br />';
  3836. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_src_x y : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />';
  3837. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image_dst_x y : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />';
  3838. // we transfert tmp into image_dst
  3839. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3840. } else {
  3841. $this->image_dst_x = $this->image_src_x;
  3842. $this->image_dst_y = $this->image_src_y;
  3843. }
  3844. // crop image (and also crops if image_ratio_crop is used)
  3845. if ((!empty($this->image_crop) || !is_null($ratio_crop))) {
  3846. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_crop, $this->image_dst_x, $this->image_dst_y, true, true);
  3847. // we adjust the cropping if we use image_ratio_crop
  3848. if (!is_null($ratio_crop)) {
  3849. if (array_key_exists('t', $ratio_crop)) $ct += $ratio_crop['t'];
  3850. if (array_key_exists('r', $ratio_crop)) $cr += $ratio_crop['r'];
  3851. if (array_key_exists('b', $ratio_crop)) $cb += $ratio_crop['b'];
  3852. if (array_key_exists('l', $ratio_crop)) $cl += $ratio_crop['l'];
  3853. }
  3854. $this->log .= '- crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />';
  3855. $this->image_dst_x = $this->image_dst_x - $cl - $cr;
  3856. $this->image_dst_y = $this->image_dst_y - $ct - $cb;
  3857. if ($this->image_dst_x < 1) $this->image_dst_x = 1;
  3858. if ($this->image_dst_y < 1) $this->image_dst_y = 1;
  3859. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3860. // we copy the image into the recieving image
  3861. imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y);
  3862. // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent
  3863. if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0 ) {
  3864. // use the background color if present
  3865. if (!empty($this->image_background_color)) {
  3866. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  3867. $fill = imagecolorallocate($tmp, $red, $green, $blue);
  3868. } else {
  3869. $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
  3870. }
  3871. // fills eventual negative margins
  3872. if ($ct < 0) imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, -$ct-1, $fill);
  3873. if ($cr < 0) imagefilledrectangle($tmp, $this->image_dst_x + $cr, 0, $this->image_dst_x, $this->image_dst_y, $fill);
  3874. if ($cb < 0) imagefilledrectangle($tmp, 0, $this->image_dst_y + $cb, $this->image_dst_x, $this->image_dst_y, $fill);
  3875. if ($cl < 0) imagefilledrectangle($tmp, 0, 0, -$cl-1, $this->image_dst_y, $fill);
  3876. }
  3877. // we transfert tmp into image_dst
  3878. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3879. }
  3880. // flip image
  3881. if ($gd_version >= 2 && !empty($this->image_flip)) {
  3882. $this->image_flip = strtolower($this->image_flip);
  3883. $this->log .= '- flip image : ' . $this->image_flip . '<br />';
  3884. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3885. for ($x = 0; $x < $this->image_dst_x; $x++) {
  3886. for ($y = 0; $y < $this->image_dst_y; $y++){
  3887. if (strpos($this->image_flip, 'v') !== false) {
  3888. imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1);
  3889. } else {
  3890. imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1);
  3891. }
  3892. }
  3893. }
  3894. // we transfert tmp into image_dst
  3895. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3896. }
  3897. // rotate image
  3898. if ($gd_version >= 2 && is_numeric($this->image_rotate)) {
  3899. if (!in_array($this->image_rotate, array(0, 90, 180, 270))) $this->image_rotate = 0;
  3900. if ($this->image_rotate != 0) {
  3901. if ($this->image_rotate == 90 || $this->image_rotate == 270) {
  3902. $tmp = $this->imagecreatenew($this->image_dst_y, $this->image_dst_x);
  3903. } else {
  3904. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3905. }
  3906. $this->log .= '- rotate image : ' . $this->image_rotate . '<br />';
  3907. for ($x = 0; $x < $this->image_dst_x; $x++) {
  3908. for ($y = 0; $y < $this->image_dst_y; $y++){
  3909. if ($this->image_rotate == 90) {
  3910. imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1);
  3911. } else if ($this->image_rotate == 180) {
  3912. imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1);
  3913. } else if ($this->image_rotate == 270) {
  3914. imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1);
  3915. } else {
  3916. imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1);
  3917. }
  3918. }
  3919. }
  3920. if ($this->image_rotate == 90 || $this->image_rotate == 270) {
  3921. $t = $this->image_dst_y;
  3922. $this->image_dst_y = $this->image_dst_x;
  3923. $this->image_dst_x = $t;
  3924. }
  3925. // we transfert tmp into image_dst
  3926. $image_dst = $this->imagetransfer($tmp, $image_dst);
  3927. }
  3928. }
  3929. // pixelate image
  3930. if ((is_numeric($this->image_pixelate) && $this->image_pixelate > 0)) {
  3931. $this->log .= '- pixelate image (' . $this->image_pixelate . 'px)<br />';
  3932. $filter = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  3933. if ($gd_version >= 2) {
  3934. imagecopyresampled($filter, $image_dst, 0, 0, 0, 0, round($this->image_dst_x / $this->image_pixelate), round($this->image_dst_y / $this->image_pixelate), $this->image_dst_x, $this->image_dst_y);
  3935. imagecopyresampled($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, round($this->image_dst_x / $this->image_pixelate), round($this->image_dst_y / $this->image_pixelate));
  3936. } else {
  3937. imagecopyresized($filter, $image_dst, 0, 0, 0, 0, round($this->image_dst_x / $this->image_pixelate), round($this->image_dst_y / $this->image_pixelate), $this->image_dst_x, $this->image_dst_y);
  3938. imagecopyresized($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, round($this->image_dst_x / $this->image_pixelate), round($this->image_dst_y / $this->image_pixelate));
  3939. }
  3940. imagedestroy($filter);
  3941. }
  3942. // unsharp mask
  3943. if ($gd_version >= 2 && $this->image_unsharp && is_numeric($this->image_unsharp_amount) && is_numeric($this->image_unsharp_radius) && is_numeric($this->image_unsharp_threshold)) {
  3944. // Unsharp Mask for PHP - version 2.1.1
  3945. // Unsharp mask algorithm by Torstein Hønsi 2003-07.
  3946. // Used with permission
  3947. // Modified to support alpha transparency
  3948. if ($this->image_unsharp_amount > 500) $this->image_unsharp_amount = 500;
  3949. $this->image_unsharp_amount = $this->image_unsharp_amount * 0.016;
  3950. if ($this->image_unsharp_radius > 50) $this->image_unsharp_radius = 50;
  3951. $this->image_unsharp_radius = $this->image_unsharp_radius * 2;
  3952. if ($this->image_unsharp_threshold > 255) $this->image_unsharp_threshold = 255;
  3953. $this->image_unsharp_radius = abs(round($this->image_unsharp_radius));
  3954. if ($this->image_unsharp_radius != 0) {
  3955. $this->image_dst_x = imagesx($image_dst); $this->image_dst_y = imagesy($image_dst);
  3956. $canvas = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true);
  3957. $blur = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true);
  3958. if (function_exists('imageconvolution')) { // PHP >= 5.1
  3959. $matrix = array(array( 1, 2, 1 ), array( 2, 4, 2 ), array( 1, 2, 1 ));
  3960. imagecopy($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
  3961. imageconvolution($blur, $matrix, 16, 0);
  3962. } else {
  3963. for ($i = 0; $i < $this->image_unsharp_radius; $i++) {
  3964. imagecopy($blur, $image_dst, 0, 0, 1, 0, $this->image_dst_x - 1, $this->image_dst_y); // left
  3965. $this->imagecopymergealpha($blur, $image_dst, 1, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // right
  3966. $this->imagecopymergealpha($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // center
  3967. imagecopy($canvas, $blur, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
  3968. $this->imagecopymergealpha($blur, $canvas, 0, 0, 0, 1, $this->image_dst_x, $this->image_dst_y - 1, 33.33333 ); // up
  3969. $this->imagecopymergealpha($blur, $canvas, 0, 1, 0, 0, $this->image_dst_x, $this->image_dst_y, 25); // down
  3970. }
  3971. }
  3972. $p_new = array();
  3973. if($this->image_unsharp_threshold>0) {
  3974. for ($x = 0; $x < $this->image_dst_x-1; $x++) {
  3975. for ($y = 0; $y < $this->image_dst_y; $y++) {
  3976. $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3977. $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y));
  3978. $p_new['red'] = (abs($p_orig['red'] - $p_blur['red']) >= $this->image_unsharp_threshold) ? max(0, min(255, ($this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red'])) + $p_orig['red'])) : $p_orig['red'];
  3979. $p_new['green'] = (abs($p_orig['green'] - $p_blur['green']) >= $this->image_unsharp_threshold) ? max(0, min(255, ($this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green'])) + $p_orig['green'])) : $p_orig['green'];
  3980. $p_new['blue'] = (abs($p_orig['blue'] - $p_blur['blue']) >= $this->image_unsharp_threshold) ? max(0, min(255, ($this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue'])) + $p_orig['blue'])) : $p_orig['blue'];
  3981. if (($p_orig['red'] != $p_new['red']) || ($p_orig['green'] != $p_new['green']) || ($p_orig['blue'] != $p_new['blue'])) {
  3982. $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']);
  3983. imagesetpixel($image_dst, $x, $y, $color);
  3984. }
  3985. }
  3986. }
  3987. } else {
  3988. for ($x = 0; $x < $this->image_dst_x; $x++) {
  3989. for ($y = 0; $y < $this->image_dst_y; $y++) {
  3990. $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  3991. $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y));
  3992. $p_new['red'] = ($this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red'])) + $p_orig['red'];
  3993. if ($p_new['red']>255) { $p_new['red']=255; } elseif ($p_new['red']<0) { $p_new['red']=0; }
  3994. $p_new['green'] = ($this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green'])) + $p_orig['green'];
  3995. if ($p_new['green']>255) { $p_new['green']=255; } elseif ($p_new['green']<0) { $p_new['green']=0; }
  3996. $p_new['blue'] = ($this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue'])) + $p_orig['blue'];
  3997. if ($p_new['blue']>255) { $p_new['blue']=255; } elseif ($p_new['blue']<0) { $p_new['blue']=0; }
  3998. $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']);
  3999. imagesetpixel($image_dst, $x, $y, $color);
  4000. }
  4001. }
  4002. }
  4003. imagedestroy($canvas);
  4004. imagedestroy($blur);
  4005. }
  4006. }
  4007. // add color overlay
  4008. if ($gd_version >= 2 && (is_numeric($this->image_overlay_opacity) && $this->image_overlay_opacity > 0 && !empty($this->image_overlay_color))) {
  4009. $this->log .= '- apply color overlay<br />';
  4010. list($red, $green, $blue) = $this->getcolors($this->image_overlay_color);
  4011. $filter = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y);
  4012. $color = imagecolorallocate($filter, $red, $green, $blue);
  4013. imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color);
  4014. $this->imagecopymergealpha($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_opacity);
  4015. imagedestroy($filter);
  4016. }
  4017. // add brightness, contrast and tint, turns to greyscale and inverts colors
  4018. if ($gd_version >= 2 && ($this->image_negative || $this->image_greyscale || is_numeric($this->image_threshold)|| is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || !empty($this->image_tint_color))) {
  4019. $this->log .= '- apply tint, light, contrast correction, negative, greyscale and threshold<br />';
  4020. if (!empty($this->image_tint_color)) list($tint_red, $tint_green, $tint_blue) = $this->getcolors($this->image_tint_color);
  4021. //imagealphablending($image_dst, true);
  4022. for($y=0; $y < $this->image_dst_y; $y++) {
  4023. for($x=0; $x < $this->image_dst_x; $x++) {
  4024. if ($this->image_greyscale) {
  4025. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4026. $r = $g = $b = round((0.2125 * $pixel['red']) + (0.7154 * $pixel['green']) + (0.0721 * $pixel['blue']));
  4027. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  4028. imagesetpixel($image_dst, $x, $y, $color);
  4029. unset($color); unset($pixel);
  4030. }
  4031. if (is_numeric($this->image_threshold)) {
  4032. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4033. $c = (round($pixel['red'] + $pixel['green'] + $pixel['blue']) / 3) - 127;
  4034. $r = $g = $b = ($c > $this->image_threshold ? 255 : 0);
  4035. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  4036. imagesetpixel($image_dst, $x, $y, $color);
  4037. unset($color); unset($pixel);
  4038. }
  4039. if (is_numeric($this->image_brightness)) {
  4040. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4041. $r = max(min(round($pixel['red'] + (($this->image_brightness * 2))), 255), 0);
  4042. $g = max(min(round($pixel['green'] + (($this->image_brightness * 2))), 255), 0);
  4043. $b = max(min(round($pixel['blue'] + (($this->image_brightness * 2))), 255), 0);
  4044. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  4045. imagesetpixel($image_dst, $x, $y, $color);
  4046. unset($color); unset($pixel);
  4047. }
  4048. if (is_numeric($this->image_contrast)) {
  4049. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4050. $r = max(min(round(($this->image_contrast + 128) * $pixel['red'] / 128), 255), 0);
  4051. $g = max(min(round(($this->image_contrast + 128) * $pixel['green'] / 128), 255), 0);
  4052. $b = max(min(round(($this->image_contrast + 128) * $pixel['blue'] / 128), 255), 0);
  4053. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  4054. imagesetpixel($image_dst, $x, $y, $color);
  4055. unset($color); unset($pixel);
  4056. }
  4057. if (!empty($this->image_tint_color)) {
  4058. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4059. $r = min(round($tint_red * $pixel['red'] / 169), 255);
  4060. $g = min(round($tint_green * $pixel['green'] / 169), 255);
  4061. $b = min(round($tint_blue * $pixel['blue'] / 169), 255);
  4062. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  4063. imagesetpixel($image_dst, $x, $y, $color);
  4064. unset($color); unset($pixel);
  4065. }
  4066. if (!empty($this->image_negative)) {
  4067. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4068. $r = round(255 - $pixel['red']);
  4069. $g = round(255 - $pixel['green']);
  4070. $b = round(255 - $pixel['blue']);
  4071. $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']);
  4072. imagesetpixel($image_dst, $x, $y, $color);
  4073. unset($color); unset($pixel);
  4074. }
  4075. }
  4076. }
  4077. }
  4078. // adds a border
  4079. if ($gd_version >= 2 && !empty($this->image_border)) {
  4080. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_border, $this->image_dst_x, $this->image_dst_y, true, false);
  4081. $this->log .= '- add border : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . '<br />';
  4082. $this->image_dst_x = $this->image_dst_x + $cl + $cr;
  4083. $this->image_dst_y = $this->image_dst_y + $ct + $cb;
  4084. if (!empty($this->image_border_color)) list($red, $green, $blue) = $this->getcolors($this->image_border_color);
  4085. $opacity = (is_numeric($this->image_border_opacity) ? (int) (127 - $this->image_border_opacity / 100 * 127): 0);
  4086. // we now create an image, that we fill with the border color
  4087. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  4088. $background = imagecolorallocatealpha($tmp, $red, $green, $blue, $opacity);
  4089. imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y, $background);
  4090. // we then copy the source image into the new image, without merging so that only the border is actually kept
  4091. imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct);
  4092. // we transfert tmp into image_dst
  4093. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4094. }
  4095. // adds a fading-to-transparent border
  4096. if ($gd_version >= 2 && !empty($this->image_border_transparent)) {
  4097. list($ct, $cr, $cb, $cl) = $this->getoffsets($this->image_border_transparent, $this->image_dst_x, $this->image_dst_y, true, false);
  4098. $this->log .= '- add transparent border : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . '<br />';
  4099. // we now create an image, that we fill with the border color
  4100. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  4101. // we then copy the source image into the new image, without the borders
  4102. imagecopy($tmp, $image_dst, $cl, $ct, $cl, $ct, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct);
  4103. // we now add the top border
  4104. $opacity = 100;
  4105. for ($y = $ct - 1; $y >= 0; $y--) {
  4106. $il = (int) ($ct > 0 ? ($cl * ($y / $ct)) : 0);
  4107. $ir = (int) ($ct > 0 ? ($cr * ($y / $ct)) : 0);
  4108. for ($x = $il; $x < $this->image_dst_x - $ir; $x++) {
  4109. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4110. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  4111. if ($alpha > 0) {
  4112. if ($alpha > 1) $alpha = 1;
  4113. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  4114. imagesetpixel($tmp, $x, $y, $color);
  4115. }
  4116. }
  4117. if ($opacity > 0) $opacity = $opacity - (100 / $ct);
  4118. }
  4119. // we now add the right border
  4120. $opacity = 100;
  4121. for ($x = $this->image_dst_x - $cr; $x < $this->image_dst_x; $x++) {
  4122. $it = (int) ($cr > 0 ? ($ct * (($this->image_dst_x - $x - 1) / $cr)) : 0);
  4123. $ib = (int) ($cr > 0 ? ($cb * (($this->image_dst_x - $x - 1) / $cr)) : 0);
  4124. for ($y = $it; $y < $this->image_dst_y - $ib; $y++) {
  4125. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4126. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  4127. if ($alpha > 0) {
  4128. if ($alpha > 1) $alpha = 1;
  4129. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  4130. imagesetpixel($tmp, $x, $y, $color);
  4131. }
  4132. }
  4133. if ($opacity > 0) $opacity = $opacity - (100 / $cr);
  4134. }
  4135. // we now add the bottom border
  4136. $opacity = 100;
  4137. for ($y = $this->image_dst_y - $cb; $y < $this->image_dst_y; $y++) {
  4138. $il = (int) ($cb > 0 ? ($cl * (($this->image_dst_y - $y - 1) / $cb)) : 0);
  4139. $ir = (int) ($cb > 0 ? ($cr * (($this->image_dst_y - $y - 1) / $cb)) : 0);
  4140. for ($x = $il; $x < $this->image_dst_x - $ir; $x++) {
  4141. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4142. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  4143. if ($alpha > 0) {
  4144. if ($alpha > 1) $alpha = 1;
  4145. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  4146. imagesetpixel($tmp, $x, $y, $color);
  4147. }
  4148. }
  4149. if ($opacity > 0) $opacity = $opacity - (100 / $cb);
  4150. }
  4151. // we now add the left border
  4152. $opacity = 100;
  4153. for ($x = $cl - 1; $x >= 0; $x--) {
  4154. $it = (int) ($cl > 0 ? ($ct * ($x / $cl)) : 0);
  4155. $ib = (int) ($cl > 0 ? ($cb * ($x / $cl)) : 0);
  4156. for ($y = $it; $y < $this->image_dst_y - $ib; $y++) {
  4157. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4158. $alpha = (1 - ($pixel['alpha'] / 127)) * $opacity / 100;
  4159. if ($alpha > 0) {
  4160. if ($alpha > 1) $alpha = 1;
  4161. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], round((1 - $alpha) * 127));
  4162. imagesetpixel($tmp, $x, $y, $color);
  4163. }
  4164. }
  4165. if ($opacity > 0) $opacity = $opacity - (100 / $cl);
  4166. }
  4167. // we transfert tmp into image_dst
  4168. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4169. }
  4170. // add frame border
  4171. if ($gd_version >= 2 && is_numeric($this->image_frame)) {
  4172. if (is_array($this->image_frame_colors)) {
  4173. $vars = $this->image_frame_colors;
  4174. $this->log .= '- add frame : ' . implode(' ', $this->image_frame_colors) . '<br />';
  4175. } else {
  4176. $this->log .= '- add frame : ' . $this->image_frame_colors . '<br />';
  4177. $vars = explode(' ', $this->image_frame_colors);
  4178. }
  4179. $nb = sizeof($vars);
  4180. $this->image_dst_x = $this->image_dst_x + ($nb * 2);
  4181. $this->image_dst_y = $this->image_dst_y + ($nb * 2);
  4182. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  4183. imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - ($nb * 2), $this->image_dst_y - ($nb * 2));
  4184. $opacity = (is_numeric($this->image_frame_opacity) ? (int) (127 - $this->image_frame_opacity / 100 * 127): 0);
  4185. for ($i=0; $i<$nb; $i++) {
  4186. list($red, $green, $blue) = $this->getcolors($vars[$i]);
  4187. $c = imagecolorallocatealpha($tmp, $red, $green, $blue, $opacity);
  4188. if ($this->image_frame == 1) {
  4189. imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c);
  4190. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $this->image_dst_x - $i -1, $i, $c);
  4191. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $i, $this->image_dst_y - $i -1, $c);
  4192. imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c);
  4193. } else {
  4194. imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c);
  4195. imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c);
  4196. imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c);
  4197. imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c);
  4198. }
  4199. }
  4200. // we transfert tmp into image_dst
  4201. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4202. }
  4203. // add bevel border
  4204. if ($gd_version >= 2 && $this->image_bevel > 0) {
  4205. if (empty($this->image_bevel_color1)) $this->image_bevel_color1 = '#FFFFFF';
  4206. if (empty($this->image_bevel_color2)) $this->image_bevel_color2 = '#000000';
  4207. list($red1, $green1, $blue1) = $this->getcolors($this->image_bevel_color1);
  4208. list($red2, $green2, $blue2) = $this->getcolors($this->image_bevel_color2);
  4209. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y);
  4210. imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y);
  4211. imagealphablending($tmp, true);
  4212. for ($i=0; $i<$this->image_bevel; $i++) {
  4213. $alpha = round(($i / $this->image_bevel) * 127);
  4214. $c1 = imagecolorallocatealpha($tmp, $red1, $green1, $blue1, $alpha);
  4215. $c2 = imagecolorallocatealpha($tmp, $red2, $green2, $blue2, $alpha);
  4216. imageline($tmp, $i, $i, $this->image_dst_x - $i -1, $i, $c1);
  4217. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i, $this->image_dst_x - $i -1, $i, $c2);
  4218. imageline($tmp, $this->image_dst_x - $i -1, $this->image_dst_y - $i -1, $i, $this->image_dst_y - $i -1, $c2);
  4219. imageline($tmp, $i, $i, $i, $this->image_dst_y - $i -1, $c1);
  4220. }
  4221. // we transfert tmp into image_dst
  4222. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4223. }
  4224. // add watermark image
  4225. if ($this->image_watermark!='' && file_exists($this->image_watermark)) {
  4226. $this->log .= '- add watermark<br />';
  4227. $this->image_watermark_position = strtolower($this->image_watermark_position);
  4228. $watermark_info = getimagesize($this->image_watermark);
  4229. $watermark_type = (array_key_exists(2, $watermark_info) ? $watermark_info[2] : null); // 1 = GIF, 2 = JPG, 3 = PNG
  4230. $watermark_checked = false;
  4231. if ($watermark_type == IMAGETYPE_GIF) {
  4232. if (!function_exists('imagecreatefromgif')) {
  4233. $this->error = $this->translate('watermark_no_create_support', array('GIF'));
  4234. } else {
  4235. $filter = @imagecreatefromgif($this->image_watermark);
  4236. if (!$filter) {
  4237. $this->error = $this->translate('watermark_create_error', array('GIF'));
  4238. } else {
  4239. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is GIF<br />';
  4240. $watermark_checked = true;
  4241. }
  4242. }
  4243. } else if ($watermark_type == IMAGETYPE_JPEG) {
  4244. if (!function_exists('imagecreatefromjpeg')) {
  4245. $this->error = $this->translate('watermark_no_create_support', array('JPEG'));
  4246. } else {
  4247. $filter = @imagecreatefromjpeg($this->image_watermark);
  4248. if (!$filter) {
  4249. $this->error = $this->translate('watermark_create_error', array('JPEG'));
  4250. } else {
  4251. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is JPEG<br />';
  4252. $watermark_checked = true;
  4253. }
  4254. }
  4255. } else if ($watermark_type == IMAGETYPE_PNG) {
  4256. if (!function_exists('imagecreatefrompng')) {
  4257. $this->error = $this->translate('watermark_no_create_support', array('PNG'));
  4258. } else {
  4259. $filter = @imagecreatefrompng($this->image_watermark);
  4260. if (!$filter) {
  4261. $this->error = $this->translate('watermark_create_error', array('PNG'));
  4262. } else {
  4263. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is PNG<br />';
  4264. $watermark_checked = true;
  4265. }
  4266. }
  4267. } else if ($watermark_type == IMAGETYPE_BMP) {
  4268. if (!method_exists($this, 'imagecreatefrombmp')) {
  4269. $this->error = $this->translate('watermark_no_create_support', array('BMP'));
  4270. } else {
  4271. $filter = @$this->imagecreatefrombmp($this->image_watermark);
  4272. if (!$filter) {
  4273. $this->error = $this->translate('watermark_create_error', array('BMP'));
  4274. } else {
  4275. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark source image is BMP<br />';
  4276. $watermark_checked = true;
  4277. }
  4278. }
  4279. } else {
  4280. $this->error = $this->translate('watermark_invalid');
  4281. }
  4282. if ($watermark_checked) {
  4283. $watermark_dst_width = $watermark_src_width = imagesx($filter);
  4284. $watermark_dst_height = $watermark_src_height = imagesy($filter);
  4285. // if watermark is too large/tall, resize it first
  4286. if ((!$this->image_watermark_no_zoom_out && ($watermark_dst_width > $this->image_dst_x || $watermark_dst_height > $this->image_dst_y))
  4287. || (!$this->image_watermark_no_zoom_in && $watermark_dst_width < $this->image_dst_x && $watermark_dst_height < $this->image_dst_y)) {
  4288. $canvas_width = $this->image_dst_x - abs($this->image_watermark_x);
  4289. $canvas_height = $this->image_dst_y - abs($this->image_watermark_y);
  4290. if (($watermark_src_width/$canvas_width) > ($watermark_src_height/$canvas_height)) {
  4291. $watermark_dst_width = $canvas_width;
  4292. $watermark_dst_height = intval($watermark_src_height*($canvas_width / $watermark_src_width));
  4293. } else {
  4294. $watermark_dst_height = $canvas_height;
  4295. $watermark_dst_width = intval($watermark_src_width*($canvas_height / $watermark_src_height));
  4296. }
  4297. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;watermark resized from '.$watermark_src_width.'x'.$watermark_src_height.' to '.$watermark_dst_width.'x'.$watermark_dst_height.'<br />';
  4298. }
  4299. // determine watermark position
  4300. $watermark_x = 0;
  4301. $watermark_y = 0;
  4302. if (is_numeric($this->image_watermark_x)) {
  4303. if ($this->image_watermark_x < 0) {
  4304. $watermark_x = $this->image_dst_x - $watermark_dst_width + $this->image_watermark_x;
  4305. } else {
  4306. $watermark_x = $this->image_watermark_x;
  4307. }
  4308. } else {
  4309. if (strpos($this->image_watermark_position, 'r') !== false) {
  4310. $watermark_x = $this->image_dst_x - $watermark_dst_width;
  4311. } else if (strpos($this->image_watermark_position, 'l') !== false) {
  4312. $watermark_x = 0;
  4313. } else {
  4314. $watermark_x = ($this->image_dst_x - $watermark_dst_width) / 2;
  4315. }
  4316. }
  4317. if (is_numeric($this->image_watermark_y)) {
  4318. if ($this->image_watermark_y < 0) {
  4319. $watermark_y = $this->image_dst_y - $watermark_dst_height + $this->image_watermark_y;
  4320. } else {
  4321. $watermark_y = $this->image_watermark_y;
  4322. }
  4323. } else {
  4324. if (strpos($this->image_watermark_position, 'b') !== false) {
  4325. $watermark_y = $this->image_dst_y - $watermark_dst_height;
  4326. } else if (strpos($this->image_watermark_position, 't') !== false) {
  4327. $watermark_y = 0;
  4328. } else {
  4329. $watermark_y = ($this->image_dst_y - $watermark_dst_height) / 2;
  4330. }
  4331. }
  4332. imagealphablending($image_dst, true);
  4333. imagecopyresampled($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_dst_width, $watermark_dst_height, $watermark_src_width, $watermark_src_height);
  4334. } else {
  4335. $this->error = $this->translate('watermark_invalid');
  4336. }
  4337. }
  4338. // add text
  4339. if (!empty($this->image_text)) {
  4340. $this->log .= '- add text<br />';
  4341. // calculate sizes in human readable format
  4342. $src_size = $this->file_src_size / 1024;
  4343. $src_size_mb = number_format($src_size / 1024, 1, ".", " ");
  4344. $src_size_kb = number_format($src_size, 1, ".", " ");
  4345. $src_size_human = ($src_size > 1024 ? $src_size_mb . " MB" : $src_size_kb . " kb");
  4346. $this->image_text = str_replace(
  4347. array('[src_name]',
  4348. '[src_name_body]',
  4349. '[src_name_ext]',
  4350. '[src_pathname]',
  4351. '[src_mime]',
  4352. '[src_size]',
  4353. '[src_size_kb]',
  4354. '[src_size_mb]',
  4355. '[src_size_human]',
  4356. '[src_x]',
  4357. '[src_y]',
  4358. '[src_pixels]',
  4359. '[src_type]',
  4360. '[src_bits]',
  4361. '[dst_path]',
  4362. '[dst_name_body]',
  4363. '[dst_name_ext]',
  4364. '[dst_name]',
  4365. '[dst_pathname]',
  4366. '[dst_x]',
  4367. '[dst_y]',
  4368. '[date]',
  4369. '[time]',
  4370. '[host]',
  4371. '[server]',
  4372. '[ip]',
  4373. '[gd_version]'),
  4374. array($this->file_src_name,
  4375. $this->file_src_name_body,
  4376. $this->file_src_name_ext,
  4377. $this->file_src_pathname,
  4378. $this->file_src_mime,
  4379. $this->file_src_size,
  4380. $src_size_kb,
  4381. $src_size_mb,
  4382. $src_size_human,
  4383. $this->image_src_x,
  4384. $this->image_src_y,
  4385. $this->image_src_pixels,
  4386. $this->image_src_type,
  4387. $this->image_src_bits,
  4388. $this->file_dst_path,
  4389. $this->file_dst_name_body,
  4390. $this->file_dst_name_ext,
  4391. $this->file_dst_name,
  4392. $this->file_dst_pathname,
  4393. $this->image_dst_x,
  4394. $this->image_dst_y,
  4395. date('Y-m-d'),
  4396. date('H:i:s'),
  4397. (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'n/a'),
  4398. (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'n/a'),
  4399. (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'n/a'),
  4400. $this->gdversion(true)),
  4401. $this->image_text);
  4402. if (!is_numeric($this->image_text_padding)) $this->image_text_padding = 0;
  4403. if (!is_numeric($this->image_text_line_spacing)) $this->image_text_line_spacing = 0;
  4404. if (!is_numeric($this->image_text_padding_x)) $this->image_text_padding_x = $this->image_text_padding;
  4405. if (!is_numeric($this->image_text_padding_y)) $this->image_text_padding_y = $this->image_text_padding;
  4406. $this->image_text_position = strtolower($this->image_text_position);
  4407. $this->image_text_direction = strtolower($this->image_text_direction);
  4408. $this->image_text_alignment = strtolower($this->image_text_alignment);
  4409. // if the font is a string, we assume that we might want to load a font
  4410. if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') {
  4411. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;try to load font ' . $this->image_text_font . '... ';
  4412. if ($this->image_text_font = @imageloadfont($this->image_text_font)) {
  4413. $this->log .= 'success<br />';
  4414. } else {
  4415. $this->log .= 'error<br />';
  4416. $this->image_text_font = 5;
  4417. }
  4418. }
  4419. $text = explode("\n", $this->image_text);
  4420. $char_width = imagefontwidth($this->image_text_font);
  4421. $char_height = imagefontheight($this->image_text_font);
  4422. $text_height = 0;
  4423. $text_width = 0;
  4424. $line_height = 0;
  4425. $line_width = 0;
  4426. foreach ($text as $k => $v) {
  4427. if ($this->image_text_direction == 'v') {
  4428. $h = ($char_width * strlen($v));
  4429. if ($h > $text_height) $text_height = $h;
  4430. $line_width = $char_height;
  4431. $text_width += $line_width + ($k < (sizeof($text)-1) ? $this->image_text_line_spacing : 0);
  4432. } else {
  4433. $w = ($char_width * strlen($v));
  4434. if ($w > $text_width) $text_width = $w;
  4435. $line_height = $char_height;
  4436. $text_height += $line_height + ($k < (sizeof($text)-1) ? $this->image_text_line_spacing : 0);
  4437. }
  4438. }
  4439. $text_width += (2 * $this->image_text_padding_x);
  4440. $text_height += (2 * $this->image_text_padding_y);
  4441. $text_x = 0;
  4442. $text_y = 0;
  4443. if (is_numeric($this->image_text_x)) {
  4444. if ($this->image_text_x < 0) {
  4445. $text_x = $this->image_dst_x - $text_width + $this->image_text_x;
  4446. } else {
  4447. $text_x = $this->image_text_x;
  4448. }
  4449. } else {
  4450. if (strpos($this->image_text_position, 'r') !== false) {
  4451. $text_x = $this->image_dst_x - $text_width;
  4452. } else if (strpos($this->image_text_position, 'l') !== false) {
  4453. $text_x = 0;
  4454. } else {
  4455. $text_x = ($this->image_dst_x - $text_width) / 2;
  4456. }
  4457. }
  4458. if (is_numeric($this->image_text_y)) {
  4459. if ($this->image_text_y < 0) {
  4460. $text_y = $this->image_dst_y - $text_height + $this->image_text_y;
  4461. } else {
  4462. $text_y = $this->image_text_y;
  4463. }
  4464. } else {
  4465. if (strpos($this->image_text_position, 'b') !== false) {
  4466. $text_y = $this->image_dst_y - $text_height;
  4467. } else if (strpos($this->image_text_position, 't') !== false) {
  4468. $text_y = 0;
  4469. } else {
  4470. $text_y = ($this->image_dst_y - $text_height) / 2;
  4471. }
  4472. }
  4473. // add a background, maybe transparent
  4474. if (!empty($this->image_text_background)) {
  4475. list($red, $green, $blue) = $this->getcolors($this->image_text_background);
  4476. if ($gd_version >= 2 && (is_numeric($this->image_text_background_opacity)) && $this->image_text_background_opacity >= 0 && $this->image_text_background_opacity <= 100) {
  4477. $filter = imagecreatetruecolor($text_width, $text_height);
  4478. $background_color = imagecolorallocate($filter, $red, $green, $blue);
  4479. imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color);
  4480. $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_opacity);
  4481. imagedestroy($filter);
  4482. } else {
  4483. $background_color = imagecolorallocate($image_dst ,$red, $green, $blue);
  4484. imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color);
  4485. }
  4486. }
  4487. $text_x += $this->image_text_padding_x;
  4488. $text_y += $this->image_text_padding_y;
  4489. $t_width = $text_width - (2 * $this->image_text_padding_x);
  4490. $t_height = $text_height - (2 * $this->image_text_padding_y);
  4491. list($red, $green, $blue) = $this->getcolors($this->image_text_color);
  4492. // add the text, maybe transparent
  4493. if ($gd_version >= 2 && (is_numeric($this->image_text_opacity)) && $this->image_text_opacity >= 0 && $this->image_text_opacity <= 100) {
  4494. if ($t_width < 0) $t_width = 0;
  4495. if ($t_height < 0) $t_height = 0;
  4496. $filter = $this->imagecreatenew($t_width, $t_height, false, true);
  4497. $text_color = imagecolorallocate($filter ,$red, $green, $blue);
  4498. foreach ($text as $k => $v) {
  4499. if ($this->image_text_direction == 'v') {
  4500. imagestringup($filter,
  4501. $this->image_text_font,
  4502. $k * ($line_width + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  4503. $text_height - (2 * $this->image_text_padding_y) - ($this->image_text_alignment == 'l' ? 0 : (($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))) ,
  4504. $v,
  4505. $text_color);
  4506. } else {
  4507. imagestring($filter,
  4508. $this->image_text_font,
  4509. ($this->image_text_alignment == 'l' ? 0 : (($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))),
  4510. $k * ($line_height + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  4511. $v,
  4512. $text_color);
  4513. }
  4514. }
  4515. $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_opacity);
  4516. imagedestroy($filter);
  4517. } else {
  4518. $text_color = imageColorAllocate($image_dst ,$red, $green, $blue);
  4519. foreach ($text as $k => $v) {
  4520. if ($this->image_text_direction == 'v') {
  4521. imagestringup($image_dst,
  4522. $this->image_text_font,
  4523. $text_x + $k * ($line_width + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  4524. $text_y + $text_height - (2 * $this->image_text_padding_y) - ($this->image_text_alignment == 'l' ? 0 : (($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))),
  4525. $v,
  4526. $text_color);
  4527. } else {
  4528. imagestring($image_dst,
  4529. $this->image_text_font,
  4530. $text_x + ($this->image_text_alignment == 'l' ? 0 : (($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2))),
  4531. $text_y + $k * ($line_height + ($k > 0 && $k < (sizeof($text)) ? $this->image_text_line_spacing : 0)),
  4532. $v,
  4533. $text_color);
  4534. }
  4535. }
  4536. }
  4537. }
  4538. // add a reflection
  4539. if ($this->image_reflection_height) {
  4540. $this->log .= '- add reflection : ' . $this->image_reflection_height . '<br />';
  4541. // we decode image_reflection_height, which can be a integer, a string in pixels or percentage
  4542. $image_reflection_height = $this->image_reflection_height;
  4543. if (strpos($image_reflection_height, '%')>0) $image_reflection_height = $this->image_dst_y * (str_replace('%','',$image_reflection_height / 100));
  4544. if (strpos($image_reflection_height, 'px')>0) $image_reflection_height = str_replace('px','',$image_reflection_height);
  4545. $image_reflection_height = (int) $image_reflection_height;
  4546. if ($image_reflection_height > $this->image_dst_y) $image_reflection_height = $this->image_dst_y;
  4547. if (empty($this->image_reflection_opacity)) $this->image_reflection_opacity = 60;
  4548. // create the new destination image
  4549. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, true);
  4550. $transparency = $this->image_reflection_opacity;
  4551. // copy the original image
  4552. imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0));
  4553. // we have to make sure the extra bit is the right color, or transparent
  4554. if ($image_reflection_height + $this->image_reflection_space > 0) {
  4555. // use the background color if present
  4556. if (!empty($this->image_background_color)) {
  4557. list($red, $green, $blue) = $this->getcolors($this->image_background_color);
  4558. $fill = imagecolorallocate($tmp, $red, $green, $blue);
  4559. } else {
  4560. $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
  4561. }
  4562. // fill in from the edge of the extra bit
  4563. imagefill($tmp, round($this->image_dst_x / 2), $this->image_dst_y + $image_reflection_height + $this->image_reflection_space - 1, $fill);
  4564. }
  4565. // copy the reflection
  4566. for ($y = 0; $y < $image_reflection_height; $y++) {
  4567. for ($x = 0; $x < $this->image_dst_x; $x++) {
  4568. $pixel_b = imagecolorsforindex($tmp, imagecolorat($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space));
  4569. $pixel_o = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $this->image_dst_y - $y - 1 + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)));
  4570. $alpha_o = 1 - ($pixel_o['alpha'] / 127);
  4571. $alpha_b = 1 - ($pixel_b['alpha'] / 127);
  4572. $opacity = $alpha_o * $transparency / 100;
  4573. if ($opacity > 0) {
  4574. $red = round((($pixel_o['red'] * $opacity) + ($pixel_b['red'] ) * $alpha_b) / ($alpha_b + $opacity));
  4575. $green = round((($pixel_o['green'] * $opacity) + ($pixel_b['green']) * $alpha_b) / ($alpha_b + $opacity));
  4576. $blue = round((($pixel_o['blue'] * $opacity) + ($pixel_b['blue'] ) * $alpha_b) / ($alpha_b + $opacity));
  4577. $alpha = ($opacity + $alpha_b);
  4578. if ($alpha > 1) $alpha = 1;
  4579. $alpha = round((1 - $alpha) * 127);
  4580. $color = imagecolorallocatealpha($tmp, $red, $green, $blue, $alpha);
  4581. imagesetpixel($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space, $color);
  4582. }
  4583. }
  4584. if ($transparency > 0) $transparency = $transparency - ($this->image_reflection_opacity / $image_reflection_height);
  4585. }
  4586. // copy the resulting image into the destination image
  4587. $this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space;
  4588. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4589. }
  4590. // change opacity
  4591. if ($gd_version >= 2 && is_numeric($this->image_opacity) && $this->image_opacity < 100) {
  4592. $this->log .= '- change opacity<br />';
  4593. // create the new destination image
  4594. $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, true);
  4595. for($y=0; $y < $this->image_dst_y; $y++) {
  4596. for($x=0; $x < $this->image_dst_x; $x++) {
  4597. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4598. $alpha = $pixel['alpha'] + round((127 - $pixel['alpha']) * (100 - $this->image_opacity) / 100);
  4599. if ($alpha > 127) $alpha = 127;
  4600. if ($alpha > 0) {
  4601. $color = imagecolorallocatealpha($tmp, $pixel['red'] , $pixel['green'], $pixel['blue'], $alpha);
  4602. imagesetpixel($tmp, $x, $y, $color);
  4603. }
  4604. }
  4605. }
  4606. // copy the resulting image into the destination image
  4607. $image_dst = $this->imagetransfer($tmp, $image_dst);
  4608. }
  4609. // reduce the JPEG image to a set desired size
  4610. if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) {
  4611. // inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net
  4612. $this->log .= '- JPEG desired file size : ' . $this->jpeg_size . '<br />';
  4613. // calculate size of each image. 75%, 50%, and 25% quality
  4614. ob_start(); imagejpeg($image_dst,null,75); $buffer = ob_get_contents(); ob_end_clean();
  4615. $size75 = strlen($buffer);
  4616. ob_start(); imagejpeg($image_dst,null,50); $buffer = ob_get_contents(); ob_end_clean();
  4617. $size50 = strlen($buffer);
  4618. ob_start(); imagejpeg($image_dst,null,25); $buffer = ob_get_contents(); ob_end_clean();
  4619. $size25 = strlen($buffer);
  4620. // make sure we won't divide by 0
  4621. if ($size50 == $size25) $size50++;
  4622. if ($size75 == $size50 || $size75 == $size25) $size75++;
  4623. // calculate gradient of size reduction by quality
  4624. $mgrad1 = 25 / ($size50-$size25);
  4625. $mgrad2 = 25 / ($size75-$size50);
  4626. $mgrad3 = 50 / ($size75-$size25);
  4627. $mgrad = ($mgrad1 + $mgrad2 + $mgrad3) / 3;
  4628. // result of approx. quality factor for expected size
  4629. $q_factor = round($mgrad * ($this->jpeg_size - $size50) + 50);
  4630. if ($q_factor<1) {
  4631. $this->jpeg_quality=1;
  4632. } elseif ($q_factor>100) {
  4633. $this->jpeg_quality=100;
  4634. } else {
  4635. $this->jpeg_quality=$q_factor;
  4636. }
  4637. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;JPEG quality factor set to ' . $this->jpeg_quality . '<br />';
  4638. }
  4639. // converts image from true color, and fix transparency if needed
  4640. $this->log .= '- converting...<br />';
  4641. switch($this->image_convert) {
  4642. case 'gif':
  4643. // if the image is true color, we convert it to a palette
  4644. if (imageistruecolor($image_dst)) {
  4645. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;true color to palette<br />';
  4646. // creates a black and white mask
  4647. $mask = array(array());
  4648. for ($x = 0; $x < $this->image_dst_x; $x++) {
  4649. for ($y = 0; $y < $this->image_dst_y; $y++) {
  4650. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4651. $mask[$x][$y] = $pixel['alpha'];
  4652. }
  4653. }
  4654. list($red, $green, $blue) = $this->getcolors($this->image_default_color);
  4655. // first, we merge the image with the background color, so we know which colors we will have
  4656. for ($x = 0; $x < $this->image_dst_x; $x++) {
  4657. for ($y = 0; $y < $this->image_dst_y; $y++) {
  4658. if ($mask[$x][$y] > 0){
  4659. // we have some transparency. we combine the color with the default color
  4660. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4661. $alpha = ($mask[$x][$y] / 127);
  4662. $pixel['red'] = round(($pixel['red'] * (1 -$alpha) + $red * ($alpha)));
  4663. $pixel['green'] = round(($pixel['green'] * (1 -$alpha) + $green * ($alpha)));
  4664. $pixel['blue'] = round(($pixel['blue'] * (1 -$alpha) + $blue * ($alpha)));
  4665. $color = imagecolorallocate($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']);
  4666. imagesetpixel($image_dst, $x, $y, $color);
  4667. }
  4668. }
  4669. }
  4670. // transforms the true color image into palette, with its merged default color
  4671. if (empty($this->image_background_color)) {
  4672. imagetruecolortopalette($image_dst, true, 255);
  4673. $transparency = imagecolorallocate($image_dst, 254, 1, 253);
  4674. imagecolortransparent($image_dst, $transparency);
  4675. // make the transparent areas transparent
  4676. for ($x = 0; $x < $this->image_dst_x; $x++) {
  4677. for ($y = 0; $y < $this->image_dst_y; $y++) {
  4678. // we test wether we have enough opacity to justify keeping the color
  4679. if ($mask[$x][$y] > 120) imagesetpixel($image_dst, $x, $y, $transparency);
  4680. }
  4681. }
  4682. }
  4683. unset($mask);
  4684. }
  4685. break;
  4686. case 'jpg':
  4687. case 'bmp':
  4688. // if the image doesn't support any transparency, then we merge it with the default color
  4689. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;fills in transparency with default color<br />';
  4690. list($red, $green, $blue) = $this->getcolors($this->image_default_color);
  4691. $transparency = imagecolorallocate($image_dst, $red, $green, $blue);
  4692. // make the transaparent areas transparent
  4693. for ($x = 0; $x < $this->image_dst_x; $x++) {
  4694. for ($y = 0; $y < $this->image_dst_y; $y++) {
  4695. // we test wether we have some transparency, in which case we will merge the colors
  4696. if (imageistruecolor($image_dst)) {
  4697. $rgba = imagecolorat($image_dst, $x, $y);
  4698. $pixel = array('red' => ($rgba >> 16) & 0xFF,
  4699. 'green' => ($rgba >> 8) & 0xFF,
  4700. 'blue' => $rgba & 0xFF,
  4701. 'alpha' => ($rgba & 0x7F000000) >> 24);
  4702. } else {
  4703. $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
  4704. }
  4705. if ($pixel['alpha'] == 127) {
  4706. // we have full transparency. we make the pixel transparent
  4707. imagesetpixel($image_dst, $x, $y, $transparency);
  4708. } else if ($pixel['alpha'] > 0) {
  4709. // we have some transparency. we combine the color with the default color
  4710. $alpha = ($pixel['alpha'] / 127);
  4711. $pixel['red'] = round(($pixel['red'] * (1 -$alpha) + $red * ($alpha)));
  4712. $pixel['green'] = round(($pixel['green'] * (1 -$alpha) + $green * ($alpha)));
  4713. $pixel['blue'] = round(($pixel['blue'] * (1 -$alpha) + $blue * ($alpha)));
  4714. $color = imagecolorclosest($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']);
  4715. imagesetpixel($image_dst, $x, $y, $color);
  4716. }
  4717. }
  4718. }
  4719. break;
  4720. default:
  4721. break;
  4722. }
  4723. // interlace options
  4724. if($this->image_interlace) imageinterlace($image_dst, true);
  4725. // outputs image
  4726. $this->log .= '- saving image...<br />';
  4727. switch($this->image_convert) {
  4728. case 'jpeg':
  4729. case 'jpg':
  4730. if (!$return_mode) {
  4731. $result = @imagejpeg($image_dst, $this->file_dst_pathname, $this->jpeg_quality);
  4732. } else {
  4733. ob_start();
  4734. $result = @imagejpeg($image_dst, null, $this->jpeg_quality);
  4735. $return_content = ob_get_contents();
  4736. ob_end_clean();
  4737. }
  4738. if (!$result) {
  4739. $this->processed = false;
  4740. $this->error = $this->translate('file_create', array('JPEG'));
  4741. } else {
  4742. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;JPEG image created<br />';
  4743. }
  4744. break;
  4745. case 'png':
  4746. imagealphablending( $image_dst, false );
  4747. imagesavealpha( $image_dst, true );
  4748. if (!$return_mode) {
  4749. if (is_numeric($this->png_compression) && version_compare(PHP_VERSION, '5.1.2') >= 0) {
  4750. $result = @imagepng($image_dst, $this->file_dst_pathname, $this->png_compression);
  4751. } else {
  4752. $result = @imagepng($image_dst, $this->file_dst_pathname);
  4753. }
  4754. } else {
  4755. ob_start();
  4756. if (is_numeric($this->png_compression) && version_compare(PHP_VERSION, '5.1.2') >= 0) {
  4757. $result = @imagepng($image_dst, null, $this->png_compression);
  4758. } else {
  4759. $result = @imagepng($image_dst);
  4760. }
  4761. $return_content = ob_get_contents();
  4762. ob_end_clean();
  4763. }
  4764. if (!$result) {
  4765. $this->processed = false;
  4766. $this->error = $this->translate('file_create', array('PNG'));
  4767. } else {
  4768. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;PNG image created<br />';
  4769. }
  4770. break;
  4771. case 'gif':
  4772. if (!$return_mode) {
  4773. $result = @imagegif($image_dst, $this->file_dst_pathname);
  4774. } else {
  4775. ob_start();
  4776. $result = @imagegif($image_dst);
  4777. $return_content = ob_get_contents();
  4778. ob_end_clean();
  4779. }
  4780. if (!$result) {
  4781. $this->processed = false;
  4782. $this->error = $this->translate('file_create', array('GIF'));
  4783. } else {
  4784. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;GIF image created<br />';
  4785. }
  4786. break;
  4787. case 'bmp':
  4788. if (!$return_mode) {
  4789. $result = $this->imagebmp($image_dst, $this->file_dst_pathname);
  4790. } else {
  4791. ob_start();
  4792. $result = $this->imagebmp($image_dst);
  4793. $return_content = ob_get_contents();
  4794. ob_end_clean();
  4795. }
  4796. if (!$result) {
  4797. $this->processed = false;
  4798. $this->error = $this->translate('file_create', array('BMP'));
  4799. } else {
  4800. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;BMP image created<br />';
  4801. }
  4802. break;
  4803. default:
  4804. $this->processed = false;
  4805. $this->error = $this->translate('no_conversion_type');
  4806. }
  4807. if ($this->processed) {
  4808. if (is_resource($image_src)) imagedestroy($image_src);
  4809. if (is_resource($image_dst)) imagedestroy($image_dst);
  4810. $this->log .= '&nbsp;&nbsp;&nbsp;&nbsp;image objects destroyed<br />';
  4811. }
  4812. }
  4813. } else {
  4814. $this->log .= '- no image processing wanted<br />';
  4815. if (!$return_mode) {
  4816. // copy the file to its final destination. we don't use move_uploaded_file here
  4817. // if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file
  4818. if (!copy($this->file_src_pathname, $this->file_dst_pathname)) {
  4819. $this->processed = false;
  4820. $this->error = $this->translate('copy_failed');
  4821. }
  4822. } else {
  4823. // returns the file, so that its content can be received by the caller
  4824. $return_content = @file_get_contents($this->file_src_pathname);
  4825. if ($return_content === FALSE) {
  4826. $this->processed = false;
  4827. $this->error = $this->translate('reading_failed');
  4828. }
  4829. }
  4830. }
  4831. }
  4832. if ($this->processed) {
  4833. $this->log .= '- <b>process OK</b><br />';
  4834. } else {
  4835. $this->log .= '- <b>error</b>: ' . $this->error . '<br />';
  4836. }
  4837. // we reinit all the vars
  4838. $this->init();
  4839. // we may return the image content
  4840. if ($return_mode) return $return_content;
  4841. }
  4842. /**
  4843. * Deletes the uploaded file from its temporary location
  4844. *
  4845. * When PHP uploads a file, it stores it in a temporary location.
  4846. * When you {@link process} the file, you actually copy the resulting file to the given location, it doesn't alter the original file.
  4847. * Once you have processed the file as many times as you wanted, you can delete the uploaded file.
  4848. * If there is open_basedir restrictions, the uploaded file is in fact a temporary file
  4849. *
  4850. * You might want not to use this function if you work on local files, as it will delete the source file
  4851. *
  4852. * @access public
  4853. */
  4854. function clean() {
  4855. $this->log .= '<b>cleanup</b><br />';
  4856. $this->log .= '- delete temp file ' . $this->file_src_pathname . '<br />';
  4857. @unlink($this->file_src_pathname);
  4858. }
  4859. /**
  4860. * Opens a BMP image
  4861. *
  4862. * This function has been written by DHKold, and is used with permission of the author
  4863. *
  4864. * @access public
  4865. */
  4866. function imagecreatefrombmp($filename) {
  4867. if (! $f1 = fopen($filename,"rb")) return false;
  4868. $file = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
  4869. if ($file['file_type'] != 19778) return false;
  4870. $bmp = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  4871. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  4872. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
  4873. $bmp['colors'] = pow(2,$bmp['bits_per_pixel']);
  4874. if ($bmp['size_bitmap'] == 0) $bmp['size_bitmap'] = $file['file_size'] - $file['bitmap_offset'];
  4875. $bmp['bytes_per_pixel'] = $bmp['bits_per_pixel']/8;
  4876. $bmp['bytes_per_pixel2'] = ceil($bmp['bytes_per_pixel']);
  4877. $bmp['decal'] = ($bmp['width']*$bmp['bytes_per_pixel']/4);
  4878. $bmp['decal'] -= floor($bmp['width']*$bmp['bytes_per_pixel']/4);
  4879. $bmp['decal'] = 4-(4*$bmp['decal']);
  4880. if ($bmp['decal'] == 4) $bmp['decal'] = 0;
  4881. $palette = array();
  4882. if ($bmp['colors'] < 16777216) {
  4883. $palette = unpack('V'.$bmp['colors'], fread($f1,$bmp['colors']*4));
  4884. }
  4885. $im = fread($f1,$bmp['size_bitmap']);
  4886. $vide = chr(0);
  4887. $res = imagecreatetruecolor($bmp['width'],$bmp['height']);
  4888. $P = 0;
  4889. $Y = $bmp['height']-1;
  4890. while ($Y >= 0) {
  4891. $X=0;
  4892. while ($X < $bmp['width']) {
  4893. if ($bmp['bits_per_pixel'] == 24)
  4894. $color = unpack("V",substr($im,$P,3).$vide);
  4895. elseif ($bmp['bits_per_pixel'] == 16) {
  4896. $color = unpack("n",substr($im,$P,2));
  4897. $color[1] = $palette[$color[1]+1];
  4898. } elseif ($bmp['bits_per_pixel'] == 8) {
  4899. $color = unpack("n",$vide.substr($im,$P,1));
  4900. $color[1] = $palette[$color[1]+1];
  4901. } elseif ($bmp['bits_per_pixel'] == 4) {
  4902. $color = unpack("n",$vide.substr($im,floor($P),1));
  4903. if (($P*2)%2 == 0) $color[1] = ($color[1] >> 4) ; else $color[1] = ($color[1] & 0x0F);
  4904. $color[1] = $palette[$color[1]+1];
  4905. } elseif ($bmp['bits_per_pixel'] == 1) {
  4906. $color = unpack("n",$vide.substr($im,floor($P),1));
  4907. if (($P*8)%8 == 0) $color[1] = $color[1] >>7;
  4908. elseif (($P*8)%8 == 1) $color[1] = ($color[1] & 0x40)>>6;
  4909. elseif (($P*8)%8 == 2) $color[1] = ($color[1] & 0x20)>>5;
  4910. elseif (($P*8)%8 == 3) $color[1] = ($color[1] & 0x10)>>4;
  4911. elseif (($P*8)%8 == 4) $color[1] = ($color[1] & 0x8)>>3;
  4912. elseif (($P*8)%8 == 5) $color[1] = ($color[1] & 0x4)>>2;
  4913. elseif (($P*8)%8 == 6) $color[1] = ($color[1] & 0x2)>>1;
  4914. elseif (($P*8)%8 == 7) $color[1] = ($color[1] & 0x1);
  4915. $color[1] = $palette[$color[1]+1];
  4916. } else
  4917. return FALSE;
  4918. imagesetpixel($res,$X,$Y,$color[1]);
  4919. $X++;
  4920. $P += $bmp['bytes_per_pixel'];
  4921. }
  4922. $Y--;
  4923. $P+=$bmp['decal'];
  4924. }
  4925. fclose($f1);
  4926. return $res;
  4927. }
  4928. /**
  4929. * Saves a BMP image
  4930. *
  4931. * This function has been published on the PHP website, and can be used freely
  4932. *
  4933. * @access public
  4934. */
  4935. function imagebmp(&$im, $filename = "") {
  4936. if (!$im) return false;
  4937. $w = imagesx($im);
  4938. $h = imagesy($im);
  4939. $result = '';
  4940. // if the image is not true color, we convert it first
  4941. if (!imageistruecolor($im)) {
  4942. $tmp = imagecreatetruecolor($w, $h);
  4943. imagecopy($tmp, $im, 0, 0, 0, 0, $w, $h);
  4944. imagedestroy($im);
  4945. $im = & $tmp;
  4946. }
  4947. $biBPLine = $w * 3;
  4948. $biStride = ($biBPLine + 3) & ~3;
  4949. $biSizeImage = $biStride * $h;
  4950. $bfOffBits = 54;
  4951. $bfSize = $bfOffBits + $biSizeImage;
  4952. $result .= substr('BM', 0, 2);
  4953. $result .= pack ('VvvV', $bfSize, 0, 0, $bfOffBits);
  4954. $result .= pack ('VVVvvVVVVVV', 40, $w, $h, 1, 24, 0, $biSizeImage, 0, 0, 0, 0);
  4955. $numpad = $biStride - $biBPLine;
  4956. for ($y = $h - 1; $y >= 0; --$y) {
  4957. for ($x = 0; $x < $w; ++$x) {
  4958. $col = imagecolorat ($im, $x, $y);
  4959. $result .= substr(pack ('V', $col), 0, 3);
  4960. }
  4961. for ($i = 0; $i < $numpad; ++$i)
  4962. $result .= pack ('C', 0);
  4963. }
  4964. if($filename==""){
  4965. echo $result;
  4966. } else {
  4967. $file = fopen($filename, "wb");
  4968. fwrite($file, $result);
  4969. fclose($file);
  4970. }
  4971. return true;
  4972. }
  4973. }
  4974. ?>