Image_lib.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Image Manipulation class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Image_lib
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/libraries/image_lib.html
  47. */
  48. class CI_Image_lib {
  49. /**
  50. * PHP extension/library to use for image manipulation
  51. * Can be: imagemagick, netpbm, gd, gd2
  52. *
  53. * @var string
  54. */
  55. public $image_library = 'gd2';
  56. /**
  57. * Path to the graphic library (if applicable)
  58. *
  59. * @var string
  60. */
  61. public $library_path = '';
  62. /**
  63. * Whether to send to browser or write to disk
  64. *
  65. * @var bool
  66. */
  67. public $dynamic_output = FALSE;
  68. /**
  69. * Path to original image
  70. *
  71. * @var string
  72. */
  73. public $source_image = '';
  74. /**
  75. * Path to the modified image
  76. *
  77. * @var string
  78. */
  79. public $new_image = '';
  80. /**
  81. * Image width
  82. *
  83. * @var int
  84. */
  85. public $width = '';
  86. /**
  87. * Image height
  88. *
  89. * @var int
  90. */
  91. public $height = '';
  92. /**
  93. * Quality percentage of new image
  94. *
  95. * @var int
  96. */
  97. public $quality = 90;
  98. /**
  99. * Whether to create a thumbnail
  100. *
  101. * @var bool
  102. */
  103. public $create_thumb = FALSE;
  104. /**
  105. * String to add to thumbnail version of image
  106. *
  107. * @var string
  108. */
  109. public $thumb_marker = '_thumb';
  110. /**
  111. * Whether to maintain aspect ratio when resizing or use hard values
  112. *
  113. * @var bool
  114. */
  115. public $maintain_ratio = TRUE;
  116. /**
  117. * auto, height, or width. Determines what to use as the master dimension
  118. *
  119. * @var string
  120. */
  121. public $master_dim = 'auto';
  122. /**
  123. * Angle at to rotate image
  124. *
  125. * @var string
  126. */
  127. public $rotation_angle = '';
  128. /**
  129. * X Coordinate for manipulation of the current image
  130. *
  131. * @var int
  132. */
  133. public $x_axis = '';
  134. /**
  135. * Y Coordinate for manipulation of the current image
  136. *
  137. * @var int
  138. */
  139. public $y_axis = '';
  140. // --------------------------------------------------------------------------
  141. // Watermark Vars
  142. // --------------------------------------------------------------------------
  143. /**
  144. * Watermark text if graphic is not used
  145. *
  146. * @var string
  147. */
  148. public $wm_text = '';
  149. /**
  150. * Type of watermarking. Options: text/overlay
  151. *
  152. * @var string
  153. */
  154. public $wm_type = 'text';
  155. /**
  156. * Default transparency for watermark
  157. *
  158. * @var int
  159. */
  160. public $wm_x_transp = 4;
  161. /**
  162. * Default transparency for watermark
  163. *
  164. * @var int
  165. */
  166. public $wm_y_transp = 4;
  167. /**
  168. * Watermark image path
  169. *
  170. * @var string
  171. */
  172. public $wm_overlay_path = '';
  173. /**
  174. * TT font
  175. *
  176. * @var string
  177. */
  178. public $wm_font_path = '';
  179. /**
  180. * Font size (different versions of GD will either use points or pixels)
  181. *
  182. * @var int
  183. */
  184. public $wm_font_size = 17;
  185. /**
  186. * Vertical alignment: T M B
  187. *
  188. * @var string
  189. */
  190. public $wm_vrt_alignment = 'B';
  191. /**
  192. * Horizontal alignment: L R C
  193. *
  194. * @var string
  195. */
  196. public $wm_hor_alignment = 'C';
  197. /**
  198. * Padding around text
  199. *
  200. * @var int
  201. */
  202. public $wm_padding = 0;
  203. /**
  204. * Lets you push text to the right
  205. *
  206. * @var int
  207. */
  208. public $wm_hor_offset = 0;
  209. /**
  210. * Lets you push text down
  211. *
  212. * @var int
  213. */
  214. public $wm_vrt_offset = 0;
  215. /**
  216. * Text color
  217. *
  218. * @var string
  219. */
  220. protected $wm_font_color = '#ffffff';
  221. /**
  222. * Dropshadow color
  223. *
  224. * @var string
  225. */
  226. protected $wm_shadow_color = '';
  227. /**
  228. * Dropshadow distance
  229. *
  230. * @var int
  231. */
  232. public $wm_shadow_distance = 2;
  233. /**
  234. * Image opacity: 1 - 100 Only works with image
  235. *
  236. * @var int
  237. */
  238. public $wm_opacity = 50;
  239. // --------------------------------------------------------------------------
  240. // Private Vars
  241. // --------------------------------------------------------------------------
  242. /**
  243. * Source image folder
  244. *
  245. * @var string
  246. */
  247. public $source_folder = '';
  248. /**
  249. * Destination image folder
  250. *
  251. * @var string
  252. */
  253. public $dest_folder = '';
  254. /**
  255. * Image mime-type
  256. *
  257. * @var string
  258. */
  259. public $mime_type = '';
  260. /**
  261. * Original image width
  262. *
  263. * @var int
  264. */
  265. public $orig_width = '';
  266. /**
  267. * Original image height
  268. *
  269. * @var int
  270. */
  271. public $orig_height = '';
  272. /**
  273. * Image format
  274. *
  275. * @var string
  276. */
  277. public $image_type = '';
  278. /**
  279. * Size of current image
  280. *
  281. * @var string
  282. */
  283. public $size_str = '';
  284. /**
  285. * Full path to source image
  286. *
  287. * @var string
  288. */
  289. public $full_src_path = '';
  290. /**
  291. * Full path to destination image
  292. *
  293. * @var string
  294. */
  295. public $full_dst_path = '';
  296. /**
  297. * File permissions
  298. *
  299. * @var int
  300. */
  301. public $file_permissions = 0644;
  302. /**
  303. * Name of function to create image
  304. *
  305. * @var string
  306. */
  307. public $create_fnc = 'imagecreatetruecolor';
  308. /**
  309. * Name of function to copy image
  310. *
  311. * @var string
  312. */
  313. public $copy_fnc = 'imagecopyresampled';
  314. /**
  315. * Error messages
  316. *
  317. * @var array
  318. */
  319. public $error_msg = array();
  320. /**
  321. * Whether to have a drop shadow on watermark
  322. *
  323. * @var bool
  324. */
  325. protected $wm_use_drop_shadow = FALSE;
  326. /**
  327. * Whether to use truetype fonts
  328. *
  329. * @var bool
  330. */
  331. public $wm_use_truetype = FALSE;
  332. /**
  333. * Initialize Image Library
  334. *
  335. * @param array $props
  336. * @return void
  337. */
  338. public function __construct($props = array())
  339. {
  340. if (count($props) > 0)
  341. {
  342. $this->initialize($props);
  343. }
  344. /**
  345. * A work-around for some improperly formatted, but
  346. * usable JPEGs; known to be produced by Samsung
  347. * smartphones' front-facing cameras.
  348. *
  349. * @see https://github.com/bcit-ci/CodeIgniter/issues/4967
  350. * @see https://bugs.php.net/bug.php?id=72404
  351. */
  352. ini_set('gd.jpeg_ignore_warning', 1);
  353. log_message('info', 'Image Lib Class Initialized');
  354. }
  355. // --------------------------------------------------------------------
  356. /**
  357. * Initialize image properties
  358. *
  359. * Resets values in case this class is used in a loop
  360. *
  361. * @return void
  362. */
  363. public function clear()
  364. {
  365. $props = array('thumb_marker', 'library_path', 'source_image', 'new_image', 'width', 'height', 'rotation_angle', 'x_axis', 'y_axis', 'wm_text', 'wm_overlay_path', 'wm_font_path', 'wm_shadow_color', 'source_folder', 'dest_folder', 'mime_type', 'orig_width', 'orig_height', 'image_type', 'size_str', 'full_src_path', 'full_dst_path');
  366. foreach ($props as $val)
  367. {
  368. $this->$val = '';
  369. }
  370. $this->image_library = 'gd2';
  371. $this->dynamic_output = FALSE;
  372. $this->quality = 90;
  373. $this->create_thumb = FALSE;
  374. $this->thumb_marker = '_thumb';
  375. $this->maintain_ratio = TRUE;
  376. $this->master_dim = 'auto';
  377. $this->wm_type = 'text';
  378. $this->wm_x_transp = 4;
  379. $this->wm_y_transp = 4;
  380. $this->wm_font_size = 17;
  381. $this->wm_vrt_alignment = 'B';
  382. $this->wm_hor_alignment = 'C';
  383. $this->wm_padding = 0;
  384. $this->wm_hor_offset = 0;
  385. $this->wm_vrt_offset = 0;
  386. $this->wm_font_color = '#ffffff';
  387. $this->wm_shadow_distance = 2;
  388. $this->wm_opacity = 50;
  389. $this->create_fnc = 'imagecreatetruecolor';
  390. $this->copy_fnc = 'imagecopyresampled';
  391. $this->error_msg = array();
  392. $this->wm_use_drop_shadow = FALSE;
  393. $this->wm_use_truetype = FALSE;
  394. }
  395. // --------------------------------------------------------------------
  396. /**
  397. * initialize image preferences
  398. *
  399. * @param array
  400. * @return bool
  401. */
  402. public function initialize($props = array())
  403. {
  404. // Convert array elements into class variables
  405. if (count($props) > 0)
  406. {
  407. foreach ($props as $key => $val)
  408. {
  409. if (property_exists($this, $key))
  410. {
  411. if (in_array($key, array('wm_font_color', 'wm_shadow_color'), TRUE))
  412. {
  413. if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
  414. {
  415. /* $matches[1] contains our hex color value, but it might be
  416. * both in the full 6-length format or the shortened 3-length
  417. * value.
  418. * We'll later need the full version, so we keep it if it's
  419. * already there and if not - we'll convert to it. We can
  420. * access string characters by their index as in an array,
  421. * so we'll do that and use concatenation to form the final
  422. * value:
  423. */
  424. $val = (strlen($matches[1]) === 6)
  425. ? '#'.$matches[1]
  426. : '#'.$matches[1][0].$matches[1][0].$matches[1][1].$matches[1][1].$matches[1][2].$matches[1][2];
  427. }
  428. else
  429. {
  430. continue;
  431. }
  432. }
  433. elseif (in_array($key, array('width', 'height'), TRUE) && ! ctype_digit((string) $val))
  434. {
  435. continue;
  436. }
  437. $this->$key = $val;
  438. }
  439. }
  440. }
  441. // Is there a source image? If not, there's no reason to continue
  442. if ($this->source_image === '')
  443. {
  444. $this->set_error('imglib_source_image_required');
  445. return FALSE;
  446. }
  447. /* Is getimagesize() available?
  448. *
  449. * We use it to determine the image properties (width/height).
  450. * Note: We need to figure out how to determine image
  451. * properties using ImageMagick and NetPBM
  452. */
  453. if ( ! function_exists('getimagesize'))
  454. {
  455. $this->set_error('imglib_gd_required_for_props');
  456. return FALSE;
  457. }
  458. $this->image_library = strtolower($this->image_library);
  459. /* Set the full server path
  460. *
  461. * The source image may or may not contain a path.
  462. * Either way, we'll try use realpath to generate the
  463. * full server path in order to more reliably read it.
  464. */
  465. if (($full_source_path = realpath($this->source_image)) !== FALSE)
  466. {
  467. $full_source_path = str_replace('\\', '/', $full_source_path);
  468. }
  469. else
  470. {
  471. $full_source_path = $this->source_image;
  472. }
  473. $x = explode('/', $full_source_path);
  474. $this->source_image = end($x);
  475. $this->source_folder = str_replace($this->source_image, '', $full_source_path);
  476. // Set the Image Properties
  477. if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
  478. {
  479. return FALSE;
  480. }
  481. /*
  482. * Assign the "new" image name/path
  483. *
  484. * If the user has set a "new_image" name it means
  485. * we are making a copy of the source image. If not
  486. * it means we are altering the original. We'll
  487. * set the destination filename and path accordingly.
  488. */
  489. if ($this->new_image === '')
  490. {
  491. $this->dest_image = $this->source_image;
  492. $this->dest_folder = $this->source_folder;
  493. }
  494. elseif (strpos($this->new_image, '/') === FALSE && strpos($this->new_image, '\\') === FALSE)
  495. {
  496. $this->dest_image = $this->new_image;
  497. $this->dest_folder = $this->source_folder;
  498. }
  499. else
  500. {
  501. // Is there a file name?
  502. if ( ! preg_match('#\.(jpg|jpeg|gif|png)$#i', $this->new_image))
  503. {
  504. $this->dest_image = $this->source_image;
  505. $this->dest_folder = $this->new_image;
  506. }
  507. else
  508. {
  509. $x = explode('/', str_replace('\\', '/', $this->new_image));
  510. $this->dest_image = end($x);
  511. $this->dest_folder = str_replace($this->dest_image, '', $this->new_image);
  512. }
  513. $this->dest_folder = realpath($this->dest_folder).'/';
  514. }
  515. /* Compile the finalized filenames/paths
  516. *
  517. * We'll create two master strings containing the
  518. * full server path to the source image and the
  519. * full server path to the destination image.
  520. * We'll also split the destination image name
  521. * so we can insert the thumbnail marker if needed.
  522. */
  523. if ($this->create_thumb === FALSE OR $this->thumb_marker === '')
  524. {
  525. $this->thumb_marker = '';
  526. }
  527. $xp = $this->explode_name($this->dest_image);
  528. $filename = $xp['name'];
  529. $file_ext = $xp['ext'];
  530. $this->full_src_path = $this->source_folder.$this->source_image;
  531. $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
  532. /* Should we maintain image proportions?
  533. *
  534. * When creating thumbs or copies, the target width/height
  535. * might not be in correct proportion with the source
  536. * image's width/height. We'll recalculate it here.
  537. */
  538. if ($this->maintain_ratio === TRUE && ($this->width !== 0 OR $this->height !== 0))
  539. {
  540. $this->image_reproportion();
  541. }
  542. /* Was a width and height specified?
  543. *
  544. * If the destination width/height was not submitted we
  545. * will use the values from the actual file
  546. */
  547. if ($this->width === '')
  548. {
  549. $this->width = $this->orig_width;
  550. }
  551. if ($this->height === '')
  552. {
  553. $this->height = $this->orig_height;
  554. }
  555. // Set the quality
  556. $this->quality = trim(str_replace('%', '', $this->quality));
  557. if ($this->quality === '' OR $this->quality === 0 OR ! ctype_digit($this->quality))
  558. {
  559. $this->quality = 90;
  560. }
  561. // Set the x/y coordinates
  562. is_numeric($this->x_axis) OR $this->x_axis = 0;
  563. is_numeric($this->y_axis) OR $this->y_axis = 0;
  564. // Watermark-related Stuff...
  565. if ($this->wm_overlay_path !== '')
  566. {
  567. $this->wm_overlay_path = str_replace('\\', '/', realpath($this->wm_overlay_path));
  568. }
  569. if ($this->wm_shadow_color !== '')
  570. {
  571. $this->wm_use_drop_shadow = TRUE;
  572. }
  573. elseif ($this->wm_use_drop_shadow === TRUE && $this->wm_shadow_color === '')
  574. {
  575. $this->wm_use_drop_shadow = FALSE;
  576. }
  577. if ($this->wm_font_path !== '')
  578. {
  579. $this->wm_use_truetype = TRUE;
  580. }
  581. return TRUE;
  582. }
  583. // --------------------------------------------------------------------
  584. /**
  585. * Image Resize
  586. *
  587. * This is a wrapper function that chooses the proper
  588. * resize function based on the protocol specified
  589. *
  590. * @return bool
  591. */
  592. public function resize()
  593. {
  594. $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
  595. return $this->$protocol('resize');
  596. }
  597. // --------------------------------------------------------------------
  598. /**
  599. * Image Crop
  600. *
  601. * This is a wrapper function that chooses the proper
  602. * cropping function based on the protocol specified
  603. *
  604. * @return bool
  605. */
  606. public function crop()
  607. {
  608. $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
  609. return $this->$protocol('crop');
  610. }
  611. // --------------------------------------------------------------------
  612. /**
  613. * Image Rotate
  614. *
  615. * This is a wrapper function that chooses the proper
  616. * rotation function based on the protocol specified
  617. *
  618. * @return bool
  619. */
  620. public function rotate()
  621. {
  622. // Allowed rotation values
  623. $degs = array(90, 180, 270, 'vrt', 'hor');
  624. if ($this->rotation_angle === '' OR ! in_array($this->rotation_angle, $degs))
  625. {
  626. $this->set_error('imglib_rotation_angle_required');
  627. return FALSE;
  628. }
  629. // Reassign the width and height
  630. if ($this->rotation_angle === 90 OR $this->rotation_angle === 270)
  631. {
  632. $this->width = $this->orig_height;
  633. $this->height = $this->orig_width;
  634. }
  635. else
  636. {
  637. $this->width = $this->orig_width;
  638. $this->height = $this->orig_height;
  639. }
  640. // Choose resizing function
  641. if ($this->image_library === 'imagemagick' OR $this->image_library === 'netpbm')
  642. {
  643. $protocol = 'image_process_'.$this->image_library;
  644. return $this->$protocol('rotate');
  645. }
  646. return ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
  647. ? $this->image_mirror_gd()
  648. : $this->image_rotate_gd();
  649. }
  650. // --------------------------------------------------------------------
  651. /**
  652. * Image Process Using GD/GD2
  653. *
  654. * This function will resize or crop
  655. *
  656. * @param string
  657. * @return bool
  658. */
  659. public function image_process_gd($action = 'resize')
  660. {
  661. $v2_override = FALSE;
  662. // If the target width/height match the source, AND if the new file name is not equal to the old file name
  663. // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
  664. if ($this->dynamic_output === FALSE && $this->orig_width === $this->width && $this->orig_height === $this->height)
  665. {
  666. if ($this->source_image !== $this->new_image && @copy($this->full_src_path, $this->full_dst_path))
  667. {
  668. chmod($this->full_dst_path, $this->file_permissions);
  669. }
  670. return TRUE;
  671. }
  672. // Let's set up our values based on the action
  673. if ($action === 'crop')
  674. {
  675. // Reassign the source width/height if cropping
  676. $this->orig_width = $this->width;
  677. $this->orig_height = $this->height;
  678. // GD 2.0 has a cropping bug so we'll test for it
  679. if ($this->gd_version() !== FALSE)
  680. {
  681. $gd_version = str_replace('0', '', $this->gd_version());
  682. $v2_override = ($gd_version == 2);
  683. }
  684. }
  685. else
  686. {
  687. // If resizing the x/y axis must be zero
  688. $this->x_axis = 0;
  689. $this->y_axis = 0;
  690. }
  691. // Create the image handle
  692. if ( ! ($src_img = $this->image_create_gd()))
  693. {
  694. return FALSE;
  695. }
  696. /* Create the image
  697. *
  698. * Old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
  699. * it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
  700. * below should that ever prove inaccurate.
  701. *
  702. * if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor') && $v2_override === FALSE)
  703. */
  704. if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor'))
  705. {
  706. $create = 'imagecreatetruecolor';
  707. $copy = 'imagecopyresampled';
  708. }
  709. else
  710. {
  711. $create = 'imagecreate';
  712. $copy = 'imagecopyresized';
  713. }
  714. $dst_img = $create($this->width, $this->height);
  715. if ($this->image_type === 3) // png we can actually preserve transparency
  716. {
  717. imagealphablending($dst_img, FALSE);
  718. imagesavealpha($dst_img, TRUE);
  719. }
  720. $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
  721. // Show the image
  722. if ($this->dynamic_output === TRUE)
  723. {
  724. $this->image_display_gd($dst_img);
  725. }
  726. elseif ( ! $this->image_save_gd($dst_img)) // Or save it
  727. {
  728. return FALSE;
  729. }
  730. // Kill the file handles
  731. imagedestroy($dst_img);
  732. imagedestroy($src_img);
  733. if ($this->dynamic_output !== TRUE)
  734. {
  735. chmod($this->full_dst_path, $this->file_permissions);
  736. }
  737. return TRUE;
  738. }
  739. // --------------------------------------------------------------------
  740. /**
  741. * Image Process Using ImageMagick
  742. *
  743. * This function will resize, crop or rotate
  744. *
  745. * @param string
  746. * @return bool
  747. */
  748. public function image_process_imagemagick($action = 'resize')
  749. {
  750. // Do we have a vaild library path?
  751. if ($this->library_path === '')
  752. {
  753. $this->set_error('imglib_libpath_invalid');
  754. return FALSE;
  755. }
  756. if ( ! preg_match('/convert$/i', $this->library_path))
  757. {
  758. $this->library_path = rtrim($this->library_path, '/').'/convert';
  759. }
  760. // Execute the command
  761. $cmd = $this->library_path.' -quality '.$this->quality;
  762. if ($action === 'crop')
  763. {
  764. $cmd .= ' -crop '.$this->width.'x'.$this->height.'+'.$this->x_axis.'+'.$this->y_axis;
  765. }
  766. elseif ($action === 'rotate')
  767. {
  768. $cmd .= ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
  769. ? ' -flop'
  770. : ' -rotate '.$this->rotation_angle;
  771. }
  772. else // Resize
  773. {
  774. if($this->maintain_ratio === TRUE)
  775. {
  776. $cmd .= ' -resize '.$this->width.'x'.$this->height;
  777. }
  778. else
  779. {
  780. $cmd .= ' -resize '.$this->width.'x'.$this->height.'\!';
  781. }
  782. }
  783. $cmd .= ' '.escapeshellarg($this->full_src_path).' '.escapeshellarg($this->full_dst_path).' 2>&1';
  784. $retval = 1;
  785. // exec() might be disabled
  786. if (function_usable('exec'))
  787. {
  788. @exec($cmd, $output, $retval);
  789. }
  790. // Did it work?
  791. if ($retval > 0)
  792. {
  793. $this->set_error('imglib_image_process_failed');
  794. return FALSE;
  795. }
  796. chmod($this->full_dst_path, $this->file_permissions);
  797. return TRUE;
  798. }
  799. // --------------------------------------------------------------------
  800. /**
  801. * Image Process Using NetPBM
  802. *
  803. * This function will resize, crop or rotate
  804. *
  805. * @param string
  806. * @return bool
  807. */
  808. public function image_process_netpbm($action = 'resize')
  809. {
  810. if ($this->library_path === '')
  811. {
  812. $this->set_error('imglib_libpath_invalid');
  813. return FALSE;
  814. }
  815. // Build the resizing command
  816. switch ($this->image_type)
  817. {
  818. case 1 :
  819. $cmd_in = 'giftopnm';
  820. $cmd_out = 'ppmtogif';
  821. break;
  822. case 2 :
  823. $cmd_in = 'jpegtopnm';
  824. $cmd_out = 'ppmtojpeg';
  825. break;
  826. case 3 :
  827. $cmd_in = 'pngtopnm';
  828. $cmd_out = 'ppmtopng';
  829. break;
  830. }
  831. if ($action === 'crop')
  832. {
  833. $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
  834. }
  835. elseif ($action === 'rotate')
  836. {
  837. switch ($this->rotation_angle)
  838. {
  839. case 90: $angle = 'r270';
  840. break;
  841. case 180: $angle = 'r180';
  842. break;
  843. case 270: $angle = 'r90';
  844. break;
  845. case 'vrt': $angle = 'tb';
  846. break;
  847. case 'hor': $angle = 'lr';
  848. break;
  849. }
  850. $cmd_inner = 'pnmflip -'.$angle.' ';
  851. }
  852. else // Resize
  853. {
  854. $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
  855. }
  856. $cmd = $this->library_path.$cmd_in.' '.escapeshellarg($this->full_src_path).' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
  857. $retval = 1;
  858. // exec() might be disabled
  859. if (function_usable('exec'))
  860. {
  861. @exec($cmd, $output, $retval);
  862. }
  863. // Did it work?
  864. if ($retval > 0)
  865. {
  866. $this->set_error('imglib_image_process_failed');
  867. return FALSE;
  868. }
  869. // With NetPBM we have to create a temporary image.
  870. // If you try manipulating the original it fails so
  871. // we have to rename the temp file.
  872. copy($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
  873. unlink($this->dest_folder.'netpbm.tmp');
  874. chmod($this->full_dst_path, $this->file_permissions);
  875. return TRUE;
  876. }
  877. // --------------------------------------------------------------------
  878. /**
  879. * Image Rotate Using GD
  880. *
  881. * @return bool
  882. */
  883. public function image_rotate_gd()
  884. {
  885. // Create the image handle
  886. if ( ! ($src_img = $this->image_create_gd()))
  887. {
  888. return FALSE;
  889. }
  890. // Set the background color
  891. // This won't work with transparent PNG files so we are
  892. // going to have to figure out how to determine the color
  893. // of the alpha channel in a future release.
  894. $white = imagecolorallocate($src_img, 255, 255, 255);
  895. // Rotate it!
  896. $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
  897. // Show the image
  898. if ($this->dynamic_output === TRUE)
  899. {
  900. $this->image_display_gd($dst_img);
  901. }
  902. elseif ( ! $this->image_save_gd($dst_img)) // ... or save it
  903. {
  904. return FALSE;
  905. }
  906. // Kill the file handles
  907. imagedestroy($dst_img);
  908. imagedestroy($src_img);
  909. chmod($this->full_dst_path, $this->file_permissions);
  910. return TRUE;
  911. }
  912. // --------------------------------------------------------------------
  913. /**
  914. * Create Mirror Image using GD
  915. *
  916. * This function will flip horizontal or vertical
  917. *
  918. * @return bool
  919. */
  920. public function image_mirror_gd()
  921. {
  922. if ( ! $src_img = $this->image_create_gd())
  923. {
  924. return FALSE;
  925. }
  926. $width = $this->orig_width;
  927. $height = $this->orig_height;
  928. if ($this->rotation_angle === 'hor')
  929. {
  930. for ($i = 0; $i < $height; $i++)
  931. {
  932. $left = 0;
  933. $right = $width - 1;
  934. while ($left < $right)
  935. {
  936. $cl = imagecolorat($src_img, $left, $i);
  937. $cr = imagecolorat($src_img, $right, $i);
  938. imagesetpixel($src_img, $left, $i, $cr);
  939. imagesetpixel($src_img, $right, $i, $cl);
  940. $left++;
  941. $right--;
  942. }
  943. }
  944. }
  945. else
  946. {
  947. for ($i = 0; $i < $width; $i++)
  948. {
  949. $top = 0;
  950. $bottom = $height - 1;
  951. while ($top < $bottom)
  952. {
  953. $ct = imagecolorat($src_img, $i, $top);
  954. $cb = imagecolorat($src_img, $i, $bottom);
  955. imagesetpixel($src_img, $i, $top, $cb);
  956. imagesetpixel($src_img, $i, $bottom, $ct);
  957. $top++;
  958. $bottom--;
  959. }
  960. }
  961. }
  962. // Show the image
  963. if ($this->dynamic_output === TRUE)
  964. {
  965. $this->image_display_gd($src_img);
  966. }
  967. elseif ( ! $this->image_save_gd($src_img)) // ... or save it
  968. {
  969. return FALSE;
  970. }
  971. // Kill the file handles
  972. imagedestroy($src_img);
  973. chmod($this->full_dst_path, $this->file_permissions);
  974. return TRUE;
  975. }
  976. // --------------------------------------------------------------------
  977. /**
  978. * Image Watermark
  979. *
  980. * This is a wrapper function that chooses the type
  981. * of watermarking based on the specified preference.
  982. *
  983. * @return bool
  984. */
  985. public function watermark()
  986. {
  987. return ($this->wm_type === 'overlay') ? $this->overlay_watermark() : $this->text_watermark();
  988. }
  989. // --------------------------------------------------------------------
  990. /**
  991. * Watermark - Graphic Version
  992. *
  993. * @return bool
  994. */
  995. public function overlay_watermark()
  996. {
  997. if ( ! function_exists('imagecolortransparent'))
  998. {
  999. $this->set_error('imglib_gd_required');
  1000. return FALSE;
  1001. }
  1002. // Fetch source image properties
  1003. $this->get_image_properties();
  1004. // Fetch watermark image properties
  1005. $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
  1006. $wm_img_type = $props['image_type'];
  1007. $wm_width = $props['width'];
  1008. $wm_height = $props['height'];
  1009. // Create two image resources
  1010. $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
  1011. $src_img = $this->image_create_gd($this->full_src_path);
  1012. // Reverse the offset if necessary
  1013. // When the image is positioned at the bottom
  1014. // we don't want the vertical offset to push it
  1015. // further down. We want the reverse, so we'll
  1016. // invert the offset. Same with the horizontal
  1017. // offset when the image is at the right
  1018. $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
  1019. $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
  1020. if ($this->wm_vrt_alignment === 'B')
  1021. $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
  1022. if ($this->wm_hor_alignment === 'R')
  1023. $this->wm_hor_offset = $this->wm_hor_offset * -1;
  1024. // Set the base x and y axis values
  1025. $x_axis = $this->wm_hor_offset + $this->wm_padding;
  1026. $y_axis = $this->wm_vrt_offset + $this->wm_padding;
  1027. // Set the vertical position
  1028. if ($this->wm_vrt_alignment === 'M')
  1029. {
  1030. $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
  1031. }
  1032. elseif ($this->wm_vrt_alignment === 'B')
  1033. {
  1034. $y_axis += $this->orig_height - $wm_height;
  1035. }
  1036. // Set the horizontal position
  1037. if ($this->wm_hor_alignment === 'C')
  1038. {
  1039. $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
  1040. }
  1041. elseif ($this->wm_hor_alignment === 'R')
  1042. {
  1043. $x_axis += $this->orig_width - $wm_width;
  1044. }
  1045. // Build the finalized image
  1046. if ($wm_img_type === 3 && function_exists('imagealphablending'))
  1047. {
  1048. @imagealphablending($src_img, TRUE);
  1049. }
  1050. // Set RGB values for text and shadow
  1051. $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
  1052. $alpha = ($rgba & 0x7F000000) >> 24;
  1053. // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
  1054. if ($alpha > 0)
  1055. {
  1056. // copy the image directly, the image's alpha transparency being the sole determinant of blending
  1057. imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
  1058. }
  1059. else
  1060. {
  1061. // set our RGB value from above to be transparent and merge the images with the specified opacity
  1062. imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
  1063. imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
  1064. }
  1065. // We can preserve transparency for PNG images
  1066. if ($this->image_type === 3)
  1067. {
  1068. imagealphablending($src_img, FALSE);
  1069. imagesavealpha($src_img, TRUE);
  1070. }
  1071. // Output the image
  1072. if ($this->dynamic_output === TRUE)
  1073. {
  1074. $this->image_display_gd($src_img);
  1075. }
  1076. elseif ( ! $this->image_save_gd($src_img)) // ... or save it
  1077. {
  1078. return FALSE;
  1079. }
  1080. imagedestroy($src_img);
  1081. imagedestroy($wm_img);
  1082. return TRUE;
  1083. }
  1084. // --------------------------------------------------------------------
  1085. /**
  1086. * Watermark - Text Version
  1087. *
  1088. * @return bool
  1089. */
  1090. public function text_watermark()
  1091. {
  1092. if ( ! ($src_img = $this->image_create_gd()))
  1093. {
  1094. return FALSE;
  1095. }
  1096. if ($this->wm_use_truetype === TRUE && ! file_exists($this->wm_font_path))
  1097. {
  1098. $this->set_error('imglib_missing_font');
  1099. return FALSE;
  1100. }
  1101. // Fetch source image properties
  1102. $this->get_image_properties();
  1103. // Reverse the vertical offset
  1104. // When the image is positioned at the bottom
  1105. // we don't want the vertical offset to push it
  1106. // further down. We want the reverse, so we'll
  1107. // invert the offset. Note: The horizontal
  1108. // offset flips itself automatically
  1109. if ($this->wm_vrt_alignment === 'B')
  1110. {
  1111. $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
  1112. }
  1113. if ($this->wm_hor_alignment === 'R')
  1114. {
  1115. $this->wm_hor_offset = $this->wm_hor_offset * -1;
  1116. }
  1117. // Set font width and height
  1118. // These are calculated differently depending on
  1119. // whether we are using the true type font or not
  1120. if ($this->wm_use_truetype === TRUE)
  1121. {
  1122. if (empty($this->wm_font_size))
  1123. {
  1124. $this->wm_font_size = 17;
  1125. }
  1126. if (function_exists('imagettfbbox'))
  1127. {
  1128. $temp = imagettfbbox($this->wm_font_size, 0, $this->wm_font_path, $this->wm_text);
  1129. $temp = $temp[2] - $temp[0];
  1130. $fontwidth = $temp / strlen($this->wm_text);
  1131. }
  1132. else
  1133. {
  1134. $fontwidth = $this->wm_font_size - ($this->wm_font_size / 4);
  1135. }
  1136. $fontheight = $this->wm_font_size;
  1137. $this->wm_vrt_offset += $this->wm_font_size;
  1138. }
  1139. else
  1140. {
  1141. $fontwidth = imagefontwidth($this->wm_font_size);
  1142. $fontheight = imagefontheight($this->wm_font_size);
  1143. }
  1144. // Set base X and Y axis values
  1145. $x_axis = $this->wm_hor_offset + $this->wm_padding;
  1146. $y_axis = $this->wm_vrt_offset + $this->wm_padding;
  1147. if ($this->wm_use_drop_shadow === FALSE)
  1148. {
  1149. $this->wm_shadow_distance = 0;
  1150. }
  1151. $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
  1152. $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
  1153. // Set vertical alignment
  1154. if ($this->wm_vrt_alignment === 'M')
  1155. {
  1156. $y_axis += ($this->orig_height / 2) + ($fontheight / 2);
  1157. }
  1158. elseif ($this->wm_vrt_alignment === 'B')
  1159. {
  1160. $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2);
  1161. }
  1162. // Set horizontal alignment
  1163. if ($this->wm_hor_alignment === 'R')
  1164. {
  1165. $x_axis += $this->orig_width - ($fontwidth * strlen($this->wm_text)) - $this->wm_shadow_distance;
  1166. }
  1167. elseif ($this->wm_hor_alignment === 'C')
  1168. {
  1169. $x_axis += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
  1170. }
  1171. if ($this->wm_use_drop_shadow)
  1172. {
  1173. // Offset from text
  1174. $x_shad = $x_axis + $this->wm_shadow_distance;
  1175. $y_shad = $y_axis + $this->wm_shadow_distance;
  1176. /* Set RGB values for shadow
  1177. *
  1178. * First character is #, so we don't really need it.
  1179. * Get the rest of the string and split it into 2-length
  1180. * hex values:
  1181. */
  1182. $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
  1183. $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2]));
  1184. // Add the shadow to the source image
  1185. if ($this->wm_use_truetype)
  1186. {
  1187. imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
  1188. }
  1189. else
  1190. {
  1191. imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
  1192. }
  1193. }
  1194. /* Set RGB values for text
  1195. *
  1196. * First character is #, so we don't really need it.
  1197. * Get the rest of the string and split it into 2-length
  1198. * hex values:
  1199. */
  1200. $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
  1201. $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
  1202. // Add the text to the source image
  1203. if ($this->wm_use_truetype)
  1204. {
  1205. imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
  1206. }
  1207. else
  1208. {
  1209. imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
  1210. }
  1211. // We can preserve transparency for PNG images
  1212. if ($this->image_type === 3)
  1213. {
  1214. imagealphablending($src_img, FALSE);
  1215. imagesavealpha($src_img, TRUE);
  1216. }
  1217. // Output the final image
  1218. if ($this->dynamic_output === TRUE)
  1219. {
  1220. $this->image_display_gd($src_img);
  1221. }
  1222. else
  1223. {
  1224. $this->image_save_gd($src_img);
  1225. }
  1226. imagedestroy($src_img);
  1227. return TRUE;
  1228. }
  1229. // --------------------------------------------------------------------
  1230. /**
  1231. * Create Image - GD
  1232. *
  1233. * This simply creates an image resource handle
  1234. * based on the type of image being processed
  1235. *
  1236. * @param string
  1237. * @param string
  1238. * @return resource
  1239. */
  1240. public function image_create_gd($path = '', $image_type = '')
  1241. {
  1242. if ($path === '')
  1243. {
  1244. $path = $this->full_src_path;
  1245. }
  1246. if ($image_type === '')
  1247. {
  1248. $image_type = $this->image_type;
  1249. }
  1250. switch ($image_type)
  1251. {
  1252. case 1:
  1253. if ( ! function_exists('imagecreatefromgif'))
  1254. {
  1255. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
  1256. return FALSE;
  1257. }
  1258. return imagecreatefromgif($path);
  1259. case 2:
  1260. if ( ! function_exists('imagecreatefromjpeg'))
  1261. {
  1262. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
  1263. return FALSE;
  1264. }
  1265. return imagecreatefromjpeg($path);
  1266. case 3:
  1267. if ( ! function_exists('imagecreatefrompng'))
  1268. {
  1269. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
  1270. return FALSE;
  1271. }
  1272. return imagecreatefrompng($path);
  1273. default:
  1274. $this->set_error(array('imglib_unsupported_imagecreate'));
  1275. return FALSE;
  1276. }
  1277. }
  1278. // --------------------------------------------------------------------
  1279. /**
  1280. * Write image file to disk - GD
  1281. *
  1282. * Takes an image resource as input and writes the file
  1283. * to the specified destination
  1284. *
  1285. * @param resource
  1286. * @return bool
  1287. */
  1288. public function image_save_gd($resource)
  1289. {
  1290. switch ($this->image_type)
  1291. {
  1292. case 1:
  1293. if ( ! function_exists('imagegif'))
  1294. {
  1295. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
  1296. return FALSE;
  1297. }
  1298. if ( ! @imagegif($resource, $this->full_dst_path))
  1299. {
  1300. $this->set_error('imglib_save_failed');
  1301. return FALSE;
  1302. }
  1303. break;
  1304. case 2:
  1305. if ( ! function_exists('imagejpeg'))
  1306. {
  1307. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
  1308. return FALSE;
  1309. }
  1310. if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
  1311. {
  1312. $this->set_error('imglib_save_failed');
  1313. return FALSE;
  1314. }
  1315. break;
  1316. case 3:
  1317. if ( ! function_exists('imagepng'))
  1318. {
  1319. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
  1320. return FALSE;
  1321. }
  1322. if ( ! @imagepng($resource, $this->full_dst_path))
  1323. {
  1324. $this->set_error('imglib_save_failed');
  1325. return FALSE;
  1326. }
  1327. break;
  1328. default:
  1329. $this->set_error(array('imglib_unsupported_imagecreate'));
  1330. return FALSE;
  1331. break;
  1332. }
  1333. return TRUE;
  1334. }
  1335. // --------------------------------------------------------------------
  1336. /**
  1337. * Dynamically outputs an image
  1338. *
  1339. * @param resource
  1340. * @return void
  1341. */
  1342. public function image_display_gd($resource)
  1343. {
  1344. header('Content-Disposition: filename='.$this->source_image.';');
  1345. header('Content-Type: '.$this->mime_type);
  1346. header('Content-Transfer-Encoding: binary');
  1347. header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
  1348. switch ($this->image_type)
  1349. {
  1350. case 1 : imagegif($resource);
  1351. break;
  1352. case 2 : imagejpeg($resource, NULL, $this->quality);
  1353. break;
  1354. case 3 : imagepng($resource);
  1355. break;
  1356. default: echo 'Unable to display the image';
  1357. break;
  1358. }
  1359. }
  1360. // --------------------------------------------------------------------
  1361. /**
  1362. * Re-proportion Image Width/Height
  1363. *
  1364. * When creating thumbs, the desired width/height
  1365. * can end up warping the image due to an incorrect
  1366. * ratio between the full-sized image and the thumb.
  1367. *
  1368. * This function lets us re-proportion the width/height
  1369. * if users choose to maintain the aspect ratio when resizing.
  1370. *
  1371. * @return void
  1372. */
  1373. public function image_reproportion()
  1374. {
  1375. if (($this->width === 0 && $this->height === 0) OR $this->orig_width === 0 OR $this->orig_height === 0
  1376. OR ( ! ctype_digit((string) $this->width) && ! ctype_digit((string) $this->height))
  1377. OR ! ctype_digit((string) $this->orig_width) OR ! ctype_digit((string) $this->orig_height))
  1378. {
  1379. return;
  1380. }
  1381. // Sanitize
  1382. $this->width = (int) $this->width;
  1383. $this->height = (int) $this->height;
  1384. if ($this->master_dim !== 'width' && $this->master_dim !== 'height')
  1385. {
  1386. if ($this->width > 0 && $this->height > 0)
  1387. {
  1388. $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0)
  1389. ? 'width' : 'height';
  1390. }
  1391. else
  1392. {
  1393. $this->master_dim = ($this->height === 0) ? 'width' : 'height';
  1394. }
  1395. }
  1396. elseif (($this->master_dim === 'width' && $this->width === 0)
  1397. OR ($this->master_dim === 'height' && $this->height === 0))
  1398. {
  1399. return;
  1400. }
  1401. if ($this->master_dim === 'width')
  1402. {
  1403. $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width);
  1404. }
  1405. else
  1406. {
  1407. $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height);
  1408. }
  1409. }
  1410. // --------------------------------------------------------------------
  1411. /**
  1412. * Get image properties
  1413. *
  1414. * A helper function that gets info about the file
  1415. *
  1416. * @param string
  1417. * @param bool
  1418. * @return mixed
  1419. */
  1420. public function get_image_properties($path = '', $return = FALSE)
  1421. {
  1422. // For now we require GD but we should
  1423. // find a way to determine this using IM or NetPBM
  1424. if ($path === '')
  1425. {
  1426. $path = $this->full_src_path;
  1427. }
  1428. if ( ! file_exists($path))
  1429. {
  1430. $this->set_error('imglib_invalid_path');
  1431. return FALSE;
  1432. }
  1433. $vals = getimagesize($path);
  1434. if ($vals === FALSE)
  1435. {
  1436. $this->set_error('imglib_invalid_image');
  1437. return FALSE;
  1438. }
  1439. $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
  1440. $mime = isset($types[$vals[2]]) ? 'image/'.$types[$vals[2]] : 'image/jpg';
  1441. if ($return === TRUE)
  1442. {
  1443. return array(
  1444. 'width' => $vals[0],
  1445. 'height' => $vals[1],
  1446. 'image_type' => $vals[2],
  1447. 'size_str' => $vals[3],
  1448. 'mime_type' => $mime
  1449. );
  1450. }
  1451. $this->orig_width = $vals[0];
  1452. $this->orig_height = $vals[1];
  1453. $this->image_type = $vals[2];
  1454. $this->size_str = $vals[3];
  1455. $this->mime_type = $mime;
  1456. return TRUE;
  1457. }
  1458. // --------------------------------------------------------------------
  1459. /**
  1460. * Size calculator
  1461. *
  1462. * This function takes a known width x height and
  1463. * recalculates it to a new size. Only one
  1464. * new variable needs to be known
  1465. *
  1466. * $props = array(
  1467. * 'width' => $width,
  1468. * 'height' => $height,
  1469. * 'new_width' => 40,
  1470. * 'new_height' => ''
  1471. * );
  1472. *
  1473. * @param array
  1474. * @return array
  1475. */
  1476. public function size_calculator($vals)
  1477. {
  1478. if ( ! is_array($vals))
  1479. {
  1480. return;
  1481. }
  1482. $allowed = array('new_width', 'new_height', 'width', 'height');
  1483. foreach ($allowed as $item)
  1484. {
  1485. if (empty($vals[$item]))
  1486. {
  1487. $vals[$item] = 0;
  1488. }
  1489. }
  1490. if ($vals['width'] === 0 OR $vals['height'] === 0)
  1491. {
  1492. return $vals;
  1493. }
  1494. if ($vals['new_width'] === 0)
  1495. {
  1496. $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
  1497. }
  1498. elseif ($vals['new_height'] === 0)
  1499. {
  1500. $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
  1501. }
  1502. return $vals;
  1503. }
  1504. // --------------------------------------------------------------------
  1505. /**
  1506. * Explode source_image
  1507. *
  1508. * This is a helper function that extracts the extension
  1509. * from the source_image. This function lets us deal with
  1510. * source_images with multiple periods, like: my.cool.jpg
  1511. * It returns an associative array with two elements:
  1512. * $array['ext'] = '.jpg';
  1513. * $array['name'] = 'my.cool';
  1514. *
  1515. * @param array
  1516. * @return array
  1517. */
  1518. public function explode_name($source_image)
  1519. {
  1520. $ext = strrchr($source_image, '.');
  1521. $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
  1522. return array('ext' => $ext, 'name' => $name);
  1523. }
  1524. // --------------------------------------------------------------------
  1525. /**
  1526. * Is GD Installed?
  1527. *
  1528. * @return bool
  1529. */
  1530. public function gd_loaded()
  1531. {
  1532. if ( ! extension_loaded('gd'))
  1533. {
  1534. /* As it is stated in the PHP manual, dl() is not always available
  1535. * and even if so - it could generate an E_WARNING message on failure
  1536. */
  1537. return (function_exists('dl') && @dl('gd.so'));
  1538. }
  1539. return TRUE;
  1540. }
  1541. // --------------------------------------------------------------------
  1542. /**
  1543. * Get GD version
  1544. *
  1545. * @return mixed
  1546. */
  1547. public function gd_version()
  1548. {
  1549. if (function_exists('gd_info'))
  1550. {
  1551. $gd_version = @gd_info();
  1552. return preg_replace('/\D/', '', $gd_version['GD Version']);
  1553. }
  1554. return FALSE;
  1555. }
  1556. // --------------------------------------------------------------------
  1557. /**
  1558. * Set error message
  1559. *
  1560. * @param string
  1561. * @return void
  1562. */
  1563. public function set_error($msg)
  1564. {
  1565. $CI =& get_instance();
  1566. $CI->lang->load('imglib');
  1567. if (is_array($msg))
  1568. {
  1569. foreach ($msg as $val)
  1570. {
  1571. $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
  1572. $this->error_msg[] = $msg;
  1573. log_message('error', $msg);
  1574. }
  1575. }
  1576. else
  1577. {
  1578. $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
  1579. $this->error_msg[] = $msg;
  1580. log_message('error', $msg);
  1581. }
  1582. }
  1583. // --------------------------------------------------------------------
  1584. /**
  1585. * Show error messages
  1586. *
  1587. * @param string
  1588. * @param string
  1589. * @return string
  1590. */
  1591. public function display_errors($open = '<p>', $close = '</p>')
  1592. {
  1593. return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
  1594. }
  1595. }