Image_lib.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  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 - 2016, 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 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://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. log_message('info', 'Image Lib Class Initialized');
  345. }
  346. // --------------------------------------------------------------------
  347. /**
  348. * Initialize image properties
  349. *
  350. * Resets values in case this class is used in a loop
  351. *
  352. * @return void
  353. */
  354. public function clear()
  355. {
  356. $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');
  357. foreach ($props as $val)
  358. {
  359. $this->$val = '';
  360. }
  361. $this->image_library = 'gd2';
  362. $this->dynamic_output = FALSE;
  363. $this->quality = 90;
  364. $this->create_thumb = FALSE;
  365. $this->thumb_marker = '_thumb';
  366. $this->maintain_ratio = TRUE;
  367. $this->master_dim = 'auto';
  368. $this->wm_type = 'text';
  369. $this->wm_x_transp = 4;
  370. $this->wm_y_transp = 4;
  371. $this->wm_font_size = 17;
  372. $this->wm_vrt_alignment = 'B';
  373. $this->wm_hor_alignment = 'C';
  374. $this->wm_padding = 0;
  375. $this->wm_hor_offset = 0;
  376. $this->wm_vrt_offset = 0;
  377. $this->wm_font_color = '#ffffff';
  378. $this->wm_shadow_distance = 2;
  379. $this->wm_opacity = 50;
  380. $this->create_fnc = 'imagecreatetruecolor';
  381. $this->copy_fnc = 'imagecopyresampled';
  382. $this->error_msg = array();
  383. $this->wm_use_drop_shadow = FALSE;
  384. $this->wm_use_truetype = FALSE;
  385. }
  386. // --------------------------------------------------------------------
  387. /**
  388. * initialize image preferences
  389. *
  390. * @param array
  391. * @return bool
  392. */
  393. public function initialize($props = array())
  394. {
  395. // Convert array elements into class variables
  396. if (count($props) > 0)
  397. {
  398. foreach ($props as $key => $val)
  399. {
  400. if (property_exists($this, $key))
  401. {
  402. if (in_array($key, array('wm_font_color', 'wm_shadow_color'), TRUE))
  403. {
  404. if (preg_match('/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i', $val, $matches))
  405. {
  406. /* $matches[1] contains our hex color value, but it might be
  407. * both in the full 6-length format or the shortened 3-length
  408. * value.
  409. * We'll later need the full version, so we keep it if it's
  410. * already there and if not - we'll convert to it. We can
  411. * access string characters by their index as in an array,
  412. * so we'll do that and use concatenation to form the final
  413. * value:
  414. */
  415. $val = (strlen($matches[1]) === 6)
  416. ? '#'.$matches[1]
  417. : '#'.$matches[1][0].$matches[1][0].$matches[1][1].$matches[1][1].$matches[1][2].$matches[1][2];
  418. }
  419. else
  420. {
  421. continue;
  422. }
  423. }
  424. elseif (in_array($key, array('width', 'height'), TRUE) && ! ctype_digit((string) $val))
  425. {
  426. continue;
  427. }
  428. $this->$key = $val;
  429. }
  430. }
  431. }
  432. // Is there a source image? If not, there's no reason to continue
  433. if ($this->source_image === '')
  434. {
  435. $this->set_error('imglib_source_image_required');
  436. return FALSE;
  437. }
  438. /* Is getimagesize() available?
  439. *
  440. * We use it to determine the image properties (width/height).
  441. * Note: We need to figure out how to determine image
  442. * properties using ImageMagick and NetPBM
  443. */
  444. if ( ! function_exists('getimagesize'))
  445. {
  446. $this->set_error('imglib_gd_required_for_props');
  447. return FALSE;
  448. }
  449. $this->image_library = strtolower($this->image_library);
  450. /* Set the full server path
  451. *
  452. * The source image may or may not contain a path.
  453. * Either way, we'll try use realpath to generate the
  454. * full server path in order to more reliably read it.
  455. */
  456. if (($full_source_path = realpath($this->source_image)) !== FALSE)
  457. {
  458. $full_source_path = str_replace('\\', '/', $full_source_path);
  459. }
  460. else
  461. {
  462. $full_source_path = $this->source_image;
  463. }
  464. $x = explode('/', $full_source_path);
  465. $this->source_image = end($x);
  466. $this->source_folder = str_replace($this->source_image, '', $full_source_path);
  467. // Set the Image Properties
  468. if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
  469. {
  470. return FALSE;
  471. }
  472. /*
  473. * Assign the "new" image name/path
  474. *
  475. * If the user has set a "new_image" name it means
  476. * we are making a copy of the source image. If not
  477. * it means we are altering the original. We'll
  478. * set the destination filename and path accordingly.
  479. */
  480. if ($this->new_image === '')
  481. {
  482. $this->dest_image = $this->source_image;
  483. $this->dest_folder = $this->source_folder;
  484. }
  485. elseif (strpos($this->new_image, '/') === FALSE)
  486. {
  487. $this->dest_folder = $this->source_folder;
  488. $this->dest_image = $this->new_image;
  489. }
  490. else
  491. {
  492. if (strpos($this->new_image, '/') === FALSE && strpos($this->new_image, '\\') === FALSE)
  493. {
  494. $full_dest_path = str_replace('\\', '/', realpath($this->new_image));
  495. }
  496. else
  497. {
  498. $full_dest_path = $this->new_image;
  499. }
  500. // Is there a file name?
  501. if ( ! preg_match('#\.(jpg|jpeg|gif|png)$#i', $full_dest_path))
  502. {
  503. $this->dest_folder = $full_dest_path.'/';
  504. $this->dest_image = $this->source_image;
  505. }
  506. else
  507. {
  508. $x = explode('/', $full_dest_path);
  509. $this->dest_image = end($x);
  510. $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
  511. }
  512. }
  513. /* Compile the finalized filenames/paths
  514. *
  515. * We'll create two master strings containing the
  516. * full server path to the source image and the
  517. * full server path to the destination image.
  518. * We'll also split the destination image name
  519. * so we can insert the thumbnail marker if needed.
  520. */
  521. if ($this->create_thumb === FALSE OR $this->thumb_marker === '')
  522. {
  523. $this->thumb_marker = '';
  524. }
  525. $xp = $this->explode_name($this->dest_image);
  526. $filename = $xp['name'];
  527. $file_ext = $xp['ext'];
  528. $this->full_src_path = $this->source_folder.$this->source_image;
  529. $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
  530. /* Should we maintain image proportions?
  531. *
  532. * When creating thumbs or copies, the target width/height
  533. * might not be in correct proportion with the source
  534. * image's width/height. We'll recalculate it here.
  535. */
  536. if ($this->maintain_ratio === TRUE && ($this->width !== 0 OR $this->height !== 0))
  537. {
  538. $this->image_reproportion();
  539. }
  540. /* Was a width and height specified?
  541. *
  542. * If the destination width/height was not submitted we
  543. * will use the values from the actual file
  544. */
  545. if ($this->width === '')
  546. {
  547. $this->width = $this->orig_width;
  548. }
  549. if ($this->height === '')
  550. {
  551. $this->height = $this->orig_height;
  552. }
  553. // Set the quality
  554. $this->quality = trim(str_replace('%', '', $this->quality));
  555. if ($this->quality === '' OR $this->quality === 0 OR ! ctype_digit($this->quality))
  556. {
  557. $this->quality = 90;
  558. }
  559. // Set the x/y coordinates
  560. is_numeric($this->x_axis) OR $this->x_axis = 0;
  561. is_numeric($this->y_axis) OR $this->y_axis = 0;
  562. // Watermark-related Stuff...
  563. if ($this->wm_overlay_path !== '')
  564. {
  565. $this->wm_overlay_path = str_replace('\\', '/', realpath($this->wm_overlay_path));
  566. }
  567. if ($this->wm_shadow_color !== '')
  568. {
  569. $this->wm_use_drop_shadow = TRUE;
  570. }
  571. elseif ($this->wm_use_drop_shadow === TRUE && $this->wm_shadow_color === '')
  572. {
  573. $this->wm_use_drop_shadow = FALSE;
  574. }
  575. if ($this->wm_font_path !== '')
  576. {
  577. $this->wm_use_truetype = TRUE;
  578. }
  579. return TRUE;
  580. }
  581. // --------------------------------------------------------------------
  582. /**
  583. * Image Resize
  584. *
  585. * This is a wrapper function that chooses the proper
  586. * resize function based on the protocol specified
  587. *
  588. * @return bool
  589. */
  590. public function resize()
  591. {
  592. $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
  593. return $this->$protocol('resize');
  594. }
  595. // --------------------------------------------------------------------
  596. /**
  597. * Image Crop
  598. *
  599. * This is a wrapper function that chooses the proper
  600. * cropping function based on the protocol specified
  601. *
  602. * @return bool
  603. */
  604. public function crop()
  605. {
  606. $protocol = ($this->image_library === 'gd2') ? 'image_process_gd' : 'image_process_'.$this->image_library;
  607. return $this->$protocol('crop');
  608. }
  609. // --------------------------------------------------------------------
  610. /**
  611. * Image Rotate
  612. *
  613. * This is a wrapper function that chooses the proper
  614. * rotation function based on the protocol specified
  615. *
  616. * @return bool
  617. */
  618. public function rotate()
  619. {
  620. // Allowed rotation values
  621. $degs = array(90, 180, 270, 'vrt', 'hor');
  622. if ($this->rotation_angle === '' OR ! in_array($this->rotation_angle, $degs))
  623. {
  624. $this->set_error('imglib_rotation_angle_required');
  625. return FALSE;
  626. }
  627. // Reassign the width and height
  628. if ($this->rotation_angle === 90 OR $this->rotation_angle === 270)
  629. {
  630. $this->width = $this->orig_height;
  631. $this->height = $this->orig_width;
  632. }
  633. else
  634. {
  635. $this->width = $this->orig_width;
  636. $this->height = $this->orig_height;
  637. }
  638. // Choose resizing function
  639. if ($this->image_library === 'imagemagick' OR $this->image_library === 'netpbm')
  640. {
  641. $protocol = 'image_process_'.$this->image_library;
  642. return $this->$protocol('rotate');
  643. }
  644. return ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
  645. ? $this->image_mirror_gd()
  646. : $this->image_rotate_gd();
  647. }
  648. // --------------------------------------------------------------------
  649. /**
  650. * Image Process Using GD/GD2
  651. *
  652. * This function will resize or crop
  653. *
  654. * @param string
  655. * @return bool
  656. */
  657. public function image_process_gd($action = 'resize')
  658. {
  659. $v2_override = FALSE;
  660. // If the target width/height match the source, AND if the new file name is not equal to the old file name
  661. // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
  662. if ($this->dynamic_output === FALSE && $this->orig_width === $this->width && $this->orig_height === $this->height)
  663. {
  664. if ($this->source_image !== $this->new_image && @copy($this->full_src_path, $this->full_dst_path))
  665. {
  666. chmod($this->full_dst_path, $this->file_permissions);
  667. }
  668. return TRUE;
  669. }
  670. // Let's set up our values based on the action
  671. if ($action === 'crop')
  672. {
  673. // Reassign the source width/height if cropping
  674. $this->orig_width = $this->width;
  675. $this->orig_height = $this->height;
  676. // GD 2.0 has a cropping bug so we'll test for it
  677. if ($this->gd_version() !== FALSE)
  678. {
  679. $gd_version = str_replace('0', '', $this->gd_version());
  680. $v2_override = ($gd_version == 2);
  681. }
  682. }
  683. else
  684. {
  685. // If resizing the x/y axis must be zero
  686. $this->x_axis = 0;
  687. $this->y_axis = 0;
  688. }
  689. // Create the image handle
  690. if ( ! ($src_img = $this->image_create_gd()))
  691. {
  692. return FALSE;
  693. }
  694. /* Create the image
  695. *
  696. * Old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
  697. * it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
  698. * below should that ever prove inaccurate.
  699. *
  700. * if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor') && $v2_override === FALSE)
  701. */
  702. if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor'))
  703. {
  704. $create = 'imagecreatetruecolor';
  705. $copy = 'imagecopyresampled';
  706. }
  707. else
  708. {
  709. $create = 'imagecreate';
  710. $copy = 'imagecopyresized';
  711. }
  712. $dst_img = $create($this->width, $this->height);
  713. if ($this->image_type === 3) // png we can actually preserve transparency
  714. {
  715. imagealphablending($dst_img, FALSE);
  716. imagesavealpha($dst_img, TRUE);
  717. }
  718. $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
  719. // Show the image
  720. if ($this->dynamic_output === TRUE)
  721. {
  722. $this->image_display_gd($dst_img);
  723. }
  724. elseif ( ! $this->image_save_gd($dst_img)) // Or save it
  725. {
  726. return FALSE;
  727. }
  728. // Kill the file handles
  729. imagedestroy($dst_img);
  730. imagedestroy($src_img);
  731. chmod($this->full_dst_path, $this->file_permissions);
  732. return TRUE;
  733. }
  734. // --------------------------------------------------------------------
  735. /**
  736. * Image Process Using ImageMagick
  737. *
  738. * This function will resize, crop or rotate
  739. *
  740. * @param string
  741. * @return bool
  742. */
  743. public function image_process_imagemagick($action = 'resize')
  744. {
  745. // Do we have a vaild library path?
  746. if ($this->library_path === '')
  747. {
  748. $this->set_error('imglib_libpath_invalid');
  749. return FALSE;
  750. }
  751. if ( ! preg_match('/convert$/i', $this->library_path))
  752. {
  753. $this->library_path = rtrim($this->library_path, '/').'/convert';
  754. }
  755. // Execute the command
  756. $cmd = $this->library_path.' -quality '.$this->quality;
  757. if ($action === 'crop')
  758. {
  759. $cmd .= ' -crop '.$this->width.'x'.$this->height.'+'.$this->x_axis.'+'.$this->y_axis;
  760. }
  761. elseif ($action === 'rotate')
  762. {
  763. $cmd .= ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt')
  764. ? ' -flop'
  765. : ' -rotate '.$this->rotation_angle;
  766. }
  767. else // Resize
  768. {
  769. if($this->maintain_ratio === TRUE)
  770. {
  771. $cmd .= ' -resize '.$this->width.'x'.$this->height;
  772. }
  773. else
  774. {
  775. $cmd .= ' -resize '.$this->width.'x'.$this->height.'\!';
  776. }
  777. }
  778. $cmd .= escapeshellarg($this->full_src_path).' '.escapeshellarg($this->full_dst_path).' 2>&1';
  779. $retval = 1;
  780. // exec() might be disabled
  781. if (function_usable('exec'))
  782. {
  783. @exec($cmd, $output, $retval);
  784. }
  785. // Did it work?
  786. if ($retval > 0)
  787. {
  788. $this->set_error('imglib_image_process_failed');
  789. return FALSE;
  790. }
  791. chmod($this->full_dst_path, $this->file_permissions);
  792. return TRUE;
  793. }
  794. // --------------------------------------------------------------------
  795. /**
  796. * Image Process Using NetPBM
  797. *
  798. * This function will resize, crop or rotate
  799. *
  800. * @param string
  801. * @return bool
  802. */
  803. public function image_process_netpbm($action = 'resize')
  804. {
  805. if ($this->library_path === '')
  806. {
  807. $this->set_error('imglib_libpath_invalid');
  808. return FALSE;
  809. }
  810. // Build the resizing command
  811. switch ($this->image_type)
  812. {
  813. case 1 :
  814. $cmd_in = 'giftopnm';
  815. $cmd_out = 'ppmtogif';
  816. break;
  817. case 2 :
  818. $cmd_in = 'jpegtopnm';
  819. $cmd_out = 'ppmtojpeg';
  820. break;
  821. case 3 :
  822. $cmd_in = 'pngtopnm';
  823. $cmd_out = 'ppmtopng';
  824. break;
  825. }
  826. if ($action === 'crop')
  827. {
  828. $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
  829. }
  830. elseif ($action === 'rotate')
  831. {
  832. switch ($this->rotation_angle)
  833. {
  834. case 90: $angle = 'r270';
  835. break;
  836. case 180: $angle = 'r180';
  837. break;
  838. case 270: $angle = 'r90';
  839. break;
  840. case 'vrt': $angle = 'tb';
  841. break;
  842. case 'hor': $angle = 'lr';
  843. break;
  844. }
  845. $cmd_inner = 'pnmflip -'.$angle.' ';
  846. }
  847. else // Resize
  848. {
  849. $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
  850. }
  851. $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
  852. $retval = 1;
  853. // exec() might be disabled
  854. if (function_usable('exec'))
  855. {
  856. @exec($cmd, $output, $retval);
  857. }
  858. // Did it work?
  859. if ($retval > 0)
  860. {
  861. $this->set_error('imglib_image_process_failed');
  862. return FALSE;
  863. }
  864. // With NetPBM we have to create a temporary image.
  865. // If you try manipulating the original it fails so
  866. // we have to rename the temp file.
  867. copy($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
  868. unlink($this->dest_folder.'netpbm.tmp');
  869. chmod($this->full_dst_path, $this->file_permissions);
  870. return TRUE;
  871. }
  872. // --------------------------------------------------------------------
  873. /**
  874. * Image Rotate Using GD
  875. *
  876. * @return bool
  877. */
  878. public function image_rotate_gd()
  879. {
  880. // Create the image handle
  881. if ( ! ($src_img = $this->image_create_gd()))
  882. {
  883. return FALSE;
  884. }
  885. // Set the background color
  886. // This won't work with transparent PNG files so we are
  887. // going to have to figure out how to determine the color
  888. // of the alpha channel in a future release.
  889. $white = imagecolorallocate($src_img, 255, 255, 255);
  890. // Rotate it!
  891. $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
  892. // Show the image
  893. if ($this->dynamic_output === TRUE)
  894. {
  895. $this->image_display_gd($dst_img);
  896. }
  897. elseif ( ! $this->image_save_gd($dst_img)) // ... or save it
  898. {
  899. return FALSE;
  900. }
  901. // Kill the file handles
  902. imagedestroy($dst_img);
  903. imagedestroy($src_img);
  904. chmod($this->full_dst_path, $this->file_permissions);
  905. return TRUE;
  906. }
  907. // --------------------------------------------------------------------
  908. /**
  909. * Create Mirror Image using GD
  910. *
  911. * This function will flip horizontal or vertical
  912. *
  913. * @return bool
  914. */
  915. public function image_mirror_gd()
  916. {
  917. if ( ! $src_img = $this->image_create_gd())
  918. {
  919. return FALSE;
  920. }
  921. $width = $this->orig_width;
  922. $height = $this->orig_height;
  923. if ($this->rotation_angle === 'hor')
  924. {
  925. for ($i = 0; $i < $height; $i++)
  926. {
  927. $left = 0;
  928. $right = $width - 1;
  929. while ($left < $right)
  930. {
  931. $cl = imagecolorat($src_img, $left, $i);
  932. $cr = imagecolorat($src_img, $right, $i);
  933. imagesetpixel($src_img, $left, $i, $cr);
  934. imagesetpixel($src_img, $right, $i, $cl);
  935. $left++;
  936. $right--;
  937. }
  938. }
  939. }
  940. else
  941. {
  942. for ($i = 0; $i < $width; $i++)
  943. {
  944. $top = 0;
  945. $bottom = $height - 1;
  946. while ($top < $bottom)
  947. {
  948. $ct = imagecolorat($src_img, $i, $top);
  949. $cb = imagecolorat($src_img, $i, $bottom);
  950. imagesetpixel($src_img, $i, $top, $cb);
  951. imagesetpixel($src_img, $i, $bottom, $ct);
  952. $top++;
  953. $bottom--;
  954. }
  955. }
  956. }
  957. // Show the image
  958. if ($this->dynamic_output === TRUE)
  959. {
  960. $this->image_display_gd($src_img);
  961. }
  962. elseif ( ! $this->image_save_gd($src_img)) // ... or save it
  963. {
  964. return FALSE;
  965. }
  966. // Kill the file handles
  967. imagedestroy($src_img);
  968. chmod($this->full_dst_path, $this->file_permissions);
  969. return TRUE;
  970. }
  971. // --------------------------------------------------------------------
  972. /**
  973. * Image Watermark
  974. *
  975. * This is a wrapper function that chooses the type
  976. * of watermarking based on the specified preference.
  977. *
  978. * @return bool
  979. */
  980. public function watermark()
  981. {
  982. return ($this->wm_type === 'overlay') ? $this->overlay_watermark() : $this->text_watermark();
  983. }
  984. // --------------------------------------------------------------------
  985. /**
  986. * Watermark - Graphic Version
  987. *
  988. * @return bool
  989. */
  990. public function overlay_watermark()
  991. {
  992. if ( ! function_exists('imagecolortransparent'))
  993. {
  994. $this->set_error('imglib_gd_required');
  995. return FALSE;
  996. }
  997. // Fetch source image properties
  998. $this->get_image_properties();
  999. // Fetch watermark image properties
  1000. $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
  1001. $wm_img_type = $props['image_type'];
  1002. $wm_width = $props['width'];
  1003. $wm_height = $props['height'];
  1004. // Create two image resources
  1005. $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
  1006. $src_img = $this->image_create_gd($this->full_src_path);
  1007. // Reverse the offset if necessary
  1008. // When the image is positioned at the bottom
  1009. // we don't want the vertical offset to push it
  1010. // further down. We want the reverse, so we'll
  1011. // invert the offset. Same with the horizontal
  1012. // offset when the image is at the right
  1013. $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
  1014. $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
  1015. if ($this->wm_vrt_alignment === 'B')
  1016. $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
  1017. if ($this->wm_hor_alignment === 'R')
  1018. $this->wm_hor_offset = $this->wm_hor_offset * -1;
  1019. // Set the base x and y axis values
  1020. $x_axis = $this->wm_hor_offset + $this->wm_padding;
  1021. $y_axis = $this->wm_vrt_offset + $this->wm_padding;
  1022. // Set the vertical position
  1023. if ($this->wm_vrt_alignment === 'M')
  1024. {
  1025. $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
  1026. }
  1027. elseif ($this->wm_vrt_alignment === 'B')
  1028. {
  1029. $y_axis += $this->orig_height - $wm_height;
  1030. }
  1031. // Set the horizontal position
  1032. if ($this->wm_hor_alignment === 'C')
  1033. {
  1034. $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
  1035. }
  1036. elseif ($this->wm_hor_alignment === 'R')
  1037. {
  1038. $x_axis += $this->orig_width - $wm_width;
  1039. }
  1040. // Build the finalized image
  1041. if ($wm_img_type === 3 && function_exists('imagealphablending'))
  1042. {
  1043. @imagealphablending($src_img, TRUE);
  1044. }
  1045. // Set RGB values for text and shadow
  1046. $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
  1047. $alpha = ($rgba & 0x7F000000) >> 24;
  1048. // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
  1049. if ($alpha > 0)
  1050. {
  1051. // copy the image directly, the image's alpha transparency being the sole determinant of blending
  1052. imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
  1053. }
  1054. else
  1055. {
  1056. // set our RGB value from above to be transparent and merge the images with the specified opacity
  1057. imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
  1058. imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
  1059. }
  1060. // We can preserve transparency for PNG images
  1061. if ($this->image_type === 3)
  1062. {
  1063. imagealphablending($src_img, FALSE);
  1064. imagesavealpha($src_img, TRUE);
  1065. }
  1066. // Output the image
  1067. if ($this->dynamic_output === TRUE)
  1068. {
  1069. $this->image_display_gd($src_img);
  1070. }
  1071. elseif ( ! $this->image_save_gd($src_img)) // ... or save it
  1072. {
  1073. return FALSE;
  1074. }
  1075. imagedestroy($src_img);
  1076. imagedestroy($wm_img);
  1077. return TRUE;
  1078. }
  1079. // --------------------------------------------------------------------
  1080. /**
  1081. * Watermark - Text Version
  1082. *
  1083. * @return bool
  1084. */
  1085. public function text_watermark()
  1086. {
  1087. if ( ! ($src_img = $this->image_create_gd()))
  1088. {
  1089. return FALSE;
  1090. }
  1091. if ($this->wm_use_truetype === TRUE && ! file_exists($this->wm_font_path))
  1092. {
  1093. $this->set_error('imglib_missing_font');
  1094. return FALSE;
  1095. }
  1096. // Fetch source image properties
  1097. $this->get_image_properties();
  1098. // Reverse the vertical offset
  1099. // When the image is positioned at the bottom
  1100. // we don't want the vertical offset to push it
  1101. // further down. We want the reverse, so we'll
  1102. // invert the offset. Note: The horizontal
  1103. // offset flips itself automatically
  1104. if ($this->wm_vrt_alignment === 'B')
  1105. {
  1106. $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
  1107. }
  1108. if ($this->wm_hor_alignment === 'R')
  1109. {
  1110. $this->wm_hor_offset = $this->wm_hor_offset * -1;
  1111. }
  1112. // Set font width and height
  1113. // These are calculated differently depending on
  1114. // whether we are using the true type font or not
  1115. if ($this->wm_use_truetype === TRUE)
  1116. {
  1117. if (empty($this->wm_font_size))
  1118. {
  1119. $this->wm_font_size = 17;
  1120. }
  1121. if (function_exists('imagettfbbox'))
  1122. {
  1123. $temp = imagettfbbox($this->wm_font_size, 0, $this->wm_font_path, $this->wm_text);
  1124. $temp = $temp[2] - $temp[0];
  1125. $fontwidth = $temp / strlen($this->wm_text);
  1126. }
  1127. else
  1128. {
  1129. $fontwidth = $this->wm_font_size - ($this->wm_font_size / 4);
  1130. }
  1131. $fontheight = $this->wm_font_size;
  1132. $this->wm_vrt_offset += $this->wm_font_size;
  1133. }
  1134. else
  1135. {
  1136. $fontwidth = imagefontwidth($this->wm_font_size);
  1137. $fontheight = imagefontheight($this->wm_font_size);
  1138. }
  1139. // Set base X and Y axis values
  1140. $x_axis = $this->wm_hor_offset + $this->wm_padding;
  1141. $y_axis = $this->wm_vrt_offset + $this->wm_padding;
  1142. if ($this->wm_use_drop_shadow === FALSE)
  1143. {
  1144. $this->wm_shadow_distance = 0;
  1145. }
  1146. $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
  1147. $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);
  1148. // Set vertical alignment
  1149. if ($this->wm_vrt_alignment === 'M')
  1150. {
  1151. $y_axis += ($this->orig_height / 2) + ($fontheight / 2);
  1152. }
  1153. elseif ($this->wm_vrt_alignment === 'B')
  1154. {
  1155. $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2);
  1156. }
  1157. // Set horizontal alignment
  1158. if ($this->wm_hor_alignment === 'R')
  1159. {
  1160. $x_axis += $this->orig_width - ($fontwidth * strlen($this->wm_text)) - $this->wm_shadow_distance;
  1161. }
  1162. elseif ($this->wm_hor_alignment === 'C')
  1163. {
  1164. $x_axis += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2);
  1165. }
  1166. if ($this->wm_use_drop_shadow)
  1167. {
  1168. // Offset from text
  1169. $x_shad = $x_axis + $this->wm_shadow_distance;
  1170. $y_shad = $y_axis + $this->wm_shadow_distance;
  1171. /* Set RGB values for shadow
  1172. *
  1173. * First character is #, so we don't really need it.
  1174. * Get the rest of the string and split it into 2-length
  1175. * hex values:
  1176. */
  1177. $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2);
  1178. $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2]));
  1179. // Add the shadow to the source image
  1180. if ($this->wm_use_truetype)
  1181. {
  1182. imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
  1183. }
  1184. else
  1185. {
  1186. imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
  1187. }
  1188. }
  1189. /* Set RGB values for text
  1190. *
  1191. * First character is #, so we don't really need it.
  1192. * Get the rest of the string and split it into 2-length
  1193. * hex values:
  1194. */
  1195. $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2);
  1196. $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2]));
  1197. // Add the text to the source image
  1198. if ($this->wm_use_truetype)
  1199. {
  1200. imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
  1201. }
  1202. else
  1203. {
  1204. imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
  1205. }
  1206. // We can preserve transparency for PNG images
  1207. if ($this->image_type === 3)
  1208. {
  1209. imagealphablending($src_img, FALSE);
  1210. imagesavealpha($src_img, TRUE);
  1211. }
  1212. // Output the final image
  1213. if ($this->dynamic_output === TRUE)
  1214. {
  1215. $this->image_display_gd($src_img);
  1216. }
  1217. else
  1218. {
  1219. $this->image_save_gd($src_img);
  1220. }
  1221. imagedestroy($src_img);
  1222. return TRUE;
  1223. }
  1224. // --------------------------------------------------------------------
  1225. /**
  1226. * Create Image - GD
  1227. *
  1228. * This simply creates an image resource handle
  1229. * based on the type of image being processed
  1230. *
  1231. * @param string
  1232. * @param string
  1233. * @return resource
  1234. */
  1235. public function image_create_gd($path = '', $image_type = '')
  1236. {
  1237. if ($path === '')
  1238. {
  1239. $path = $this->full_src_path;
  1240. }
  1241. if ($image_type === '')
  1242. {
  1243. $image_type = $this->image_type;
  1244. }
  1245. switch ($image_type)
  1246. {
  1247. case 1:
  1248. if ( ! function_exists('imagecreatefromgif'))
  1249. {
  1250. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
  1251. return FALSE;
  1252. }
  1253. return imagecreatefromgif($path);
  1254. case 2:
  1255. if ( ! function_exists('imagecreatefromjpeg'))
  1256. {
  1257. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
  1258. return FALSE;
  1259. }
  1260. return imagecreatefromjpeg($path);
  1261. case 3:
  1262. if ( ! function_exists('imagecreatefrompng'))
  1263. {
  1264. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
  1265. return FALSE;
  1266. }
  1267. return imagecreatefrompng($path);
  1268. default:
  1269. $this->set_error(array('imglib_unsupported_imagecreate'));
  1270. return FALSE;
  1271. }
  1272. }
  1273. // --------------------------------------------------------------------
  1274. /**
  1275. * Write image file to disk - GD
  1276. *
  1277. * Takes an image resource as input and writes the file
  1278. * to the specified destination
  1279. *
  1280. * @param resource
  1281. * @return bool
  1282. */
  1283. public function image_save_gd($resource)
  1284. {
  1285. switch ($this->image_type)
  1286. {
  1287. case 1:
  1288. if ( ! function_exists('imagegif'))
  1289. {
  1290. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
  1291. return FALSE;
  1292. }
  1293. if ( ! @imagegif($resource, $this->full_dst_path))
  1294. {
  1295. $this->set_error('imglib_save_failed');
  1296. return FALSE;
  1297. }
  1298. break;
  1299. case 2:
  1300. if ( ! function_exists('imagejpeg'))
  1301. {
  1302. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
  1303. return FALSE;
  1304. }
  1305. if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
  1306. {
  1307. $this->set_error('imglib_save_failed');
  1308. return FALSE;
  1309. }
  1310. break;
  1311. case 3:
  1312. if ( ! function_exists('imagepng'))
  1313. {
  1314. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
  1315. return FALSE;
  1316. }
  1317. if ( ! @imagepng($resource, $this->full_dst_path))
  1318. {
  1319. $this->set_error('imglib_save_failed');
  1320. return FALSE;
  1321. }
  1322. break;
  1323. default:
  1324. $this->set_error(array('imglib_unsupported_imagecreate'));
  1325. return FALSE;
  1326. break;
  1327. }
  1328. return TRUE;
  1329. }
  1330. // --------------------------------------------------------------------
  1331. /**
  1332. * Dynamically outputs an image
  1333. *
  1334. * @param resource
  1335. * @return void
  1336. */
  1337. public function image_display_gd($resource)
  1338. {
  1339. header('Content-Disposition: filename='.$this->source_image.';');
  1340. header('Content-Type: '.$this->mime_type);
  1341. header('Content-Transfer-Encoding: binary');
  1342. header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
  1343. switch ($this->image_type)
  1344. {
  1345. case 1 : imagegif($resource);
  1346. break;
  1347. case 2 : imagejpeg($resource, NULL, $this->quality);
  1348. break;
  1349. case 3 : imagepng($resource);
  1350. break;
  1351. default: echo 'Unable to display the image';
  1352. break;
  1353. }
  1354. }
  1355. // --------------------------------------------------------------------
  1356. /**
  1357. * Re-proportion Image Width/Height
  1358. *
  1359. * When creating thumbs, the desired width/height
  1360. * can end up warping the image due to an incorrect
  1361. * ratio between the full-sized image and the thumb.
  1362. *
  1363. * This function lets us re-proportion the width/height
  1364. * if users choose to maintain the aspect ratio when resizing.
  1365. *
  1366. * @return void
  1367. */
  1368. public function image_reproportion()
  1369. {
  1370. if (($this->width === 0 && $this->height === 0) OR $this->orig_width === 0 OR $this->orig_height === 0
  1371. OR ( ! ctype_digit((string) $this->width) && ! ctype_digit((string) $this->height))
  1372. OR ! ctype_digit((string) $this->orig_width) OR ! ctype_digit((string) $this->orig_height))
  1373. {
  1374. return;
  1375. }
  1376. // Sanitize
  1377. $this->width = (int) $this->width;
  1378. $this->height = (int) $this->height;
  1379. if ($this->master_dim !== 'width' && $this->master_dim !== 'height')
  1380. {
  1381. if ($this->width > 0 && $this->height > 0)
  1382. {
  1383. $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0)
  1384. ? 'width' : 'height';
  1385. }
  1386. else
  1387. {
  1388. $this->master_dim = ($this->height === 0) ? 'width' : 'height';
  1389. }
  1390. }
  1391. elseif (($this->master_dim === 'width' && $this->width === 0)
  1392. OR ($this->master_dim === 'height' && $this->height === 0))
  1393. {
  1394. return;
  1395. }
  1396. if ($this->master_dim === 'width')
  1397. {
  1398. $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width);
  1399. }
  1400. else
  1401. {
  1402. $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height);
  1403. }
  1404. }
  1405. // --------------------------------------------------------------------
  1406. /**
  1407. * Get image properties
  1408. *
  1409. * A helper function that gets info about the file
  1410. *
  1411. * @param string
  1412. * @param bool
  1413. * @return mixed
  1414. */
  1415. public function get_image_properties($path = '', $return = FALSE)
  1416. {
  1417. // For now we require GD but we should
  1418. // find a way to determine this using IM or NetPBM
  1419. if ($path === '')
  1420. {
  1421. $path = $this->full_src_path;
  1422. }
  1423. if ( ! file_exists($path))
  1424. {
  1425. $this->set_error('imglib_invalid_path');
  1426. return FALSE;
  1427. }
  1428. $vals = getimagesize($path);
  1429. $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
  1430. $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg';
  1431. if ($return === TRUE)
  1432. {
  1433. return array(
  1434. 'width' => $vals[0],
  1435. 'height' => $vals[1],
  1436. 'image_type' => $vals[2],
  1437. 'size_str' => $vals[3],
  1438. 'mime_type' => $mime
  1439. );
  1440. }
  1441. $this->orig_width = $vals[0];
  1442. $this->orig_height = $vals[1];
  1443. $this->image_type = $vals[2];
  1444. $this->size_str = $vals[3];
  1445. $this->mime_type = $mime;
  1446. return TRUE;
  1447. }
  1448. // --------------------------------------------------------------------
  1449. /**
  1450. * Size calculator
  1451. *
  1452. * This function takes a known width x height and
  1453. * recalculates it to a new size. Only one
  1454. * new variable needs to be known
  1455. *
  1456. * $props = array(
  1457. * 'width' => $width,
  1458. * 'height' => $height,
  1459. * 'new_width' => 40,
  1460. * 'new_height' => ''
  1461. * );
  1462. *
  1463. * @param array
  1464. * @return array
  1465. */
  1466. public function size_calculator($vals)
  1467. {
  1468. if ( ! is_array($vals))
  1469. {
  1470. return;
  1471. }
  1472. $allowed = array('new_width', 'new_height', 'width', 'height');
  1473. foreach ($allowed as $item)
  1474. {
  1475. if (empty($vals[$item]))
  1476. {
  1477. $vals[$item] = 0;
  1478. }
  1479. }
  1480. if ($vals['width'] === 0 OR $vals['height'] === 0)
  1481. {
  1482. return $vals;
  1483. }
  1484. if ($vals['new_width'] === 0)
  1485. {
  1486. $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
  1487. }
  1488. elseif ($vals['new_height'] === 0)
  1489. {
  1490. $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
  1491. }
  1492. return $vals;
  1493. }
  1494. // --------------------------------------------------------------------
  1495. /**
  1496. * Explode source_image
  1497. *
  1498. * This is a helper function that extracts the extension
  1499. * from the source_image. This function lets us deal with
  1500. * source_images with multiple periods, like: my.cool.jpg
  1501. * It returns an associative array with two elements:
  1502. * $array['ext'] = '.jpg';
  1503. * $array['name'] = 'my.cool';
  1504. *
  1505. * @param array
  1506. * @return array
  1507. */
  1508. public function explode_name($source_image)
  1509. {
  1510. $ext = strrchr($source_image, '.');
  1511. $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
  1512. return array('ext' => $ext, 'name' => $name);
  1513. }
  1514. // --------------------------------------------------------------------
  1515. /**
  1516. * Is GD Installed?
  1517. *
  1518. * @return bool
  1519. */
  1520. public function gd_loaded()
  1521. {
  1522. if ( ! extension_loaded('gd'))
  1523. {
  1524. /* As it is stated in the PHP manual, dl() is not always available
  1525. * and even if so - it could generate an E_WARNING message on failure
  1526. */
  1527. return (function_exists('dl') && @dl('gd.so'));
  1528. }
  1529. return TRUE;
  1530. }
  1531. // --------------------------------------------------------------------
  1532. /**
  1533. * Get GD version
  1534. *
  1535. * @return mixed
  1536. */
  1537. public function gd_version()
  1538. {
  1539. if (function_exists('gd_info'))
  1540. {
  1541. $gd_version = @gd_info();
  1542. return preg_replace('/\D/', '', $gd_version['GD Version']);
  1543. }
  1544. return FALSE;
  1545. }
  1546. // --------------------------------------------------------------------
  1547. /**
  1548. * Set error message
  1549. *
  1550. * @param string
  1551. * @return void
  1552. */
  1553. public function set_error($msg)
  1554. {
  1555. $CI =& get_instance();
  1556. $CI->lang->load('imglib');
  1557. if (is_array($msg))
  1558. {
  1559. foreach ($msg as $val)
  1560. {
  1561. $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val);
  1562. $this->error_msg[] = $msg;
  1563. log_message('error', $msg);
  1564. }
  1565. }
  1566. else
  1567. {
  1568. $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg);
  1569. $this->error_msg[] = $msg;
  1570. log_message('error', $msg);
  1571. }
  1572. }
  1573. // --------------------------------------------------------------------
  1574. /**
  1575. * Show error messages
  1576. *
  1577. * @param string
  1578. * @param string
  1579. * @return string
  1580. */
  1581. public function display_errors($open = '<p>', $close = '</p>')
  1582. {
  1583. return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
  1584. }
  1585. }