phpthumb.class.php 201 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434
  1. <?php
  2. //////////////////////////////////////////////////////////////
  3. // phpThumb() by James Heinrich <info@silisoftware.com> //
  4. // available at http://phpthumb.sourceforge.net //
  5. // and/or https://github.com/JamesHeinrich/phpThumb //
  6. //////////////////////////////////////////////////////////////
  7. /// //
  8. // See: phpthumb.readme.txt for usage instructions //
  9. // ///
  10. //////////////////////////////////////////////////////////////
  11. ob_start();
  12. if (!include_once __DIR__ .'/phpthumb.functions.php' ) {
  13. ob_end_flush();
  14. die('failed to include_once("'. __DIR__ .'/phpthumb.functions.php")');
  15. }
  16. ob_end_clean();
  17. class phpthumb {
  18. // public:
  19. // START PARAMETERS (for object mode and phpThumb.php)
  20. // See phpthumb.readme.txt for descriptions of what each of these values are
  21. public $src = null; // SouRCe filename
  22. public $new = null; // NEW image (phpThumb.php only)
  23. public $w = null; // Width
  24. public $h = null; // Height
  25. public $wp = null; // Width (Portrait Images Only)
  26. public $hp = null; // Height (Portrait Images Only)
  27. public $wl = null; // Width (Landscape Images Only)
  28. public $hl = null; // Height (Landscape Images Only)
  29. public $ws = null; // Width (Square Images Only)
  30. public $hs = null; // Height (Square Images Only)
  31. public $f = null; // output image Format
  32. public $q = 75; // jpeg output Quality
  33. public $sx = null; // Source crop top-left X position
  34. public $sy = null; // Source crop top-left Y position
  35. public $sw = null; // Source crop Width
  36. public $sh = null; // Source crop Height
  37. public $zc = null; // Zoom Crop
  38. public $bc = null; // Border Color
  39. public $bg = null; // BackGround color
  40. public $fltr = array(); // FiLTeRs
  41. public $goto = null; // GO TO url after processing
  42. public $err = null; // default ERRor image filename
  43. public $xto = null; // extract eXif Thumbnail Only
  44. public $ra = null; // Rotate by Angle
  45. public $ar = null; // Auto Rotate
  46. public $aoe = null; // Allow Output Enlargement
  47. public $far = null; // Fixed Aspect Ratio
  48. public $iar = null; // Ignore Aspect Ratio
  49. public $maxb = null; // MAXimum Bytes
  50. public $down = null; // DOWNload thumbnail filename
  51. public $md5s = null; // MD5 hash of Source image
  52. public $sfn = 0; // Source Frame Number
  53. public $dpi = 150; // Dots Per Inch for vector source formats
  54. public $sia = null; // Save Image As filename
  55. public $file = null; // >>>deprecated, DO NOT USE, will be removed in future versions<<<
  56. public $phpThumbDebug = null;
  57. // END PARAMETERS
  58. // public:
  59. // START CONFIGURATION OPTIONS (for object mode only)
  60. // See phpThumb.config.php for descriptions of what each of these settings do
  61. // * Directory Configuration
  62. public $config_cache_directory = null;
  63. public $config_cache_directory_depth = 0;
  64. public $config_cache_disable_warning = true;
  65. public $config_cache_source_enabled = false;
  66. public $config_cache_source_directory = null;
  67. public $config_temp_directory = null;
  68. public $config_document_root = null;
  69. // * Default output configuration:
  70. public $config_output_format = 'jpeg';
  71. public $config_output_maxwidth = 0;
  72. public $config_output_maxheight = 0;
  73. public $config_output_interlace = true;
  74. // * Error message configuration
  75. public $config_error_image_width = 400;
  76. public $config_error_image_height = 100;
  77. public $config_error_message_image_default = '';
  78. public $config_error_bgcolor = 'CCCCFF';
  79. public $config_error_textcolor = 'FF0000';
  80. public $config_error_fontsize = 1;
  81. public $config_error_die_on_error = false;
  82. public $config_error_silent_die_on_error = false;
  83. public $config_error_die_on_source_failure = true;
  84. // * Anti-Hotlink Configuration:
  85. public $config_nohotlink_enabled = true;
  86. public $config_nohotlink_valid_domains = array();
  87. public $config_nohotlink_erase_image = true;
  88. public $config_nohotlink_text_message = 'Off-server thumbnailing is not allowed';
  89. // * Off-server Linking Configuration:
  90. public $config_nooffsitelink_enabled = false;
  91. public $config_nooffsitelink_valid_domains = array();
  92. public $config_nooffsitelink_require_refer = false;
  93. public $config_nooffsitelink_erase_image = true;
  94. public $config_nooffsitelink_watermark_src = '';
  95. public $config_nooffsitelink_text_message = 'Off-server linking is not allowed';
  96. // * Border & Background default colors
  97. public $config_border_hexcolor = '000000';
  98. public $config_background_hexcolor = 'FFFFFF';
  99. // * TrueType Fonts
  100. public $config_ttf_directory = './fonts';
  101. public $config_max_source_pixels = null;
  102. public $config_use_exif_thumbnail_for_speed = false;
  103. public $config_allow_local_http_src = false;
  104. public $config_imagemagick_path = null;
  105. public $config_prefer_imagemagick = true;
  106. public $config_imagemagick_use_thumbnail = true;
  107. public $config_cache_maxage = null;
  108. public $config_cache_maxsize = null;
  109. public $config_cache_maxfiles = null;
  110. public $config_cache_source_filemtime_ignore_local = false;
  111. public $config_cache_source_filemtime_ignore_remote = true;
  112. public $config_cache_default_only_suffix = false;
  113. public $config_cache_force_passthru = true;
  114. public $config_cache_prefix = ''; // default value set in the constructor below
  115. // * MySQL
  116. public $config_mysql_extension = null;
  117. public $config_mysql_query = null;
  118. public $config_mysql_hostname = null;
  119. public $config_mysql_username = null;
  120. public $config_mysql_password = null;
  121. public $config_mysql_database = null;
  122. // * Security
  123. public $config_high_security_enabled = true;
  124. public $config_high_security_password = null;
  125. public $config_high_security_url_separator = '&';
  126. public $config_disable_debug = true;
  127. public $config_allow_src_above_docroot = false;
  128. public $config_allow_src_above_phpthumb = true;
  129. public $config_auto_allow_symlinks = true; // allow symlink target directories without explicitly whitelisting them
  130. public $config_additional_allowed_dirs = array(); // additional directories to allow source images to be read from
  131. public $config_file_create_mask = 0755;
  132. public $config_dir_create_mask = 0755;
  133. // * HTTP fopen
  134. public $config_http_fopen_timeout = 10;
  135. public $config_http_follow_redirect = true;
  136. // * Compatability
  137. public $config_disable_pathinfo_parsing = false;
  138. public $config_disable_imagecopyresampled = false;
  139. public $config_disable_onlycreateable_passthru = false;
  140. public $config_disable_realpath = false;
  141. public $config_http_user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7';
  142. // END CONFIGURATION OPTIONS
  143. // public: error messages (read-only; persistant)
  144. public $debugmessages = array();
  145. public $debugtiming = array();
  146. public $fatalerror = null;
  147. // private: (should not be modified directly)
  148. public $thumbnailQuality = 75;
  149. public $thumbnailFormat = null;
  150. public $sourceFilename = null;
  151. public $rawImageData = null;
  152. public $IMresizedData = null;
  153. public $outputImageData = null;
  154. public $useRawIMoutput = false;
  155. public $gdimg_output = null;
  156. public $gdimg_source = null;
  157. public $getimagesizeinfo = null;
  158. public $source_width = null;
  159. public $source_height = null;
  160. public $thumbnailCropX = null;
  161. public $thumbnailCropY = null;
  162. public $thumbnailCropW = null;
  163. public $thumbnailCropH = null;
  164. public $exif_thumbnail_width = null;
  165. public $exif_thumbnail_height = null;
  166. public $exif_thumbnail_type = null;
  167. public $exif_thumbnail_data = null;
  168. public $exif_raw_data = null;
  169. public $thumbnail_width = null;
  170. public $thumbnail_height = null;
  171. public $thumbnail_image_width = null;
  172. public $thumbnail_image_height = null;
  173. public $tempFilesToDelete = array();
  174. public $cache_filename = null;
  175. public $AlphaCapableFormats = array( 'png', 'ico', 'gif', 'webp');
  176. public $is_alpha = false;
  177. public $iswindows = null;
  178. public $issafemode = null;
  179. public $php_memory_limit = null;
  180. public $phpthumb_version = '1.7.15-201902101903';
  181. //////////////////////////////////////////////////////////////////////
  182. // public: constructor
  183. public function __construct() {
  184. $this->phpThumb();
  185. }
  186. public function phpThumb() {
  187. $this->DebugTimingMessage('phpThumb() constructor', __FILE__, __LINE__);
  188. $this->DebugMessage('phpThumb() v'.$this->phpthumb_version, __FILE__, __LINE__);
  189. foreach (array(ini_get('memory_limit'), get_cfg_var('memory_limit')) as $php_config_memory_limit) {
  190. if ('' !== $php_config_memory_limit) {
  191. if (strtoupper($php_config_memory_limit[ strlen($php_config_memory_limit) - 1 ]) == 'G') { // PHP memory limit expressed in Gigabytes
  192. $php_config_memory_limit = (int) substr($php_config_memory_limit, 0, -1) * 1073741824;
  193. } elseif (strtoupper($php_config_memory_limit[ strlen($php_config_memory_limit) - 1 ]) == 'M') { // PHP memory limit expressed in Megabytes
  194. $php_config_memory_limit = (int) substr($php_config_memory_limit, 0, -1) * 1048576;
  195. }
  196. $this->php_memory_limit = max($this->php_memory_limit, $php_config_memory_limit);
  197. }
  198. }
  199. if ($this->php_memory_limit > 0) { // could be "-1" for "no limit"
  200. $this->config_max_source_pixels = round($this->php_memory_limit * 0.20); // 20% of memory_limit
  201. }
  202. $this->iswindows = (bool) (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN');
  203. $this->issafemode = (bool) preg_match('#(1|ON)#i', ini_get('safe_mode'));
  204. $this->config_document_root = (!empty($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : $this->config_document_root);
  205. $this->config_cache_prefix = ( isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'].'_' : '');
  206. $this->purgeTempFiles(); // purge existing temp files if re-initializing object
  207. $php_sapi_name = strtolower(function_exists('php_sapi_name') ? PHP_SAPI : '');
  208. if ($php_sapi_name == 'cli') {
  209. $this->config_allow_src_above_docroot = true;
  210. }
  211. if (!$this->config_disable_debug) {
  212. // if debug mode is enabled, force phpThumbDebug output, do not allow normal thumbnails to be generated
  213. $this->phpThumbDebug = (null === $this->phpThumbDebug ? 9 : max(1, (int) $this->phpThumbDebug));
  214. }
  215. }
  216. public function __destruct() {
  217. $this->purgeTempFiles();
  218. }
  219. // public:
  220. public function purgeTempFiles() {
  221. foreach ($this->tempFilesToDelete as $tempFileToDelete) {
  222. if (file_exists($tempFileToDelete)) {
  223. $this->DebugMessage('Deleting temp file "'.$tempFileToDelete.'"', __FILE__, __LINE__);
  224. @unlink($tempFileToDelete);
  225. }
  226. }
  227. $this->tempFilesToDelete = array();
  228. return true;
  229. }
  230. // public:
  231. public function setSourceFilename($sourceFilename) {
  232. //$this->resetObject();
  233. //$this->rawImageData = null;
  234. $this->sourceFilename = $sourceFilename;
  235. $this->src = $sourceFilename;
  236. if (null === $this->config_output_format) {
  237. $sourceFileExtension = strtolower(substr(strrchr($sourceFilename, '.'), 1));
  238. if (preg_match('#^[a-z]{3,4}$#', $sourceFileExtension)) {
  239. $this->config_output_format = $sourceFileExtension;
  240. $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->config_output_format to "'.$sourceFileExtension.'"', __FILE__, __LINE__);
  241. } else {
  242. $this->DebugMessage('setSourceFilename('.$sourceFilename.') did NOT set $this->config_output_format to "'.$sourceFileExtension.'" because it did not seem like an appropriate image format', __FILE__, __LINE__);
  243. }
  244. }
  245. $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->sourceFilename to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
  246. return true;
  247. }
  248. // public:
  249. public function setSourceData($rawImageData, $sourceFilename='') {
  250. //$this->resetObject();
  251. //$this->sourceFilename = null;
  252. $this->rawImageData = $rawImageData;
  253. $this->DebugMessage('setSourceData() setting $this->rawImageData ('.strlen($this->rawImageData).' bytes; magic="'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).'))', __FILE__, __LINE__);
  254. if ($this->config_cache_source_enabled) {
  255. $sourceFilename = ($sourceFilename ? $sourceFilename : md5($rawImageData));
  256. if (!is_dir($this->config_cache_source_directory)) {
  257. $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not a directory');
  258. } elseif (!@is_writable($this->config_cache_source_directory)) {
  259. $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not writable');
  260. }
  261. $this->DebugMessage('setSourceData() attempting to save source image to "'.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).'"', __FILE__, __LINE__);
  262. if ($fp = @fopen($this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename), 'wb')) {
  263. fwrite($fp, $rawImageData);
  264. fclose($fp);
  265. } elseif (!$this->phpThumbDebug) {
  266. $this->ErrorImage('setSourceData() failed to write to source cache ('.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).')');
  267. }
  268. }
  269. return true;
  270. }
  271. // public:
  272. public function setSourceImageResource($gdimg) {
  273. //$this->resetObject();
  274. $this->gdimg_source = $gdimg;
  275. return true;
  276. }
  277. // public:
  278. public function setParameter($param, $value) {
  279. if ($param == 'src') {
  280. $this->setSourceFilename($this->ResolveFilenameToAbsolute($value));
  281. } elseif (@is_array($this->$param)) {
  282. if (is_array($value)) {
  283. foreach ($value as $arraykey => $arrayvalue) {
  284. array_push($this->$param, $arrayvalue);
  285. }
  286. } else {
  287. array_push($this->$param, $value);
  288. }
  289. } else {
  290. $this->$param = $value;
  291. }
  292. return true;
  293. }
  294. // public:
  295. public function getParameter($param) {
  296. //if (property_exists('phpThumb', $param)) {
  297. return $this->$param;
  298. //}
  299. //$this->DebugMessage('setParameter() attempting to get non-existant parameter "'.$param.'"', __FILE__, __LINE__);
  300. //return false;
  301. }
  302. // public:
  303. public function GenerateThumbnail() {
  304. $this->setOutputFormat();
  305. $this->phpThumbDebug('8a');
  306. $this->ResolveSource();
  307. $this->phpThumbDebug('8b');
  308. $this->SetCacheFilename();
  309. $this->phpThumbDebug('8c');
  310. $this->ExtractEXIFgetImageSize();
  311. $this->phpThumbDebug('8d');
  312. if ($this->useRawIMoutput) {
  313. $this->DebugMessage('Skipping rest of GenerateThumbnail() because ($this->useRawIMoutput == true)', __FILE__, __LINE__);
  314. return true;
  315. }
  316. $this->phpThumbDebug('8e');
  317. if (!$this->SourceImageToGD()) {
  318. $this->DebugMessage('SourceImageToGD() failed', __FILE__, __LINE__);
  319. return false;
  320. }
  321. $this->phpThumbDebug('8f');
  322. $this->Rotate();
  323. $this->phpThumbDebug('8g');
  324. $this->CreateGDoutput();
  325. $this->phpThumbDebug('8h');
  326. // default values, also applicable for far="C"
  327. $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2);
  328. $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2);
  329. if (($this->far == 'L') || ($this->far == 'TL') || ($this->far == 'BL')) {
  330. $destination_offset_x = 0;
  331. }
  332. if (($this->far == 'R') || ($this->far == 'TR') || ($this->far == 'BR')) {
  333. $destination_offset_x = round($this->thumbnail_width - $this->thumbnail_image_width);
  334. }
  335. if (($this->far == 'T') || ($this->far == 'TL') || ($this->far == 'TR')) {
  336. $destination_offset_y = 0;
  337. }
  338. if (($this->far == 'B') || ($this->far == 'BL') || ($this->far == 'BR')) {
  339. $destination_offset_y = round($this->thumbnail_height - $this->thumbnail_image_height);
  340. }
  341. // // copy/resize image to appropriate dimensions
  342. // $borderThickness = 0;
  343. // if (!empty($this->fltr)) {
  344. // foreach ($this->fltr as $key => $value) {
  345. // if (preg_match('#^bord\|([0-9]+)#', $value, $matches)) {
  346. // $borderThickness = $matches[1];
  347. // break;
  348. // }
  349. // }
  350. // }
  351. // if ($borderThickness > 0) {
  352. // //$this->DebugMessage('Skipping ImageResizeFunction() because BorderThickness="'.$borderThickness.'"', __FILE__, __LINE__);
  353. // $this->thumbnail_image_height /= 2;
  354. // }
  355. $this->ImageResizeFunction(
  356. $this->gdimg_output,
  357. $this->gdimg_source,
  358. $destination_offset_x,
  359. $destination_offset_y,
  360. $this->thumbnailCropX,
  361. $this->thumbnailCropY,
  362. $this->thumbnail_image_width,
  363. $this->thumbnail_image_height,
  364. $this->thumbnailCropW,
  365. $this->thumbnailCropH
  366. );
  367. $this->DebugMessage('memory_get_usage() after copy-resize = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__);
  368. imagedestroy($this->gdimg_source);
  369. $this->DebugMessage('memory_get_usage() after imagedestroy = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__);
  370. $this->phpThumbDebug('8i');
  371. $this->AntiOffsiteLinking();
  372. $this->phpThumbDebug('8j');
  373. $this->ApplyFilters();
  374. $this->phpThumbDebug('8k');
  375. $this->AlphaChannelFlatten();
  376. $this->phpThumbDebug('8l');
  377. $this->MaxFileSize();
  378. $this->phpThumbDebug('8m');
  379. $this->DebugMessage('GenerateThumbnail() completed successfully', __FILE__, __LINE__);
  380. return true;
  381. }
  382. // public:
  383. public function RenderOutput() {
  384. if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) {
  385. $this->DebugMessage('RenderOutput() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
  386. return false;
  387. }
  388. if (!$this->thumbnailFormat) {
  389. $this->DebugMessage('RenderOutput() failed because $this->thumbnailFormat is empty', __FILE__, __LINE__);
  390. return false;
  391. }
  392. if ($this->useRawIMoutput) {
  393. $this->DebugMessage('RenderOutput copying $this->IMresizedData ('.strlen($this->IMresizedData).' bytes) to $this->outputImage', __FILE__, __LINE__);
  394. $this->outputImageData = $this->IMresizedData;
  395. return true;
  396. }
  397. $builtin_formats = array();
  398. if (function_exists('imagetypes')) {
  399. $imagetypes = imagetypes();
  400. $builtin_formats['wbmp'] = (bool) ($imagetypes & IMG_WBMP);
  401. $builtin_formats['jpg'] = (bool) ($imagetypes & IMG_JPG);
  402. $builtin_formats['gif'] = (bool) ($imagetypes & IMG_GIF);
  403. $builtin_formats['png'] = (bool) ($imagetypes & IMG_PNG);
  404. if (defined('IMG_WEBP')) {
  405. $builtin_formats['webp'] = (bool) ($imagetypes & IMG_WEBP); // PHP 5.6.25, 7.0.10
  406. }
  407. if (defined('IMG_BMP')) {
  408. $builtin_formats['bmp'] = (bool) ($imagetypes & IMG_BMP); // PHP 7.2.0
  409. }
  410. }
  411. $this->DebugMessage('imageinterlace($this->gdimg_output, '. (int) $this->config_output_interlace .')', __FILE__, __LINE__);
  412. imageinterlace($this->gdimg_output, (int) $this->config_output_interlace);
  413. $this->DebugMessage('RenderOutput() attempting image'.strtolower(@$this->thumbnailFormat).'($this->gdimg_output)', __FILE__, __LINE__);
  414. ob_start();
  415. switch ($this->thumbnailFormat) {
  416. case 'wbmp':
  417. if (empty($builtin_formats['wbmp'])) {
  418. $this->DebugMessage('GD does not have required built-in support for WBMP output', __FILE__, __LINE__);
  419. ob_end_clean();
  420. return false;
  421. }
  422. imagewbmp($this->gdimg_output, null, $this->thumbnailQuality);
  423. $this->outputImageData = ob_get_contents();
  424. break;
  425. case 'jpeg':
  426. case 'jpg': // should be "jpeg" not "jpg" but just in case...
  427. if (empty($builtin_formats['jpg'])) {
  428. $this->DebugMessage('GD does not have required built-in support for JPEG output', __FILE__, __LINE__);
  429. ob_end_clean();
  430. return false;
  431. }
  432. imagejpeg($this->gdimg_output, null, $this->thumbnailQuality);
  433. $this->outputImageData = ob_get_contents();
  434. break;
  435. case 'png':
  436. if (empty($builtin_formats['png'])) {
  437. $this->DebugMessage('GD does not have required built-in support for PNG output', __FILE__, __LINE__);
  438. ob_end_clean();
  439. return false;
  440. }
  441. if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.1.2', '>=')) {
  442. // https://github.com/JamesHeinrich/phpThumb/issues/24
  443. /* http://php.net/manual/en/function.imagepng.php:
  444. from php source (gd.h):
  445. 2.0.12: Compression level: 0-9 or -1, where 0 is NO COMPRESSION at all,
  446. :: 1 is FASTEST but produces larger files, 9 provides the best
  447. :: compression (smallest files) but takes a long time to compress, and
  448. :: -1 selects the default compiled into the zlib library.
  449. Conclusion: Based on the Zlib manual (http://www.zlib.net/manual.html) the default compression level is set to 6.
  450. */
  451. if (($this->thumbnailQuality >= -1) && ($this->thumbnailQuality <= 9)) {
  452. $PNGquality = $this->thumbnailQuality;
  453. } else {
  454. $this->DebugMessage('Specified thumbnailQuality "'.$this->thumbnailQuality.'" is outside the accepted range (0-9, or -1). Using 6 as default value.', __FILE__, __LINE__);
  455. $PNGquality = 6;
  456. }
  457. imagepng($this->gdimg_output, null, $PNGquality);
  458. } else {
  459. imagepng($this->gdimg_output);
  460. }
  461. $this->outputImageData = ob_get_contents();
  462. break;
  463. case 'gif':
  464. if (empty($builtin_formats['gif'])) {
  465. $this->DebugMessage('GD does not have required built-in support for GIF output', __FILE__, __LINE__);
  466. ob_end_clean();
  467. return false;
  468. }
  469. imagegif($this->gdimg_output);
  470. $this->outputImageData = ob_get_contents();
  471. break;
  472. case 'webp':
  473. if (empty($builtin_formats['webp'])) {
  474. $this->DebugMessage('GD does not have required built-in support for WebP output', __FILE__, __LINE__);
  475. ob_end_clean();
  476. return false;
  477. }
  478. imagewebp($this->gdimg_output);
  479. $this->outputImageData = ob_get_contents();
  480. break;
  481. case 'bmp':
  482. if (!empty($builtin_formats['bmp'])) {
  483. imagebmp($this->gdimg_output);
  484. $this->outputImageData = ob_get_contents();
  485. break;
  486. }
  487. $this->DebugMessage('GD does not have required built-in support for BMP output', __FILE__, __LINE__);
  488. if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) {
  489. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__);
  490. ob_end_clean();
  491. return false;
  492. }
  493. $phpthumb_bmp = new phpthumb_bmp();
  494. $this->outputImageData = $phpthumb_bmp->GD2BMPstring($this->gdimg_output);
  495. unset($phpthumb_bmp);
  496. break;
  497. case 'ico':
  498. if (!@include_once __DIR__ .'/phpthumb.ico.php' ) {
  499. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__);
  500. ob_end_clean();
  501. return false;
  502. }
  503. $phpthumb_ico = new phpthumb_ico();
  504. $arrayOfOutputImages = array($this->gdimg_output);
  505. $this->outputImageData = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages);
  506. unset($phpthumb_ico);
  507. break;
  508. default:
  509. $this->DebugMessage('RenderOutput failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
  510. ob_end_clean();
  511. return false;
  512. }
  513. ob_end_clean();
  514. if (!$this->outputImageData) {
  515. $this->DebugMessage('RenderOutput() for "'.$this->thumbnailFormat.'" failed', __FILE__, __LINE__);
  516. ob_end_clean();
  517. return false;
  518. }
  519. $this->DebugMessage('RenderOutput() completing with $this->outputImageData = '.strlen($this->outputImageData).' bytes', __FILE__, __LINE__);
  520. return true;
  521. }
  522. // public:
  523. public function RenderToFile($filename) {
  524. if (preg_match('#^[a-z0-9]+://#i', $filename)) {
  525. $this->DebugMessage('RenderToFile() failed because $filename ('.$filename.') is a URL', __FILE__, __LINE__);
  526. return false;
  527. }
  528. // render thumbnail to this file only, do not cache, do not output to browser
  529. //$renderfilename = $this->ResolveFilenameToAbsolute(dirname($filename)).DIRECTORY_SEPARATOR.basename($filename);
  530. $renderfilename = $filename;
  531. if (($filename{0} != '/') && ($filename{0} != '\\') && ($filename{1} != ':')) {
  532. $renderfilename = $this->ResolveFilenameToAbsolute($renderfilename);
  533. }
  534. if (!@is_writable(dirname($renderfilename))) {
  535. $this->DebugMessage('RenderToFile() failed because "'.dirname($renderfilename).'/" is not writable', __FILE__, __LINE__);
  536. return false;
  537. }
  538. if (@is_file($renderfilename) && !@is_writable($renderfilename)) {
  539. $this->DebugMessage('RenderToFile() failed because "'.$renderfilename.'" is not writable', __FILE__, __LINE__);
  540. return false;
  541. }
  542. if ($this->RenderOutput()) {
  543. if (file_put_contents($renderfilename, $this->outputImageData)) {
  544. @chmod($renderfilename, $this->getParameter('config_file_create_mask'));
  545. $this->DebugMessage('RenderToFile('.$renderfilename.') succeeded', __FILE__, __LINE__);
  546. return true;
  547. }
  548. if (!@file_exists($renderfilename)) {
  549. $this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] did not appear to fail, but the output image does not exist either...', __FILE__, __LINE__);
  550. }
  551. } else {
  552. $this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] failed', __FILE__, __LINE__);
  553. }
  554. return false;
  555. }
  556. // public:
  557. public function OutputThumbnail() {
  558. $this->purgeTempFiles();
  559. if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) {
  560. $this->DebugMessage('OutputThumbnail() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__);
  561. return false;
  562. }
  563. if (headers_sent()) {
  564. return $this->ErrorImage('OutputThumbnail() failed - headers already sent');
  565. }
  566. $downloadfilename = phpthumb_functions::SanitizeFilename(is_string($this->sia) ? $this->sia : ($this->down ? $this->down : 'phpThumb_generated_thumbnail'.'.'.$this->thumbnailFormat));
  567. $this->DebugMessage('Content-Disposition header filename set to "'.$downloadfilename.'"', __FILE__, __LINE__);
  568. if ($downloadfilename) {
  569. header('Content-Disposition: '.($this->down ? 'attachment' : 'inline').'; filename="'.$downloadfilename.'"');
  570. } else {
  571. $this->DebugMessage('failed to send Content-Disposition header because $downloadfilename is empty', __FILE__, __LINE__);
  572. }
  573. if ($this->useRawIMoutput) {
  574. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  575. echo $this->IMresizedData;
  576. } else {
  577. $this->DebugMessage('imageinterlace($this->gdimg_output, '. (int) $this->config_output_interlace .')', __FILE__, __LINE__);
  578. imageinterlace($this->gdimg_output, (int) $this->config_output_interlace);
  579. switch ($this->thumbnailFormat) {
  580. case 'jpeg':
  581. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  582. $ImageOutFunction = 'image'.$this->thumbnailFormat;
  583. @$ImageOutFunction($this->gdimg_output, null, $this->thumbnailQuality);
  584. break;
  585. case 'png':
  586. case 'gif':
  587. case 'webp':
  588. $ImageOutFunction = 'image'.$this->thumbnailFormat;
  589. if (!function_exists($ImageOutFunction)) {
  590. $this->DebugMessage($ImageOutFunction.' is not available', __FILE__, __LINE__);
  591. return false;
  592. }
  593. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  594. @$ImageOutFunction($this->gdimg_output);
  595. break;
  596. case 'bmp':
  597. if (function_exists('imagebmp')) {
  598. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  599. imagebmp($this->gdimg_output);
  600. break;
  601. }
  602. if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) {
  603. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__);
  604. return false;
  605. }
  606. $phpthumb_bmp = new phpthumb_bmp();
  607. if (is_object($phpthumb_bmp)) {
  608. $bmp_data = $phpthumb_bmp->GD2BMPstring($this->gdimg_output);
  609. unset($phpthumb_bmp);
  610. if (!$bmp_data) {
  611. $this->DebugMessage('$phpthumb_bmp->GD2BMPstring() failed', __FILE__, __LINE__);
  612. return false;
  613. }
  614. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  615. echo $bmp_data;
  616. } else {
  617. $this->DebugMessage('new phpthumb_bmp() failed', __FILE__, __LINE__);
  618. return false;
  619. }
  620. break;
  621. case 'ico':
  622. if (!@include_once __DIR__ .'/phpthumb.ico.php' ) {
  623. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__);
  624. return false;
  625. }
  626. $phpthumb_ico = new phpthumb_ico();
  627. if (is_object($phpthumb_ico)) {
  628. $arrayOfOutputImages = array($this->gdimg_output);
  629. $ico_data = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages);
  630. unset($phpthumb_ico);
  631. if (!$ico_data) {
  632. $this->DebugMessage('$phpthumb_ico->GD2ICOstring() failed', __FILE__, __LINE__);
  633. return false;
  634. }
  635. header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat));
  636. echo $ico_data;
  637. } else {
  638. $this->DebugMessage('new phpthumb_ico() failed', __FILE__, __LINE__);
  639. return false;
  640. }
  641. break;
  642. default:
  643. $this->DebugMessage('OutputThumbnail failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__);
  644. return false;
  645. break;
  646. }
  647. }
  648. return true;
  649. }
  650. // public:
  651. public function CleanUpCacheDirectory() {
  652. $this->DebugMessage('CleanUpCacheDirectory() set to purge ('.(null === $this->config_cache_maxage ? 'NULL' : number_format($this->config_cache_maxage / 86400, 1)).' days; '.(null === $this->config_cache_maxsize ? 'NULL' : number_format($this->config_cache_maxsize / 1048576, 2)).' MB; '.(null === $this->config_cache_maxfiles ? 'NULL' : number_format($this->config_cache_maxfiles)).' files)', __FILE__, __LINE__);
  653. if (!is_writable($this->config_cache_directory)) {
  654. $this->DebugMessage('CleanUpCacheDirectory() skipped because "'.$this->config_cache_directory.'" is not writable', __FILE__, __LINE__);
  655. return true;
  656. }
  657. // cache status of cache directory for 1 hour to avoid hammering the filesystem functions
  658. $phpThumbCacheStats_filename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheStats.txt';
  659. if (file_exists($phpThumbCacheStats_filename) && is_readable($phpThumbCacheStats_filename) && (filemtime($phpThumbCacheStats_filename) >= (time() - 3600))) {
  660. $this->DebugMessage('CleanUpCacheDirectory() skipped because "'.$phpThumbCacheStats_filename.'" is recently modified', __FILE__, __LINE__);
  661. return true;
  662. }
  663. if (!@touch($phpThumbCacheStats_filename)) {
  664. $this->DebugMessage('touch('.$phpThumbCacheStats_filename.') failed', __FILE__, __LINE__);
  665. }
  666. $DeletedKeys = array();
  667. $AllFilesInCacheDirectory = array();
  668. if (($this->config_cache_maxage > 0) || ($this->config_cache_maxsize > 0) || ($this->config_cache_maxfiles > 0)) {
  669. $CacheDirOldFilesAge = array();
  670. $CacheDirOldFilesSize = array();
  671. $AllFilesInCacheDirectory = phpthumb_functions::GetAllFilesInSubfolders($this->config_cache_directory);
  672. foreach ($AllFilesInCacheDirectory as $fullfilename) {
  673. if (preg_match('#'.preg_quote($this->config_cache_prefix).'#i', $fullfilename) && file_exists($fullfilename)) {
  674. $CacheDirOldFilesAge[$fullfilename] = @fileatime($fullfilename);
  675. if ($CacheDirOldFilesAge[$fullfilename] == 0) {
  676. $CacheDirOldFilesAge[$fullfilename] = @filemtime($fullfilename);
  677. }
  678. $CacheDirOldFilesSize[$fullfilename] = @filesize($fullfilename);
  679. }
  680. }
  681. if (empty($CacheDirOldFilesSize)) {
  682. $this->DebugMessage('CleanUpCacheDirectory() skipped because $CacheDirOldFilesSize is empty (phpthumb_functions::GetAllFilesInSubfolders('.$this->config_cache_directory.') found no files)', __FILE__, __LINE__);
  683. return true;
  684. }
  685. $DeletedKeys['zerobyte'] = array();
  686. foreach ($CacheDirOldFilesSize as $fullfilename => $filesize) {
  687. // purge all zero-size files more than an hour old (to prevent trying to delete just-created and/or in-use files)
  688. $cutofftime = time() - 3600;
  689. if (($filesize == 0) && ($CacheDirOldFilesAge[$fullfilename] < $cutofftime)) {
  690. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  691. if (@unlink($fullfilename)) {
  692. $DeletedKeys['zerobyte'][] = $fullfilename;
  693. unset($CacheDirOldFilesSize[$fullfilename]);
  694. unset($CacheDirOldFilesAge[$fullfilename]);
  695. }
  696. }
  697. }
  698. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['zerobyte']).' zero-byte files', __FILE__, __LINE__);
  699. asort($CacheDirOldFilesAge);
  700. if ($this->config_cache_maxfiles > 0) {
  701. $TotalCachedFiles = count($CacheDirOldFilesAge);
  702. $DeletedKeys['maxfiles'] = array();
  703. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  704. if ($TotalCachedFiles > $this->config_cache_maxfiles) {
  705. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  706. if (@unlink($fullfilename)) {
  707. $TotalCachedFiles--;
  708. $DeletedKeys['maxfiles'][] = $fullfilename;
  709. }
  710. } else {
  711. // there are few enough files to keep the rest
  712. break;
  713. }
  714. }
  715. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxfiles']).' files based on (config_cache_maxfiles='.$this->config_cache_maxfiles.')', __FILE__, __LINE__);
  716. foreach ($DeletedKeys['maxfiles'] as $fullfilename) {
  717. unset($CacheDirOldFilesAge[$fullfilename]);
  718. unset($CacheDirOldFilesSize[$fullfilename]);
  719. }
  720. }
  721. if ($this->config_cache_maxage > 0) {
  722. $mindate = time() - $this->config_cache_maxage;
  723. $DeletedKeys['maxage'] = array();
  724. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  725. if ($filedate > 0) {
  726. if ($filedate < $mindate) {
  727. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  728. if (@unlink($fullfilename)) {
  729. $DeletedKeys['maxage'][] = $fullfilename;
  730. }
  731. } else {
  732. // the rest of the files are new enough to keep
  733. break;
  734. }
  735. }
  736. }
  737. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxage']).' files based on (config_cache_maxage='.$this->config_cache_maxage.')', __FILE__, __LINE__);
  738. foreach ($DeletedKeys['maxage'] as $fullfilename) {
  739. unset($CacheDirOldFilesAge[$fullfilename]);
  740. unset($CacheDirOldFilesSize[$fullfilename]);
  741. }
  742. }
  743. if ($this->config_cache_maxsize > 0) {
  744. $TotalCachedFileSize = array_sum($CacheDirOldFilesSize);
  745. $DeletedKeys['maxsize'] = array();
  746. foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) {
  747. if ($TotalCachedFileSize > $this->config_cache_maxsize) {
  748. $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__);
  749. if (@unlink($fullfilename)) {
  750. $TotalCachedFileSize -= $CacheDirOldFilesSize[$fullfilename];
  751. $DeletedKeys['maxsize'][] = $fullfilename;
  752. }
  753. } else {
  754. // the total filesizes are small enough to keep the rest of the files
  755. break;
  756. }
  757. }
  758. $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxsize']).' files based on (config_cache_maxsize='.$this->config_cache_maxsize.')', __FILE__, __LINE__);
  759. foreach ($DeletedKeys['maxsize'] as $fullfilename) {
  760. unset($CacheDirOldFilesAge[$fullfilename]);
  761. unset($CacheDirOldFilesSize[$fullfilename]);
  762. }
  763. }
  764. } else {
  765. $this->DebugMessage('skipping CleanUpCacheDirectory() because config set to not use it', __FILE__, __LINE__);
  766. }
  767. $totalpurged = 0;
  768. foreach ($DeletedKeys as $key => $value) {
  769. $totalpurged += count($value);
  770. }
  771. $this->DebugMessage('CleanUpCacheDirectory() purged '.$totalpurged.' files (from '.count($AllFilesInCacheDirectory).') based on config settings', __FILE__, __LINE__);
  772. if ($totalpurged > 0) {
  773. $empty_dirs = array();
  774. foreach ($AllFilesInCacheDirectory as $fullfilename) {
  775. if (is_dir($fullfilename)) {
  776. $empty_dirs[$this->realPathSafe($fullfilename)] = 1;
  777. } else {
  778. unset($empty_dirs[$this->realPathSafe(dirname($fullfilename))]);
  779. }
  780. }
  781. krsort($empty_dirs);
  782. $totalpurgeddirs = 0;
  783. foreach ($empty_dirs as $empty_dir => $dummy) {
  784. if ($empty_dir == $this->config_cache_directory) {
  785. // shouldn't happen, but just in case, don't let it delete actual cache directory
  786. continue;
  787. } elseif (@rmdir($empty_dir)) {
  788. $totalpurgeddirs++;
  789. } else {
  790. $this->DebugMessage('failed to rmdir('.$empty_dir.')', __FILE__, __LINE__);
  791. }
  792. }
  793. $this->DebugMessage('purged '.$totalpurgeddirs.' empty directories', __FILE__, __LINE__);
  794. }
  795. return true;
  796. }
  797. //////////////////////////////////////////////////////////////////////
  798. // private: re-initializator (call between rendering multiple images with one object)
  799. public function resetObject() {
  800. $class_vars = get_class_vars(get_class($this));
  801. foreach ($class_vars as $key => $value) {
  802. // do not clobber debug or config info
  803. if (!preg_match('#^(config_|debug|fatalerror)#i', $key)) {
  804. $this->$key = $value;
  805. }
  806. }
  807. $this->phpThumb(); // re-initialize some class variables
  808. return true;
  809. }
  810. //////////////////////////////////////////////////////////////////////
  811. public function ResolveSource() {
  812. if (is_resource($this->gdimg_source)) {
  813. $this->DebugMessage('ResolveSource() exiting because is_resource($this->gdimg_source)', __FILE__, __LINE__);
  814. return true;
  815. }
  816. if ($this->rawImageData) {
  817. $this->sourceFilename = null;
  818. $this->DebugMessage('ResolveSource() exiting because $this->rawImageData is set ('.number_format(strlen($this->rawImageData)).' bytes)', __FILE__, __LINE__);
  819. return true;
  820. }
  821. if ($this->sourceFilename) {
  822. $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->sourceFilename);
  823. $this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'"', __FILE__, __LINE__);
  824. } elseif ($this->src) {
  825. $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src);
  826. $this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'" from $this->src ('.$this->src.')', __FILE__, __LINE__);
  827. } else {
  828. return $this->ErrorImage('$this->sourceFilename and $this->src are both empty');
  829. }
  830. if ($this->iswindows && ((substr($this->sourceFilename, 0, 2) == '//') || (substr($this->sourceFilename, 0, 2) == '\\\\'))) {
  831. // Windows \\share\filename.ext
  832. } elseif (preg_match('#^[a-z0-9]+://#i', $this->sourceFilename, $protocol_matches)) {
  833. if (preg_match('#^(f|ht)tps?\://#i', $this->sourceFilename)) {
  834. // URL
  835. if ($this->config_http_user_agent) {
  836. ini_set('user_agent', $this->config_http_user_agent);
  837. }
  838. } else {
  839. return $this->ErrorImage('only FTP and HTTP/HTTPS protocols are allowed, "'.$protocol_matches[1].'" is not');
  840. }
  841. } elseif (!@file_exists($this->sourceFilename)) {
  842. return $this->ErrorImage('"'.$this->sourceFilename.'" does not exist');
  843. } elseif (!@is_file($this->sourceFilename)) {
  844. return $this->ErrorImage('"'.$this->sourceFilename.'" is not a file');
  845. }
  846. return true;
  847. }
  848. public function setOutputFormat() {
  849. static $alreadyCalled = false;
  850. if ($this->thumbnailFormat && $alreadyCalled) {
  851. return true;
  852. }
  853. $alreadyCalled = true;
  854. $AvailableImageOutputFormats = array();
  855. $AvailableImageOutputFormats[] = 'text';
  856. if (@is_readable( __DIR__ .'/phpthumb.ico.php')) {
  857. $AvailableImageOutputFormats[] = 'ico';
  858. }
  859. if (@is_readable( __DIR__ .'/phpthumb.bmp.php')) {
  860. $AvailableImageOutputFormats[] = 'bmp';
  861. }
  862. $this->thumbnailFormat = 'ico';
  863. // Set default output format based on what image types are available
  864. if (function_exists('imagetypes')) {
  865. $imagetypes = imagetypes();
  866. if ($imagetypes & IMG_WBMP) {
  867. $this->thumbnailFormat = 'wbmp';
  868. $AvailableImageOutputFormats[] = 'wbmp';
  869. }
  870. if ($imagetypes & IMG_GIF) {
  871. $this->thumbnailFormat = 'gif';
  872. $AvailableImageOutputFormats[] = 'gif';
  873. }
  874. if ($imagetypes & IMG_PNG) {
  875. $this->thumbnailFormat = 'png';
  876. $AvailableImageOutputFormats[] = 'png';
  877. }
  878. if ($imagetypes & IMG_JPG) {
  879. $this->thumbnailFormat = 'jpeg';
  880. $AvailableImageOutputFormats[] = 'jpeg';
  881. }
  882. if ($imagetypes & IMG_WEBP) {
  883. $this->thumbnailFormat = 'webp';
  884. $AvailableImageOutputFormats[] = 'webp';
  885. }
  886. } else {
  887. $this->DebugMessage('imagetypes() does not exist - GD support might not be enabled?', __FILE__, __LINE__);
  888. }
  889. if ($this->ImageMagickVersion()) {
  890. $IMformats = array('jpeg', 'png', 'gif', 'bmp', 'ico', 'wbmp', 'webp');
  891. $this->DebugMessage('Addding ImageMagick formats to $AvailableImageOutputFormats ('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
  892. foreach ($IMformats as $key => $format) {
  893. $AvailableImageOutputFormats[] = $format;
  894. }
  895. }
  896. $AvailableImageOutputFormats = array_unique($AvailableImageOutputFormats);
  897. $this->DebugMessage('$AvailableImageOutputFormats = array('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__);
  898. $this->f = preg_replace('#[^a-z]#', '', strtolower($this->f));
  899. if (strtolower($this->config_output_format) == 'jpg') {
  900. $this->config_output_format = 'jpeg';
  901. }
  902. if (strtolower($this->f) == 'jpg') {
  903. $this->f = 'jpeg';
  904. }
  905. if (phpthumb_functions::CaseInsensitiveInArray($this->config_output_format, $AvailableImageOutputFormats)) {
  906. // set output format to config default if that format is available
  907. $this->DebugMessage('$this->thumbnailFormat set to $this->config_output_format "'.strtolower($this->config_output_format).'"', __FILE__, __LINE__);
  908. $this->thumbnailFormat = strtolower($this->config_output_format);
  909. } elseif ($this->config_output_format) {
  910. $this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->config_output_format ('.strtolower($this->config_output_format).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
  911. }
  912. if ($this->f && phpthumb_functions::CaseInsensitiveInArray($this->f, $AvailableImageOutputFormats) ) {
  913. // override output format if $this->f is set and that format is available
  914. $this->DebugMessage('$this->thumbnailFormat set to $this->f "'.strtolower($this->f).'"', __FILE__, __LINE__);
  915. $this->thumbnailFormat = strtolower($this->f);
  916. } elseif ($this->f) {
  917. $this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->f ('.strtolower($this->f).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__);
  918. }
  919. // for JPEG images, quality 1 (worst) to 99 (best)
  920. // quality < 25 is nasty, with not much size savings - not recommended
  921. // problems with 100 - invalid JPEG?
  922. $this->thumbnailQuality = max(1, min(99, ($this->q ? (int) $this->q : 75)));
  923. $this->DebugMessage('$this->thumbnailQuality set to "'.$this->thumbnailQuality.'"', __FILE__, __LINE__);
  924. return true;
  925. }
  926. public function setCacheDirectory() {
  927. // resolve cache directory to absolute pathname
  928. $this->DebugMessage('setCacheDirectory() starting with config_cache_directory = "'.$this->config_cache_directory.'"', __FILE__, __LINE__);
  929. if ($this->config_cache_directory[ 0 ] == '.') {
  930. if (preg_match('#^(f|ht)tps?\://#i', $this->src)) {
  931. if (!$this->config_cache_disable_warning) {
  932. $this->ErrorImage('$this->config_cache_directory ('.$this->config_cache_directory.') cannot be used for remote images. Adjust "cache_directory" or "cache_disable_warning" in phpThumb.config.php');
  933. }
  934. } elseif ($this->src) {
  935. // resolve relative cache directory to source image
  936. $this->config_cache_directory = dirname($this->ResolveFilenameToAbsolute($this->src)).DIRECTORY_SEPARATOR.$this->config_cache_directory;
  937. } else {
  938. // $this->new is probably set
  939. }
  940. }
  941. if (substr($this->config_cache_directory, -1) == '/') {
  942. $this->config_cache_directory = substr($this->config_cache_directory, 0, -1);
  943. }
  944. if ($this->iswindows) {
  945. $this->config_cache_directory = str_replace('/', DIRECTORY_SEPARATOR, $this->config_cache_directory);
  946. }
  947. if ($this->config_cache_directory) {
  948. $real_cache_path = $this->realPathSafe($this->config_cache_directory);
  949. if (!$real_cache_path) {
  950. $this->DebugMessage('$this->realPathSafe($this->config_cache_directory) failed for "'.$this->config_cache_directory.'"', __FILE__, __LINE__);
  951. if (!is_dir($this->config_cache_directory)) {
  952. $this->DebugMessage('!is_dir('.$this->config_cache_directory.')', __FILE__, __LINE__);
  953. }
  954. }
  955. if ($real_cache_path) {
  956. $this->DebugMessage('setting config_cache_directory to $this->realPathSafe('.$this->config_cache_directory.') = "'.$real_cache_path.'"', __FILE__, __LINE__);
  957. $this->config_cache_directory = $real_cache_path;
  958. }
  959. }
  960. if (!is_dir($this->config_cache_directory)) {
  961. if (!$this->config_cache_disable_warning) {
  962. $this->ErrorImage('$this->config_cache_directory ('.$this->config_cache_directory.') does not exist. Adjust "cache_directory" or "cache_disable_warning" in phpThumb.config.php');
  963. }
  964. $this->DebugMessage('$this->config_cache_directory ('.$this->config_cache_directory.') is not a directory', __FILE__, __LINE__);
  965. $this->config_cache_directory = null;
  966. } elseif (!@is_writable($this->config_cache_directory)) {
  967. $this->DebugMessage('$this->config_cache_directory is not writable ('.$this->config_cache_directory.')', __FILE__, __LINE__);
  968. }
  969. $this->InitializeTempDirSetting();
  970. if (!@is_dir($this->config_temp_directory) && !@is_writable($this->config_temp_directory) && @is_dir($this->config_cache_directory) && @is_writable($this->config_cache_directory)) {
  971. $this->DebugMessage('setting $this->config_temp_directory = $this->config_cache_directory ('.$this->config_cache_directory.')', __FILE__, __LINE__);
  972. $this->config_temp_directory = $this->config_cache_directory;
  973. }
  974. return true;
  975. }
  976. /* Takes the array of path segments up to now, and the next segment (maybe a modifier: empty, . or ..)
  977. Applies it, adding or removing from $segments as a result. Returns nothing. */
  978. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  979. public function applyPathSegment(&$segments, $segment) {
  980. if ($segment == '.') {
  981. return; // always remove
  982. }
  983. if ($segment == '') {
  984. $test = array_pop($segments);
  985. if (null === $test) {
  986. $segments[] = $segment; // keep the first empty block
  987. } elseif ($test == '') {
  988. $test = array_pop($segments);
  989. if (null === $test) {
  990. $segments[] = $test;
  991. $segments[] = $segment; // keep the second one too
  992. } else { // put both back and ignore segment
  993. $segments[] = $test;
  994. $segments[] = $test;
  995. }
  996. } else {
  997. $segments[] = $test; // ignore empty blocks
  998. }
  999. } else {
  1000. if ($segment == '..') {
  1001. $test = array_pop($segments);
  1002. if (null === $test) {
  1003. $segments[] = $segment;
  1004. } elseif ($test == '..') {
  1005. $segments[] = $test;
  1006. $segments[] = $segment;
  1007. } else {
  1008. if ($test == '') {
  1009. $segments[] = $test;
  1010. } // else nothing, remove both
  1011. }
  1012. } else {
  1013. $segments[] = $segment;
  1014. }
  1015. }
  1016. }
  1017. /* Takes array of path components, normalizes it: removes empty slots and '.', collapses '..' and folder names. Returns array. */
  1018. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  1019. public function normalizePath($segments) {
  1020. $parts = array();
  1021. foreach ($segments as $segment) {
  1022. $this->applyPathSegment($parts, $segment);
  1023. }
  1024. return $parts;
  1025. }
  1026. /* True if the provided path points (without resolving symbolic links) into one of the allowed directories. */
  1027. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  1028. public function matchPath($path, $allowed_dirs) {
  1029. if (!empty($allowed_dirs)) {
  1030. foreach ($allowed_dirs as $one_dir) {
  1031. if (preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $this->realPathSafe($one_dir))).'#', $path)) {
  1032. return true;
  1033. }
  1034. }
  1035. }
  1036. return false;
  1037. }
  1038. /* True if the provided path points inside one of open_basedirs (or if open_basedirs are disabled) */
  1039. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  1040. public function isInOpenBasedir($path) {
  1041. static $open_basedirs = null;
  1042. if (null === $open_basedirs) {
  1043. $ini_text = ini_get('open_basedir');
  1044. $this->DebugMessage('open_basedir: "'.$ini_text.'"', __FILE__, __LINE__);
  1045. $open_basedirs = array();
  1046. if (strlen($ini_text) > 0) {
  1047. foreach (preg_split('#[;:]#', $ini_text) as $key => $value) {
  1048. $open_basedirs[$key] = $this->realPathSafe($value);
  1049. }
  1050. }
  1051. }
  1052. return (empty($open_basedirs) || $this->matchPath($path, $open_basedirs));
  1053. }
  1054. /* Resolves all symlinks in $path, checking that each continuous part ends in an allowed zone. Returns null, if any component leads outside of allowed zone. */
  1055. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  1056. public function resolvePath($path, $allowed_dirs) {
  1057. $this->DebugMessage('resolvePath: '.$path.' (allowed_dirs: '.print_r($allowed_dirs, true).')', __FILE__, __LINE__);
  1058. // add base path to the top of the list
  1059. if (!$this->config_allow_src_above_docroot) {
  1060. array_unshift($allowed_dirs, $this->realPathSafe($this->config_document_root));
  1061. } else {
  1062. if (!$this->config_allow_src_above_phpthumb) {
  1063. array_unshift($allowed_dirs, $this->realPathSafe( __DIR__ ));
  1064. } else {
  1065. // no checks are needed, offload the work to realpath and forget about it
  1066. $this->DebugMessage('resolvePath: checks disabled, returning '.$this->realPathSafe($path), __FILE__, __LINE__);
  1067. return $this->realPathSafe($path);
  1068. }
  1069. }
  1070. if ($path == '') {
  1071. return null; // save us trouble
  1072. }
  1073. do {
  1074. $this->DebugMessage('resolvePath: iteration, path='.$path.', base path = '.$allowed_dirs[0], __FILE__, __LINE__);
  1075. $parts = array();
  1076. // do not use "cleaner" foreach version of this loop as later code relies on both $segments and $i
  1077. // http://support.silisoftware.com/phpBB3/viewtopic.php?t=964
  1078. $segments = explode(DIRECTORY_SEPARATOR, $path);
  1079. for ($i = 0, $iMax = count($segments); $i < $iMax; $i++) {
  1080. $this->applyPathSegment($parts, $segments[$i]);
  1081. $thispart = implode(DIRECTORY_SEPARATOR, $parts);
  1082. if ($this->isInOpenBasedir($thispart)) {
  1083. if (is_link($thispart)) {
  1084. break;
  1085. }
  1086. }
  1087. }
  1088. $this->DebugMessage('resolvePath: stop at component '.$i, __FILE__, __LINE__);
  1089. // test the part up to here
  1090. $path = implode(DIRECTORY_SEPARATOR, $parts);
  1091. $this->DebugMessage('resolvePath: stop at path='.$path, __FILE__, __LINE__);
  1092. if (!$this->matchPath($path, $allowed_dirs)) {
  1093. $this->DebugMessage('resolvePath: no match, returning null', __FILE__, __LINE__);
  1094. return null;
  1095. }
  1096. if ($i >= count($segments)) { // reached end
  1097. $this->DebugMessage('resolvePath: path parsed, over', __FILE__, __LINE__);
  1098. break;
  1099. }
  1100. // else it's symlink, rewrite path
  1101. $path = readlink($path);
  1102. $this->DebugMessage('resolvePath: symlink matched, target='.$path, __FILE__, __LINE__);
  1103. /*
  1104. Replace base path with symlink target.
  1105. Assuming:
  1106. /www/img/external -> /external
  1107. This is allowed:
  1108. GET /www/img/external/../external/test/pic.jpg
  1109. This isn't:
  1110. GET /www/img/external/../www/img/pic.jpg
  1111. So there's only one base path which is the last symlink target, but any number of stable whitelisted paths.
  1112. */
  1113. if ($this->config_auto_allow_symlinks) {
  1114. $allowed_dirs[0] = $path;
  1115. }
  1116. $path = $path.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, array_slice($segments,$i + 1));
  1117. } while (true);
  1118. return $path;
  1119. }
  1120. public function realPathSafe($filename) {
  1121. // http://php.net/manual/en/function.realpath.php -- "Note: The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE"
  1122. // realPathSafe() provides a reasonable facsimile of realpath() but does not resolve symbolic links, nor does it check that the file/path actually exists
  1123. if (!$this->config_disable_realpath) {
  1124. return realpath($filename);
  1125. }
  1126. // http://stackoverflow.com/questions/21421569
  1127. $newfilename = preg_replace('#[\\/]+#', DIRECTORY_SEPARATOR, $filename);
  1128. if (!preg_match('#^'.DIRECTORY_SEPARATOR.'#', $newfilename)) {
  1129. $newfilename = __DIR__ .DIRECTORY_SEPARATOR.$newfilename;
  1130. }
  1131. do {
  1132. $beforeloop = $newfilename;
  1133. // Replace all sequences of more than one / with a single one [[ If you're working on a system that treats // at the start of a path as special, make sure you replace multiple / characters at the start with two of them. This is the only place where POSIX allows (but does not mandate) special handling for multiples, in all other cases, multiple / characters are equivalent to a single one.]]
  1134. $newfilename = preg_replace('#'.DIRECTORY_SEPARATOR.'+#', DIRECTORY_SEPARATOR, $newfilename);
  1135. // Replace all occurrences of /./ with /
  1136. $newfilename = preg_replace('#'.DIRECTORY_SEPARATOR.'\\.'.DIRECTORY_SEPARATOR.'#', DIRECTORY_SEPARATOR, $newfilename);
  1137. // Remove ./ if at the start
  1138. $newfilename = preg_replace('#^\\.'.DIRECTORY_SEPARATOR.'#', '', $newfilename);
  1139. // Remove /. if at the end
  1140. $newfilename = preg_replace('#'.DIRECTORY_SEPARATOR.'\\.$#', '', $newfilename);
  1141. // Replace /anything/../ with /
  1142. $newfilename = preg_replace('#'.DIRECTORY_SEPARATOR.'[^'.DIRECTORY_SEPARATOR.']+'.DIRECTORY_SEPARATOR.'\\.\\.'.DIRECTORY_SEPARATOR.'#', DIRECTORY_SEPARATOR, $newfilename);
  1143. // Remove /anything/.. if at the end
  1144. $newfilename = preg_replace('#'.DIRECTORY_SEPARATOR.'[^'.DIRECTORY_SEPARATOR.']+'.DIRECTORY_SEPARATOR.'\\.\\.$#', '', $newfilename);
  1145. } while ($newfilename != $beforeloop);
  1146. return $newfilename;
  1147. }
  1148. public function ResolveFilenameToAbsolute($filename) {
  1149. if (empty($filename)) {
  1150. return false;
  1151. }
  1152. if (preg_match('#^[a-z0-9]+\:/{1,2}#i', $filename)) {
  1153. // eg: http://host/path/file.jpg (HTTP URL)
  1154. // eg: ftp://host/path/file.jpg (FTP URL)
  1155. // eg: data1:/path/file.jpg (Netware path)
  1156. //$AbsoluteFilename = $filename;
  1157. return $filename;
  1158. } elseif ($this->iswindows && isset($filename{1}) && ($filename{1} == ':')) {
  1159. // absolute pathname (Windows)
  1160. $AbsoluteFilename = $filename;
  1161. } elseif ($this->iswindows && ((substr($filename, 0, 2) == '//') || (substr($filename, 0, 2) == '\\\\'))) {
  1162. // absolute pathname (Windows)
  1163. $AbsoluteFilename = $filename;
  1164. } elseif ($filename{0} == '/') {
  1165. if (@is_readable($filename) && !@is_readable($this->config_document_root.$filename)) {
  1166. // absolute filename (*nix)
  1167. $AbsoluteFilename = $filename;
  1168. } elseif (isset($filename{1}) && ($filename{1} == '~')) {
  1169. // /~user/path
  1170. if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray($filename)) {
  1171. $AbsoluteFilename = $ApacheLookupURIarray['filename'];
  1172. } else {
  1173. $AbsoluteFilename = $this->realPathSafe($filename);
  1174. if (@is_readable($AbsoluteFilename)) {
  1175. $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.$filename.'", but the correct filename ('.$AbsoluteFilename.') seems to have been resolved with $this->realPathSafe($filename)', __FILE__, __LINE__);
  1176. } elseif (is_dir(dirname($AbsoluteFilename))) {
  1177. $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname($filename).'", but the correct directory ('.dirname($AbsoluteFilename).') seems to have been resolved with $this->realPathSafe(.)', __FILE__, __LINE__);
  1178. } else {
  1179. return $this->ErrorImage('phpthumb_functions::ApacheLookupURIarray() failed for "'.$filename.'". This has been known to fail on Apache2 - try using the absolute filename for the source image (ex: "/home/user/httpdocs/image.jpg" instead of "/~user/image.jpg")');
  1180. }
  1181. }
  1182. } else {
  1183. // relative filename (any OS)
  1184. if (preg_match('#^'.preg_quote($this->config_document_root).'#', $filename)) {
  1185. $AbsoluteFilename = $filename;
  1186. $this->DebugMessage('ResolveFilenameToAbsolute() NOT prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__);
  1187. } else {
  1188. $AbsoluteFilename = $this->config_document_root.$filename;
  1189. $this->DebugMessage('ResolveFilenameToAbsolute() prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__);
  1190. }
  1191. }
  1192. } else {
  1193. // relative to current directory (any OS)
  1194. $AbsoluteFilename = __DIR__ .DIRECTORY_SEPARATOR.preg_replace('#[/\\\\]#', DIRECTORY_SEPARATOR, $filename);
  1195. if (substr(dirname(@$_SERVER['PHP_SELF']), 0, 2) == '/~') {
  1196. if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray(dirname(@$_SERVER['PHP_SELF']))) {
  1197. $AbsoluteFilename = $ApacheLookupURIarray['filename'].DIRECTORY_SEPARATOR.$filename;
  1198. } else {
  1199. $AbsoluteFilename = $this->realPathSafe('.').DIRECTORY_SEPARATOR.$filename;
  1200. if (@is_readable($AbsoluteFilename)) {
  1201. $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'", but the correct filename ('.$AbsoluteFilename.') seems to have been resolved with $this->realPathSafe(.)/$filename', __FILE__, __LINE__);
  1202. } elseif (is_dir(dirname($AbsoluteFilename))) {
  1203. $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'", but the correct directory ('.dirname($AbsoluteFilename).') seems to have been resolved with $this->realPathSafe(.)', __FILE__, __LINE__);
  1204. } else {
  1205. return $this->ErrorImage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'". This has been known to fail on Apache2 - try using the absolute filename for the source image');
  1206. }
  1207. }
  1208. }
  1209. }
  1210. /*
  1211. // removed 2014-May-30: http://support.silisoftware.com/phpBB3/viewtopic.php?t=961
  1212. if (is_link($AbsoluteFilename)) {
  1213. $this->DebugMessage('is_link()==true, changing "'.$AbsoluteFilename.'" to "'.readlink($AbsoluteFilename).'"', __FILE__, __LINE__);
  1214. $AbsoluteFilename = readlink($AbsoluteFilename);
  1215. }
  1216. if ($this->realPathSafe($AbsoluteFilename)) {
  1217. $AbsoluteFilename = $this->realPathSafe($AbsoluteFilename);
  1218. }
  1219. */
  1220. if ($this->iswindows) {
  1221. $AbsoluteFilename = preg_replace('#^'.preg_quote($this->realPathSafe($this->config_document_root)).'#i', str_replace('\\', '\\\\', $this->realPathSafe($this->config_document_root)), $AbsoluteFilename);
  1222. $AbsoluteFilename = str_replace(DIRECTORY_SEPARATOR, '/', $AbsoluteFilename);
  1223. }
  1224. $resolvedAbsoluteFilename = $this->resolvePath($AbsoluteFilename, $this->config_additional_allowed_dirs);
  1225. if (!$this->config_allow_src_above_docroot && !preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $this->realPathSafe($this->config_document_root))).'#', $resolvedAbsoluteFilename)) {
  1226. $this->DebugMessage('!$this->config_allow_src_above_docroot therefore setting "'.$AbsoluteFilename.'" (outside "'.$this->realPathSafe($this->config_document_root).'") to null', __FILE__, __LINE__);
  1227. return false;
  1228. }
  1229. if (!$this->config_allow_src_above_phpthumb && !preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', __DIR__ )).'#', $resolvedAbsoluteFilename)) {
  1230. $this->DebugMessage('!$this->config_allow_src_above_phpthumb therefore setting "'.$AbsoluteFilename.'" (outside "'. __DIR__ .'") to null', __FILE__, __LINE__);
  1231. return false;
  1232. }
  1233. return $resolvedAbsoluteFilename;
  1234. }
  1235. public function file_exists_ignoreopenbasedir($filename, $cached=true) {
  1236. static $open_basedirs = null;
  1237. static $file_exists_cache = array();
  1238. if (!$cached || !isset($file_exists_cache[$filename])) {
  1239. if (null === $open_basedirs) {
  1240. $open_basedirs = preg_split('#[;:]#', ini_get('open_basedir'));
  1241. }
  1242. if (empty($open_basedirs) || in_array(dirname($filename), $open_basedirs)) {
  1243. $file_exists_cache[$filename] = file_exists($filename);
  1244. } elseif ($this->iswindows) {
  1245. $ls_filename = trim(phpthumb_functions::SafeExec('dir /b '.phpthumb_functions::escapeshellarg_replacement($filename)));
  1246. $file_exists_cache[$filename] = ($ls_filename == basename($filename)); // command dir /b return only filename without path
  1247. } else {
  1248. $ls_filename = trim(phpthumb_functions::SafeExec('ls '.phpthumb_functions::escapeshellarg_replacement($filename)));
  1249. $file_exists_cache[$filename] = ($ls_filename == $filename);
  1250. }
  1251. }
  1252. return $file_exists_cache[$filename];
  1253. }
  1254. public function ImageMagickWhichConvert() {
  1255. static $WhichConvert = null;
  1256. if (null === $WhichConvert) {
  1257. if ($this->iswindows) {
  1258. $WhichConvert = false;
  1259. } else {
  1260. $IMwhichConvertCacheFilename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheIMwhichConvert.txt';
  1261. if (($cachedwhichconvertstring = @file_get_contents($IMwhichConvertCacheFilename)) !== false) {
  1262. $WhichConvert = $cachedwhichconvertstring;
  1263. } else {
  1264. $WhichConvert = trim(phpthumb_functions::SafeExec('which convert'));
  1265. @file_put_contents($IMwhichConvertCacheFilename, $WhichConvert);
  1266. @chmod($IMwhichConvertCacheFilename, $this->getParameter('config_file_create_mask'));
  1267. }
  1268. }
  1269. }
  1270. return $WhichConvert;
  1271. }
  1272. public function ImageMagickCommandlineBase() {
  1273. static $commandline = null;
  1274. if (null === $commandline) {
  1275. if ($this->issafemode) {
  1276. $commandline = '';
  1277. return $commandline;
  1278. }
  1279. $IMcommandlineBaseCacheFilename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheIMcommandlineBase.txt';
  1280. if (($commandline = @file_get_contents($IMcommandlineBaseCacheFilename)) !== false) {
  1281. return $commandline;
  1282. }
  1283. $commandline = (null !== $this->config_imagemagick_path ? $this->config_imagemagick_path : '');
  1284. if ($this->config_imagemagick_path && ($this->config_imagemagick_path != $this->realPathSafe($this->config_imagemagick_path))) {
  1285. if (@is_executable($this->realPathSafe($this->config_imagemagick_path))) {
  1286. $this->DebugMessage('Changing $this->config_imagemagick_path ('.$this->config_imagemagick_path.') to $this->realPathSafe($this->config_imagemagick_path) ('.$this->realPathSafe($this->config_imagemagick_path).')', __FILE__, __LINE__);
  1287. $this->config_imagemagick_path = $this->realPathSafe($this->config_imagemagick_path);
  1288. } else {
  1289. $this->DebugMessage('Leaving $this->config_imagemagick_path as ('.$this->config_imagemagick_path.') because !is_execuatable($this->realPathSafe($this->config_imagemagick_path)) ('.$this->realPathSafe($this->config_imagemagick_path).')', __FILE__, __LINE__);
  1290. }
  1291. }
  1292. $this->DebugMessage(' file_exists('.$this->config_imagemagick_path.') = '. (int) (@file_exists($this->config_imagemagick_path)), __FILE__, __LINE__);
  1293. $this->DebugMessage('file_exists_ignoreopenbasedir('.$this->config_imagemagick_path.') = '. (int) $this->file_exists_ignoreopenbasedir($this->config_imagemagick_path), __FILE__, __LINE__);
  1294. $this->DebugMessage(' is_file('.$this->config_imagemagick_path.') = '. (int) (@is_file($this->config_imagemagick_path)), __FILE__, __LINE__);
  1295. $this->DebugMessage(' is_executable('.$this->config_imagemagick_path.') = '. (int) (@is_executable($this->config_imagemagick_path)), __FILE__, __LINE__);
  1296. if ($this->file_exists_ignoreopenbasedir($this->config_imagemagick_path)) {
  1297. $this->DebugMessage('using ImageMagick path from $this->config_imagemagick_path ('.$this->config_imagemagick_path.')', __FILE__, __LINE__);
  1298. if ($this->iswindows) {
  1299. $commandline = substr($this->config_imagemagick_path, 0, 2).' && cd '.phpthumb_functions::escapeshellarg_replacement(str_replace('/', DIRECTORY_SEPARATOR, substr(dirname($this->config_imagemagick_path), 2))).' && '.phpthumb_functions::escapeshellarg_replacement(basename($this->config_imagemagick_path));
  1300. } else {
  1301. $commandline = phpthumb_functions::escapeshellarg_replacement($this->config_imagemagick_path);
  1302. }
  1303. } else {
  1304. $which_convert = $this->ImageMagickWhichConvert();
  1305. $IMversion = $this->ImageMagickVersion();
  1306. if ($which_convert && ($which_convert{0} == '/') && $this->file_exists_ignoreopenbasedir($which_convert)) {
  1307. // `which convert` *should* return the path if "convert" exist, or nothing if it doesn't
  1308. // other things *may* get returned, like "sh: convert: not found" or "no convert in /usr/local/bin /usr/sbin /usr/bin /usr/ccs/bin"
  1309. // so only do this if the value returned exists as a file
  1310. $this->DebugMessage('using ImageMagick path from `which convert` ('.$which_convert.')', __FILE__, __LINE__);
  1311. $commandline = 'convert';
  1312. } elseif ($IMversion) {
  1313. $this->DebugMessage('setting ImageMagick path to $this->config_imagemagick_path ('.$this->config_imagemagick_path.') ['.$IMversion.']', __FILE__, __LINE__);
  1314. $commandline = $this->config_imagemagick_path;
  1315. } else {
  1316. $this->DebugMessage('ImageMagickThumbnailToGD() aborting because cannot find convert in $this->config_imagemagick_path ('.$this->config_imagemagick_path.'), and `which convert` returned ('.$which_convert.')', __FILE__, __LINE__);
  1317. $commandline = '';
  1318. }
  1319. }
  1320. @file_put_contents($IMcommandlineBaseCacheFilename, $commandline);
  1321. @chmod($IMcommandlineBaseCacheFilename, $this->getParameter('config_file_create_mask'));
  1322. }
  1323. return $commandline;
  1324. }
  1325. public function ImageMagickVersion($returnRAW=false) {
  1326. static $versionstring = null;
  1327. if (null === $versionstring) {
  1328. $versionstring = array(0=>false, 1=>false);
  1329. $IMversionCacheFilename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheIMversion.txt';
  1330. if ($cachedversionstring = @file_get_contents($IMversionCacheFilename)) {
  1331. $versionstring = explode("\n", $cachedversionstring, 2);
  1332. $versionstring[0] = ($versionstring[0] ? $versionstring[0] : false); // "false" is stored as an empty string in the cache file
  1333. $versionstring[1] = ($versionstring[1] ? $versionstring[1] : false); // "false" is stored as an empty string in the cache file
  1334. } else {
  1335. $commandline = $this->ImageMagickCommandlineBase();
  1336. $commandline = (null !== $commandline ? $commandline : '');
  1337. if ($commandline) {
  1338. $commandline .= ' --version';
  1339. $this->DebugMessage('ImageMagick version checked with "'.$commandline.'"', __FILE__, __LINE__);
  1340. $versionstring[1] = trim(phpthumb_functions::SafeExec($commandline));
  1341. if (preg_match('#^Version: [^\d]*([ 0-9\\.\\:Q/\\-]+)#i', $versionstring[1], $matches)) {
  1342. $versionstring[0] = trim($matches[1]);
  1343. } else {
  1344. $versionstring[0] = false;
  1345. $this->DebugMessage('ImageMagick did not return recognized version string ('.$versionstring[1].')', __FILE__, __LINE__);
  1346. }
  1347. $this->DebugMessage('ImageMagick convert --version says "'.@$matches[0].'"', __FILE__, __LINE__);
  1348. }
  1349. @file_put_contents($IMversionCacheFilename, $versionstring[0]."\n".$versionstring[1]);
  1350. @chmod($IMversionCacheFilename, $this->getParameter('config_file_create_mask'));
  1351. }
  1352. }
  1353. return $versionstring[ (int) $returnRAW ];
  1354. }
  1355. public function ImageMagickSwitchAvailable($switchname) {
  1356. static $IMoptions = null;
  1357. if (null === $IMoptions) {
  1358. $IMoptions = array();
  1359. $commandline = $this->ImageMagickCommandlineBase();
  1360. if (null !== $commandline) {
  1361. $commandline .= ' -help';
  1362. $IMhelp_lines = explode("\n", phpthumb_functions::SafeExec($commandline));
  1363. foreach ($IMhelp_lines as $line) {
  1364. if (preg_match('#^[\\+\\-]([a-z\\-]+) #', trim($line), $matches)) {
  1365. $IMoptions[$matches[1]] = true;
  1366. }
  1367. }
  1368. }
  1369. }
  1370. if (is_array($switchname)) {
  1371. $allOK = true;
  1372. foreach ($switchname as $key => $value) {
  1373. if (!isset($IMoptions[$value])) {
  1374. $allOK = false;
  1375. break;
  1376. }
  1377. }
  1378. $this->DebugMessage('ImageMagickSwitchAvailable('.implode(';', $switchname).') = '. (int) $allOK .'', __FILE__, __LINE__);
  1379. } else {
  1380. $allOK = isset($IMoptions[$switchname]);
  1381. $this->DebugMessage('ImageMagickSwitchAvailable('.$switchname.') = '. (int) $allOK .'', __FILE__, __LINE__);
  1382. }
  1383. return $allOK;
  1384. }
  1385. public function ImageMagickFormatsList() {
  1386. static $IMformatsList = null;
  1387. if (null === $IMformatsList) {
  1388. $IMformatsList = '';
  1389. $commandline = $this->ImageMagickCommandlineBase();
  1390. if (null !== $commandline) {
  1391. $commandline = dirname($commandline).DIRECTORY_SEPARATOR.str_replace('convert', 'identify', basename($commandline));
  1392. $commandline .= ' -list format';
  1393. $IMformatsList = phpthumb_functions::SafeExec($commandline);
  1394. }
  1395. }
  1396. return $IMformatsList;
  1397. }
  1398. public function SourceDataToTempFile() {
  1399. if ($IMtempSourceFilename = $this->phpThumb_tempnam()) {
  1400. $IMtempSourceFilename = $this->realPathSafe($IMtempSourceFilename);
  1401. ob_start();
  1402. $fp_tempfile = fopen($IMtempSourceFilename, 'wb');
  1403. $tempfile_open_error = ob_get_contents();
  1404. ob_end_clean();
  1405. if ($fp_tempfile) {
  1406. fwrite($fp_tempfile, $this->rawImageData);
  1407. fclose($fp_tempfile);
  1408. @chmod($IMtempSourceFilename, $this->getParameter('config_file_create_mask'));
  1409. $this->sourceFilename = $IMtempSourceFilename;
  1410. $this->DebugMessage('ImageMagickThumbnailToGD() setting $this->sourceFilename to "'.$IMtempSourceFilename.'" from $this->rawImageData ('.strlen($this->rawImageData).' bytes)', __FILE__, __LINE__);
  1411. } else {
  1412. $this->DebugMessage('ImageMagickThumbnailToGD() FAILED setting $this->sourceFilename to "'.$IMtempSourceFilename.'" (failed to open for writing: "'.$tempfile_open_error.'")', __FILE__, __LINE__);
  1413. }
  1414. unset($tempfile_open_error, $IMtempSourceFilename);
  1415. return true;
  1416. }
  1417. $this->DebugMessage('SourceDataToTempFile() FAILED because $this->phpThumb_tempnam() failed', __FILE__, __LINE__);
  1418. return false;
  1419. }
  1420. public function ImageMagickThumbnailToGD() {
  1421. // http://www.imagemagick.org/script/command-line-options.php
  1422. $this->useRawIMoutput = true;
  1423. if (phpthumb_functions::gd_version()) {
  1424. // if GD is not available, must use whatever ImageMagick can output
  1425. // $UnAllowedParameters contains options that can only be processed in GD, not ImageMagick
  1426. // note: 'fltr' *may* need to be processed by GD, but we'll check that in more detail below
  1427. $UnAllowedParameters = array('xto', 'ar', 'bg', 'bc');
  1428. // 'ra' may be part of this list, if not a multiple of 90 degrees
  1429. foreach ($UnAllowedParameters as $parameter) {
  1430. if (isset($this->$parameter)) {
  1431. $this->DebugMessage('$this->useRawIMoutput=false because "'.$parameter.'" is set', __FILE__, __LINE__);
  1432. $this->useRawIMoutput = false;
  1433. break;
  1434. }
  1435. }
  1436. }
  1437. $this->DebugMessage('$this->useRawIMoutput='.($this->useRawIMoutput ? 'true' : 'false').' after checking $UnAllowedParameters', __FILE__, __LINE__);
  1438. $ImageCreateFunction = '';
  1439. $outputFormat = $this->thumbnailFormat;
  1440. if (phpthumb_functions::gd_version()) {
  1441. if ($this->useRawIMoutput) {
  1442. switch ($this->thumbnailFormat) {
  1443. case 'gif':
  1444. $ImageCreateFunction = 'imagecreatefromgif';
  1445. $this->is_alpha = true;
  1446. break;
  1447. case 'png':
  1448. $ImageCreateFunction = 'imagecreatefrompng';
  1449. $this->is_alpha = true;
  1450. break;
  1451. case 'jpg':
  1452. case 'jpeg':
  1453. $ImageCreateFunction = 'imagecreatefromjpeg';
  1454. break;
  1455. case 'webp':
  1456. $ImageCreateFunction = 'imagecreatefromwebp';
  1457. $this->is_alpha = true;
  1458. break;
  1459. default:
  1460. $this->DebugMessage('Forcing output to PNG because $this->thumbnailFormat ('.$this->thumbnailFormat.' is not a GD-supported format)', __FILE__, __LINE__);
  1461. $outputFormat = 'png';
  1462. $ImageCreateFunction = 'imagecreatefrompng';
  1463. $this->is_alpha = true;
  1464. $this->useRawIMoutput = false;
  1465. break;
  1466. }
  1467. if (!function_exists($ImageCreateFunction)) {
  1468. // ImageMagickThumbnailToGD() depends on imagecreatefrompng/imagecreatefromgif
  1469. //$this->DebugMessage('ImageMagickThumbnailToGD() aborting because '.@$ImageCreateFunction.'() is not available', __FILE__, __LINE__);
  1470. $this->useRawIMoutput = true;
  1471. //return false;
  1472. }
  1473. } else {
  1474. $outputFormat = 'png';
  1475. $ImageCreateFunction = 'imagecreatefrompng';
  1476. $this->is_alpha = true;
  1477. $this->useRawIMoutput = false;
  1478. }
  1479. }
  1480. // http://freealter.org/doc_distrib/ImageMagick-5.1.1/www/convert.html
  1481. if (!$this->sourceFilename && $this->rawImageData) {
  1482. $this->SourceDataToTempFile();
  1483. }
  1484. if (!$this->sourceFilename) {
  1485. $this->DebugMessage('ImageMagickThumbnailToGD() aborting because $this->sourceFilename is empty', __FILE__, __LINE__);
  1486. $this->useRawIMoutput = false;
  1487. return false;
  1488. }
  1489. if ($this->issafemode) {
  1490. $this->DebugMessage('ImageMagickThumbnailToGD() aborting because safe_mode is enabled', __FILE__, __LINE__);
  1491. $this->useRawIMoutput = false;
  1492. return false;
  1493. }
  1494. // TO BE FIXED
  1495. //if (true) {
  1496. // $this->DebugMessage('ImageMagickThumbnailToGD() aborting it is broken right now', __FILE__, __LINE__);
  1497. // $this->useRawIMoutput = false;
  1498. // return false;
  1499. //}
  1500. $commandline = $this->ImageMagickCommandlineBase();
  1501. if ($commandline) {
  1502. $commandline .= ' '.phpthumb_functions::escapeshellarg_replacement(preg_replace('#[/\\\\]#', DIRECTORY_SEPARATOR, $this->sourceFilename).(($outputFormat == 'gif') ? '' : '['. (int) $this->sfn .']')); // [0] means first frame of (GIF) animation, can be ignored
  1503. if ($IMtempfilename = $this->phpThumb_tempnam()) {
  1504. $IMtempfilename = $this->realPathSafe($IMtempfilename);
  1505. $IMuseExplicitImageOutputDimensions = false;
  1506. if ($this->ImageMagickSwitchAvailable('thumbnail') && $this->config_imagemagick_use_thumbnail) {
  1507. $IMresizeParameter = 'thumbnail';
  1508. } else {
  1509. $IMresizeParameter = 'resize';
  1510. // some (older? around 2002) versions of IM won't accept "-resize 100x" but require "-resize 100x100"
  1511. $commandline_test = $this->ImageMagickCommandlineBase().' logo: -resize 1x '.phpthumb_functions::escapeshellarg_replacement($IMtempfilename).' 2>&1';
  1512. $IMresult_test = phpthumb_functions::SafeExec($commandline_test);
  1513. $IMuseExplicitImageOutputDimensions = preg_match('#image dimensions are zero#i', $IMresult_test);
  1514. $this->DebugMessage('IMuseExplicitImageOutputDimensions = '. (int) $IMuseExplicitImageOutputDimensions, __FILE__, __LINE__);
  1515. if ($fp_im_temp = @fopen($IMtempfilename, 'wb')) {
  1516. // erase temp image so ImageMagick logo doesn't get output if other processing fails
  1517. fclose($fp_im_temp);
  1518. @chmod($IMtempfilename, $this->getParameter('config_file_create_mask'));
  1519. }
  1520. }
  1521. ob_start();
  1522. $getimagesize = getimagesize($this->sourceFilename);
  1523. $GetImageSizeError = ob_get_contents();
  1524. ob_end_clean();
  1525. if (is_array($getimagesize)) {
  1526. $this->DebugMessage('getimagesize('.$this->sourceFilename.') SUCCEEDED: '.print_r($getimagesize, true), __FILE__, __LINE__);
  1527. } else {
  1528. $this->DebugMessage('getimagesize('.$this->sourceFilename.') FAILED with error "'.$GetImageSizeError.'"', __FILE__, __LINE__);
  1529. }
  1530. if (null !== $this->dpi && $this->ImageMagickSwitchAvailable('density')) {
  1531. // for vector source formats only (WMF, PDF, etc)
  1532. if (is_array($getimagesize) && isset($getimagesize[2]) && ($getimagesize[2] == IMAGETYPE_PNG)) {
  1533. // explicitly exclude PNG from "-flatten" to make sure transparency is preserved
  1534. // https://github.com/JamesHeinrich/phpThumb/issues/65#issuecomment-256454015
  1535. } else {
  1536. $commandline .= ' -flatten';
  1537. $commandline .= ' -density '.phpthumb_functions::escapeshellarg_replacement($this->dpi);
  1538. }
  1539. }
  1540. if (is_array($getimagesize)) {
  1541. $this->DebugMessage('getimagesize('.$this->sourceFilename.') returned [w='.$getimagesize[0].';h='.$getimagesize[1].';f='.$getimagesize[2].']', __FILE__, __LINE__);
  1542. $this->source_width = $getimagesize[0];
  1543. $this->source_height = $getimagesize[1];
  1544. $this->DebugMessage('source dimensions set to '.$this->source_width.'x'.$this->source_height, __FILE__, __LINE__);
  1545. $this->SetOrientationDependantWidthHeight();
  1546. if (!preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat)) {
  1547. // not a transparency-capable format
  1548. $commandline .= ' -background '.phpthumb_functions::escapeshellarg_replacement('#'.($this->bg ? $this->bg : 'FFFFFF'));
  1549. if (!stristr($commandline, ' -flatten')) {
  1550. $commandline .= ' -flatten';
  1551. }
  1552. } else {
  1553. if ($getimagesize[2] == IMAGETYPE_PNG && !$this->bg) {
  1554. $commandline .= ' -background none';
  1555. }
  1556. }
  1557. if ($getimagesize[2] == IMAGETYPE_GIF) {
  1558. $commandline .= ' -coalesce'; // may be needed for animated GIFs
  1559. }
  1560. if ($this->source_width || $this->source_height) {
  1561. if ($this->zc) {
  1562. $borderThickness = 0;
  1563. if (!empty($this->fltr)) {
  1564. foreach ($this->fltr as $key => $value) {
  1565. if (preg_match('#^bord\|([\d]+)#', $value, $matches)) {
  1566. $borderThickness = $matches[1];
  1567. break;
  1568. }
  1569. }
  1570. }
  1571. $wAll = (int) max($this->w, $this->wp, $this->wl, $this->ws) - (2 * $borderThickness);
  1572. $hAll = (int) max($this->h, $this->hp, $this->hl, $this->hs) - (2 * $borderThickness);
  1573. $imAR = $this->source_width / $this->source_height;
  1574. $zcAR = (($wAll && $hAll) ? $wAll / $hAll : 1);
  1575. $side = phpthumb_functions::nonempty_min($this->source_width, $this->source_height, max($wAll, $hAll));
  1576. $sideX = phpthumb_functions::nonempty_min($this->source_width, $wAll, round($hAll * $zcAR));
  1577. $sideY = phpthumb_functions::nonempty_min( $this->source_height, $hAll, round($wAll / $zcAR));
  1578. $thumbnailH = round(max($sideY, ($sideY * $zcAR) / $imAR));
  1579. if ($this->aoe == 1) {
  1580. $commandline .= ' -'.$IMresizeParameter.' "'.$wAll.'x'.$hAll.'^"';
  1581. } else {
  1582. $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(($IMuseExplicitImageOutputDimensions ? $thumbnailH : '').'x'.$thumbnailH);
  1583. }
  1584. switch (strtoupper($this->zc)) {
  1585. case 'T':
  1586. $commandline .= ' -gravity north';
  1587. break;
  1588. case 'B':
  1589. $commandline .= ' -gravity south';
  1590. break;
  1591. case 'L':
  1592. $commandline .= ' -gravity west';
  1593. break;
  1594. case 'R':
  1595. $commandline .= ' -gravity east';
  1596. break;
  1597. case 'TL':
  1598. $commandline .= ' -gravity northwest';
  1599. break;
  1600. case 'TR':
  1601. $commandline .= ' -gravity northeast';
  1602. break;
  1603. case 'BL':
  1604. $commandline .= ' -gravity southwest';
  1605. break;
  1606. case 'BR':
  1607. $commandline .= ' -gravity southeast';
  1608. break;
  1609. case '1':
  1610. case 'C':
  1611. default:
  1612. $commandline .= ' -gravity center';
  1613. break;
  1614. }
  1615. if (($wAll > 0) && ($hAll > 0)) {
  1616. $commandline .= ' -crop '.phpthumb_functions::escapeshellarg_replacement($wAll.'x'.$hAll.'+0+0');
  1617. } else {
  1618. $commandline .= ' -crop '.phpthumb_functions::escapeshellarg_replacement($side.'x'.$side.'+0+0');
  1619. }
  1620. if ($this->ImageMagickSwitchAvailable('repage')) {
  1621. $commandline .= ' +repage';
  1622. } else {
  1623. $this->DebugMessage('Skipping "+repage" because ImageMagick (v'.$this->ImageMagickVersion().') does not support it', __FILE__, __LINE__);
  1624. }
  1625. } elseif ($this->sw || $this->sh || $this->sx || $this->sy) {
  1626. $crop_param = '';
  1627. $crop_param .= ($this->sw ? (($this->sw < 2) ? round($this->sw * $this->source_width) : $this->sw) : $this->source_width);
  1628. $crop_param .= 'x'.($this->sh ? (($this->sh < 2) ? round($this->sh * $this->source_height) : $this->sh) : $this->source_height);
  1629. $crop_param .= '+'.(($this->sx < 2) ? round($this->sx * $this->source_width) : $this->sx);
  1630. $crop_param .= '+'.(($this->sy < 2) ? round($this->sy * $this->source_height) : $this->sy);
  1631. // TO BE FIXED
  1632. // makes 1x1 output
  1633. // http://trainspotted.com/phpThumb/phpThumb.php?src=/content/CNR/47/CNR-4728-LD-L-20110723-898.jpg&w=100&h=100&far=1&f=png&fltr[]=lvl&sx=0.05&sy=0.25&sw=0.92&sh=0.42
  1634. // '/usr/bin/convert' -density 150 -thumbnail 100x100 -contrast-stretch '0.1%' '/var/www/vhosts/trainspotted.com/httpdocs/content/CNR/47/CNR-4728-LD-L-20110723-898.jpg[0]' png:'/var/www/vhosts/trainspotted.com/httpdocs/phpThumb/_cache/pThumbIIUlvj'
  1635. $commandline .= ' -crop '.phpthumb_functions::escapeshellarg_replacement($crop_param);
  1636. // this is broken for aoe=1, but unsure how to fix. Send advice to info@silisoftware.com
  1637. if ($this->w || $this->h) {
  1638. //if ($this->ImageMagickSwitchAvailable('repage')) {
  1639. if (false) {
  1640. // TO BE FIXED
  1641. // newer versions of ImageMagick require -repage <geometry>
  1642. $commandline .= ' -repage';
  1643. } else {
  1644. $this->DebugMessage('Skipping "-repage" because ImageMagick (v'.$this->ImageMagickVersion().') does not support it', __FILE__, __LINE__);
  1645. }
  1646. if ($IMuseExplicitImageOutputDimensions) {
  1647. if ($this->w && !$this->h) {
  1648. $this->h = ceil($this->w / ($this->source_width / $this->source_height));
  1649. } elseif ($this->h && !$this->w) {
  1650. $this->w = ceil($this->h * ($this->source_width / $this->source_height));
  1651. }
  1652. }
  1653. $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($this->w.'x'.$this->h);
  1654. }
  1655. } else {
  1656. if ($this->iar && ((int) $this->w > 0) && ((int) $this->h > 0)) {
  1657. list($nw, $nh) = phpthumb_functions::TranslateWHbyAngle($this->w, $this->h, $this->ra);
  1658. $nw = ((round($nw) != 0) ? round($nw) : '');
  1659. $nh = ((round($nh) != 0) ? round($nh) : '');
  1660. $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($nw.'x'.$nh.'!');
  1661. } elseif ($this->far && ((int) $this->w > 0) && ((int) $this->h > 0)) {
  1662. $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(phpthumb_functions::nonempty_min($this->w, $getimagesize[0]).'x'.phpthumb_functions::nonempty_min($this->h, $getimagesize[1]));
  1663. $commandline .= ' -gravity center';
  1664. $commandline .= ' -background '.phpthumb_functions::escapeshellarg_replacement('#'.$this->bg);
  1665. $commandline .= ' -extent '.phpthumb_functions::escapeshellarg_replacement($this->w.'x'.$this->h);
  1666. } else {
  1667. $this->w = (($this->aoe && $this->w) ? $this->w : ($this->w ? phpthumb_functions::nonempty_min($this->w, $getimagesize[0]) : ''));
  1668. $this->h = (($this->aoe && $this->h) ? $this->h : ($this->h ? phpthumb_functions::nonempty_min($this->h, $getimagesize[1]) : ''));
  1669. if ($this->w || $this->h) {
  1670. if ($IMuseExplicitImageOutputDimensions) {
  1671. if ($this->w && !$this->h) {
  1672. $this->h = ceil($this->w / ($this->source_width / $this->source_height));
  1673. } elseif ($this->h && !$this->w) {
  1674. $this->w = ceil($this->h * ($this->source_width / $this->source_height));
  1675. }
  1676. }
  1677. list($nw, $nh) = phpthumb_functions::TranslateWHbyAngle($this->w, $this->h, $this->ra);
  1678. $nw = ((round($nw) != 0) ? round($nw) : '');
  1679. $nh = ((round($nh) != 0) ? round($nh) : '');
  1680. $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($nw.'x'.$nh);
  1681. }
  1682. }
  1683. }
  1684. }
  1685. } else {
  1686. $this->DebugMessage('getimagesize('.$this->sourceFilename.') failed', __FILE__, __LINE__);
  1687. if ($this->w || $this->h) {
  1688. $exactDimensionsBang = (($this->iar && ((int) $this->w > 0) && ((int) $this->h > 0)) ? '!' : '');
  1689. if ($IMuseExplicitImageOutputDimensions) {
  1690. // unknown source aspect ratio, just put large number and hope IM figures it out
  1691. $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(($this->w ? $this->w : '9999').'x'.($this->h ? $this->h : '9999').$exactDimensionsBang);
  1692. } else {
  1693. $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($this->w.'x'.$this->h.$exactDimensionsBang);
  1694. }
  1695. }
  1696. }
  1697. if ($this->ra) {
  1698. $this->ra = (int) $this->ra;
  1699. if ($this->ImageMagickSwitchAvailable('rotate')) {
  1700. if (!preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat) || phpthumb_functions::version_compare_replacement($this->ImageMagickVersion(), '6.3.7', '>=')) {
  1701. $this->DebugMessage('Using ImageMagick rotate', __FILE__, __LINE__);
  1702. $commandline .= ' -rotate '.phpthumb_functions::escapeshellarg_replacement($this->ra);
  1703. if (($this->ra % 90) != 0) {
  1704. if (preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat)) {
  1705. // alpha-capable format
  1706. $commandline .= ' -background rgba(255,255,255,0)';
  1707. } else {
  1708. $commandline .= ' -background '.phpthumb_functions::escapeshellarg_replacement('#'.($this->bg ? $this->bg : 'FFFFFF'));
  1709. }
  1710. }
  1711. $this->ra = 0;
  1712. } else {
  1713. $this->DebugMessage('Not using ImageMagick rotate because alpha background buggy before v6.3.7', __FILE__, __LINE__);
  1714. }
  1715. } else {
  1716. $this->DebugMessage('Not using ImageMagick rotate because not supported', __FILE__, __LINE__);
  1717. }
  1718. }
  1719. $successfullyProcessedFilters = array();
  1720. foreach ($this->fltr as $filterkey => $filtercommand) {
  1721. @list($command, $parameter) = explode('|', $filtercommand, 2);
  1722. switch ($command) {
  1723. case 'brit':
  1724. if ($this->ImageMagickSwitchAvailable('modulate')) {
  1725. $commandline .= ' -modulate '.phpthumb_functions::escapeshellarg_replacement((100 + (int) $parameter).',100,100');
  1726. $successfullyProcessedFilters[] = $filterkey;
  1727. }
  1728. break;
  1729. case 'cont':
  1730. if ($this->ImageMagickSwitchAvailable('contrast')) {
  1731. $contDiv10 = round((int) $parameter / 10);
  1732. if ($contDiv10 > 0) {
  1733. $contDiv10 = min($contDiv10, 100);
  1734. for ($i = 0; $i < $contDiv10; $i++) {
  1735. $commandline .= ' -contrast'; // increase contrast by 10%
  1736. }
  1737. } elseif ($contDiv10 < 0) {
  1738. $contDiv10 = max($contDiv10, -100);
  1739. for ($i = $contDiv10; $i < 0; $i++) {
  1740. $commandline .= ' +contrast'; // decrease contrast by 10%
  1741. }
  1742. } else {
  1743. // do nothing
  1744. }
  1745. $successfullyProcessedFilters[] = $filterkey;
  1746. }
  1747. break;
  1748. case 'ds':
  1749. if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) {
  1750. if ($parameter == 100) {
  1751. $commandline .= ' -colorspace GRAY';
  1752. $commandline .= ' -modulate 100,0,100';
  1753. } else {
  1754. $commandline .= ' -modulate '.phpthumb_functions::escapeshellarg_replacement('100,'.(100 - (int) $parameter).',100');
  1755. }
  1756. $successfullyProcessedFilters[] = $filterkey;
  1757. }
  1758. break;
  1759. case 'sat':
  1760. if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) {
  1761. if ($parameter == -100) {
  1762. $commandline .= ' -colorspace GRAY';
  1763. $commandline .= ' -modulate 100,0,100';
  1764. } else {
  1765. $commandline .= ' -modulate '.phpthumb_functions::escapeshellarg_replacement('100,'.(100 + (int) $parameter).',100');
  1766. }
  1767. $successfullyProcessedFilters[] = $filterkey;
  1768. }
  1769. break;
  1770. case 'gray':
  1771. if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) {
  1772. $commandline .= ' -colorspace GRAY';
  1773. $commandline .= ' -modulate 100,0,100';
  1774. $successfullyProcessedFilters[] = $filterkey;
  1775. }
  1776. break;
  1777. case 'clr':
  1778. if ($this->ImageMagickSwitchAvailable(array('fill', 'colorize'))) {
  1779. @list($amount, $color) = explode('|', $parameter);
  1780. $commandline .= ' -fill '.phpthumb_functions::escapeshellarg_replacement('#'.preg_replace('#[^0-9A-F]#i', '', $color));
  1781. $commandline .= ' -colorize '.phpthumb_functions::escapeshellarg_replacement(min(max((int) $amount, 0), 100));
  1782. $successfullyProcessedFilters[] = $filterkey;
  1783. }
  1784. break;
  1785. case 'sep':
  1786. if ($this->ImageMagickSwitchAvailable('sepia-tone')) {
  1787. @list($amount, $color) = explode('|', $parameter);
  1788. $amount = ($amount ? $amount : 80);
  1789. if (!$color) {
  1790. $commandline .= ' -sepia-tone '.phpthumb_functions::escapeshellarg_replacement(min(max((int) $amount, 0), 100).'%');
  1791. $successfullyProcessedFilters[] = $filterkey;
  1792. }
  1793. }
  1794. break;
  1795. case 'gam':
  1796. @list($amount) = explode('|', $parameter);
  1797. $amount = min(max((float) $amount, 0.001), 10);
  1798. if (number_format($amount, 3) != '1.000') {
  1799. if ($this->ImageMagickSwitchAvailable('gamma')) {
  1800. $commandline .= ' -gamma '.phpthumb_functions::escapeshellarg_replacement($amount);
  1801. $successfullyProcessedFilters[] = $filterkey;
  1802. }
  1803. }
  1804. break;
  1805. case 'neg':
  1806. if ($this->ImageMagickSwitchAvailable('negate')) {
  1807. $commandline .= ' -negate';
  1808. $successfullyProcessedFilters[] = $filterkey;
  1809. }
  1810. break;
  1811. case 'th':
  1812. @list($amount) = explode('|', $parameter);
  1813. if ($this->ImageMagickSwitchAvailable(array('threshold', 'dither', 'monochrome'))) {
  1814. $commandline .= ' -threshold '.phpthumb_functions::escapeshellarg_replacement(round(min(max((int) $amount, 0), 255) / 2.55).'%');
  1815. $commandline .= ' -dither';
  1816. $commandline .= ' -monochrome';
  1817. $successfullyProcessedFilters[] = $filterkey;
  1818. }
  1819. break;
  1820. case 'rcd':
  1821. if ($this->ImageMagickSwitchAvailable(array('colors', 'dither'))) {
  1822. @list($colors, $dither) = explode('|', $parameter);
  1823. $colors = ($colors ? (int) $colors : 256);
  1824. $dither = ((strlen($dither) > 0) ? (bool) $dither : true);
  1825. $commandline .= ' -colors '.phpthumb_functions::escapeshellarg_replacement(max($colors, 8)); // ImageMagick will otherwise fail with "cannot quantize to fewer than 8 colors"
  1826. $commandline .= ($dither ? ' -dither' : ' +dither');
  1827. $successfullyProcessedFilters[] = $filterkey;
  1828. }
  1829. break;
  1830. case 'flip':
  1831. if ($this->ImageMagickSwitchAvailable(array('flip', 'flop'))) {
  1832. if (strpos(strtolower($parameter), 'x') !== false) {
  1833. $commandline .= ' -flop';
  1834. }
  1835. if (strpos(strtolower($parameter), 'y') !== false) {
  1836. $commandline .= ' -flip';
  1837. }
  1838. $successfullyProcessedFilters[] = $filterkey;
  1839. }
  1840. break;
  1841. case 'edge':
  1842. if ($this->ImageMagickSwitchAvailable('edge')) {
  1843. $parameter = (!empty($parameter) ? $parameter : 2);
  1844. $commandline .= ' -edge '.phpthumb_functions::escapeshellarg_replacement(!empty($parameter) ? (int) $parameter : 1);
  1845. $successfullyProcessedFilters[] = $filterkey;
  1846. }
  1847. break;
  1848. case 'emb':
  1849. if ($this->ImageMagickSwitchAvailable(array('emboss', 'negate'))) {
  1850. $parameter = (!empty($parameter) ? $parameter : 2);
  1851. $commandline .= ' -emboss '.phpthumb_functions::escapeshellarg_replacement((int) $parameter);
  1852. if ($parameter < 2) {
  1853. $commandline .= ' -negate'; // ImageMagick negates the image for some reason with '-emboss 1';
  1854. }
  1855. $successfullyProcessedFilters[] = $filterkey;
  1856. }
  1857. break;
  1858. case 'lvl':
  1859. @list($band, $method, $threshold) = explode('|', $parameter);
  1860. $band = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band)) : '*');
  1861. $method = ((strlen($method) > 0) ? (int) $method : 2);
  1862. $threshold = ((strlen($threshold) > 0) ? min(max((float) $threshold, 0), 100) : 0.1);
  1863. $band = preg_replace('#[^RGBA\\*]#', '', strtoupper($band));
  1864. if (($method > 1) && !$this->ImageMagickSwitchAvailable(array('channel', 'contrast-stretch'))) {
  1865. // Because ImageMagick processing happens before PHP-GD filters, and because some
  1866. // clipping is involved in the "lvl" filter, if "lvl" happens before "wb" then the
  1867. // "wb" filter will have (almost) no effect. Therefore, if "wb" is enabled then
  1868. // force the "lvl" filter to be processed by GD, not ImageMagick.
  1869. foreach ($this->fltr as $fltr_key => $fltr_value) {
  1870. list($fltr_cmd) = explode('|', $fltr_value);
  1871. if ($fltr_cmd == 'wb') {
  1872. $this->DebugMessage('Setting "lvl" filter method to "0" (from "'.$method.'") because white-balance filter also enabled', __FILE__, __LINE__);
  1873. $method = 0;
  1874. }
  1875. }
  1876. }
  1877. switch ($method) {
  1878. case 0: // internal RGB
  1879. case 1: // internal grayscale
  1880. break;
  1881. case 2: // ImageMagick "contrast-stretch"
  1882. if ($this->ImageMagickSwitchAvailable('contrast-stretch')) {
  1883. if ($band != '*') {
  1884. $commandline .= ' -channel '.phpthumb_functions::escapeshellarg_replacement(strtoupper($band));
  1885. }
  1886. $threshold = preg_replace('#[^0-9\\.]#', '', $threshold); // should be unneccesary, but just to be double-sure
  1887. //$commandline .= ' -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%');
  1888. $commandline .= ' -contrast-stretch \''.$threshold.'%\'';
  1889. if ($band != '*') {
  1890. $commandline .= ' +channel';
  1891. }
  1892. $successfullyProcessedFilters[] = $filterkey;
  1893. }
  1894. break;
  1895. case 3: // ImageMagick "normalize"
  1896. if ($this->ImageMagickSwitchAvailable('normalize')) {
  1897. if ($band != '*') {
  1898. $commandline .= ' -channel '.phpthumb_functions::escapeshellarg_replacement(strtoupper($band));
  1899. }
  1900. $commandline .= ' -normalize';
  1901. if ($band != '*') {
  1902. $commandline .= ' +channel';
  1903. }
  1904. $successfullyProcessedFilters[] = $filterkey;
  1905. }
  1906. break;
  1907. default:
  1908. $this->DebugMessage('unsupported method ('.$method.') for "lvl" filter', __FILE__, __LINE__);
  1909. break;
  1910. }
  1911. if (isset($this->fltr[$filterkey]) && ($method > 1)) {
  1912. $this->fltr[$filterkey] = $command.'|'.$band.'|0|'.$threshold;
  1913. $this->DebugMessage('filter "lvl" remapped from method "'.$method.'" to method "0" because ImageMagick support is missing', __FILE__, __LINE__);
  1914. }
  1915. break;
  1916. case 'wb':
  1917. if ($this->ImageMagickSwitchAvailable(array('channel', 'contrast-stretch'))) {
  1918. @list($threshold) = explode('|', $parameter);
  1919. $threshold = (!empty($threshold) ? min(max((float) $threshold, 0), 100) : 0.1);
  1920. $threshold = preg_replace('#[^0-9\\.]#', '', $threshold); // should be unneccesary, but just to be double-sure
  1921. //$commandline .= ' -channel R -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%'); // doesn't work on Windows because most versions of PHP do not properly
  1922. //$commandline .= ' -channel G -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%'); // escape special characters (such as %) and just replace them with spaces
  1923. //$commandline .= ' -channel B -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%'); // https://bugs.php.net/bug.php?id=43261
  1924. $commandline .= ' -channel R -contrast-stretch \''.$threshold.'%\'';
  1925. $commandline .= ' -channel G -contrast-stretch \''.$threshold.'%\'';
  1926. $commandline .= ' -channel B -contrast-stretch \''.$threshold.'%\'';
  1927. $commandline .= ' +channel';
  1928. $successfullyProcessedFilters[] = $filterkey;
  1929. }
  1930. break;
  1931. case 'blur':
  1932. if ($this->ImageMagickSwitchAvailable('blur')) {
  1933. @list($radius) = explode('|', $parameter);
  1934. $radius = (!empty($radius) ? min(max((int) $radius, 0), 25) : 1);
  1935. $commandline .= ' -blur '.phpthumb_functions::escapeshellarg_replacement($radius);
  1936. $successfullyProcessedFilters[] = $filterkey;
  1937. }
  1938. break;
  1939. case 'gblr':
  1940. @list($radius) = explode('|', $parameter);
  1941. $radius = (!empty($radius) ? min(max((int) $radius, 0), 25) : 1);
  1942. // "-gaussian" changed to "-gaussian-blur" sometime around 2009
  1943. if ($this->ImageMagickSwitchAvailable('gaussian-blur')) {
  1944. $commandline .= ' -gaussian-blur '.phpthumb_functions::escapeshellarg_replacement($radius);
  1945. $successfullyProcessedFilters[] = $filterkey;
  1946. } elseif ($this->ImageMagickSwitchAvailable('gaussian')) {
  1947. $commandline .= ' -gaussian '.phpthumb_functions::escapeshellarg_replacement($radius);
  1948. $successfullyProcessedFilters[] = $filterkey;
  1949. }
  1950. break;
  1951. case 'usm':
  1952. if ($this->ImageMagickSwitchAvailable('unsharp')) {
  1953. @list($amount, $radius, $threshold) = explode('|', $parameter);
  1954. $amount = ($amount ? min(max((int) $amount, 0), 255) : 80);
  1955. $radius = ($radius ? min(max((int) $radius, 0), 10) : 0.5);
  1956. $threshold = ('' !== $threshold ? min(max((int) $threshold, 0), 50) : 3);
  1957. $commandline .= ' -unsharp '.phpthumb_functions::escapeshellarg_replacement(number_format(($radius * 2) - 1, 2, '.', '').'x1+'.number_format($amount / 100, 2, '.', '').'+'.number_format($threshold / 100, 2, '.', ''));
  1958. $successfullyProcessedFilters[] = $filterkey;
  1959. }
  1960. break;
  1961. case 'bord':
  1962. if ($this->ImageMagickSwitchAvailable(array('border', 'bordercolor', 'thumbnail', 'crop'))) {
  1963. if (!$this->zc) {
  1964. @list($width, $rX, $rY, $color) = explode('|', $parameter);
  1965. $width = (int) $width;
  1966. $rX = (int) $rX;
  1967. $rY = (int) $rY;
  1968. if ($width && !$rX && !$rY) {
  1969. if (!phpthumb_functions::IsHexColor($color)) {
  1970. $color = ((!empty($this->bc) && phpthumb_functions::IsHexColor($this->bc)) ? $this->bc : '000000');
  1971. }
  1972. $commandline .= ' -border '.phpthumb_functions::escapeshellarg_replacement((int) $width);
  1973. $commandline .= ' -bordercolor '.phpthumb_functions::escapeshellarg_replacement('#'.$color);
  1974. if (preg_match('# \\-crop "([\d]+)x([\d]+)\\+0\\+0" #', $commandline, $matches)) {
  1975. $commandline = str_replace(' -crop "'.$matches[1].'x'.$matches[2].'+0+0" ', ' -crop '.phpthumb_functions::escapeshellarg_replacement(($matches[1] - (2 * $width)).'x'.($matches[2] - (2 * $width)).'+0+0').' ', $commandline);
  1976. } elseif (preg_match('# \\-'.$IMresizeParameter.' "([0-9]+)x([0-9]+)" #', $commandline, $matches)) {
  1977. $commandline = str_replace(' -'.$IMresizeParameter.' "'.$matches[1].'x'.$matches[2].'" ', ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(($matches[1] - (2 * $width)).'x'.($matches[2] - (2 * $width))).' ', $commandline);
  1978. }
  1979. $successfullyProcessedFilters[] = $filterkey;
  1980. }
  1981. }
  1982. }
  1983. break;
  1984. case 'crop':
  1985. break;
  1986. case 'sblr':
  1987. break;
  1988. case 'mean':
  1989. break;
  1990. case 'smth':
  1991. break;
  1992. case 'bvl':
  1993. break;
  1994. case 'wmi':
  1995. break;
  1996. case 'wmt':
  1997. break;
  1998. case 'over':
  1999. break;
  2000. case 'hist':
  2001. break;
  2002. case 'fram':
  2003. break;
  2004. case 'drop':
  2005. break;
  2006. case 'mask':
  2007. break;
  2008. case 'elip':
  2009. break;
  2010. case 'ric':
  2011. break;
  2012. case 'stc':
  2013. break;
  2014. case 'size':
  2015. break;
  2016. default:
  2017. $this->DebugMessage('Unknown $this->fltr['.$filterkey.'] ('.$filtercommand.') -- deleting filter command', __FILE__, __LINE__);
  2018. $successfullyProcessedFilters[] = $filterkey;
  2019. break;
  2020. }
  2021. if (!isset($this->fltr[$filterkey])) {
  2022. $this->DebugMessage('Processed $this->fltr['.$filterkey.'] ('.$filtercommand.') with ImageMagick', __FILE__, __LINE__);
  2023. } else {
  2024. $this->DebugMessage('Skipping $this->fltr['.$filterkey.'] ('.$filtercommand.') with ImageMagick', __FILE__, __LINE__);
  2025. }
  2026. }
  2027. $this->DebugMessage('Remaining $this->fltr after ImageMagick: ('.$this->phpThumbDebugVarDump($this->fltr).')', __FILE__, __LINE__);
  2028. if (count($this->fltr) > 0) {
  2029. $this->useRawIMoutput = false;
  2030. }
  2031. if (preg_match('#jpe?g#i', $outputFormat) && $this->q) {
  2032. if ($this->ImageMagickSwitchAvailable(array('quality', 'interlace'))) {
  2033. $commandline .= ' -quality '.phpthumb_functions::escapeshellarg_replacement($this->thumbnailQuality);
  2034. if ($this->config_output_interlace) {
  2035. // causes weird things with animated GIF... leave for JPEG only
  2036. $commandline .= ' -interlace line '; // Use Line or Plane to create an interlaced PNG or GIF or progressive JPEG image
  2037. }
  2038. }
  2039. }
  2040. $commandline .= ' '.$outputFormat.':'.phpthumb_functions::escapeshellarg_replacement($IMtempfilename);
  2041. if (!$this->iswindows) {
  2042. $commandline .= ' 2>&1';
  2043. }
  2044. $this->DebugMessage('ImageMagick called as ('.$commandline.')', __FILE__, __LINE__);
  2045. $IMresult = phpthumb_functions::SafeExec($commandline);
  2046. clearstatcache();
  2047. if (!@file_exists($IMtempfilename) || !@filesize($IMtempfilename)) {
  2048. $this->FatalError('ImageMagick failed with message ('.trim($IMresult).')');
  2049. $this->DebugMessage('ImageMagick failed with message ('.trim($IMresult).')', __FILE__, __LINE__);
  2050. if ($this->iswindows && !$IMresult) {
  2051. $this->DebugMessage('Check to make sure that PHP has read+write permissions to "'.dirname($IMtempfilename).'"', __FILE__, __LINE__);
  2052. }
  2053. } else {
  2054. foreach ($successfullyProcessedFilters as $dummy => $filterkey) {
  2055. unset($this->fltr[$filterkey]);
  2056. }
  2057. $this->IMresizedData = file_get_contents($IMtempfilename);
  2058. $getimagesize_imresized = @getimagesize($IMtempfilename);
  2059. $this->DebugMessage('getimagesize('.$IMtempfilename.') returned [w='.$getimagesize_imresized[0].';h='.$getimagesize_imresized[1].';f='.$getimagesize_imresized[2].']', __FILE__, __LINE__);
  2060. if (($this->config_max_source_pixels > 0) && (($getimagesize_imresized[0] * $getimagesize_imresized[1]) > $this->config_max_source_pixels)) {
  2061. $this->DebugMessage('skipping ImageMagickThumbnailToGD::'.$ImageCreateFunction.'() because IM output is too large ('.$getimagesize_imresized[0].'x'.$getimagesize_imresized[0].' = '.($getimagesize_imresized[0] * $getimagesize_imresized[1]).' > '.$this->config_max_source_pixels.')', __FILE__, __LINE__);
  2062. } elseif (function_exists(@$ImageCreateFunction) && ($this->gdimg_source = @$ImageCreateFunction($IMtempfilename))) {
  2063. $this->source_width = imagesx($this->gdimg_source);
  2064. $this->source_height = imagesy($this->gdimg_source);
  2065. $this->DebugMessage('ImageMagickThumbnailToGD::'.$ImageCreateFunction.'() succeeded, $this->gdimg_source is now ('.$this->source_width.'x'.$this->source_height.')', __FILE__, __LINE__);
  2066. $this->DebugMessage('ImageMagickThumbnailToGD() returning $this->IMresizedData ('.strlen($this->IMresizedData).' bytes)', __FILE__, __LINE__);
  2067. } else {
  2068. $this->useRawIMoutput = true;
  2069. $this->DebugMessage('$this->useRawIMoutput set to TRUE because '.@$ImageCreateFunction.'('.$IMtempfilename.') failed', __FILE__, __LINE__);
  2070. }
  2071. if (file_exists($IMtempfilename)) {
  2072. $this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__);
  2073. @unlink($IMtempfilename);
  2074. }
  2075. return true;
  2076. }
  2077. if (file_exists($IMtempfilename)) {
  2078. $this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__);
  2079. @unlink($IMtempfilename);
  2080. }
  2081. } elseif ($this->issafemode) {
  2082. $this->DebugMessage('ImageMagickThumbnailToGD() aborting because PHP safe_mode is enabled and phpThumb_tempnam() failed', __FILE__, __LINE__);
  2083. $this->useRawIMoutput = false;
  2084. } else {
  2085. if (file_exists($IMtempfilename)) {
  2086. $this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__);
  2087. @unlink($IMtempfilename);
  2088. }
  2089. $this->DebugMessage('ImageMagickThumbnailToGD() aborting, phpThumb_tempnam() failed', __FILE__, __LINE__);
  2090. }
  2091. } else {
  2092. $this->DebugMessage('ImageMagickThumbnailToGD() aborting because ImageMagickCommandlineBase() failed', __FILE__, __LINE__);
  2093. }
  2094. $this->useRawIMoutput = false;
  2095. return false;
  2096. }
  2097. public function Rotate() {
  2098. if ($this->ra || $this->ar) {
  2099. if (!function_exists('imagerotate')) {
  2100. $this->DebugMessage('!function_exists(imagerotate)', __FILE__, __LINE__);
  2101. return false;
  2102. }
  2103. if (!include_once __DIR__ .'/phpthumb.filters.php' ) {
  2104. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.filters.php" which is required for applying filters ('.implode(';', $this->fltr).')', __FILE__, __LINE__);
  2105. return false;
  2106. }
  2107. $this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor);
  2108. if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
  2109. return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"');
  2110. }
  2111. $rotate_angle = 0;
  2112. if ($this->ra) {
  2113. $rotate_angle = (float) $this->ra;
  2114. } else {
  2115. if ($this->ar == 'x') {
  2116. if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.2.0', '>=')) {
  2117. if ($this->sourceFilename) {
  2118. if (function_exists('exif_read_data')) {
  2119. if ($exif_data = @exif_read_data($this->sourceFilename, 'IFD0')) {
  2120. // http://sylvana.net/jpegcrop/exif_orientation.html
  2121. switch (@$exif_data['Orientation']) {
  2122. case 1:
  2123. $rotate_angle = 0;
  2124. break;
  2125. case 3:
  2126. $rotate_angle = 180;
  2127. break;
  2128. case 6:
  2129. $rotate_angle = 270;
  2130. break;
  2131. case 8:
  2132. $rotate_angle = 90;
  2133. break;
  2134. default:
  2135. $this->DebugMessage('EXIF auto-rotate failed because unknown $exif_data[Orientation] "'.@$exif_data['Orientation'].'"', __FILE__, __LINE__);
  2136. return false;
  2137. break;
  2138. }
  2139. $this->DebugMessage('EXIF auto-rotate set to '.$rotate_angle.' degrees ($exif_data[Orientation] = "'.@$exif_data['Orientation'].'")', __FILE__, __LINE__);
  2140. } else {
  2141. $this->DebugMessage('failed: exif_read_data('.$this->sourceFilename.')', __FILE__, __LINE__);
  2142. return false;
  2143. }
  2144. } else {
  2145. $this->DebugMessage('!function_exists(exif_read_data)', __FILE__, __LINE__);
  2146. return false;
  2147. }
  2148. } else {
  2149. $this->DebugMessage('Cannot auto-rotate from EXIF data because $this->sourceFilename is empty', __FILE__, __LINE__);
  2150. return false;
  2151. }
  2152. } else {
  2153. $this->DebugMessage('Cannot auto-rotate from EXIF data because PHP is less than v4.2.0 ('. PHP_VERSION .')', __FILE__, __LINE__);
  2154. return false;
  2155. }
  2156. } elseif (($this->ar == 'l') && ($this->source_height > $this->source_width)) {
  2157. $rotate_angle = 270;
  2158. } elseif (($this->ar == 'L') && ($this->source_height > $this->source_width)) {
  2159. $rotate_angle = 90;
  2160. } elseif (($this->ar == 'p') && ($this->source_width > $this->source_height)) {
  2161. $rotate_angle = 90;
  2162. } elseif (($this->ar == 'P') && ($this->source_width > $this->source_height)) {
  2163. $rotate_angle = 270;
  2164. }
  2165. }
  2166. if ($rotate_angle % 90) {
  2167. $this->is_alpha = true;
  2168. }
  2169. phpthumb_filters::ImprovedImageRotate($this->gdimg_source, $rotate_angle, $this->config_background_hexcolor, $this->bg, $this);
  2170. $this->source_width = imagesx($this->gdimg_source);
  2171. $this->source_height = imagesy($this->gdimg_source);
  2172. }
  2173. return true;
  2174. }
  2175. public function FixedAspectRatio() {
  2176. // optional fixed-dimension images (regardless of aspect ratio)
  2177. if (!$this->far) {
  2178. // do nothing
  2179. return true;
  2180. }
  2181. if (!$this->w || !$this->h) {
  2182. return false;
  2183. }
  2184. $this->thumbnail_width = $this->w;
  2185. $this->thumbnail_height = $this->h;
  2186. $this->is_alpha = true;
  2187. if ($this->thumbnail_image_width >= $this->thumbnail_width) {
  2188. $aspectratio = $this->thumbnail_image_height / $this->thumbnail_image_width;
  2189. if ($this->w) {
  2190. $this->thumbnail_image_height = round($this->thumbnail_image_width * $aspectratio);
  2191. $this->thumbnail_height = ($this->h ? $this->h : $this->thumbnail_image_height);
  2192. } elseif ($this->thumbnail_image_height < $this->thumbnail_height) {
  2193. $this->thumbnail_image_height = $this->thumbnail_height;
  2194. $this->thumbnail_image_width = round($this->thumbnail_image_height / $aspectratio);
  2195. }
  2196. } else {
  2197. $aspectratio = $this->thumbnail_image_width / $this->thumbnail_image_height;
  2198. if ($this->h) {
  2199. $this->thumbnail_image_width = round($this->thumbnail_image_height * $aspectratio);
  2200. } elseif ($this->thumbnail_image_width < $this->thumbnail_width) {
  2201. $this->thumbnail_image_width = $this->thumbnail_width;
  2202. $this->thumbnail_image_height = round($this->thumbnail_image_width / $aspectratio);
  2203. }
  2204. }
  2205. return true;
  2206. }
  2207. public function OffsiteDomainIsAllowed($hostname, $allowed_domains) {
  2208. static $domain_is_allowed = array();
  2209. $hostname = strtolower($hostname);
  2210. if (!isset($domain_is_allowed[$hostname])) {
  2211. $domain_is_allowed[$hostname] = false;
  2212. foreach ($allowed_domains as $valid_domain) {
  2213. $starpos = strpos($valid_domain, '*');
  2214. if ($starpos !== false) {
  2215. $valid_domain = substr($valid_domain, $starpos + 1);
  2216. if (preg_match('#'.preg_quote($valid_domain).'$#', $hostname)) {
  2217. $domain_is_allowed[$hostname] = true;
  2218. break;
  2219. }
  2220. } else {
  2221. if (strtolower($valid_domain) === $hostname) {
  2222. $domain_is_allowed[$hostname] = true;
  2223. break;
  2224. }
  2225. }
  2226. }
  2227. }
  2228. return $domain_is_allowed[$hostname];
  2229. }
  2230. public function AntiOffsiteLinking() {
  2231. // Optional anti-offsite hijacking of the thumbnail script
  2232. $allow = true;
  2233. if ($allow && $this->config_nooffsitelink_enabled && (@$_SERVER['HTTP_REFERER'] || $this->config_nooffsitelink_require_refer)) {
  2234. $this->DebugMessage('AntiOffsiteLinking() checking $_SERVER[HTTP_REFERER] "'.@$_SERVER['HTTP_REFERER'].'"', __FILE__, __LINE__);
  2235. foreach ($this->config_nooffsitelink_valid_domains as $key => $valid_domain) {
  2236. // $_SERVER['HTTP_HOST'] contains the port number, so strip it out here to make default configuration work
  2237. list($clean_domain) = explode(':', $valid_domain);
  2238. $this->config_nooffsitelink_valid_domains[$key] = $clean_domain;
  2239. }
  2240. $parsed_url = phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']);
  2241. if (!$this->OffsiteDomainIsAllowed(@$parsed_url['host'], $this->config_nooffsitelink_valid_domains)) {
  2242. $allow = false;
  2243. //$this->DebugMessage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is NOT in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')', __FILE__, __LINE__);
  2244. $this->ErrorImage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is NOT in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')');
  2245. } else {
  2246. $this->DebugMessage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')', __FILE__, __LINE__);
  2247. }
  2248. }
  2249. if ($allow && $this->config_nohotlink_enabled && preg_match('#^(f|ht)tps?\://#i', $this->src)) {
  2250. $parsed_url = phpthumb_functions::ParseURLbetter($this->src);
  2251. //if (!phpthumb_functions::CaseInsensitiveInArray(@$parsed_url['host'], $this->config_nohotlink_valid_domains)) {
  2252. if (!$this->OffsiteDomainIsAllowed(@$parsed_url['host'], $this->config_nohotlink_valid_domains)) {
  2253. // This domain is not allowed
  2254. $allow = false;
  2255. $this->DebugMessage('AntiOffsiteLinking() - "'.$parsed_url['host'].'" is NOT in $this->config_nohotlink_valid_domains ('.implode(';', $this->config_nohotlink_valid_domains).')', __FILE__, __LINE__);
  2256. } else {
  2257. $this->DebugMessage('AntiOffsiteLinking() - "'.$parsed_url['host'].'" is in $this->config_nohotlink_valid_domains ('.implode(';', $this->config_nohotlink_valid_domains).')', __FILE__, __LINE__);
  2258. }
  2259. }
  2260. if ($allow) {
  2261. $this->DebugMessage('AntiOffsiteLinking() says this is allowed', __FILE__, __LINE__);
  2262. return true;
  2263. }
  2264. if (!phpthumb_functions::IsHexColor($this->config_error_bgcolor)) {
  2265. return $this->ErrorImage('Invalid hex color string "'.$this->config_error_bgcolor.'" for $this->config_error_bgcolor');
  2266. }
  2267. if (!phpthumb_functions::IsHexColor($this->config_error_textcolor)) {
  2268. return $this->ErrorImage('Invalid hex color string "'.$this->config_error_textcolor.'" for $this->config_error_textcolor');
  2269. }
  2270. if ($this->config_nooffsitelink_erase_image) {
  2271. return $this->ErrorImage($this->config_nooffsitelink_text_message, $this->thumbnail_width, $this->thumbnail_height);
  2272. } else {
  2273. $this->config_nooffsitelink_watermark_src = $this->ResolveFilenameToAbsolute($this->config_nooffsitelink_watermark_src);
  2274. if (is_file($this->config_nooffsitelink_watermark_src)) {
  2275. if (!include_once __DIR__ .'/phpthumb.filters.php' ) {
  2276. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.filters.php" which is required for applying watermark', __FILE__, __LINE__);
  2277. return false;
  2278. }
  2279. $watermark_img = $this->ImageCreateFromStringReplacement(file_get_contents($this->config_nooffsitelink_watermark_src));
  2280. $phpthumbFilters = new phpthumb_filters();
  2281. $phpthumbFilters->phpThumbObject = &$this;
  2282. $opacity = 50;
  2283. $margin = 5;
  2284. $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $watermark_img, '*', $opacity, $margin);
  2285. imagedestroy($watermark_img);
  2286. unset($phpthumbFilters);
  2287. } else {
  2288. $nohotlink_text_array = explode("\n", wordwrap($this->config_nooffsitelink_text_message, floor($this->thumbnail_width / imagefontwidth($this->config_error_fontsize)), "\n"));
  2289. $nohotlink_text_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_error_textcolor);
  2290. $topoffset = round(($this->thumbnail_height - (count($nohotlink_text_array) * imagefontheight($this->config_error_fontsize))) / 2);
  2291. $rowcounter = 0;
  2292. $this->DebugMessage('AntiOffsiteLinking() writing '.count($nohotlink_text_array).' lines of text "'.$this->config_nooffsitelink_text_message.'" (in #'.$this->config_error_textcolor.') on top of image', __FILE__, __LINE__);
  2293. foreach ($nohotlink_text_array as $textline) {
  2294. $leftoffset = max(0, round(($this->thumbnail_width - (strlen($textline) * imagefontwidth($this->config_error_fontsize))) / 2));
  2295. imagestring($this->gdimg_output, $this->config_error_fontsize, $leftoffset, $topoffset + ($rowcounter++ * imagefontheight($this->config_error_fontsize)), $textline, $nohotlink_text_color);
  2296. }
  2297. }
  2298. }
  2299. return true;
  2300. }
  2301. public function AlphaChannelFlatten() {
  2302. if (!$this->is_alpha) {
  2303. // image doesn't have alpha transparency, no need to flatten
  2304. $this->DebugMessage('skipping AlphaChannelFlatten() because !$this->is_alpha', __FILE__, __LINE__);
  2305. return false;
  2306. }
  2307. switch ($this->thumbnailFormat) {
  2308. case 'png':
  2309. case 'ico':
  2310. // image has alpha transparency, but output as PNG or ICO which can handle it
  2311. $this->DebugMessage('skipping AlphaChannelFlatten() because ($this->thumbnailFormat == "'.$this->thumbnailFormat.'")', __FILE__, __LINE__);
  2312. return false;
  2313. break;
  2314. case 'gif':
  2315. // image has alpha transparency, but output as GIF which can handle only single-color transparency
  2316. $CurrentImageColorTransparent = imagecolortransparent($this->gdimg_output);
  2317. if ($CurrentImageColorTransparent == -1) {
  2318. // no transparent color defined
  2319. if (phpthumb_functions::gd_version() < 2.0) {
  2320. $this->DebugMessage('AlphaChannelFlatten() failed because GD version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2321. return false;
  2322. }
  2323. if ($img_alpha_mixdown_dither = @imagecreatetruecolor(imagesx($this->gdimg_output), imagesy($this->gdimg_output))) {
  2324. $dither_color = array();
  2325. for ($i = 0; $i <= 255; $i++) {
  2326. $dither_color[$i] = imagecolorallocate($img_alpha_mixdown_dither, $i, $i, $i);
  2327. }
  2328. // scan through current truecolor image copy alpha channel to temp image as grayscale
  2329. for ($x = 0; $x < $this->thumbnail_width; $x++) {
  2330. for ($y = 0; $y < $this->thumbnail_height; $y++) {
  2331. $PixelColor = phpthumb_functions::GetPixelColor($this->gdimg_output, $x, $y);
  2332. imagesetpixel($img_alpha_mixdown_dither, $x, $y, $dither_color[ $PixelColor[ 'alpha'] * 2 ]);
  2333. }
  2334. }
  2335. // dither alpha channel grayscale version down to 2 colors
  2336. imagetruecolortopalette($img_alpha_mixdown_dither, true, 2);
  2337. // reduce color palette to 256-1 colors (leave one palette position for transparent color)
  2338. imagetruecolortopalette($this->gdimg_output, true, 255);
  2339. // allocate a new color for transparent color index
  2340. $TransparentColor = imagecolorallocate($this->gdimg_output, 1, 254, 253);
  2341. imagecolortransparent($this->gdimg_output, $TransparentColor);
  2342. // scan through alpha channel image and note pixels with >50% transparency
  2343. for ($x = 0; $x < $this->thumbnail_width; $x++) {
  2344. for ($y = 0; $y < $this->thumbnail_height; $y++) {
  2345. $AlphaChannelPixel = phpthumb_functions::GetPixelColor($img_alpha_mixdown_dither, $x, $y);
  2346. if ($AlphaChannelPixel['red'] > 127) {
  2347. imagesetpixel($this->gdimg_output, $x, $y, $TransparentColor);
  2348. }
  2349. }
  2350. }
  2351. imagedestroy($img_alpha_mixdown_dither);
  2352. $this->DebugMessage('AlphaChannelFlatten() set image to 255+1 colors with transparency for GIF output', __FILE__, __LINE__);
  2353. return true;
  2354. } else {
  2355. $this->DebugMessage('AlphaChannelFlatten() failed imagecreate('.imagesx($this->gdimg_output).', '.imagesy($this->gdimg_output).')', __FILE__, __LINE__);
  2356. return false;
  2357. }
  2358. } else {
  2359. // a single transparent color already defined, leave as-is
  2360. $this->DebugMessage('skipping AlphaChannelFlatten() because ($this->thumbnailFormat == "'.$this->thumbnailFormat.'") and imagecolortransparent() returned "'.$CurrentImageColorTransparent.'"', __FILE__, __LINE__);
  2361. return true;
  2362. }
  2363. break;
  2364. }
  2365. $this->DebugMessage('continuing AlphaChannelFlatten() for output format "'.$this->thumbnailFormat.'"', __FILE__, __LINE__);
  2366. // image has alpha transparency, and is being output in a format that doesn't support it -- flatten
  2367. if ($gdimg_flatten_temp = phpthumb_functions::ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height)) {
  2368. $this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor);
  2369. if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
  2370. return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"');
  2371. }
  2372. $background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_background_hexcolor);
  2373. imagefilledrectangle($gdimg_flatten_temp, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $background_color);
  2374. imagecopy($gdimg_flatten_temp, $this->gdimg_output, 0, 0, 0, 0, $this->thumbnail_width, $this->thumbnail_height);
  2375. imagealphablending($this->gdimg_output, true);
  2376. imagesavealpha($this->gdimg_output, false);
  2377. imagecolortransparent($this->gdimg_output, -1);
  2378. imagecopy($this->gdimg_output, $gdimg_flatten_temp, 0, 0, 0, 0, $this->thumbnail_width, $this->thumbnail_height);
  2379. imagedestroy($gdimg_flatten_temp);
  2380. return true;
  2381. } else {
  2382. $this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__);
  2383. }
  2384. return false;
  2385. }
  2386. public function ApplyFilters() {
  2387. if ($this->fltr && is_array($this->fltr)) {
  2388. if (!include_once __DIR__ .'/phpthumb.filters.php' ) {
  2389. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.filters.php" which is required for applying filters ('.implode(';', $this->fltr).')', __FILE__, __LINE__);
  2390. return false;
  2391. }
  2392. $phpthumbFilters = new phpthumb_filters();
  2393. $phpthumbFilters->phpThumbObject = &$this;
  2394. foreach ($this->fltr as $filtercommand) {
  2395. @list($command, $parameter) = explode('|', $filtercommand, 2);
  2396. $this->DebugMessage('Attempting to process filter command "'.$command.'('.$parameter.')"', __FILE__, __LINE__);
  2397. switch ($command) {
  2398. case 'brit': // Brightness
  2399. $phpthumbFilters->Brightness($this->gdimg_output, $parameter);
  2400. break;
  2401. case 'cont': // Contrast
  2402. $phpthumbFilters->Contrast($this->gdimg_output, $parameter);
  2403. break;
  2404. case 'ds': // Desaturation
  2405. $phpthumbFilters->Desaturate($this->gdimg_output, $parameter, '');
  2406. break;
  2407. case 'sat': // Saturation
  2408. $phpthumbFilters->Saturation($this->gdimg_output, $parameter, '');
  2409. break;
  2410. case 'gray': // Grayscale
  2411. $phpthumbFilters->Grayscale($this->gdimg_output);
  2412. break;
  2413. case 'clr': // Colorize
  2414. if (phpthumb_functions::gd_version() < 2) {
  2415. $this->DebugMessage('Skipping Colorize() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2416. break;
  2417. }
  2418. @list($amount, $color) = explode('|', $parameter, 2);
  2419. $phpthumbFilters->Colorize($this->gdimg_output, $amount, $color);
  2420. break;
  2421. case 'sep': // Sepia
  2422. if (phpthumb_functions::gd_version() < 2) {
  2423. $this->DebugMessage('Skipping Sepia() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2424. break;
  2425. }
  2426. @list($amount, $color) = explode('|', $parameter, 2);
  2427. $phpthumbFilters->Sepia($this->gdimg_output, $amount, $color);
  2428. break;
  2429. case 'gam': // Gamma correction
  2430. $phpthumbFilters->Gamma($this->gdimg_output, $parameter);
  2431. break;
  2432. case 'neg': // Negative colors
  2433. $phpthumbFilters->Negative($this->gdimg_output);
  2434. break;
  2435. case 'th': // Threshold
  2436. $phpthumbFilters->Threshold($this->gdimg_output, $parameter);
  2437. break;
  2438. case 'rcd': // ReduceColorDepth
  2439. if (phpthumb_functions::gd_version() < 2) {
  2440. $this->DebugMessage('Skipping ReduceColorDepth() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2441. break;
  2442. }
  2443. @list($colors, $dither) = explode('|', $parameter, 2);
  2444. $colors = ($colors ? (int) $colors : 256);
  2445. $dither = ((strlen($dither) > 0) ? (bool) $dither : true);
  2446. $phpthumbFilters->ReduceColorDepth($this->gdimg_output, $colors, $dither);
  2447. break;
  2448. case 'flip': // Flip
  2449. $phpthumbFilters->Flip($this->gdimg_output, strpos(strtolower($parameter), 'x') !== false, strpos(strtolower($parameter), 'y') !== false);
  2450. break;
  2451. case 'edge': // EdgeDetect
  2452. $phpthumbFilters->EdgeDetect($this->gdimg_output);
  2453. break;
  2454. case 'emb': // Emboss
  2455. $phpthumbFilters->Emboss($this->gdimg_output);
  2456. break;
  2457. case 'bvl': // Bevel
  2458. @list($width, $color1, $color2) = explode('|', $parameter, 3);
  2459. $phpthumbFilters->Bevel($this->gdimg_output, $width, $color1, $color2);
  2460. break;
  2461. case 'lvl': // autoLevels
  2462. @list($band, $method, $threshold) = explode('|', $parameter, 3);
  2463. $band = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band)) : '*');
  2464. $method = ((strlen($method) > 0) ? (int) $method : 2);
  2465. $threshold = ((strlen($threshold) > 0) ? (float) $threshold : 0.1);
  2466. $phpthumbFilters->HistogramStretch($this->gdimg_output, $band, $method, $threshold);
  2467. break;
  2468. case 'wb': // WhiteBalance
  2469. $phpthumbFilters->WhiteBalance($this->gdimg_output, $parameter);
  2470. break;
  2471. case 'hist': // Histogram overlay
  2472. if (phpthumb_functions::gd_version() < 2) {
  2473. $this->DebugMessage('Skipping HistogramOverlay() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2474. break;
  2475. }
  2476. @list($bands, $colors, $width, $height, $alignment, $opacity, $margin_x, $margin_y) = explode('|', $parameter, 8);
  2477. $bands = ($bands ? $bands : '*');
  2478. $colors = ($colors ? $colors : '');
  2479. $width = ($width ? $width : 0.25);
  2480. $height = ($height ? $height : 0.25);
  2481. $alignment = ($alignment ? $alignment : 'BR');
  2482. $opacity = ($opacity ? $opacity : 50);
  2483. $margin_x = ($margin_x ? $margin_x : 5);
  2484. // $margin_y -- it wasn't forgotten, let the value always pass unchanged
  2485. $phpthumbFilters->HistogramOverlay($this->gdimg_output, $bands, $colors, $width, $height, $alignment, $opacity, $margin_x, $margin_y);
  2486. break;
  2487. case 'fram': // Frame
  2488. @list($frame_width, $edge_width, $color_frame, $color1, $color2) = explode('|', $parameter, 5);
  2489. $phpthumbFilters->Frame($this->gdimg_output, $frame_width, $edge_width, $color_frame, $color1, $color2);
  2490. break;
  2491. case 'drop': // DropShadow
  2492. if (phpthumb_functions::gd_version() < 2) {
  2493. $this->DebugMessage('Skipping DropShadow() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2494. return false;
  2495. }
  2496. $this->is_alpha = true;
  2497. @list($distance, $width, $color, $angle, $fade) = explode('|', $parameter, 5);
  2498. $phpthumbFilters->DropShadow($this->gdimg_output, $distance, $width, $color, $angle, $fade);
  2499. break;
  2500. case 'mask': // Mask cropping
  2501. if (phpthumb_functions::gd_version() < 2) {
  2502. $this->DebugMessage('Skipping Mask() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2503. return false;
  2504. }
  2505. @list($mask_filename, $invert) = explode('|', $parameter, 2);
  2506. $mask_filename = $this->ResolveFilenameToAbsolute($mask_filename);
  2507. if (@is_readable($mask_filename) && ($fp_mask = @fopen($mask_filename, 'rb'))) {
  2508. $MaskImageData = '';
  2509. do {
  2510. $buffer = fread($fp_mask, 8192);
  2511. $MaskImageData .= $buffer;
  2512. } while (strlen($buffer) > 0);
  2513. fclose($fp_mask);
  2514. if ($gdimg_mask = $this->ImageCreateFromStringReplacement($MaskImageData)) {
  2515. if ($invert && phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) {
  2516. imagefilter($gdimg_mask, IMG_FILTER_NEGATE);
  2517. }
  2518. $this->is_alpha = true;
  2519. $phpthumbFilters->ApplyMask($gdimg_mask, $this->gdimg_output);
  2520. imagedestroy($gdimg_mask);
  2521. } else {
  2522. $this->DebugMessage('ImageCreateFromStringReplacement() failed for "'.$mask_filename.'"', __FILE__, __LINE__);
  2523. }
  2524. } else {
  2525. $this->DebugMessage('Cannot open mask file "'.$mask_filename.'"', __FILE__, __LINE__);
  2526. }
  2527. break;
  2528. case 'elip': // Ellipse cropping
  2529. if (phpthumb_functions::gd_version() < 2) {
  2530. $this->DebugMessage('Skipping Ellipse() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2531. return false;
  2532. }
  2533. $this->is_alpha = true;
  2534. $phpthumbFilters->Ellipse($this->gdimg_output);
  2535. break;
  2536. case 'ric': // RoundedImageCorners
  2537. if (phpthumb_functions::gd_version() < 2) {
  2538. $this->DebugMessage('Skipping RoundedImageCorners() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2539. return false;
  2540. }
  2541. @list($radius_x, $radius_y) = explode('|', $parameter, 2);
  2542. if (($radius_x < 1) || ($radius_y < 1)) {
  2543. $this->DebugMessage('Skipping RoundedImageCorners('.$radius_x.', '.$radius_y.') because x/y radius is less than 1', __FILE__, __LINE__);
  2544. break;
  2545. }
  2546. $this->is_alpha = true;
  2547. $phpthumbFilters->RoundedImageCorners($this->gdimg_output, $radius_x, $radius_y);
  2548. break;
  2549. case 'crop': // Crop
  2550. @list($left, $right, $top, $bottom) = explode('|', $parameter, 4);
  2551. $phpthumbFilters->Crop($this->gdimg_output, $left, $right, $top, $bottom);
  2552. break;
  2553. case 'bord': // Border
  2554. @list($border_width, $radius_x, $radius_y, $hexcolor_border) = explode('|', $parameter, 4);
  2555. $this->is_alpha = true;
  2556. $phpthumbFilters->ImageBorder($this->gdimg_output, $border_width, $radius_x, $radius_y, $hexcolor_border);
  2557. break;
  2558. case 'over': // Overlay
  2559. @list($filename, $underlay, $margin, $opacity) = explode('|', $parameter, 4);
  2560. $underlay = (bool) ($underlay ? $underlay : false);
  2561. $margin = ((strlen($margin) > 0) ? $margin : ($underlay ? 0.1 : 0.0));
  2562. $opacity = ((strlen($opacity) > 0) ? $opacity : 100);
  2563. if (($margin > 0) && ($margin < 1)) {
  2564. $margin = min(0.499, $margin);
  2565. } elseif (($margin > -1) && ($margin < 0)) {
  2566. $margin = max(-0.499, $margin);
  2567. }
  2568. $filename = $this->ResolveFilenameToAbsolute($filename);
  2569. if (@is_readable($filename) && ($fp_watermark = @fopen($filename, 'rb'))) {
  2570. $WatermarkImageData = '';
  2571. do {
  2572. $buffer = fread($fp_watermark, 8192);
  2573. $WatermarkImageData .= $buffer;
  2574. } while (strlen($buffer) > 0);
  2575. fclose($fp_watermark);
  2576. if ($img_watermark = $this->ImageCreateFromStringReplacement($WatermarkImageData)) {
  2577. if (($margin > 0) && ($margin < 1)) {
  2578. $resized_x = max(1, imagesx($this->gdimg_output) - round(2 * (imagesx($this->gdimg_output) * $margin)));
  2579. $resized_y = max(1, imagesy($this->gdimg_output) - round(2 * (imagesy($this->gdimg_output) * $margin)));
  2580. } else {
  2581. $resized_x = max(1, imagesx($this->gdimg_output) - round(2 * $margin));
  2582. $resized_y = max(1, imagesy($this->gdimg_output) - round(2 * $margin));
  2583. }
  2584. if ($underlay) {
  2585. if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction(imagesx($this->gdimg_output), imagesy($this->gdimg_output))) {
  2586. imagealphablending($img_watermark_resized, false);
  2587. imagesavealpha($img_watermark_resized, true);
  2588. $this->ImageResizeFunction($img_watermark_resized, $img_watermark, 0, 0, 0, 0, imagesx($img_watermark_resized), imagesy($img_watermark_resized), imagesx($img_watermark), imagesy($img_watermark));
  2589. if ($img_source_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) {
  2590. imagealphablending($img_source_resized, false);
  2591. imagesavealpha($img_source_resized, true);
  2592. $this->ImageResizeFunction($img_source_resized, $this->gdimg_output, 0, 0, 0, 0, imagesx($img_source_resized), imagesy($img_source_resized), imagesx($this->gdimg_output), imagesy($this->gdimg_output));
  2593. $phpthumbFilters->WatermarkOverlay($img_watermark_resized, $img_source_resized, 'C', $opacity, $margin);
  2594. imagecopy($this->gdimg_output, $img_watermark_resized, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output));
  2595. } else {
  2596. $this->DebugMessage('phpthumb_functions::ImageCreateFunction('.$resized_x.', '.$resized_y.')', __FILE__, __LINE__);
  2597. }
  2598. imagedestroy($img_watermark_resized);
  2599. } else {
  2600. $this->DebugMessage('phpthumb_functions::ImageCreateFunction('.imagesx($this->gdimg_output).', '.imagesy($this->gdimg_output).')', __FILE__, __LINE__);
  2601. }
  2602. } else { // overlay
  2603. if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) {
  2604. imagealphablending($img_watermark_resized, false);
  2605. imagesavealpha($img_watermark_resized, true);
  2606. $this->ImageResizeFunction($img_watermark_resized, $img_watermark, 0, 0, 0, 0, imagesx($img_watermark_resized), imagesy($img_watermark_resized), imagesx($img_watermark), imagesy($img_watermark));
  2607. $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $img_watermark_resized, 'C', $opacity, $margin);
  2608. imagedestroy($img_watermark_resized);
  2609. } else {
  2610. $this->DebugMessage('phpthumb_functions::ImageCreateFunction('.$resized_x.', '.$resized_y.')', __FILE__, __LINE__);
  2611. }
  2612. }
  2613. imagedestroy($img_watermark);
  2614. } else {
  2615. $this->DebugMessage('ImageCreateFromStringReplacement() failed for "'.$filename.'"', __FILE__, __LINE__);
  2616. }
  2617. } else {
  2618. $this->DebugMessage('Cannot open overlay file "'.$filename.'"', __FILE__, __LINE__);
  2619. }
  2620. break;
  2621. case 'wmi': // WaterMarkImage
  2622. @list($filename, $alignment, $opacity, $margin['x'], $margin['y'], $rotate_angle) = explode('|', $parameter, 6);
  2623. // $margin can be pixel margin or percent margin if $alignment is text, or max width/height if $alignment is position like "50x75"
  2624. $alignment = ($alignment ? $alignment : 'BR');
  2625. $opacity = ('' != $opacity ? (int) $opacity : 50);
  2626. $rotate_angle = ('' != $rotate_angle ? (int) $rotate_angle : 0);
  2627. if (!preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) {
  2628. $margins = array('x', 'y');
  2629. foreach ($margins as $xy) {
  2630. $margin[$xy] = ('' !== $margin[ $xy ] ? $margin[ $xy] : 5);
  2631. if (($margin[$xy] > 0) && ($margin[$xy] < 1)) {
  2632. $margin[$xy] = min(0.499, $margin[$xy]);
  2633. } elseif (($margin[$xy] > -1) && ($margin[$xy] < 0)) {
  2634. $margin[$xy] = max(-0.499, $margin[$xy]);
  2635. }
  2636. }
  2637. }
  2638. $filename = $this->ResolveFilenameToAbsolute($filename);
  2639. if (@is_readable($filename)) {
  2640. if ($img_watermark = $this->ImageCreateFromFilename($filename)) {
  2641. if ($rotate_angle !== 0) {
  2642. $phpthumbFilters->ImprovedImageRotate($img_watermark, $rotate_angle, 'FFFFFF', null, $this);
  2643. }
  2644. if (preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) {
  2645. $watermark_max_width = (int) ($margin[ 'x'] ? $margin[ 'x'] : imagesx($img_watermark));
  2646. $watermark_max_height = (int) ($margin[ 'y'] ? $margin[ 'y'] : imagesy($img_watermark));
  2647. $scale = phpthumb_functions::ScaleToFitInBox(imagesx($img_watermark), imagesy($img_watermark), $watermark_max_width, $watermark_max_height, true, true);
  2648. $this->DebugMessage('Scaling watermark by a factor of '.number_format($scale, 4), __FILE__, __LINE__);
  2649. if (($scale > 1) || ($scale < 1)) {
  2650. if ($img_watermark2 = phpthumb_functions::ImageCreateFunction($scale * imagesx($img_watermark), $scale * imagesy($img_watermark))) {
  2651. imagealphablending($img_watermark2, false);
  2652. imagesavealpha($img_watermark2, true);
  2653. $this->ImageResizeFunction($img_watermark2, $img_watermark, 0, 0, 0, 0, imagesx($img_watermark2), imagesy($img_watermark2), imagesx($img_watermark), imagesy($img_watermark));
  2654. $img_watermark = $img_watermark2;
  2655. } else {
  2656. $this->DebugMessage('ImageCreateFunction('.($scale * imagesx($img_watermark)).', '.($scale * imagesx($img_watermark)).') failed', __FILE__, __LINE__);
  2657. }
  2658. }
  2659. $watermark_dest_x = round($matches[1] - (imagesx($img_watermark) / 2));
  2660. $watermark_dest_y = round($matches[2] - (imagesy($img_watermark) / 2));
  2661. $alignment = $watermark_dest_x.'x'.$watermark_dest_y;
  2662. }
  2663. $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $img_watermark, $alignment, $opacity, $margin['x'], $margin['y']);
  2664. imagedestroy($img_watermark);
  2665. if (isset($img_watermark2) && is_resource($img_watermark2)) {
  2666. imagedestroy($img_watermark2);
  2667. }
  2668. } else {
  2669. $this->DebugMessage('ImageCreateFromFilename() failed for "'.$filename.'"', __FILE__, __LINE__);
  2670. }
  2671. } else {
  2672. $this->DebugMessage('!is_readable('.$filename.')', __FILE__, __LINE__);
  2673. }
  2674. break;
  2675. case 'wmt': // WaterMarkText
  2676. @list($text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle, $bg_color, $bg_opacity, $fillextend, $lineheight) = explode('|', $parameter, 12);
  2677. $text = ($text ? $text : '');
  2678. $size = ($size ? $size : 3);
  2679. $alignment = ($alignment ? $alignment : 'BR');
  2680. $hex_color = ($hex_color ? $hex_color : '000000');
  2681. $ttffont = ($ttffont ? $ttffont : '');
  2682. $opacity = ('' != $opacity ? $opacity : 50);
  2683. $margin = ('' != $margin ? $margin : 5);
  2684. $angle = ('' != $angle ? $angle : 0);
  2685. $bg_color = ($bg_color ? $bg_color : false);
  2686. $bg_opacity = ($bg_opacity ? $bg_opacity : 0);
  2687. $fillextend = ($fillextend ? $fillextend : '');
  2688. $lineheight = ($lineheight ? $lineheight : 1.0);
  2689. if (basename($ttffont) == $ttffont) {
  2690. $ttffont = $this->realPathSafe($this->config_ttf_directory.DIRECTORY_SEPARATOR.$ttffont);
  2691. } else {
  2692. $ttffont = $this->ResolveFilenameToAbsolute($ttffont);
  2693. }
  2694. $phpthumbFilters->WatermarkText($this->gdimg_output, $text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle, $bg_color, $bg_opacity, $fillextend, $lineheight);
  2695. break;
  2696. case 'blur': // Blur
  2697. @list($radius) = explode('|', $parameter, 1);
  2698. $radius = ($radius ? $radius : 1);
  2699. if (phpthumb_functions::gd_version() >= 2) {
  2700. $phpthumbFilters->Blur($this->gdimg_output, $radius);
  2701. } else {
  2702. $this->DebugMessage('Skipping Blur() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2703. }
  2704. break;
  2705. case 'gblr': // Gaussian Blur
  2706. $phpthumbFilters->BlurGaussian($this->gdimg_output);
  2707. break;
  2708. case 'sblr': // Selective Blur
  2709. $phpthumbFilters->BlurSelective($this->gdimg_output);
  2710. break;
  2711. case 'mean': // MeanRemoval blur
  2712. $phpthumbFilters->MeanRemoval($this->gdimg_output);
  2713. break;
  2714. case 'smth': // Smooth blur
  2715. $phpthumbFilters->Smooth($this->gdimg_output, $parameter);
  2716. break;
  2717. case 'usm': // UnSharpMask sharpening
  2718. @list($amount, $radius, $threshold) = explode('|', $parameter, 3);
  2719. $amount = ($amount ? $amount : 80);
  2720. $radius = ($radius ? $radius : 0.5);
  2721. $threshold = ('' !== $threshold ? $threshold : 3);
  2722. if (phpthumb_functions::gd_version() >= 2.0) {
  2723. ob_start();
  2724. if (!@include_once __DIR__ .'/phpthumb.unsharp.php' ) {
  2725. $include_error = ob_get_contents();
  2726. if ($include_error) {
  2727. $this->DebugMessage('include_once("'. __DIR__ .'/phpthumb.unsharp.php") generated message: "'.$include_error.'"', __FILE__, __LINE__);
  2728. }
  2729. $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.unsharp.php" which is required for unsharp masking', __FILE__, __LINE__);
  2730. ob_end_clean();
  2731. return false;
  2732. }
  2733. ob_end_clean();
  2734. phpUnsharpMask::applyUnsharpMask($this->gdimg_output, $amount, $radius, $threshold);
  2735. } else {
  2736. $this->DebugMessage('Skipping unsharp mask because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2737. return false;
  2738. }
  2739. break;
  2740. case 'size': // Resize
  2741. @list($newwidth, $newheight, $stretch) = explode('|', $parameter);
  2742. $newwidth = (!$newwidth ? imagesx($this->gdimg_output) : ((($newwidth > 0) && ($newwidth < 1)) ? round($newwidth * imagesx($this->gdimg_output)) : round($newwidth)));
  2743. $newheight = (!$newheight ? imagesy($this->gdimg_output) : ((($newheight > 0) && ($newheight < 1)) ? round($newheight * imagesy($this->gdimg_output)) : round($newheight)));
  2744. $stretch = ($stretch ? true : false);
  2745. if ($stretch) {
  2746. $scale_x = phpthumb_functions::ScaleToFitInBox(imagesx($this->gdimg_output), imagesx($this->gdimg_output), $newwidth, $newwidth, true, true);
  2747. $scale_y = phpthumb_functions::ScaleToFitInBox(imagesy($this->gdimg_output), imagesy($this->gdimg_output), $newheight, $newheight, true, true);
  2748. } else {
  2749. $scale_x = phpthumb_functions::ScaleToFitInBox(imagesx($this->gdimg_output), imagesy($this->gdimg_output), $newwidth, $newheight, true, true);
  2750. $scale_y = $scale_x;
  2751. }
  2752. $this->DebugMessage('Scaling watermark ('.($stretch ? 'with' : 'without').' stretch) by a factor of "'.number_format($scale_x, 4).' x '.number_format($scale_y, 4).'"', __FILE__, __LINE__);
  2753. if (($scale_x > 1) || ($scale_x < 1) || ($scale_y > 1) || ($scale_y < 1)) {
  2754. if ($img_temp = phpthumb_functions::ImageCreateFunction(imagesx($this->gdimg_output), imagesy($this->gdimg_output))) {
  2755. imagecopy($img_temp, $this->gdimg_output, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output));
  2756. if ($this->gdimg_output = phpthumb_functions::ImageCreateFunction($scale_x * imagesx($img_temp), $scale_y * imagesy($img_temp))) {
  2757. imagealphablending($this->gdimg_output, false);
  2758. imagesavealpha($this->gdimg_output, true);
  2759. $this->ImageResizeFunction($this->gdimg_output, $img_temp, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output), imagesx($img_temp), imagesy($img_temp));
  2760. } else {
  2761. $this->DebugMessage('ImageCreateFunction('.($scale_x * imagesx($img_temp)).', '.($scale_y * imagesy($img_temp)).') failed', __FILE__, __LINE__);
  2762. }
  2763. imagedestroy($img_temp);
  2764. } else {
  2765. $this->DebugMessage('ImageCreateFunction('.imagesx($this->gdimg_output).', '.imagesy($this->gdimg_output).') failed', __FILE__, __LINE__);
  2766. }
  2767. }
  2768. break;
  2769. case 'rot': // ROTate
  2770. @list($angle, $bgcolor) = explode('|', $parameter, 2);
  2771. $phpthumbFilters->ImprovedImageRotate($this->gdimg_output, $angle, $bgcolor, null, $this);
  2772. break;
  2773. case 'stc': // Source Transparent Color
  2774. @list($hexcolor, $min_limit, $max_limit) = explode('|', $parameter, 3);
  2775. if (!phpthumb_functions::IsHexColor($hexcolor)) {
  2776. $this->DebugMessage('Skipping SourceTransparentColor hex color is invalid ('.$hexcolor.')', __FILE__, __LINE__);
  2777. return false;
  2778. }
  2779. $min_limit = ('' !== $min_limit ? $min_limit : 5);
  2780. $max_limit = ('' !== $max_limit ? $max_limit : 10);
  2781. if ($gdimg_mask = $phpthumbFilters->SourceTransparentColorMask($this->gdimg_output, $hexcolor, $min_limit, $max_limit)) {
  2782. $this->is_alpha = true;
  2783. $phpthumbFilters->ApplyMask($gdimg_mask, $this->gdimg_output);
  2784. imagedestroy($gdimg_mask);
  2785. } else {
  2786. $this->DebugMessage('SourceTransparentColorMask() failed for "'.$hexcolor.','.$min_limit.','.$max_limit.'"', __FILE__, __LINE__);
  2787. }
  2788. break;
  2789. }
  2790. $this->DebugMessage('Finished processing filter command "'.$command.'('.$parameter.')"', __FILE__, __LINE__);
  2791. }
  2792. }
  2793. return true;
  2794. }
  2795. public function MaxFileSize() {
  2796. if (phpthumb_functions::gd_version() < 2) {
  2797. $this->DebugMessage('Skipping MaxFileSize() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  2798. return false;
  2799. }
  2800. if ($this->maxb > 0) {
  2801. switch ($this->thumbnailFormat) {
  2802. case 'png':
  2803. case 'gif':
  2804. $imgRenderFunction = 'image'.$this->thumbnailFormat;
  2805. ob_start();
  2806. $imgRenderFunction($this->gdimg_output);
  2807. $imgdata = ob_get_contents();
  2808. ob_end_clean();
  2809. if (strlen($imgdata) > $this->maxb) {
  2810. for ($i = 8; $i >= 1; $i--) {
  2811. $tempIMG = imagecreatetruecolor(imagesx($this->gdimg_output), imagesy($this->gdimg_output));
  2812. imagecopy($tempIMG, $this->gdimg_output, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output));
  2813. imagetruecolortopalette($tempIMG, true, pow(2, $i));
  2814. ob_start();
  2815. $imgRenderFunction($tempIMG);
  2816. $imgdata = ob_get_contents();
  2817. ob_end_clean();
  2818. if (strlen($imgdata) <= $this->maxb) {
  2819. imagetruecolortopalette($this->gdimg_output, true, pow(2, $i));
  2820. break;
  2821. }
  2822. }
  2823. }
  2824. break;
  2825. case 'jpeg':
  2826. ob_start();
  2827. imagejpeg($this->gdimg_output);
  2828. $imgdata = ob_get_contents();
  2829. ob_end_clean();
  2830. if (strlen($imgdata) > $this->maxb) {
  2831. for ($i = 3; $i < 20; $i++) {
  2832. $q = round(100 * (1 - log10($i / 2)));
  2833. ob_start();
  2834. imagejpeg($this->gdimg_output, null, $q);
  2835. $imgdata = ob_get_contents();
  2836. ob_end_clean();
  2837. $this->thumbnailQuality = $q;
  2838. if (strlen($imgdata) <= $this->maxb) {
  2839. break;
  2840. }
  2841. }
  2842. }
  2843. if (strlen($imgdata) > $this->maxb) {
  2844. return false;
  2845. }
  2846. break;
  2847. default:
  2848. return false;
  2849. }
  2850. }
  2851. return true;
  2852. }
  2853. public function CalculateThumbnailDimensions() {
  2854. $this->DebugMessage('CalculateThumbnailDimensions() starting with [W,H,sx,sy,sw,sh] initially set to ['.$this->source_width.','.$this->source_height.','.$this->sx.','.$this->sy.','.$this->sw.','.$this->sh.']', __FILE__, __LINE__);
  2855. //echo $this->source_width.'x'.$this->source_height.'<hr>';
  2856. $this->thumbnailCropX = ($this->sx ? (($this->sx >= 2) ? $this->sx : round($this->sx * $this->source_width)) : 0);
  2857. //echo $this->thumbnailCropX.'<br>';
  2858. $this->thumbnailCropY = ($this->sy ? (($this->sy >= 2) ? $this->sy : round($this->sy * $this->source_height)) : 0);
  2859. //echo $this->thumbnailCropY.'<br>';
  2860. $this->thumbnailCropW = ($this->sw ? (($this->sw >= 2) ? $this->sw : round($this->sw * $this->source_width)) : $this->source_width);
  2861. //echo $this->thumbnailCropW.'<br>';
  2862. $this->thumbnailCropH = ($this->sh ? (($this->sh >= 2) ? $this->sh : round($this->sh * $this->source_height)) : $this->source_height);
  2863. //echo $this->thumbnailCropH.'<hr>';
  2864. // limit source area to original image area
  2865. $this->thumbnailCropW = max(1, min($this->thumbnailCropW, $this->source_width - $this->thumbnailCropX));
  2866. $this->thumbnailCropH = max(1, min($this->thumbnailCropH, $this->source_height - $this->thumbnailCropY));
  2867. $this->DebugMessage('CalculateThumbnailDimensions() starting with [x,y,w,h] initially set to ['.$this->thumbnailCropX.','.$this->thumbnailCropY.','.$this->thumbnailCropW.','.$this->thumbnailCropH.']', __FILE__, __LINE__);
  2868. if ($this->zc && $this->w && $this->h) {
  2869. // Zoom Crop
  2870. // retain proportional resizing we did above, but crop off larger dimension so smaller
  2871. // dimension fully fits available space
  2872. $scaling_X = $this->source_width / $this->w;
  2873. $scaling_Y = $this->source_height / $this->h;
  2874. if ($scaling_X > $scaling_Y) {
  2875. // some of the width will need to be cropped
  2876. $allowable_width = $this->source_width / $scaling_X * $scaling_Y;
  2877. $this->thumbnailCropW = round($allowable_width);
  2878. $this->thumbnailCropX = round(($this->source_width - $allowable_width) / 2);
  2879. } elseif ($scaling_Y > $scaling_X) {
  2880. // some of the height will need to be cropped
  2881. $allowable_height = $this->source_height / $scaling_Y * $scaling_X;
  2882. $this->thumbnailCropH = round($allowable_height);
  2883. $this->thumbnailCropY = round(($this->source_height - $allowable_height) / 2);
  2884. } else {
  2885. // image fits perfectly, no cropping needed
  2886. }
  2887. $this->thumbnail_width = $this->w;
  2888. $this->thumbnail_height = $this->h;
  2889. $this->thumbnail_image_width = $this->thumbnail_width;
  2890. $this->thumbnail_image_height = $this->thumbnail_height;
  2891. } elseif ($this->iar && $this->w && $this->h) {
  2892. // Ignore Aspect Ratio
  2893. // stretch image to fit exactly 'w' x 'h'
  2894. $this->thumbnail_width = $this->w;
  2895. $this->thumbnail_height = $this->h;
  2896. $this->thumbnail_image_width = $this->thumbnail_width;
  2897. $this->thumbnail_image_height = $this->thumbnail_height;
  2898. } else {
  2899. $original_aspect_ratio = $this->thumbnailCropW / $this->thumbnailCropH;
  2900. if ($this->aoe) {
  2901. if ($this->w && $this->h) {
  2902. $maxwidth = min($this->w, $this->h * $original_aspect_ratio);
  2903. $maxheight = min($this->h, $this->w / $original_aspect_ratio);
  2904. } elseif ($this->w) {
  2905. $maxwidth = $this->w;
  2906. $maxheight = $this->w / $original_aspect_ratio;
  2907. } elseif ($this->h) {
  2908. $maxwidth = $this->h * $original_aspect_ratio;
  2909. $maxheight = $this->h;
  2910. } else {
  2911. $maxwidth = $this->thumbnailCropW;
  2912. $maxheight = $this->thumbnailCropH;
  2913. }
  2914. } else {
  2915. $maxwidth = phpthumb_functions::nonempty_min($this->w, $this->thumbnailCropW, $this->config_output_maxwidth);
  2916. $maxheight = phpthumb_functions::nonempty_min($this->h, $this->thumbnailCropH, $this->config_output_maxheight);
  2917. //echo $maxwidth.'x'.$maxheight.'<br>';
  2918. $maxwidth = min($maxwidth, $maxheight * $original_aspect_ratio);
  2919. $maxheight = min($maxheight, $maxwidth / $original_aspect_ratio);
  2920. //echo $maxwidth.'x'.$maxheight.'<hr>';
  2921. }
  2922. $this->thumbnail_image_width = $maxwidth;
  2923. $this->thumbnail_image_height = $maxheight;
  2924. $this->thumbnail_width = $maxwidth;
  2925. $this->thumbnail_height = $maxheight;
  2926. $this->FixedAspectRatio();
  2927. }
  2928. $this->thumbnail_width = max(1, floor($this->thumbnail_width));
  2929. $this->thumbnail_height = max(1, floor($this->thumbnail_height));
  2930. return true;
  2931. }
  2932. public function CreateGDoutput() {
  2933. $this->CalculateThumbnailDimensions();
  2934. // create the GD image (either true-color or 256-color, depending on GD version)
  2935. $this->gdimg_output = phpthumb_functions::ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height);
  2936. // images that have transparency must have the background filled with the configured 'bg' color otherwise the transparent color will appear as black
  2937. imagesavealpha($this->gdimg_output, true);
  2938. if ($this->is_alpha && phpthumb_functions::gd_version() >= 2) {
  2939. imagealphablending($this->gdimg_output, false);
  2940. $output_full_alpha = phpthumb_functions::ImageColorAllocateAlphaSafe($this->gdimg_output, 255, 255, 255, 127);
  2941. imagefilledrectangle($this->gdimg_output, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $output_full_alpha);
  2942. } else {
  2943. $current_transparent_color = imagecolortransparent($this->gdimg_source);
  2944. if ($this->bg || (@$current_transparent_color >= 0)) {
  2945. $this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor);
  2946. if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) {
  2947. return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"');
  2948. }
  2949. $background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_background_hexcolor);
  2950. imagefilledrectangle($this->gdimg_output, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $background_color);
  2951. }
  2952. }
  2953. $this->DebugMessage('CreateGDoutput() returning canvas "'.$this->thumbnail_width.'x'.$this->thumbnail_height.'"', __FILE__, __LINE__);
  2954. return true;
  2955. }
  2956. public function SetOrientationDependantWidthHeight() {
  2957. $this->DebugMessage('SetOrientationDependantWidthHeight() starting with "'.$this->source_width.'"x"'.$this->source_height.'"', __FILE__, __LINE__);
  2958. if ($this->source_height > $this->source_width) {
  2959. // portrait
  2960. $this->w = phpthumb_functions::OneOfThese($this->wp, $this->w, $this->ws, $this->wl);
  2961. $this->h = phpthumb_functions::OneOfThese($this->hp, $this->h, $this->hs, $this->hl);
  2962. } elseif ($this->source_height < $this->source_width) {
  2963. // landscape
  2964. $this->w = phpthumb_functions::OneOfThese($this->wl, $this->w, $this->ws, $this->wp);
  2965. $this->h = phpthumb_functions::OneOfThese($this->hl, $this->h, $this->hs, $this->hp);
  2966. } else {
  2967. // square
  2968. $this->w = phpthumb_functions::OneOfThese($this->ws, $this->w, $this->wl, $this->wp);
  2969. $this->h = phpthumb_functions::OneOfThese($this->hs, $this->h, $this->hl, $this->hp);
  2970. }
  2971. //$this->w = round($this->w ? $this->w : (($this->h && $this->source_height) ? $this->h * $this->source_width / $this->source_height : $this->w));
  2972. //$this->h = round($this->h ? $this->h : (($this->w && $this->source_width) ? $this->w * $this->source_height / $this->source_width : $this->h));
  2973. $this->DebugMessage('SetOrientationDependantWidthHeight() setting w="'. (int) $this->w .'", h="'. (int) $this->h .'"', __FILE__, __LINE__);
  2974. return true;
  2975. }
  2976. public function ExtractEXIFgetImageSize() {
  2977. $this->DebugMessage('starting ExtractEXIFgetImageSize()', __FILE__, __LINE__);
  2978. if (preg_match('#^http:#i', $this->src) && !$this->sourceFilename && $this->rawImageData) {
  2979. $this->SourceDataToTempFile();
  2980. }
  2981. if (null === $this->getimagesizeinfo) {
  2982. if ($this->sourceFilename) {
  2983. $this->getimagesizeinfo = @getimagesize($this->sourceFilename);
  2984. $this->source_width = $this->getimagesizeinfo[0];
  2985. $this->source_height = $this->getimagesizeinfo[1];
  2986. $this->DebugMessage('getimagesize('.$this->sourceFilename.') says image is '.$this->source_width.'x'.$this->source_height, __FILE__, __LINE__);
  2987. } else {
  2988. $this->DebugMessage('skipping getimagesize() because $this->sourceFilename is empty', __FILE__, __LINE__);
  2989. }
  2990. } else {
  2991. $this->DebugMessage('skipping getimagesize() because !is_null($this->getimagesizeinfo)', __FILE__, __LINE__);
  2992. }
  2993. if (is_resource($this->gdimg_source)) {
  2994. $this->source_width = imagesx($this->gdimg_source);
  2995. $this->source_height = imagesy($this->gdimg_source);
  2996. $this->SetOrientationDependantWidthHeight();
  2997. } elseif ($this->rawImageData && !$this->sourceFilename) {
  2998. if ($this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
  2999. $this->DebugMessage('NOT bypassing EXIF and getimagesize sections because source image is too large for GD ('.$this->source_width.'x'.$this->source_width.'='.($this->source_width * $this->source_height * 5).'MB)', __FILE__, __LINE__);
  3000. } else {
  3001. $this->DebugMessage('bypassing EXIF and getimagesize sections because $this->rawImageData is set, and $this->sourceFilename is not set, and source image is not too large for GD ('.$this->source_width.'x'.$this->source_width.'='.($this->source_width * $this->source_height * 5).'MB)', __FILE__, __LINE__);
  3002. }
  3003. }
  3004. if (!empty($this->getimagesizeinfo)) {
  3005. // great
  3006. $this->getimagesizeinfo['filesize'] = @filesize($this->sourceFilename);
  3007. } elseif (!$this->rawImageData) {
  3008. $this->DebugMessage('getimagesize("'.$this->sourceFilename.'") failed', __FILE__, __LINE__);
  3009. }
  3010. if ($this->config_prefer_imagemagick) {
  3011. if ($this->ImageMagickThumbnailToGD()) {
  3012. return true;
  3013. }
  3014. $this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__);
  3015. }
  3016. $this->source_width = $this->getimagesizeinfo[0];
  3017. $this->source_height = $this->getimagesizeinfo[1];
  3018. $this->SetOrientationDependantWidthHeight();
  3019. if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.2.0', '>=') && function_exists('exif_read_data')) {
  3020. switch ($this->getimagesizeinfo[2]) {
  3021. case IMAGETYPE_JPEG:
  3022. case IMAGETYPE_TIFF_II:
  3023. case IMAGETYPE_TIFF_MM:
  3024. $this->exif_raw_data = @exif_read_data($this->sourceFilename, 0, true);
  3025. break;
  3026. }
  3027. }
  3028. if (function_exists('exif_thumbnail') && ($this->getimagesizeinfo[2] == IMAGETYPE_JPEG)) {
  3029. // Extract EXIF info from JPEGs
  3030. $this->exif_thumbnail_width = '';
  3031. $this->exif_thumbnail_height = '';
  3032. $this->exif_thumbnail_type = '';
  3033. // The parameters width, height and imagetype are available since PHP v4.3.0
  3034. if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.3.0', '>=')) {
  3035. $this->exif_thumbnail_data = @exif_thumbnail($this->sourceFilename, $this->exif_thumbnail_width, $this->exif_thumbnail_height, $this->exif_thumbnail_type);
  3036. } else {
  3037. // older versions of exif_thumbnail output an error message but NOT return false on failure
  3038. ob_start();
  3039. $this->exif_thumbnail_data = exif_thumbnail($this->sourceFilename);
  3040. $exit_thumbnail_error = ob_get_contents();
  3041. ob_end_clean();
  3042. if (!$exit_thumbnail_error && $this->exif_thumbnail_data) {
  3043. if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
  3044. $this->exif_thumbnail_width = imagesx($gdimg_exif_temp);
  3045. $this->exif_thumbnail_height = imagesy($gdimg_exif_temp);
  3046. $this->exif_thumbnail_type = 2; // (2 == JPEG) before PHP v4.3.0 only JPEG format EXIF thumbnails are returned
  3047. unset($gdimg_exif_temp);
  3048. } else {
  3049. return $this->ErrorImage('Failed - $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data) in '.__FILE__.' on line '.__LINE__);
  3050. }
  3051. }
  3052. }
  3053. } elseif (!function_exists('exif_thumbnail')) {
  3054. $this->DebugMessage('exif_thumbnail() does not exist, cannot extract EXIF thumbnail', __FILE__, __LINE__);
  3055. }
  3056. $this->DebugMessage('EXIF thumbnail extraction: (size='.strlen($this->exif_thumbnail_data).'; type="'.$this->exif_thumbnail_type.'"; '. (int) $this->exif_thumbnail_width .'x'. (int) $this->exif_thumbnail_height .')', __FILE__, __LINE__);
  3057. // see if EXIF thumbnail can be used directly with no processing
  3058. if ($this->config_use_exif_thumbnail_for_speed && $this->exif_thumbnail_data) {
  3059. while (true) {
  3060. if (!$this->xto) {
  3061. $source_ar = $this->source_width / $this->source_height;
  3062. $exif_ar = $this->exif_thumbnail_width / $this->exif_thumbnail_height;
  3063. if (number_format($source_ar, 2) != number_format($exif_ar, 2)) {
  3064. $this->DebugMessage('not using EXIF thumbnail because $source_ar != $exif_ar ('.$source_ar.' != '.$exif_ar.')', __FILE__, __LINE__);
  3065. break;
  3066. }
  3067. if ($this->w && ($this->w != $this->exif_thumbnail_width)) {
  3068. $this->DebugMessage('not using EXIF thumbnail because $this->w != $this->exif_thumbnail_width ('.$this->w.' != '.$this->exif_thumbnail_width.')', __FILE__, __LINE__);
  3069. break;
  3070. }
  3071. if ($this->h && ($this->h != $this->exif_thumbnail_height)) {
  3072. $this->DebugMessage('not using EXIF thumbnail because $this->h != $this->exif_thumbnail_height ('.$this->h.' != '.$this->exif_thumbnail_height.')', __FILE__, __LINE__);
  3073. break;
  3074. }
  3075. $CannotBeSetParameters = array('sx', 'sy', 'sh', 'sw', 'far', 'bg', 'bc', 'fltr', 'phpThumbDebug');
  3076. foreach ($CannotBeSetParameters as $parameter) {
  3077. if ($this->$parameter) {
  3078. break 2;
  3079. }
  3080. }
  3081. }
  3082. $this->DebugMessage('setting $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data)', __FILE__, __LINE__);
  3083. $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data);
  3084. $this->source_width = imagesx($this->gdimg_source);
  3085. $this->source_height = imagesy($this->gdimg_source);
  3086. return true;
  3087. }
  3088. }
  3089. if (($this->config_max_source_pixels > 0) && (($this->source_width * $this->source_height) > $this->config_max_source_pixels)) {
  3090. // Source image is larger than would fit in available PHP memory.
  3091. // If ImageMagick is installed, use it to generate the thumbnail.
  3092. // Else, if an EXIF thumbnail is available, use that as the source image.
  3093. // Otherwise, no choice but to fail with an error message
  3094. $this->DebugMessage('image is '.$this->source_width.'x'.$this->source_height.' and therefore contains more pixels ('.($this->source_width * $this->source_height).') than $this->config_max_source_pixels setting ('.$this->config_max_source_pixels.')', __FILE__, __LINE__);
  3095. if (!$this->config_prefer_imagemagick && $this->ImageMagickThumbnailToGD()) {
  3096. // excellent, we have a thumbnailed source image
  3097. return true;
  3098. }
  3099. }
  3100. return true;
  3101. }
  3102. public function SetCacheFilename() {
  3103. if (null !== $this->cache_filename) {
  3104. $this->DebugMessage('$this->cache_filename already set, skipping SetCacheFilename()', __FILE__, __LINE__);
  3105. return true;
  3106. }
  3107. if (null === $this->config_cache_directory) {
  3108. $this->setCacheDirectory();
  3109. if (!$this->config_cache_directory) {
  3110. $this->DebugMessage('SetCacheFilename() failed because $this->config_cache_directory is empty', __FILE__, __LINE__);
  3111. return false;
  3112. }
  3113. }
  3114. $this->setOutputFormat();
  3115. if (!$this->sourceFilename && !$this->rawImageData && $this->src) {
  3116. $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src);
  3117. }
  3118. if ($this->config_cache_default_only_suffix && $this->sourceFilename) {
  3119. // simplified cache filenames:
  3120. // only use default parameters in phpThumb.config.php
  3121. // substitute source filename into * in $this->config_cache_default_only_suffix
  3122. // (eg: '*_thumb' becomes 'picture_thumb.jpg')
  3123. if (strpos($this->config_cache_default_only_suffix, '*') === false) {
  3124. $this->DebugMessage('aborting simplified caching filename because no * in "'.$this->config_cache_default_only_suffix.'"', __FILE__, __LINE__);
  3125. } else {
  3126. preg_match('#(.+)(\\.[a-z0-9]+)?$#i', basename($this->sourceFilename), $matches);
  3127. $this->cache_filename = $this->config_cache_directory.DIRECTORY_SEPARATOR.rawurlencode(str_replace('*', @$matches[1], $this->config_cache_default_only_suffix)).'.'.strtolower($this->thumbnailFormat);
  3128. return true;
  3129. }
  3130. }
  3131. $this->cache_filename = '';
  3132. if ($this->new) {
  3133. $broad_directory_name = strtolower(md5($this->new));
  3134. $this->cache_filename .= '_new'.$broad_directory_name;
  3135. } elseif ($this->md5s) {
  3136. // source image MD5 hash provided
  3137. $this->DebugMessage('SetCacheFilename() _raw set from $this->md5s = "'.$this->md5s.'"', __FILE__, __LINE__);
  3138. $broad_directory_name = $this->md5s;
  3139. $this->cache_filename .= '_raw'.$this->md5s;
  3140. } elseif (!$this->src && $this->rawImageData) {
  3141. $this->DebugMessage('SetCacheFilename() _raw set from md5($this->rawImageData) = "'.md5($this->rawImageData).'"', __FILE__, __LINE__);
  3142. $broad_directory_name = strtolower(md5($this->rawImageData));
  3143. $this->cache_filename .= '_raw'.$broad_directory_name;
  3144. } else {
  3145. $this->DebugMessage('SetCacheFilename() _src set from md5($this->sourceFilename) "'.$this->sourceFilename.'" = "'.md5($this->sourceFilename).'"', __FILE__, __LINE__);
  3146. $broad_directory_name = strtolower(md5($this->sourceFilename));
  3147. $this->cache_filename .= '_src'.$broad_directory_name;
  3148. }
  3149. if (!empty($_SERVER['HTTP_REFERER']) && $this->config_nooffsitelink_enabled) {
  3150. $parsed_url1 = @phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']);
  3151. $parsed_url2 = @phpthumb_functions::ParseURLbetter('http://'.@$_SERVER['HTTP_HOST']);
  3152. if (@$parsed_url1['host'] && @$parsed_url2['host'] && ($parsed_url1['host'] != $parsed_url2['host'])) {
  3153. // include "_offsite" only if nooffsitelink_enabled and if referrer doesn't match the domain of the current server
  3154. $this->cache_filename .= '_offsite';
  3155. }
  3156. }
  3157. $ParametersString = '';
  3158. if ($this->fltr && is_array($this->fltr)) {
  3159. $ParametersString .= '_fltr'.implode('_fltr', $this->fltr);
  3160. }
  3161. $FilenameParameters1 = array('ar', 'bg', 'bc', 'far', 'sx', 'sy', 'sw', 'sh', 'zc');
  3162. foreach ($FilenameParameters1 as $key) {
  3163. if ($this->$key) {
  3164. $ParametersString .= '_'.$key.$this->$key;
  3165. }
  3166. }
  3167. $FilenameParameters2 = array('h', 'w', 'wl', 'wp', 'ws', 'hp', 'hs', 'xto', 'ra', 'iar', 'aoe', 'maxb', 'sfn', 'dpi');
  3168. foreach ($FilenameParameters2 as $key) {
  3169. if ($this->$key) {
  3170. $ParametersString .= '_'.$key. (int) $this->$key;
  3171. }
  3172. }
  3173. if ($this->thumbnailFormat == 'jpeg') {
  3174. // only JPEG output has variable quality option
  3175. $ParametersString .= '_q'. (int) $this->thumbnailQuality;
  3176. }
  3177. $this->DebugMessage('SetCacheFilename() _par set from md5('.$ParametersString.')', __FILE__, __LINE__);
  3178. $this->cache_filename .= '_par'.strtolower(md5($ParametersString));
  3179. if ($this->md5s) {
  3180. // source image MD5 hash provided
  3181. // do not source image modification date --
  3182. // cached image will be used even if file was modified or removed
  3183. } elseif (!$this->config_cache_source_filemtime_ignore_remote && preg_match('#^(f|ht)tps?\://#i', $this->src)) {
  3184. $this->cache_filename .= '_dat'. (int) phpthumb_functions::filedate_remote($this->src);
  3185. } elseif (!$this->config_cache_source_filemtime_ignore_local && $this->src && !$this->rawImageData) {
  3186. $this->cache_filename .= '_dat'. (int) (@filemtime($this->sourceFilename));
  3187. }
  3188. $this->cache_filename .= '.'.strtolower($this->thumbnailFormat);
  3189. $broad_directories = '';
  3190. for ($i = 0; $i < $this->config_cache_directory_depth; $i++) {
  3191. $broad_directories .= DIRECTORY_SEPARATOR.substr($broad_directory_name, 0, $i + 1);
  3192. }
  3193. $this->cache_filename = $this->config_cache_directory.$broad_directories.DIRECTORY_SEPARATOR.$this->config_cache_prefix.rawurlencode($this->cache_filename);
  3194. return true;
  3195. }
  3196. public function SourceImageIsTooLarge($width, $height) {
  3197. if (!$this->config_max_source_pixels) {
  3198. return false;
  3199. }
  3200. if ($this->php_memory_limit && function_exists('memory_get_usage')) {
  3201. $available_memory = $this->php_memory_limit - memory_get_usage();
  3202. return (bool) (($width * $height * 5) > $available_memory);
  3203. }
  3204. return (bool) (($width * $height) > $this->config_max_source_pixels);
  3205. }
  3206. public function ImageCreateFromFilename($filename) {
  3207. // try to create GD image source directly via GD, if possible,
  3208. // rather than buffering to memory and creating with imagecreatefromstring
  3209. $ImageCreateWasAttempted = false;
  3210. $gd_image = false;
  3211. $this->DebugMessage('starting ImageCreateFromFilename('.$filename.')', __FILE__, __LINE__);
  3212. if ($filename && ($getimagesizeinfo = @getimagesize($filename))) {
  3213. if (!$this->SourceImageIsTooLarge($getimagesizeinfo[0], $getimagesizeinfo[1])) {
  3214. $ImageCreateFromFunction = array(
  3215. 1 => 'imagecreatefromgif',
  3216. 2 => 'imagecreatefromjpeg',
  3217. 3 => 'imagecreatefrompng',
  3218. 15 => 'imagecreatefromwbmp',
  3219. );
  3220. $this->DebugMessage('ImageCreateFromFilename found ($getimagesizeinfo[2]=='.@$getimagesizeinfo[2].')', __FILE__, __LINE__);
  3221. switch (@$getimagesizeinfo[2]) {
  3222. case 1: // GIF
  3223. case 2: // JPEG
  3224. case 3: // PNG
  3225. case 15: // WBMP
  3226. $ImageCreateFromFunctionName = $ImageCreateFromFunction[$getimagesizeinfo[2]];
  3227. if (function_exists($ImageCreateFromFunctionName)) {
  3228. $this->DebugMessage('Calling '.$ImageCreateFromFunctionName.'('.$filename.')', __FILE__, __LINE__);
  3229. $ImageCreateWasAttempted = true;
  3230. $gd_image = $ImageCreateFromFunctionName($filename);
  3231. } else {
  3232. $this->DebugMessage('NOT calling '.$ImageCreateFromFunctionName.'('.$filename.') because !function_exists('.$ImageCreateFromFunctionName.')', __FILE__, __LINE__);
  3233. }
  3234. break;
  3235. case 4: // SWF
  3236. case 5: // PSD
  3237. case 6: // BMP
  3238. case 7: // TIFF (LE)
  3239. case 8: // TIFF (BE)
  3240. case 9: // JPC
  3241. case 10: // JP2
  3242. case 11: // JPX
  3243. case 12: // JB2
  3244. case 13: // SWC
  3245. case 14: // IFF
  3246. case 16: // XBM
  3247. $this->DebugMessage('No built-in image creation function for image type "'.@$getimagesizeinfo[2].'" ($getimagesizeinfo[2])', __FILE__, __LINE__);
  3248. break;
  3249. default:
  3250. $this->DebugMessage('Unknown value for $getimagesizeinfo[2]: "'.@$getimagesizeinfo[2].'"', __FILE__, __LINE__);
  3251. break;
  3252. }
  3253. } else {
  3254. $this->DebugMessage('image is '.$getimagesizeinfo[0].'x'.$getimagesizeinfo[1].' and therefore contains more pixels ('.($getimagesizeinfo[0] * $getimagesizeinfo[1]).') than $this->config_max_source_pixels setting ('.$this->config_max_source_pixels.')', __FILE__, __LINE__);
  3255. return false;
  3256. }
  3257. } else {
  3258. $this->DebugMessage('empty $filename or getimagesize('.$filename.') failed', __FILE__, __LINE__);
  3259. }
  3260. if (!$gd_image) {
  3261. // cannot create from filename, attempt to create source image with imagecreatefromstring, if possible
  3262. if ($ImageCreateWasAttempted) {
  3263. $this->DebugMessage($ImageCreateFromFunctionName.'() was attempted but FAILED', __FILE__, __LINE__);
  3264. }
  3265. $this->DebugMessage('Populating $rawimagedata', __FILE__, __LINE__);
  3266. $rawimagedata = '';
  3267. if ($fp = @fopen($filename, 'rb')) {
  3268. $filesize = filesize($filename);
  3269. $blocksize = 8192;
  3270. $blockreads = ceil($filesize / $blocksize);
  3271. for ($i = 0; $i < $blockreads; $i++) {
  3272. $rawimagedata .= fread($fp, $blocksize);
  3273. }
  3274. fclose($fp);
  3275. } else {
  3276. $this->DebugMessage('cannot fopen('.$filename.')', __FILE__, __LINE__);
  3277. }
  3278. if ($rawimagedata) {
  3279. $this->DebugMessage('attempting ImageCreateFromStringReplacement($rawimagedata ('.strlen($rawimagedata).' bytes), true)', __FILE__, __LINE__);
  3280. $gd_image = $this->ImageCreateFromStringReplacement($rawimagedata, true);
  3281. }
  3282. }
  3283. return $gd_image;
  3284. }
  3285. public function SourceImageToGD() {
  3286. if (is_resource($this->gdimg_source)) {
  3287. $this->source_width = imagesx($this->gdimg_source);
  3288. $this->source_height = imagesy($this->gdimg_source);
  3289. $this->DebugMessage('skipping SourceImageToGD() because $this->gdimg_source is already a resource ('.$this->source_width.'x'.$this->source_height.')', __FILE__, __LINE__);
  3290. return true;
  3291. }
  3292. $this->DebugMessage('starting SourceImageToGD()', __FILE__, __LINE__);
  3293. if ($this->config_prefer_imagemagick) {
  3294. if (empty($this->sourceFilename) && !empty($this->rawImageData)) {
  3295. $this->DebugMessage('Copying raw image data to temp file and trying again with ImageMagick', __FILE__, __LINE__);
  3296. if ($tempnam = $this->phpThumb_tempnam()) {
  3297. if (file_put_contents($tempnam, $this->rawImageData)) {
  3298. $this->sourceFilename = $tempnam;
  3299. if ($this->ImageMagickThumbnailToGD()) {
  3300. // excellent, we have a thumbnailed source image
  3301. $this->DebugMessage('ImageMagickThumbnailToGD() succeeded', __FILE__, __LINE__);
  3302. } else {
  3303. $this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__);
  3304. }
  3305. @chmod($tempnam, $this->getParameter('config_file_create_mask'));
  3306. } else {
  3307. $this->DebugMessage('failed to put $this->rawImageData into temp file "'.$tempnam.'"', __FILE__, __LINE__);
  3308. }
  3309. } else {
  3310. $this->DebugMessage('failed to generate temp file name', __FILE__, __LINE__);
  3311. }
  3312. }
  3313. }
  3314. if (!$this->gdimg_source && $this->rawImageData) {
  3315. if ($this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
  3316. $memory_get_usage = (function_exists('memory_get_usage') ? memory_get_usage() : 0);
  3317. return $this->ErrorImage('Source image is too large ('.$this->source_width.'x'.$this->source_height.' = '.number_format($this->source_width * $this->source_height / 1000000, 1).'Mpx, max='.number_format($this->config_max_source_pixels / 1000000, 1).'Mpx) for GD creation (either install ImageMagick or increase PHP memory_limit to at least '.ceil(($memory_get_usage + (5 * $this->source_width * $this->source_height)) / 1048576).'M).');
  3318. }
  3319. if ($this->md5s && ($this->md5s != md5($this->rawImageData))) {
  3320. return $this->ErrorImage('$this->md5s != md5($this->rawImageData)'."\n".'"'.$this->md5s.'" != '."\n".'"'.md5($this->rawImageData).'"');
  3321. }
  3322. //if ($this->issafemode) {
  3323. // return $this->ErrorImage('Cannot generate thumbnails from raw image data when PHP SAFE_MODE enabled');
  3324. //}
  3325. $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->rawImageData);
  3326. if (!$this->gdimg_source) {
  3327. if (substr($this->rawImageData, 0, 2) === 'BM') {
  3328. $this->getimagesizeinfo[2] = 6; // BMP
  3329. } elseif (substr($this->rawImageData, 0, 4) === 'II'."\x2A\x00") {
  3330. $this->getimagesizeinfo[2] = 7; // TIFF (littlendian)
  3331. } elseif (substr($this->rawImageData, 0, 4) === 'MM'."\x00\x2A") {
  3332. $this->getimagesizeinfo[2] = 8; // TIFF (bigendian)
  3333. }
  3334. $this->DebugMessage('SourceImageToGD.ImageCreateFromStringReplacement() failed with unknown image type "'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).')', __FILE__, __LINE__);
  3335. // return $this->ErrorImage('Unknown image type identified by "'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).') in SourceImageToGD()['.__LINE__.']');
  3336. }
  3337. } elseif (!$this->gdimg_source && $this->sourceFilename) {
  3338. if ($this->md5s && ($this->md5s != phpthumb_functions::md5_file_safe($this->sourceFilename))) {
  3339. return $this->ErrorImage('$this->md5s != md5(sourceFilename)'."\n".'"'.$this->md5s.'" != '."\n".'"'.phpthumb_functions::md5_file_safe($this->sourceFilename).'"');
  3340. }
  3341. switch (@$this->getimagesizeinfo[2]) {
  3342. case 1:
  3343. case 3:
  3344. // GIF or PNG input file may have transparency
  3345. $this->is_alpha = true;
  3346. break;
  3347. }
  3348. if (!$this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
  3349. $this->gdimg_source = $this->ImageCreateFromFilename($this->sourceFilename);
  3350. }
  3351. }
  3352. while (true) {
  3353. if ($this->gdimg_source) {
  3354. $this->DebugMessage('Not using EXIF thumbnail data because $this->gdimg_source is already set', __FILE__, __LINE__);
  3355. break;
  3356. }
  3357. if (!$this->exif_thumbnail_data) {
  3358. $this->DebugMessage('Not using EXIF thumbnail data because $this->exif_thumbnail_data is empty', __FILE__, __LINE__);
  3359. break;
  3360. }
  3361. if (ini_get('safe_mode')) {
  3362. if (!$this->SourceImageIsTooLarge($this->source_width, $this->source_height)) {
  3363. $this->DebugMessage('Using EXIF thumbnail data because source image too large and safe_mode enabled', __FILE__, __LINE__);
  3364. $this->aoe = true;
  3365. } else {
  3366. break;
  3367. }
  3368. } else {
  3369. if (!$this->config_use_exif_thumbnail_for_speed) {
  3370. $this->DebugMessage('Not using EXIF thumbnail data because $this->config_use_exif_thumbnail_for_speed is FALSE', __FILE__, __LINE__);
  3371. break;
  3372. }
  3373. if (($this->thumbnailCropX != 0) || ($this->thumbnailCropY != 0)) {
  3374. $this->DebugMessage('Not using EXIF thumbnail data because source cropping is enabled ('.$this->thumbnailCropX.','.$this->thumbnailCropY.')', __FILE__, __LINE__);
  3375. break;
  3376. }
  3377. if (($this->w > $this->exif_thumbnail_width) || ($this->h > $this->exif_thumbnail_height)) {
  3378. $this->DebugMessage('Not using EXIF thumbnail data because EXIF thumbnail is too small ('.$this->exif_thumbnail_width.'x'.$this->exif_thumbnail_height.' vs '.$this->w.'x'.$this->h.')', __FILE__, __LINE__);
  3379. break;
  3380. }
  3381. $source_ar = $this->source_width / $this->source_height;
  3382. $exif_ar = $this->exif_thumbnail_width / $this->exif_thumbnail_height;
  3383. if (number_format($source_ar, 2) != number_format($exif_ar, 2)) {
  3384. $this->DebugMessage('not using EXIF thumbnail because $source_ar != $exif_ar ('.$source_ar.' != '.$exif_ar.')', __FILE__, __LINE__);
  3385. break;
  3386. }
  3387. }
  3388. // EXIF thumbnail exists, and is equal to or larger than destination thumbnail, and will be use as source image
  3389. $this->DebugMessage('Trying to use EXIF thumbnail as source image', __FILE__, __LINE__);
  3390. if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
  3391. $this->DebugMessage('Successfully using EXIF thumbnail as source image', __FILE__, __LINE__);
  3392. $this->gdimg_source = $gdimg_exif_temp;
  3393. $this->source_width = $this->exif_thumbnail_width;
  3394. $this->source_height = $this->exif_thumbnail_height;
  3395. $this->thumbnailCropW = $this->source_width;
  3396. $this->thumbnailCropH = $this->source_height;
  3397. return true;
  3398. } else {
  3399. $this->DebugMessage('$this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false) failed', __FILE__, __LINE__);
  3400. }
  3401. break;
  3402. }
  3403. if (!$this->gdimg_source) {
  3404. $this->DebugMessage('$this->gdimg_source is still empty', __FILE__, __LINE__);
  3405. $this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__);
  3406. $imageHeader = '';
  3407. $gd_info = gd_info();
  3408. $GDreadSupport = false;
  3409. switch (@$this->getimagesizeinfo[2]) {
  3410. case 1:
  3411. $imageHeader = 'Content-Type: image/gif';
  3412. $GDreadSupport = (bool) @$gd_info['GIF Read Support'];
  3413. break;
  3414. case 2:
  3415. $imageHeader = 'Content-Type: image/jpeg';
  3416. $GDreadSupport = (bool) @$gd_info['JPG Support'];
  3417. break;
  3418. case 3:
  3419. $imageHeader = 'Content-Type: image/png';
  3420. $GDreadSupport = (bool) @$gd_info['PNG Support'];
  3421. break;
  3422. }
  3423. if ($imageHeader) {
  3424. // cannot create image for whatever reason (maybe imagecreatefromjpeg et al are not available?)
  3425. // and ImageMagick is not available either, no choice but to output original (not resized/modified) data and exit
  3426. if ($this->config_error_die_on_source_failure) {
  3427. $errormessages = array();
  3428. $errormessages[] = 'All attempts to create GD image source failed.';
  3429. if ($this->fatalerror) {
  3430. $errormessages[] = $this->fatalerror;
  3431. }
  3432. if ($this->issafemode) {
  3433. $errormessages[] = 'Safe Mode enabled, therefore ImageMagick is unavailable. (disable Safe Mode if possible)';
  3434. } elseif (!$this->ImageMagickVersion()) {
  3435. $errormessages[] = 'ImageMagick is not installed (it is highly recommended that you install it).';
  3436. }
  3437. if ($this->SourceImageIsTooLarge($this->getimagesizeinfo[0], $this->getimagesizeinfo[1])) {
  3438. $memory_get_usage = (function_exists('memory_get_usage') ? memory_get_usage() : 0);
  3439. $errormessages[] = 'Source image is too large ('.$this->getimagesizeinfo[0].'x'.$this->getimagesizeinfo[1].' = '.number_format($this->getimagesizeinfo[0] * $this->getimagesizeinfo[1] / 1000000, 1).'Mpx, max='.number_format($this->config_max_source_pixels / 1000000, 1).'Mpx) for GD creation (either install ImageMagick or increase PHP memory_limit to at least '.ceil(($memory_get_usage + (5 * $this->getimagesizeinfo[0] * $this->getimagesizeinfo[1])) / 1048576).'M).';
  3440. } elseif (!$GDreadSupport) {
  3441. $errormessages[] = 'GD does not have read support for "'.$imageHeader.'".';
  3442. } else {
  3443. $errormessages[] = 'Source image probably corrupt.';
  3444. }
  3445. $this->ErrorImage(implode("\n", $errormessages));
  3446. } else {
  3447. $this->DebugMessage('All attempts to create GD image source failed ('.(ini_get('safe_mode') ? 'Safe Mode enabled, ImageMagick unavailable and source image probably too large for GD': ($GDreadSupport ? 'source image probably corrupt' : 'GD does not have read support for "'.$imageHeader.'"')).'), cannot generate thumbnail');
  3448. //$this->DebugMessage('All attempts to create GD image source failed ('.($GDreadSupport ? 'source image probably corrupt' : 'GD does not have read support for "'.$imageHeader.'"').'), outputing raw image', __FILE__, __LINE__);
  3449. //if (!$this->phpThumbDebug) {
  3450. // header($imageHeader);
  3451. // echo $this->rawImageData;
  3452. // exit;
  3453. //}
  3454. return false;
  3455. }
  3456. }
  3457. //switch (substr($this->rawImageData, 0, 2)) {
  3458. // case 'BM':
  3459. switch (@$this->getimagesizeinfo[2]) {
  3460. case 6:
  3461. ob_start();
  3462. if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) {
  3463. ob_end_clean();
  3464. return $this->ErrorImage('include_once('. __DIR__ .'/phpthumb.bmp.php) failed');
  3465. }
  3466. ob_end_clean();
  3467. if ($fp = @fopen($this->sourceFilename, 'rb')) {
  3468. $this->rawImageData = '';
  3469. while (!feof($fp)) {
  3470. $this->rawImageData .= fread($fp, 32768);
  3471. }
  3472. fclose($fp);
  3473. }
  3474. $phpthumb_bmp = new phpthumb_bmp();
  3475. $this->gdimg_source = $phpthumb_bmp->phpthumb_bmp2gd($this->rawImageData, phpthumb_functions::gd_version() >= 2.0);
  3476. unset($phpthumb_bmp);
  3477. if ($this->gdimg_source) {
  3478. $this->DebugMessage('$phpthumb_bmp->phpthumb_bmp2gd() succeeded', __FILE__, __LINE__);
  3479. } else {
  3480. return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on BMP source conversion' : 'phpthumb_bmp2gd() failed');
  3481. }
  3482. break;
  3483. //}
  3484. //switch (substr($this->rawImageData, 0, 4)) {
  3485. // case 'II'."\x2A\x00":
  3486. // case 'MM'."\x00\x2A":
  3487. case 7:
  3488. case 8:
  3489. return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on TIFF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support TIFF source images without it');
  3490. break;
  3491. //case "\xD7\xCD\xC6\x9A":
  3492. // return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on WMF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support WMF source images without it');
  3493. // break;
  3494. }
  3495. if (!$this->gdimg_source) {
  3496. if ($this->rawImageData) {
  3497. $HeaderFourBytes = substr($this->rawImageData, 0, 4);
  3498. } elseif ($this->sourceFilename) {
  3499. if ($fp = @fopen($this->sourceFilename, 'rb')) {
  3500. $HeaderFourBytes = fread($fp, 4);
  3501. fclose($fp);
  3502. } else {
  3503. return $this->ErrorImage('failed to open "'.$this->sourceFilename.'" SourceImageToGD() ['.__LINE__.']');
  3504. }
  3505. } else {
  3506. return $this->ErrorImage('Unable to create image, neither filename nor image data suppplied in SourceImageToGD() ['.__LINE__.']');
  3507. }
  3508. if (!$this->ImageMagickVersion() && !phpthumb_functions::gd_version()) {
  3509. return $this->ErrorImage('Neither GD nor ImageMagick seem to be installed on this server. At least one (preferably GD), or better both, MUST be installed for phpThumb to work.');
  3510. } elseif ($HeaderFourBytes == "\xD7\xCD\xC6\x9A") { // WMF
  3511. return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on WMF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support WMF source images without it');
  3512. } elseif ($HeaderFourBytes == '%PDF') { // "%PDF"
  3513. return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick and GhostScript are both required for PDF source images; GhostScript may not be properly configured' : 'ImageMagick and/or GhostScript are unavailable and phpThumb() does not support PDF source images without them');
  3514. } elseif (substr($HeaderFourBytes, 0, 3) == "\xFF\xD8\xFF") { // JPEG
  3515. return $this->ErrorImage('Image (JPEG) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting');
  3516. } elseif ($HeaderFourBytes == '%PNG') { // "%PNG"
  3517. return $this->ErrorImage('Image (PNG) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting');
  3518. } elseif (substr($HeaderFourBytes, 0, 3) == 'GIF') { // GIF
  3519. return $this->ErrorImage('Image (GIF) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting');
  3520. }
  3521. return $this->ErrorImage('Unknown image type identified by "'.$HeaderFourBytes.'" ('.phpthumb_functions::HexCharDisplay($HeaderFourBytes).') in SourceImageToGD() ['.__LINE__.']');
  3522. }
  3523. }
  3524. if (!$this->gdimg_source) {
  3525. if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) {
  3526. $this->DebugMessage('All other attempts failed, but successfully using EXIF thumbnail as source image', __FILE__, __LINE__);
  3527. $this->gdimg_source = $gdimg_exif_temp;
  3528. // override allow-enlarging setting if EXIF thumbnail is the only source available
  3529. // otherwise thumbnails larger than the EXIF thumbnail will be created at EXIF size
  3530. $this->aoe = true;
  3531. return true;
  3532. }
  3533. return false;
  3534. }
  3535. $this->source_width = imagesx($this->gdimg_source);
  3536. $this->source_height = imagesy($this->gdimg_source);
  3537. return true;
  3538. }
  3539. public function phpThumbDebugVarDump($var) {
  3540. if (null === $var) {
  3541. return 'NULL';
  3542. } elseif (is_bool($var)) {
  3543. return ($var ? 'TRUE' : 'FALSE');
  3544. } elseif (is_string($var)) {
  3545. return 'string('.strlen($var).')'.str_repeat(' ', max(0, 3 - strlen(strlen($var)))).' "'.$var.'"';
  3546. } elseif (is_int($var)) {
  3547. return 'integer '.$var;
  3548. } elseif (is_float($var)) {
  3549. return 'float '.$var;
  3550. } elseif (is_array($var)) {
  3551. ob_start();
  3552. var_dump($var);
  3553. $vardumpoutput = ob_get_contents();
  3554. ob_end_clean();
  3555. return strtr($vardumpoutput, "\n\r\t", ' ');
  3556. }
  3557. return gettype($var);
  3558. }
  3559. public function phpThumbDebug($level='') {
  3560. if ($level && ($this->phpThumbDebug !== $level)) {
  3561. return true;
  3562. }
  3563. if ($this->config_disable_debug) {
  3564. return $this->ErrorImage('phpThumbDebug disabled');
  3565. }
  3566. $FunctionsExistance = array('exif_thumbnail', 'gd_info', 'image_type_to_mime_type', 'getimagesize', 'imagecopyresampled', 'imagecopyresized', 'imagecreate', 'imagecreatefromstring', 'imagecreatetruecolor', 'imageistruecolor', 'imagerotate', 'imagetypes', 'version_compare', 'imagecreatefromgif', 'imagecreatefromjpeg', 'imagecreatefrompng', 'imagecreatefromwbmp', 'imagecreatefromxbm', 'imagecreatefromxpm', 'imagecreatefromstring', 'imagecreatefromgd', 'imagecreatefromgd2', 'imagecreatefromgd2part', 'imagejpeg', 'imagegif', 'imagepng', 'imagewbmp');
  3567. $ParameterNames = array('src', 'new', 'w', 'h', 'f', 'q', 'sx', 'sy', 'sw', 'sh', 'far', 'bg', 'bc', 'file', 'goto', 'err', 'xto', 'ra', 'ar', 'aoe', 'iar', 'maxb');
  3568. $ConfigVariableNames = array('document_root', 'temp_directory', 'output_format', 'output_maxwidth', 'output_maxheight', 'error_message_image_default', 'error_bgcolor', 'error_textcolor', 'error_fontsize', 'error_die_on_error', 'error_silent_die_on_error', 'error_die_on_source_failure', 'nohotlink_enabled', 'nohotlink_valid_domains', 'nohotlink_erase_image', 'nohotlink_text_message', 'nooffsitelink_enabled', 'nooffsitelink_valid_domains', 'nooffsitelink_require_refer', 'nooffsitelink_erase_image', 'nooffsitelink_text_message', 'high_security_enabled', 'allow_src_above_docroot', 'allow_src_above_phpthumb', 'max_source_pixels', 'use_exif_thumbnail_for_speed', 'border_hexcolor', 'background_hexcolor', 'ttf_directory', 'disable_pathinfo_parsing', 'disable_imagecopyresampled');
  3569. $OtherVariableNames = array('phpThumbDebug', 'thumbnailQuality', 'thumbnailFormat', 'gdimg_output', 'gdimg_source', 'sourceFilename', 'source_width', 'source_height', 'thumbnailCropX', 'thumbnailCropY', 'thumbnailCropW', 'thumbnailCropH', 'exif_thumbnail_width', 'exif_thumbnail_height', 'exif_thumbnail_type', 'thumbnail_width', 'thumbnail_height', 'thumbnail_image_width', 'thumbnail_image_height');
  3570. $DebugOutput = array();
  3571. $DebugOutput[] = 'phpThumb() version = '.$this->phpthumb_version;
  3572. $DebugOutput[] = 'phpversion() = '.@PHP_VERSION;
  3573. $DebugOutput[] = 'PHP_OS = '.PHP_OS;
  3574. $DebugOutput[] = '$_SERVER[SERVER_SOFTWARE] = '.@$_SERVER['SERVER_SOFTWARE'];
  3575. $DebugOutput[] = '__FILE__ = '.__FILE__;
  3576. $DebugOutput[] = 'realpath(.) = '.@realpath('.');
  3577. $DebugOutput[] = '$_SERVER[PHP_SELF] = '.@$_SERVER['PHP_SELF'];
  3578. $DebugOutput[] = '$_SERVER[HOST_NAME] = '.@$_SERVER['HOST_NAME'];
  3579. $DebugOutput[] = '$_SERVER[HTTP_REFERER] = '.@$_SERVER['HTTP_REFERER'];
  3580. $DebugOutput[] = '$_SERVER[QUERY_STRING] = '.@$_SERVER['QUERY_STRING'];
  3581. $DebugOutput[] = '$_SERVER[PATH_INFO] = '.@$_SERVER['PATH_INFO'];
  3582. $DebugOutput[] = '$_SERVER[DOCUMENT_ROOT] = '.@$_SERVER['DOCUMENT_ROOT'];
  3583. $DebugOutput[] = 'getenv(DOCUMENT_ROOT) = '.@getenv('DOCUMENT_ROOT');
  3584. $DebugOutput[] = '';
  3585. $DebugOutput[] = 'get_magic_quotes_gpc() = '.$this->phpThumbDebugVarDump(@get_magic_quotes_gpc());
  3586. $DebugOutput[] = 'get_magic_quotes_runtime() = '.$this->phpThumbDebugVarDump(@get_magic_quotes_runtime());
  3587. $DebugOutput[] = 'error_reporting() = '.$this->phpThumbDebugVarDump(error_reporting());
  3588. $DebugOutput[] = 'ini_get(error_reporting) = '.$this->phpThumbDebugVarDump(@ini_get('error_reporting'));
  3589. $DebugOutput[] = 'ini_get(display_errors) = '.$this->phpThumbDebugVarDump(@ini_get('display_errors'));
  3590. $DebugOutput[] = 'ini_get(allow_url_fopen) = '.$this->phpThumbDebugVarDump(@ini_get('allow_url_fopen'));
  3591. $DebugOutput[] = 'ini_get(disable_functions) = '.$this->phpThumbDebugVarDump(@ini_get('disable_functions'));
  3592. $DebugOutput[] = 'get_cfg_var(disable_functions) = '.$this->phpThumbDebugVarDump(@get_cfg_var('disable_functions'));
  3593. $DebugOutput[] = 'ini_get(safe_mode) = '.$this->phpThumbDebugVarDump(@ini_get('safe_mode'));
  3594. $DebugOutput[] = 'ini_get(open_basedir) = '.$this->phpThumbDebugVarDump(@ini_get('open_basedir'));
  3595. $DebugOutput[] = 'ini_get(max_execution_time) = '.$this->phpThumbDebugVarDump(@ini_get('max_execution_time'));
  3596. $DebugOutput[] = 'ini_get(memory_limit) = '.$this->phpThumbDebugVarDump(@ini_get('memory_limit'));
  3597. $DebugOutput[] = 'get_cfg_var(memory_limit) = '.$this->phpThumbDebugVarDump(@get_cfg_var('memory_limit'));
  3598. $DebugOutput[] = 'memory_get_usage() = '.(function_exists('memory_get_usage') ? $this->phpThumbDebugVarDump(@memory_get_usage()) : 'n/a');
  3599. $DebugOutput[] = '';
  3600. $DebugOutput[] = '$this->config_prefer_imagemagick = '.$this->phpThumbDebugVarDump($this->config_prefer_imagemagick);
  3601. $DebugOutput[] = '$this->config_imagemagick_path = '.$this->phpThumbDebugVarDump($this->config_imagemagick_path);
  3602. $DebugOutput[] = '$this->ImageMagickWhichConvert() = '.$this->ImageMagickWhichConvert();
  3603. $IMpathUsed = ($this->config_imagemagick_path ? $this->config_imagemagick_path : $this->ImageMagickWhichConvert());
  3604. $DebugOutput[] = '[actual ImageMagick path used] = '.$this->phpThumbDebugVarDump($IMpathUsed);
  3605. $DebugOutput[] = 'file_exists([actual ImageMagick path used]) = '.$this->phpThumbDebugVarDump(@file_exists($IMpathUsed));
  3606. $DebugOutput[] = 'ImageMagickVersion(false) = '.$this->ImageMagickVersion(false);
  3607. $DebugOutput[] = 'ImageMagickVersion(true) = '.$this->ImageMagickVersion(true);
  3608. $DebugOutput[] = '';
  3609. $DebugOutput[] = '$this->config_cache_directory = '.$this->phpThumbDebugVarDump($this->config_cache_directory);
  3610. $DebugOutput[] = '$this->config_cache_directory_depth = '.$this->phpThumbDebugVarDump($this->config_cache_directory_depth);
  3611. $DebugOutput[] = '$this->config_cache_disable_warning = '.$this->phpThumbDebugVarDump($this->config_cache_disable_warning);
  3612. $DebugOutput[] = '$this->config_cache_maxage = '.$this->phpThumbDebugVarDump($this->config_cache_maxage);
  3613. $DebugOutput[] = '$this->config_cache_maxsize = '.$this->phpThumbDebugVarDump($this->config_cache_maxsize);
  3614. $DebugOutput[] = '$this->config_cache_maxfiles = '.$this->phpThumbDebugVarDump($this->config_cache_maxfiles);
  3615. $DebugOutput[] = '$this->config_cache_force_passthru = '.$this->phpThumbDebugVarDump($this->config_cache_force_passthru);
  3616. $DebugOutput[] = '$this->cache_filename = '.$this->phpThumbDebugVarDump($this->cache_filename);
  3617. $DebugOutput[] = 'is_readable($this->config_cache_directory) = '.$this->phpThumbDebugVarDump(@is_readable($this->config_cache_directory));
  3618. $DebugOutput[] = 'is_writable($this->config_cache_directory) = '.$this->phpThumbDebugVarDump(@is_writable($this->config_cache_directory));
  3619. $DebugOutput[] = 'is_readable($this->cache_filename) = '.$this->phpThumbDebugVarDump(@is_readable($this->cache_filename));
  3620. $DebugOutput[] = 'is_writable($this->cache_filename) = '.(@file_exists($this->cache_filename) ? $this->phpThumbDebugVarDump(@is_writable($this->cache_filename)) : 'n/a');
  3621. $DebugOutput[] = '';
  3622. foreach ($ConfigVariableNames as $varname) {
  3623. $varname = 'config_'.$varname;
  3624. $value = $this->$varname;
  3625. $DebugOutput[] = '$this->'.str_pad($varname, 37, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3626. }
  3627. $DebugOutput[] = '';
  3628. foreach ($OtherVariableNames as $varname) {
  3629. $value = $this->$varname;
  3630. $DebugOutput[] = '$this->'.str_pad($varname, 27, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3631. }
  3632. $DebugOutput[] = 'strlen($this->rawImageData) = '.strlen(@$this->rawImageData);
  3633. $DebugOutput[] = 'strlen($this->exif_thumbnail_data) = '.strlen(@$this->exif_thumbnail_data);
  3634. $DebugOutput[] = '';
  3635. foreach ($ParameterNames as $varname) {
  3636. $value = $this->$varname;
  3637. $DebugOutput[] = '$this->'.str_pad($varname, 4, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3638. }
  3639. $DebugOutput[] = '';
  3640. foreach ($FunctionsExistance as $functionname) {
  3641. $DebugOutput[] = 'builtin_function_exists('.$functionname.')'.str_repeat(' ', 23 - strlen($functionname)).' = '.$this->phpThumbDebugVarDump(phpthumb_functions::builtin_function_exists($functionname));
  3642. }
  3643. $DebugOutput[] = '';
  3644. $gd_info = gd_info();
  3645. foreach ($gd_info as $key => $value) {
  3646. $DebugOutput[] = 'gd_info.'.str_pad($key, 34, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3647. }
  3648. $DebugOutput[] = '';
  3649. $exif_info = phpthumb_functions::exif_info();
  3650. foreach ($exif_info as $key => $value) {
  3651. $DebugOutput[] = 'exif_info.'.str_pad($key, 26, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3652. }
  3653. $DebugOutput[] = '';
  3654. if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray(dirname(@$_SERVER['PHP_SELF']))) {
  3655. foreach ($ApacheLookupURIarray as $key => $value) {
  3656. $DebugOutput[] = 'ApacheLookupURIarray.'.str_pad($key, 15, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value);
  3657. }
  3658. } else {
  3659. $DebugOutput[] = 'ApacheLookupURIarray() -- FAILED';
  3660. }
  3661. $DebugOutput[] = '';
  3662. if (isset($_GET) && is_array($_GET)) {
  3663. foreach ($_GET as $key => $value) {
  3664. $DebugOutput[] = '$_GET['.$key.']'.str_repeat(' ', 30 - strlen($key)).'= '.$this->phpThumbDebugVarDump($value);
  3665. }
  3666. }
  3667. if (isset($_POST) && is_array($_POST)) {
  3668. foreach ($_POST as $key => $value) {
  3669. $DebugOutput[] = '$_POST['.$key.']'.str_repeat(' ', 29 - strlen($key)).'= '.$this->phpThumbDebugVarDump($value);
  3670. }
  3671. }
  3672. $DebugOutput[] = '';
  3673. $DebugOutput[] = '$this->debugmessages:';
  3674. foreach ($this->debugmessages as $errorstring) {
  3675. $DebugOutput[] = ' * '.$errorstring;
  3676. }
  3677. $DebugOutput[] = '';
  3678. $DebugOutput[] = '$this->debugtiming:';
  3679. foreach ($this->debugtiming as $timestamp => $timingstring) {
  3680. $DebugOutput[] = ' * '.$timestamp.' '.$timingstring;
  3681. }
  3682. $DebugOutput[] = ' * Total processing time: '.number_format(max(array_keys($this->debugtiming)) - min(array_keys($this->debugtiming)), 6);
  3683. $this->f = (isset($_GET['f']) ? $_GET['f'] : $this->f); // debug modes 0-2 don't recognize text mode otherwise
  3684. return $this->ErrorImage(implode("\n", $DebugOutput), 700, 500, true);
  3685. }
  3686. public function FatalError($text) {
  3687. if (null === $this->fatalerror) {
  3688. $this->fatalerror = $text;
  3689. }
  3690. return true;
  3691. }
  3692. public function ErrorImage($text, $width=0, $height=0, $forcedisplay=false) {
  3693. $width = ($width ? $width : $this->config_error_image_width);
  3694. $height = ($height ? $height : $this->config_error_image_height);
  3695. $text = 'phpThumb() v'.$this->phpthumb_version."\n".'http://phpthumb.sourceforge.net'."\n\n".($this->config_disable_debug ? 'Error messages disabled.'."\n\n".'edit phpThumb.config.php and (temporarily) set'."\n".'$PHPTHUMB_CONFIG[\'disable_debug\'] = false;'."\n".'to view the details of this error' : $text);
  3696. $this->FatalError($text);
  3697. $this->DebugMessage($text, __FILE__, __LINE__);
  3698. $this->purgeTempFiles();
  3699. if ($this->config_error_silent_die_on_error) {
  3700. exit;
  3701. }
  3702. if ($this->phpThumbDebug && !$forcedisplay) {
  3703. return false;
  3704. }
  3705. if (!$this->config_error_die_on_error && !$forcedisplay) {
  3706. return false;
  3707. }
  3708. if ($this->err || $this->config_error_message_image_default) {
  3709. // Show generic custom error image instead of error message
  3710. // for use on production sites where you don't want debug messages
  3711. if (($this->err == 'showerror') || $this->phpThumbDebug) {
  3712. // fall through and actually show error message even if default error image is set
  3713. } else {
  3714. header('Location: '.($this->err ? $this->err : $this->config_error_message_image_default));
  3715. exit;
  3716. }
  3717. }
  3718. $this->setOutputFormat();
  3719. if (!$this->thumbnailFormat || !$this->config_disable_debug || (phpthumb_functions::gd_version() < 1)) {
  3720. $this->thumbnailFormat = 'text';
  3721. }
  3722. if (@$this->thumbnailFormat == 'text') {
  3723. // bypass all GD functions and output text error message
  3724. if (!headers_sent()) {
  3725. header('Content-type: text/plain');
  3726. echo $text;
  3727. } else {
  3728. echo '<pre>'.htmlspecialchars($text).'</pre>';
  3729. }
  3730. exit;
  3731. }
  3732. $FontWidth = imagefontwidth($this->config_error_fontsize);
  3733. $FontHeight = imagefontheight($this->config_error_fontsize);
  3734. $LinesOfText = explode("\n", @wordwrap($text, floor($width / $FontWidth), "\n", true));
  3735. $height = max($height, count($LinesOfText) * $FontHeight);
  3736. $headers_file = '';
  3737. $headers_line = '';
  3738. if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.3.0', '>=') && headers_sent($headers_file, $headers_line)) {
  3739. echo "\n".'**Headers already sent in file "'.$headers_file.'" on line "'.$headers_line.'", dumping error message as text:**<br><pre>'."\n\n".$text."\n".'</pre>';
  3740. } elseif (headers_sent()) {
  3741. echo "\n".'**Headers already sent, dumping error message as text:**<br><pre>'."\n\n".$text."\n".'</pre>';
  3742. } elseif ($gdimg_error = imagecreate($width, $height)) {
  3743. $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_error, $this->config_error_bgcolor, true);
  3744. $text_color = phpthumb_functions::ImageHexColorAllocate($gdimg_error, $this->config_error_textcolor, true);
  3745. imagefilledrectangle($gdimg_error, 0, 0, $width, $height, $background_color);
  3746. $lineYoffset = 0;
  3747. foreach ($LinesOfText as $line) {
  3748. imagestring($gdimg_error, $this->config_error_fontsize, 2, $lineYoffset, $line, $text_color);
  3749. $lineYoffset += $FontHeight;
  3750. }
  3751. if (function_exists('imagetypes')) {
  3752. $imagetypes = imagetypes();
  3753. if ($imagetypes & IMG_PNG) {
  3754. header('Content-Type: image/png');
  3755. imagepng($gdimg_error);
  3756. } elseif ($imagetypes & IMG_GIF) {
  3757. header('Content-Type: image/gif');
  3758. imagegif($gdimg_error);
  3759. } elseif ($imagetypes & IMG_JPG) {
  3760. header('Content-Type: image/jpeg');
  3761. imagejpeg($gdimg_error);
  3762. } elseif ($imagetypes & IMG_WBMP) {
  3763. header('Content-Type: image/vnd.wap.wbmp');
  3764. imagewbmp($gdimg_error);
  3765. }
  3766. }
  3767. imagedestroy($gdimg_error);
  3768. }
  3769. if (!headers_sent()) {
  3770. echo "\n".'**Failed to send graphical error image, dumping error message as text:**<br>'."\n\n".$text;
  3771. }
  3772. exit;
  3773. }
  3774. public function ImageCreateFromStringReplacement(&$RawImageData, $DieOnErrors=false) {
  3775. // there are serious bugs in the non-bundled versions of GD which may cause
  3776. // PHP to segfault when calling imagecreatefromstring() - avoid if at all possible
  3777. // when not using a bundled version of GD2
  3778. if (!phpthumb_functions::gd_version()) {
  3779. if ($DieOnErrors) {
  3780. if (!headers_sent()) {
  3781. // base64-encoded error image in GIF format
  3782. $ERROR_NOGD = 'R0lGODlhIAAgALMAAAAAABQUFCQkJDY2NkZGRldXV2ZmZnJycoaGhpSUlKWlpbe3t8XFxdXV1eTk5P7+/iwAAAAAIAAgAAAE/vDJSau9WILtTAACUinDNijZtAHfCojS4W5H+qxD8xibIDE9h0OwWaRWDIljJSkUJYsN4bihMB8th3IToAKs1VtYM75cyV8sZ8vygtOE5yMKmGbO4jRdICQCjHdlZzwzNW4qZSQmKDaNjhUMBX4BBAlmMywFSRWEmAI6b5gAlhNxokGhooAIK5o/pi9vEw4Lfj4OLTAUpj6IabMtCwlSFw0DCKBoFqwAB04AjI54PyZ+yY3TD0ss2YcVmN/gvpcu4TOyFivWqYJlbAHPpOntvxNAACcmGHjZzAZqzSzcq5fNjxFmAFw9iFRunD1epU6tsIPmFCAJnWYE0FURk7wJDA0MTKpEzoWAAskiAAA7';
  3783. header('Content-Type: image/gif');
  3784. echo base64_decode($ERROR_NOGD);
  3785. } else {
  3786. echo '*** ERROR: No PHP-GD support available ***';
  3787. }
  3788. exit;
  3789. } else {
  3790. $this->DebugMessage('ImageCreateFromStringReplacement() failed: gd_version says "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__);
  3791. return false;
  3792. }
  3793. }
  3794. if (phpthumb_functions::gd_is_bundled()) {
  3795. $this->DebugMessage('ImageCreateFromStringReplacement() calling built-in imagecreatefromstring()', __FILE__, __LINE__);
  3796. return @imagecreatefromstring($RawImageData);
  3797. }
  3798. if ($this->issafemode) {
  3799. $this->DebugMessage('ImageCreateFromStringReplacement() failed: cannot create temp file in SAFE_MODE', __FILE__, __LINE__);
  3800. return false;
  3801. }
  3802. switch (substr($RawImageData, 0, 3)) {
  3803. case 'GIF':
  3804. $ICFSreplacementFunctionName = 'imagecreatefromgif';
  3805. break;
  3806. case "\xFF\xD8\xFF":
  3807. $ICFSreplacementFunctionName = 'imagecreatefromjpeg';
  3808. break;
  3809. case "\x89".'PN':
  3810. $ICFSreplacementFunctionName = 'imagecreatefrompng';
  3811. break;
  3812. default:
  3813. $this->DebugMessage('ImageCreateFromStringReplacement() failed: unknown fileformat signature "'.phpthumb_functions::HexCharDisplay(substr($RawImageData, 0, 3)).'"', __FILE__, __LINE__);
  3814. return false;
  3815. break;
  3816. }
  3817. $ErrorMessage = '';
  3818. if ($tempnam = $this->phpThumb_tempnam()) {
  3819. if ($fp_tempnam = @fopen($tempnam, 'wb')) {
  3820. fwrite($fp_tempnam, $RawImageData);
  3821. fclose($fp_tempnam);
  3822. @chmod($tempnam, $this->getParameter('config_file_create_mask'));
  3823. if (($ICFSreplacementFunctionName == 'imagecreatefromgif') && !function_exists($ICFSreplacementFunctionName)) {
  3824. // Need to create from GIF file, but imagecreatefromgif does not exist
  3825. ob_start();
  3826. if (!@include_once __DIR__ .'/phpthumb.gif.php' ) {
  3827. $ErrorMessage = 'Failed to include required file "'. __DIR__ .'/phpthumb.gif.php" in '.__FILE__.' on line '.__LINE__;
  3828. $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
  3829. }
  3830. ob_end_clean();
  3831. // gif_loadFileToGDimageResource() cannot read from raw data, write to file first
  3832. if ($tempfilename = $this->phpThumb_tempnam()) {
  3833. if ($fp_tempfile = @fopen($tempfilename, 'wb')) {
  3834. fwrite($fp_tempfile, $RawImageData);
  3835. fclose($fp_tempfile);
  3836. $gdimg_source = gif_loadFileToGDimageResource($tempfilename);
  3837. $this->DebugMessage('gif_loadFileToGDimageResource('.$tempfilename.') completed', __FILE__, __LINE__);
  3838. $this->DebugMessage('deleting "'.$tempfilename.'"', __FILE__, __LINE__);
  3839. unlink($tempfilename);
  3840. return $gdimg_source;
  3841. } else {
  3842. $ErrorMessage = 'Failed to open tempfile in '.__FILE__.' on line '.__LINE__;
  3843. $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
  3844. }
  3845. } else {
  3846. $ErrorMessage = 'Failed to open generate tempfile name in '.__FILE__.' on line '.__LINE__;
  3847. $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
  3848. }
  3849. } elseif (function_exists($ICFSreplacementFunctionName) && ($gdimg_source = @$ICFSreplacementFunctionName($tempnam))) {
  3850. // great
  3851. $this->DebugMessage($ICFSreplacementFunctionName.'('.$tempnam.') succeeded', __FILE__, __LINE__);
  3852. $this->DebugMessage('deleting "'.$tempnam.'"', __FILE__, __LINE__);
  3853. unlink($tempnam);
  3854. return $gdimg_source;
  3855. } else {
  3856. // GD functions not available, or failed to create image
  3857. $this->DebugMessage($ICFSreplacementFunctionName.'('.$tempnam.') '.(function_exists($ICFSreplacementFunctionName) ? 'failed' : 'does not exist'), __FILE__, __LINE__);
  3858. if (isset($_GET['phpThumbDebug'])) {
  3859. $this->phpThumbDebug();
  3860. }
  3861. }
  3862. } else {
  3863. $ErrorMessage = 'Failed to fopen('.$tempnam.', "wb") in '.__FILE__.' on line '.__LINE__."\n".'You may need to set $PHPTHUMB_CONFIG[temp_directory] in phpThumb.config.php';
  3864. if ($this->issafemode) {
  3865. $ErrorMessage = 'ImageCreateFromStringReplacement() failed in '.__FILE__.' on line '.__LINE__.': cannot create temp file in SAFE_MODE';
  3866. }
  3867. $this->DebugMessage($ErrorMessage, __FILE__, __LINE__);
  3868. }
  3869. $this->DebugMessage('deleting "'.$tempnam.'"', __FILE__, __LINE__);
  3870. @unlink($tempnam);
  3871. } else {
  3872. $ErrorMessage = 'Failed to generate phpThumb_tempnam() in '.__FILE__.' on line '.__LINE__."\n".'You may need to set $PHPTHUMB_CONFIG[temp_directory] in phpThumb.config.php';
  3873. if ($this->issafemode) {
  3874. $ErrorMessage = 'ImageCreateFromStringReplacement() failed in '.__FILE__.' on line '.__LINE__.': cannot create temp file in SAFE_MODE';
  3875. }
  3876. }
  3877. if ($DieOnErrors && $ErrorMessage) {
  3878. return $this->ErrorImage($ErrorMessage);
  3879. }
  3880. return false;
  3881. }
  3882. public function ImageResizeFunction(&$dst_im, &$src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH) {
  3883. $this->DebugMessage('ImageResizeFunction($o, $s, '.$dstX.', '.$dstY.', '.$srcX.', '.$srcY.', '.$dstW.', '.$dstH.', '.$srcW.', '.$srcH.')', __FILE__, __LINE__);
  3884. if (($dstW == $srcW) && ($dstH == $srcH)) {
  3885. return imagecopy($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH);
  3886. }
  3887. if (phpthumb_functions::gd_version() >= 2.0) {
  3888. if ($this->config_disable_imagecopyresampled) {
  3889. return phpthumb_functions::ImageCopyResampleBicubic($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
  3890. }
  3891. return imagecopyresampled($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
  3892. }
  3893. return imagecopyresized($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
  3894. }
  3895. public function InitializeTempDirSetting() {
  3896. $this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe((function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : ''))); // sys_get_temp_dir added in PHP v5.2.1
  3897. $this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe(ini_get('upload_tmp_dir')));
  3898. $this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe(getenv('TMPDIR')));
  3899. $this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe(getenv('TMP')));
  3900. return true;
  3901. }
  3902. public function phpThumb_tempnam() {
  3903. $this->InitializeTempDirSetting();
  3904. $tempnam = $this->realPathSafe(tempnam($this->config_temp_directory, 'pThumb'));
  3905. $this->tempFilesToDelete[$tempnam] = $tempnam;
  3906. $this->DebugMessage('phpThumb_tempnam() returning "'.$tempnam.'"', __FILE__, __LINE__);
  3907. return $tempnam;
  3908. }
  3909. public function DebugMessage($message, $file='', $line='') {
  3910. $this->debugmessages[] = $message.($file ? ' in file "'.(basename($file) ? basename($file) : $file).'"' : '').($line ? ' on line '.$line : '');
  3911. return true;
  3912. }
  3913. public function DebugTimingMessage($message, $file='', $line='', $timestamp=0) {
  3914. if (!$timestamp) {
  3915. $timestamp = array_sum(explode(' ', microtime()));
  3916. }
  3917. $this->debugtiming[number_format($timestamp, 6, '.', '')] = ': '.$message.($file ? ' in file "'.(basename($file) ? basename($file) : $file).'"' : '').($line ? ' on line '.$line : '');
  3918. return true;
  3919. }
  3920. }