PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/magehelp/system/libraries/Image_lib.php

https://bitbucket.org/jit_bec/shopifine
PHP | 1537 lines | 900 code | 221 blank | 416 comment | 193 complexity | 517eace294d2524378794e7ac552dac0 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Image Manipulation class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Image_lib
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/libraries/image_lib.html
  24. */
  25. class CI_Image_lib {
  26. var $image_library = 'gd2'; // Can be: imagemagick, netpbm, gd, gd2
  27. var $library_path = '';
  28. var $dynamic_output = FALSE; // Whether to send to browser or write to disk
  29. var $source_image = '';
  30. var $new_image = '';
  31. var $width = '';
  32. var $height = '';
  33. var $quality = '90';
  34. var $create_thumb = FALSE;
  35. var $thumb_marker = '_thumb';
  36. var $maintain_ratio = TRUE; // Whether to maintain aspect ratio when resizing or use hard values
  37. var $master_dim = 'auto'; // auto, height, or width. Determines what to use as the master dimension
  38. var $rotation_angle = '';
  39. var $x_axis = '';
  40. var $y_axis = '';
  41. // Watermark Vars
  42. var $wm_text = ''; // Watermark text if graphic is not used
  43. var $wm_type = 'text'; // Type of watermarking. Options: text/overlay
  44. var $wm_x_transp = 4;
  45. var $wm_y_transp = 4;
  46. var $wm_overlay_path = ''; // Watermark image path
  47. var $wm_font_path = ''; // TT font
  48. var $wm_font_size = 17; // Font size (different versions of GD will either use points or pixels)
  49. var $wm_vrt_alignment = 'B'; // Vertical alignment: T M B
  50. var $wm_hor_alignment = 'C'; // Horizontal alignment: L R C
  51. var $wm_padding = 0; // Padding around text
  52. var $wm_hor_offset = 0; // Lets you push text to the right
  53. var $wm_vrt_offset = 0; // Lets you push text down
  54. var $wm_font_color = '#ffffff'; // Text color
  55. var $wm_shadow_color = ''; // Dropshadow color
  56. var $wm_shadow_distance = 2; // Dropshadow distance
  57. var $wm_opacity = 50; // Image opacity: 1 - 100 Only works with image
  58. // Private Vars
  59. var $source_folder = '';
  60. var $dest_folder = '';
  61. var $mime_type = '';
  62. var $orig_width = '';
  63. var $orig_height = '';
  64. var $image_type = '';
  65. var $size_str = '';
  66. var $full_src_path = '';
  67. var $full_dst_path = '';
  68. var $create_fnc = 'imagecreatetruecolor';
  69. var $copy_fnc = 'imagecopyresampled';
  70. var $error_msg = array();
  71. var $wm_use_drop_shadow = FALSE;
  72. var $wm_use_truetype = FALSE;
  73. /**
  74. * Constructor
  75. *
  76. * @param string
  77. * @return void
  78. */
  79. public function __construct($props = array())
  80. {
  81. if (count($props) > 0)
  82. {
  83. $this->initialize($props);
  84. }
  85. log_message('debug', "Image Lib Class Initialized");
  86. }
  87. // --------------------------------------------------------------------
  88. /**
  89. * Initialize image properties
  90. *
  91. * Resets values in case this class is used in a loop
  92. *
  93. * @access public
  94. * @return void
  95. */
  96. function clear()
  97. {
  98. $props = array('source_folder', 'dest_folder', 'source_image', 'full_src_path', 'full_dst_path', 'new_image', 'image_type', 'size_str', 'quality', 'orig_width', 'orig_height', 'rotation_angle', 'x_axis', 'y_axis', 'create_fnc', 'copy_fnc', 'wm_overlay_path', 'wm_use_truetype', 'dynamic_output', 'wm_font_size', 'wm_text', 'wm_vrt_alignment', 'wm_hor_alignment', 'wm_padding', 'wm_hor_offset', 'wm_vrt_offset', 'wm_font_color', 'wm_use_drop_shadow', 'wm_shadow_color', 'wm_shadow_distance', 'wm_opacity');
  99. foreach ($props as $val)
  100. {
  101. $this->$val = '';
  102. }
  103. // special consideration for master_dim
  104. $this->master_dim = 'auto';
  105. }
  106. // --------------------------------------------------------------------
  107. /**
  108. * initialize image preferences
  109. *
  110. * @access public
  111. * @param array
  112. * @return bool
  113. */
  114. function initialize($props = array())
  115. {
  116. /*
  117. * Convert array elements into class variables
  118. */
  119. if (count($props) > 0)
  120. {
  121. foreach ($props as $key => $val)
  122. {
  123. $this->$key = $val;
  124. }
  125. }
  126. /*
  127. * Is there a source image?
  128. *
  129. * If not, there's no reason to continue
  130. *
  131. */
  132. if ($this->source_image == '')
  133. {
  134. $this->set_error('imglib_source_image_required');
  135. return FALSE;
  136. }
  137. /*
  138. * Is getimagesize() Available?
  139. *
  140. * We use it to determine the image properties (width/height).
  141. * Note: We need to figure out how to determine image
  142. * properties using ImageMagick and NetPBM
  143. *
  144. */
  145. if ( ! function_exists('getimagesize'))
  146. {
  147. $this->set_error('imglib_gd_required_for_props');
  148. return FALSE;
  149. }
  150. $this->image_library = strtolower($this->image_library);
  151. /*
  152. * Set the full server path
  153. *
  154. * The source image may or may not contain a path.
  155. * Either way, we'll try use realpath to generate the
  156. * full server path in order to more reliably read it.
  157. *
  158. */
  159. if (function_exists('realpath') AND @realpath($this->source_image) !== FALSE)
  160. {
  161. $full_source_path = str_replace("\\", "/", realpath($this->source_image));
  162. }
  163. else
  164. {
  165. $full_source_path = $this->source_image;
  166. }
  167. $x = explode('/', $full_source_path);
  168. $this->source_image = end($x);
  169. $this->source_folder = str_replace($this->source_image, '', $full_source_path);
  170. // Set the Image Properties
  171. if ( ! $this->get_image_properties($this->source_folder.$this->source_image))
  172. {
  173. return FALSE;
  174. }
  175. /*
  176. * Assign the "new" image name/path
  177. *
  178. * If the user has set a "new_image" name it means
  179. * we are making a copy of the source image. If not
  180. * it means we are altering the original. We'll
  181. * set the destination filename and path accordingly.
  182. *
  183. */
  184. if ($this->new_image == '')
  185. {
  186. $this->dest_image = $this->source_image;
  187. $this->dest_folder = $this->source_folder;
  188. }
  189. else
  190. {
  191. if (strpos($this->new_image, '/') === FALSE)
  192. {
  193. $this->dest_folder = $this->source_folder;
  194. $this->dest_image = $this->new_image;
  195. }
  196. else
  197. {
  198. if (function_exists('realpath') AND @realpath($this->new_image) !== FALSE)
  199. {
  200. $full_dest_path = str_replace("\\", "/", realpath($this->new_image));
  201. }
  202. else
  203. {
  204. $full_dest_path = $this->new_image;
  205. }
  206. // Is there a file name?
  207. if ( ! preg_match("#\.(jpg|jpeg|gif|png)$#i", $full_dest_path))
  208. {
  209. $this->dest_folder = $full_dest_path.'/';
  210. $this->dest_image = $this->source_image;
  211. }
  212. else
  213. {
  214. $x = explode('/', $full_dest_path);
  215. $this->dest_image = end($x);
  216. $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path);
  217. }
  218. }
  219. }
  220. /*
  221. * Compile the finalized filenames/paths
  222. *
  223. * We'll create two master strings containing the
  224. * full server path to the source image and the
  225. * full server path to the destination image.
  226. * We'll also split the destination image name
  227. * so we can insert the thumbnail marker if needed.
  228. *
  229. */
  230. if ($this->create_thumb === FALSE OR $this->thumb_marker == '')
  231. {
  232. $this->thumb_marker = '';
  233. }
  234. $xp = $this->explode_name($this->dest_image);
  235. $filename = $xp['name'];
  236. $file_ext = $xp['ext'];
  237. $this->full_src_path = $this->source_folder.$this->source_image;
  238. $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext;
  239. /*
  240. * Should we maintain image proportions?
  241. *
  242. * When creating thumbs or copies, the target width/height
  243. * might not be in correct proportion with the source
  244. * image's width/height. We'll recalculate it here.
  245. *
  246. */
  247. if ($this->maintain_ratio === TRUE && ($this->width != '' AND $this->height != ''))
  248. {
  249. $this->image_reproportion();
  250. }
  251. /*
  252. * Was a width and height specified?
  253. *
  254. * If the destination width/height was
  255. * not submitted we will use the values
  256. * from the actual file
  257. *
  258. */
  259. if ($this->width == '')
  260. $this->width = $this->orig_width;
  261. if ($this->height == '')
  262. $this->height = $this->orig_height;
  263. // Set the quality
  264. $this->quality = trim(str_replace("%", "", $this->quality));
  265. if ($this->quality == '' OR $this->quality == 0 OR ! is_numeric($this->quality))
  266. $this->quality = 90;
  267. // Set the x/y coordinates
  268. $this->x_axis = ($this->x_axis == '' OR ! is_numeric($this->x_axis)) ? 0 : $this->x_axis;
  269. $this->y_axis = ($this->y_axis == '' OR ! is_numeric($this->y_axis)) ? 0 : $this->y_axis;
  270. // Watermark-related Stuff...
  271. if ($this->wm_font_color != '')
  272. {
  273. if (strlen($this->wm_font_color) == 6)
  274. {
  275. $this->wm_font_color = '#'.$this->wm_font_color;
  276. }
  277. }
  278. if ($this->wm_shadow_color != '')
  279. {
  280. if (strlen($this->wm_shadow_color) == 6)
  281. {
  282. $this->wm_shadow_color = '#'.$this->wm_shadow_color;
  283. }
  284. }
  285. if ($this->wm_overlay_path != '')
  286. {
  287. $this->wm_overlay_path = str_replace("\\", "/", realpath($this->wm_overlay_path));
  288. }
  289. if ($this->wm_shadow_color != '')
  290. {
  291. $this->wm_use_drop_shadow = TRUE;
  292. }
  293. if ($this->wm_font_path != '')
  294. {
  295. $this->wm_use_truetype = TRUE;
  296. }
  297. return TRUE;
  298. }
  299. // --------------------------------------------------------------------
  300. /**
  301. * Image Resize
  302. *
  303. * This is a wrapper function that chooses the proper
  304. * resize function based on the protocol specified
  305. *
  306. * @access public
  307. * @return bool
  308. */
  309. function resize()
  310. {
  311. $protocol = 'image_process_'.$this->image_library;
  312. if (preg_match('/gd2$/i', $protocol))
  313. {
  314. $protocol = 'image_process_gd';
  315. }
  316. return $this->$protocol('resize');
  317. }
  318. // --------------------------------------------------------------------
  319. /**
  320. * Image Crop
  321. *
  322. * This is a wrapper function that chooses the proper
  323. * cropping function based on the protocol specified
  324. *
  325. * @access public
  326. * @return bool
  327. */
  328. function crop()
  329. {
  330. $protocol = 'image_process_'.$this->image_library;
  331. if (preg_match('/gd2$/i', $protocol))
  332. {
  333. $protocol = 'image_process_gd';
  334. }
  335. return $this->$protocol('crop');
  336. }
  337. // --------------------------------------------------------------------
  338. /**
  339. * Image Rotate
  340. *
  341. * This is a wrapper function that chooses the proper
  342. * rotation function based on the protocol specified
  343. *
  344. * @access public
  345. * @return bool
  346. */
  347. function rotate()
  348. {
  349. // Allowed rotation values
  350. $degs = array(90, 180, 270, 'vrt', 'hor');
  351. if ($this->rotation_angle == '' OR ! in_array($this->rotation_angle, $degs))
  352. {
  353. $this->set_error('imglib_rotation_angle_required');
  354. return FALSE;
  355. }
  356. // Reassign the width and height
  357. if ($this->rotation_angle == 90 OR $this->rotation_angle == 270)
  358. {
  359. $this->width = $this->orig_height;
  360. $this->height = $this->orig_width;
  361. }
  362. else
  363. {
  364. $this->width = $this->orig_width;
  365. $this->height = $this->orig_height;
  366. }
  367. // Choose resizing function
  368. if ($this->image_library == 'imagemagick' OR $this->image_library == 'netpbm')
  369. {
  370. $protocol = 'image_process_'.$this->image_library;
  371. return $this->$protocol('rotate');
  372. }
  373. if ($this->rotation_angle == 'hor' OR $this->rotation_angle == 'vrt')
  374. {
  375. return $this->image_mirror_gd();
  376. }
  377. else
  378. {
  379. return $this->image_rotate_gd();
  380. }
  381. }
  382. // --------------------------------------------------------------------
  383. /**
  384. * Image Process Using GD/GD2
  385. *
  386. * This function will resize or crop
  387. *
  388. * @access public
  389. * @param string
  390. * @return bool
  391. */
  392. function image_process_gd($action = 'resize')
  393. {
  394. $v2_override = FALSE;
  395. // If the target width/height match the source, AND if the new file name is not equal to the old file name
  396. // we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
  397. if ($this->dynamic_output === FALSE)
  398. {
  399. if ($this->orig_width == $this->width AND $this->orig_height == $this->height)
  400. {
  401. if ($this->source_image != $this->new_image)
  402. {
  403. if (@copy($this->full_src_path, $this->full_dst_path))
  404. {
  405. @chmod($this->full_dst_path, FILE_WRITE_MODE);
  406. }
  407. }
  408. return TRUE;
  409. }
  410. }
  411. // Let's set up our values based on the action
  412. if ($action == 'crop')
  413. {
  414. // Reassign the source width/height if cropping
  415. $this->orig_width = $this->width;
  416. $this->orig_height = $this->height;
  417. // GD 2.0 has a cropping bug so we'll test for it
  418. if ($this->gd_version() !== FALSE)
  419. {
  420. $gd_version = str_replace('0', '', $this->gd_version());
  421. $v2_override = ($gd_version == 2) ? TRUE : FALSE;
  422. }
  423. }
  424. else
  425. {
  426. // If resizing the x/y axis must be zero
  427. $this->x_axis = 0;
  428. $this->y_axis = 0;
  429. }
  430. // Create the image handle
  431. if ( ! ($src_img = $this->image_create_gd()))
  432. {
  433. return FALSE;
  434. }
  435. // Create The Image
  436. //
  437. // old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater"
  438. // it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment
  439. // below should that ever prove inaccurate.
  440. //
  441. // if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor') AND $v2_override == FALSE)
  442. if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
  443. {
  444. $create = 'imagecreatetruecolor';
  445. $copy = 'imagecopyresampled';
  446. }
  447. else
  448. {
  449. $create = 'imagecreate';
  450. $copy = 'imagecopyresized';
  451. }
  452. $dst_img = $create($this->width, $this->height);
  453. if ($this->image_type == 3) // png we can actually preserve transparency
  454. {
  455. imagealphablending($dst_img, FALSE);
  456. imagesavealpha($dst_img, TRUE);
  457. }
  458. $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);
  459. // Show the image
  460. if ($this->dynamic_output == TRUE)
  461. {
  462. $this->image_display_gd($dst_img);
  463. }
  464. else
  465. {
  466. // Or save it
  467. if ( ! $this->image_save_gd($dst_img))
  468. {
  469. return FALSE;
  470. }
  471. }
  472. // Kill the file handles
  473. imagedestroy($dst_img);
  474. imagedestroy($src_img);
  475. // Set the file to 777
  476. @chmod($this->full_dst_path, FILE_WRITE_MODE);
  477. return TRUE;
  478. }
  479. // --------------------------------------------------------------------
  480. /**
  481. * Image Process Using ImageMagick
  482. *
  483. * This function will resize, crop or rotate
  484. *
  485. * @access public
  486. * @param string
  487. * @return bool
  488. */
  489. function image_process_imagemagick($action = 'resize')
  490. {
  491. // Do we have a vaild library path?
  492. if ($this->library_path == '')
  493. {
  494. $this->set_error('imglib_libpath_invalid');
  495. return FALSE;
  496. }
  497. if ( ! preg_match("/convert$/i", $this->library_path))
  498. {
  499. $this->library_path = rtrim($this->library_path, '/').'/';
  500. $this->library_path .= 'convert';
  501. }
  502. // Execute the command
  503. $cmd = $this->library_path." -quality ".$this->quality;
  504. if ($action == 'crop')
  505. {
  506. $cmd .= " -crop ".$this->width."x".$this->height."+".$this->x_axis."+".$this->y_axis." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
  507. }
  508. elseif ($action == 'rotate')
  509. {
  510. switch ($this->rotation_angle)
  511. {
  512. case 'hor' : $angle = '-flop';
  513. break;
  514. case 'vrt' : $angle = '-flip';
  515. break;
  516. default : $angle = '-rotate '.$this->rotation_angle;
  517. break;
  518. }
  519. $cmd .= " ".$angle." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
  520. }
  521. else // Resize
  522. {
  523. $cmd .= " -resize ".$this->width."x".$this->height." \"$this->full_src_path\" \"$this->full_dst_path\" 2>&1";
  524. }
  525. $retval = 1;
  526. @exec($cmd, $output, $retval);
  527. // Did it work?
  528. if ($retval > 0)
  529. {
  530. $this->set_error('imglib_image_process_failed');
  531. return FALSE;
  532. }
  533. // Set the file to 777
  534. @chmod($this->full_dst_path, FILE_WRITE_MODE);
  535. return TRUE;
  536. }
  537. // --------------------------------------------------------------------
  538. /**
  539. * Image Process Using NetPBM
  540. *
  541. * This function will resize, crop or rotate
  542. *
  543. * @access public
  544. * @param string
  545. * @return bool
  546. */
  547. function image_process_netpbm($action = 'resize')
  548. {
  549. if ($this->library_path == '')
  550. {
  551. $this->set_error('imglib_libpath_invalid');
  552. return FALSE;
  553. }
  554. // Build the resizing command
  555. switch ($this->image_type)
  556. {
  557. case 1 :
  558. $cmd_in = 'giftopnm';
  559. $cmd_out = 'ppmtogif';
  560. break;
  561. case 2 :
  562. $cmd_in = 'jpegtopnm';
  563. $cmd_out = 'ppmtojpeg';
  564. break;
  565. case 3 :
  566. $cmd_in = 'pngtopnm';
  567. $cmd_out = 'ppmtopng';
  568. break;
  569. }
  570. if ($action == 'crop')
  571. {
  572. $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height;
  573. }
  574. elseif ($action == 'rotate')
  575. {
  576. switch ($this->rotation_angle)
  577. {
  578. case 90 : $angle = 'r270';
  579. break;
  580. case 180 : $angle = 'r180';
  581. break;
  582. case 270 : $angle = 'r90';
  583. break;
  584. case 'vrt' : $angle = 'tb';
  585. break;
  586. case 'hor' : $angle = 'lr';
  587. break;
  588. }
  589. $cmd_inner = 'pnmflip -'.$angle.' ';
  590. }
  591. else // Resize
  592. {
  593. $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height;
  594. }
  595. $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp';
  596. $retval = 1;
  597. @exec($cmd, $output, $retval);
  598. // Did it work?
  599. if ($retval > 0)
  600. {
  601. $this->set_error('imglib_image_process_failed');
  602. return FALSE;
  603. }
  604. // With NetPBM we have to create a temporary image.
  605. // If you try manipulating the original it fails so
  606. // we have to rename the temp file.
  607. copy ($this->dest_folder.'netpbm.tmp', $this->full_dst_path);
  608. unlink ($this->dest_folder.'netpbm.tmp');
  609. @chmod($this->full_dst_path, FILE_WRITE_MODE);
  610. return TRUE;
  611. }
  612. // --------------------------------------------------------------------
  613. /**
  614. * Image Rotate Using GD
  615. *
  616. * @access public
  617. * @return bool
  618. */
  619. function image_rotate_gd()
  620. {
  621. // Create the image handle
  622. if ( ! ($src_img = $this->image_create_gd()))
  623. {
  624. return FALSE;
  625. }
  626. // Set the background color
  627. // This won't work with transparent PNG files so we are
  628. // going to have to figure out how to determine the color
  629. // of the alpha channel in a future release.
  630. $white = imagecolorallocate($src_img, 255, 255, 255);
  631. // Rotate it!
  632. $dst_img = imagerotate($src_img, $this->rotation_angle, $white);
  633. // Save the Image
  634. if ($this->dynamic_output == TRUE)
  635. {
  636. $this->image_display_gd($dst_img);
  637. }
  638. else
  639. {
  640. // Or save it
  641. if ( ! $this->image_save_gd($dst_img))
  642. {
  643. return FALSE;
  644. }
  645. }
  646. // Kill the file handles
  647. imagedestroy($dst_img);
  648. imagedestroy($src_img);
  649. // Set the file to 777
  650. @chmod($this->full_dst_path, FILE_WRITE_MODE);
  651. return TRUE;
  652. }
  653. // --------------------------------------------------------------------
  654. /**
  655. * Create Mirror Image using GD
  656. *
  657. * This function will flip horizontal or vertical
  658. *
  659. * @access public
  660. * @return bool
  661. */
  662. function image_mirror_gd()
  663. {
  664. if ( ! $src_img = $this->image_create_gd())
  665. {
  666. return FALSE;
  667. }
  668. $width = $this->orig_width;
  669. $height = $this->orig_height;
  670. if ($this->rotation_angle == 'hor')
  671. {
  672. for ($i = 0; $i < $height; $i++)
  673. {
  674. $left = 0;
  675. $right = $width-1;
  676. while ($left < $right)
  677. {
  678. $cl = imagecolorat($src_img, $left, $i);
  679. $cr = imagecolorat($src_img, $right, $i);
  680. imagesetpixel($src_img, $left, $i, $cr);
  681. imagesetpixel($src_img, $right, $i, $cl);
  682. $left++;
  683. $right--;
  684. }
  685. }
  686. }
  687. else
  688. {
  689. for ($i = 0; $i < $width; $i++)
  690. {
  691. $top = 0;
  692. $bot = $height-1;
  693. while ($top < $bot)
  694. {
  695. $ct = imagecolorat($src_img, $i, $top);
  696. $cb = imagecolorat($src_img, $i, $bot);
  697. imagesetpixel($src_img, $i, $top, $cb);
  698. imagesetpixel($src_img, $i, $bot, $ct);
  699. $top++;
  700. $bot--;
  701. }
  702. }
  703. }
  704. // Show the image
  705. if ($this->dynamic_output == TRUE)
  706. {
  707. $this->image_display_gd($src_img);
  708. }
  709. else
  710. {
  711. // Or save it
  712. if ( ! $this->image_save_gd($src_img))
  713. {
  714. return FALSE;
  715. }
  716. }
  717. // Kill the file handles
  718. imagedestroy($src_img);
  719. // Set the file to 777
  720. @chmod($this->full_dst_path, FILE_WRITE_MODE);
  721. return TRUE;
  722. }
  723. // --------------------------------------------------------------------
  724. /**
  725. * Image Watermark
  726. *
  727. * This is a wrapper function that chooses the type
  728. * of watermarking based on the specified preference.
  729. *
  730. * @access public
  731. * @param string
  732. * @return bool
  733. */
  734. function watermark()
  735. {
  736. if ($this->wm_type == 'overlay')
  737. {
  738. return $this->overlay_watermark();
  739. }
  740. else
  741. {
  742. return $this->text_watermark();
  743. }
  744. }
  745. // --------------------------------------------------------------------
  746. /**
  747. * Watermark - Graphic Version
  748. *
  749. * @access public
  750. * @return bool
  751. */
  752. function overlay_watermark()
  753. {
  754. if ( ! function_exists('imagecolortransparent'))
  755. {
  756. $this->set_error('imglib_gd_required');
  757. return FALSE;
  758. }
  759. // Fetch source image properties
  760. $this->get_image_properties();
  761. // Fetch watermark image properties
  762. $props = $this->get_image_properties($this->wm_overlay_path, TRUE);
  763. $wm_img_type = $props['image_type'];
  764. $wm_width = $props['width'];
  765. $wm_height = $props['height'];
  766. // Create two image resources
  767. $wm_img = $this->image_create_gd($this->wm_overlay_path, $wm_img_type);
  768. $src_img = $this->image_create_gd($this->full_src_path);
  769. // Reverse the offset if necessary
  770. // When the image is positioned at the bottom
  771. // we don't want the vertical offset to push it
  772. // further down. We want the reverse, so we'll
  773. // invert the offset. Same with the horizontal
  774. // offset when the image is at the right
  775. $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
  776. $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
  777. if ($this->wm_vrt_alignment == 'B')
  778. $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
  779. if ($this->wm_hor_alignment == 'R')
  780. $this->wm_hor_offset = $this->wm_hor_offset * -1;
  781. // Set the base x and y axis values
  782. $x_axis = $this->wm_hor_offset + $this->wm_padding;
  783. $y_axis = $this->wm_vrt_offset + $this->wm_padding;
  784. // Set the vertical position
  785. switch ($this->wm_vrt_alignment)
  786. {
  787. case 'T':
  788. break;
  789. case 'M': $y_axis += ($this->orig_height / 2) - ($wm_height / 2);
  790. break;
  791. case 'B': $y_axis += $this->orig_height - $wm_height;
  792. break;
  793. }
  794. // Set the horizontal position
  795. switch ($this->wm_hor_alignment)
  796. {
  797. case 'L':
  798. break;
  799. case 'C': $x_axis += ($this->orig_width / 2) - ($wm_width / 2);
  800. break;
  801. case 'R': $x_axis += $this->orig_width - $wm_width;
  802. break;
  803. }
  804. // Build the finalized image
  805. if ($wm_img_type == 3 AND function_exists('imagealphablending'))
  806. {
  807. @imagealphablending($src_img, TRUE);
  808. }
  809. // Set RGB values for text and shadow
  810. $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp);
  811. $alpha = ($rgba & 0x7F000000) >> 24;
  812. // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency
  813. if ($alpha > 0)
  814. {
  815. // copy the image directly, the image's alpha transparency being the sole determinant of blending
  816. imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height);
  817. }
  818. else
  819. {
  820. // set our RGB value from above to be transparent and merge the images with the specified opacity
  821. imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp));
  822. imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity);
  823. }
  824. // Output the image
  825. if ($this->dynamic_output == TRUE)
  826. {
  827. $this->image_display_gd($src_img);
  828. }
  829. else
  830. {
  831. if ( ! $this->image_save_gd($src_img))
  832. {
  833. return FALSE;
  834. }
  835. }
  836. imagedestroy($src_img);
  837. imagedestroy($wm_img);
  838. return TRUE;
  839. }
  840. // --------------------------------------------------------------------
  841. /**
  842. * Watermark - Text Version
  843. *
  844. * @access public
  845. * @return bool
  846. */
  847. function text_watermark()
  848. {
  849. if ( ! ($src_img = $this->image_create_gd()))
  850. {
  851. return FALSE;
  852. }
  853. if ($this->wm_use_truetype == TRUE AND ! file_exists($this->wm_font_path))
  854. {
  855. $this->set_error('imglib_missing_font');
  856. return FALSE;
  857. }
  858. // Fetch source image properties
  859. $this->get_image_properties();
  860. // Set RGB values for text and shadow
  861. $this->wm_font_color = str_replace('#', '', $this->wm_font_color);
  862. $this->wm_shadow_color = str_replace('#', '', $this->wm_shadow_color);
  863. $R1 = hexdec(substr($this->wm_font_color, 0, 2));
  864. $G1 = hexdec(substr($this->wm_font_color, 2, 2));
  865. $B1 = hexdec(substr($this->wm_font_color, 4, 2));
  866. $R2 = hexdec(substr($this->wm_shadow_color, 0, 2));
  867. $G2 = hexdec(substr($this->wm_shadow_color, 2, 2));
  868. $B2 = hexdec(substr($this->wm_shadow_color, 4, 2));
  869. $txt_color = imagecolorclosest($src_img, $R1, $G1, $B1);
  870. $drp_color = imagecolorclosest($src_img, $R2, $G2, $B2);
  871. // Reverse the vertical offset
  872. // When the image is positioned at the bottom
  873. // we don't want the vertical offset to push it
  874. // further down. We want the reverse, so we'll
  875. // invert the offset. Note: The horizontal
  876. // offset flips itself automatically
  877. if ($this->wm_vrt_alignment == 'B')
  878. $this->wm_vrt_offset = $this->wm_vrt_offset * -1;
  879. if ($this->wm_hor_alignment == 'R')
  880. $this->wm_hor_offset = $this->wm_hor_offset * -1;
  881. // Set font width and height
  882. // These are calculated differently depending on
  883. // whether we are using the true type font or not
  884. if ($this->wm_use_truetype == TRUE)
  885. {
  886. if ($this->wm_font_size == '')
  887. $this->wm_font_size = '17';
  888. $fontwidth = $this->wm_font_size-($this->wm_font_size/4);
  889. $fontheight = $this->wm_font_size;
  890. $this->wm_vrt_offset += $this->wm_font_size;
  891. }
  892. else
  893. {
  894. $fontwidth = imagefontwidth($this->wm_font_size);
  895. $fontheight = imagefontheight($this->wm_font_size);
  896. }
  897. // Set base X and Y axis values
  898. $x_axis = $this->wm_hor_offset + $this->wm_padding;
  899. $y_axis = $this->wm_vrt_offset + $this->wm_padding;
  900. // Set verticle alignment
  901. if ($this->wm_use_drop_shadow == FALSE)
  902. $this->wm_shadow_distance = 0;
  903. $this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
  904. $this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
  905. switch ($this->wm_vrt_alignment)
  906. {
  907. case "T" :
  908. break;
  909. case "M": $y_axis += ($this->orig_height/2)+($fontheight/2);
  910. break;
  911. case "B": $y_axis += ($this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight/2));
  912. break;
  913. }
  914. $x_shad = $x_axis + $this->wm_shadow_distance;
  915. $y_shad = $y_axis + $this->wm_shadow_distance;
  916. // Set horizontal alignment
  917. switch ($this->wm_hor_alignment)
  918. {
  919. case "L":
  920. break;
  921. case "R":
  922. if ($this->wm_use_drop_shadow)
  923. $x_shad += ($this->orig_width - $fontwidth*strlen($this->wm_text));
  924. $x_axis += ($this->orig_width - $fontwidth*strlen($this->wm_text));
  925. break;
  926. case "C":
  927. if ($this->wm_use_drop_shadow)
  928. $x_shad += floor(($this->orig_width - $fontwidth*strlen($this->wm_text))/2);
  929. $x_axis += floor(($this->orig_width -$fontwidth*strlen($this->wm_text))/2);
  930. break;
  931. }
  932. // Add the text to the source image
  933. if ($this->wm_use_truetype)
  934. {
  935. if ($this->wm_use_drop_shadow)
  936. imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text);
  937. imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text);
  938. }
  939. else
  940. {
  941. if ($this->wm_use_drop_shadow)
  942. imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color);
  943. imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color);
  944. }
  945. // Output the final image
  946. if ($this->dynamic_output == TRUE)
  947. {
  948. $this->image_display_gd($src_img);
  949. }
  950. else
  951. {
  952. $this->image_save_gd($src_img);
  953. }
  954. imagedestroy($src_img);
  955. return TRUE;
  956. }
  957. // --------------------------------------------------------------------
  958. /**
  959. * Create Image - GD
  960. *
  961. * This simply creates an image resource handle
  962. * based on the type of image being processed
  963. *
  964. * @access public
  965. * @param string
  966. * @return resource
  967. */
  968. function image_create_gd($path = '', $image_type = '')
  969. {
  970. if ($path == '')
  971. $path = $this->full_src_path;
  972. if ($image_type == '')
  973. $image_type = $this->image_type;
  974. switch ($image_type)
  975. {
  976. case 1 :
  977. if ( ! function_exists('imagecreatefromgif'))
  978. {
  979. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
  980. return FALSE;
  981. }
  982. return imagecreatefromgif($path);
  983. break;
  984. case 2 :
  985. if ( ! function_exists('imagecreatefromjpeg'))
  986. {
  987. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
  988. return FALSE;
  989. }
  990. return imagecreatefromjpeg($path);
  991. break;
  992. case 3 :
  993. if ( ! function_exists('imagecreatefrompng'))
  994. {
  995. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
  996. return FALSE;
  997. }
  998. return imagecreatefrompng($path);
  999. break;
  1000. }
  1001. $this->set_error(array('imglib_unsupported_imagecreate'));
  1002. return FALSE;
  1003. }
  1004. // --------------------------------------------------------------------
  1005. /**
  1006. * Write image file to disk - GD
  1007. *
  1008. * Takes an image resource as input and writes the file
  1009. * to the specified destination
  1010. *
  1011. * @access public
  1012. * @param resource
  1013. * @return bool
  1014. */
  1015. function image_save_gd($resource)
  1016. {
  1017. switch ($this->image_type)
  1018. {
  1019. case 1 :
  1020. if ( ! function_exists('imagegif'))
  1021. {
  1022. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
  1023. return FALSE;
  1024. }
  1025. if ( ! @imagegif($resource, $this->full_dst_path))
  1026. {
  1027. $this->set_error('imglib_save_failed');
  1028. return FALSE;
  1029. }
  1030. break;
  1031. case 2 :
  1032. if ( ! function_exists('imagejpeg'))
  1033. {
  1034. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported'));
  1035. return FALSE;
  1036. }
  1037. if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality))
  1038. {
  1039. $this->set_error('imglib_save_failed');
  1040. return FALSE;
  1041. }
  1042. break;
  1043. case 3 :
  1044. if ( ! function_exists('imagepng'))
  1045. {
  1046. $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
  1047. return FALSE;
  1048. }
  1049. if ( ! @imagepng($resource, $this->full_dst_path))
  1050. {
  1051. $this->set_error('imglib_save_failed');
  1052. return FALSE;
  1053. }
  1054. break;
  1055. default :
  1056. $this->set_error(array('imglib_unsupported_imagecreate'));
  1057. return FALSE;
  1058. break;
  1059. }
  1060. return TRUE;
  1061. }
  1062. // --------------------------------------------------------------------
  1063. /**
  1064. * Dynamically outputs an image
  1065. *
  1066. * @access public
  1067. * @param resource
  1068. * @return void
  1069. */
  1070. function image_display_gd($resource)
  1071. {
  1072. header("Content-Disposition: filename={$this->source_image};");
  1073. header("Content-Type: {$this->mime_type}");
  1074. header('Content-Transfer-Encoding: binary');
  1075. header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
  1076. switch ($this->image_type)
  1077. {
  1078. case 1 : imagegif($resource);
  1079. break;
  1080. case 2 : imagejpeg($resource, '', $this->quality);
  1081. break;
  1082. case 3 : imagepng($resource);
  1083. break;
  1084. default : echo 'Unable to display the image';
  1085. break;
  1086. }
  1087. }
  1088. // --------------------------------------------------------------------
  1089. /**
  1090. * Re-proportion Image Width/Height
  1091. *
  1092. * When creating thumbs, the desired width/height
  1093. * can end up warping the image due to an incorrect
  1094. * ratio between the full-sized image and the thumb.
  1095. *
  1096. * This function lets us re-proportion the width/height
  1097. * if users choose to maintain the aspect ratio when resizing.
  1098. *
  1099. * @access public
  1100. * @return void
  1101. */
  1102. function image_reproportion()
  1103. {
  1104. if ( ! is_numeric($this->width) OR ! is_numeric($this->height) OR $this->width == 0 OR $this->height == 0)
  1105. return;
  1106. if ( ! is_numeric($this->orig_width) OR ! is_numeric($this->orig_height) OR $this->orig_width == 0 OR $this->orig_height == 0)
  1107. return;
  1108. $new_width = ceil($this->orig_width*$this->height/$this->orig_height);
  1109. $new_height = ceil($this->width*$this->orig_height/$this->orig_width);
  1110. $ratio = (($this->orig_height/$this->orig_width) - ($this->height/$this->width));
  1111. if ($this->master_dim != 'width' AND $this->master_dim != 'height')
  1112. {
  1113. $this->master_dim = ($ratio < 0) ? 'width' : 'height';
  1114. }
  1115. if (($this->width != $new_width) AND ($this->height != $new_height))
  1116. {
  1117. if ($this->master_dim == 'height')
  1118. {
  1119. $this->width = $new_width;
  1120. }
  1121. else
  1122. {
  1123. $this->height = $new_height;
  1124. }
  1125. }
  1126. }
  1127. // --------------------------------------------------------------------
  1128. /**
  1129. * Get image properties
  1130. *
  1131. * A helper function that gets info about the file
  1132. *
  1133. * @access public
  1134. * @param string
  1135. * @return mixed
  1136. */
  1137. function get_image_properties($path = '', $return = FALSE)
  1138. {
  1139. // For now we require GD but we should
  1140. // find a way to determine this using IM or NetPBM
  1141. if ($path == '')
  1142. $path = $this->full_src_path;
  1143. if ( ! file_exists($path))
  1144. {
  1145. $this->set_error('imglib_invalid_path');
  1146. return FALSE;
  1147. }
  1148. $vals = @getimagesize($path);
  1149. $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
  1150. $mime = (isset($types[$vals['2']])) ? 'image/'.$types[$vals['2']] : 'image/jpg';
  1151. if ($return == TRUE)
  1152. {
  1153. $v['width'] = $vals['0'];
  1154. $v['height'] = $vals['1'];
  1155. $v['image_type'] = $vals['2'];
  1156. $v['size_str'] = $vals['3'];
  1157. $v['mime_type'] = $mime;
  1158. return $v;
  1159. }
  1160. $this->orig_width = $vals['0'];
  1161. $this->orig_height = $vals['1'];
  1162. $this->image_type = $vals['2'];
  1163. $this->size_str = $vals['3'];
  1164. $this->mime_type = $mime;
  1165. return TRUE;
  1166. }
  1167. // --------------------------------------------------------------------
  1168. /**
  1169. * Size calculator
  1170. *
  1171. * This function takes a known width x height and
  1172. * recalculates it to a new size. Only one
  1173. * new variable needs to be known
  1174. *
  1175. * $props = array(
  1176. * 'width' => $width,
  1177. * 'height' => $height,
  1178. * 'new_width' => 40,
  1179. * 'new_height' => ''
  1180. * );
  1181. *
  1182. * @access public
  1183. * @param array
  1184. * @return array
  1185. */
  1186. function size_calculator($vals)
  1187. {
  1188. if ( ! is_array($vals))
  1189. {
  1190. return;
  1191. }
  1192. $allowed = array('new_width', 'new_height', 'width', 'height');
  1193. foreach ($allowed as $item)
  1194. {
  1195. if ( ! isset($vals[$item]) OR $vals[$item] == '')
  1196. $vals[$item] = 0;
  1197. }
  1198. if ($vals['width'] == 0 OR $vals['height'] == 0)
  1199. {
  1200. return $vals;
  1201. }
  1202. if ($vals['new_width'] == 0)
  1203. {
  1204. $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']);
  1205. }
  1206. elseif ($vals['new_height'] == 0)
  1207. {
  1208. $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']);
  1209. }
  1210. return $vals;
  1211. }
  1212. // --------------------------------------------------------------------
  1213. /**
  1214. * Explode source_image
  1215. *
  1216. * This is a helper function that extracts the extension
  1217. * from the source_image. This function lets us deal with
  1218. * source_images with multiple periods, like: my.cool.jpg
  1219. * It returns an associative array with two elements:
  1220. * $array['ext'] = '.jpg';
  1221. * $array['name'] = 'my.cool';
  1222. *
  1223. * @access public
  1224. * @param array
  1225. * @return array
  1226. */
  1227. function explode_name($source_image)
  1228. {
  1229. $ext = strrchr($source_image, '.');
  1230. $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext));
  1231. return array('ext' => $ext, 'name' => $name);
  1232. }
  1233. // --------------------------------------------------------------------
  1234. /**
  1235. * Is GD Installed?
  1236. *
  1237. * @access public
  1238. * @return bool
  1239. */
  1240. function gd_loaded()
  1241. {
  1242. if ( ! extension_loaded('gd'))
  1243. {
  1244. if ( ! dl('gd.so'))
  1245. {
  1246. return FALSE;
  1247. }
  1248. }
  1249. return TRUE;
  1250. }
  1251. // --------------------------------------------------------------------
  1252. /**
  1253. * Get GD version
  1254. *
  1255. * @access public
  1256. * @return mixed
  1257. */
  1258. function gd_version()
  1259. {
  1260. if (function_exists('gd_info'))
  1261. {
  1262. $gd_version = @gd_info();
  1263. $gd_version = preg_replace("/\D/", "", $gd_version['GD Version']);
  1264. return $gd_version;
  1265. }
  1266. return FALSE;
  1267. }
  1268. // --------------------------------------------------------------------
  1269. /**
  1270. * Set error message
  1271. *
  1272. * @access public
  1273. * @param string
  1274. * @return void
  1275. */
  1276. function set_error($msg)
  1277. {
  1278. $CI =& get_instance();
  1279. $CI->lang->load('imglib');
  1280. if (is_array($msg))
  1281. {
  1282. foreach ($msg as $val)
  1283. {
  1284. $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
  1285. $this->error_msg[] = $msg;
  1286. log_message('error', $msg);
  1287. }
  1288. }
  1289. else
  1290. {
  1291. $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
  1292. $this->error_msg[] = $msg;
  1293. log_message('error', $msg);
  1294. }
  1295. }
  1296. // --------------------------------------------------------------------
  1297. /**
  1298. * Show error messages
  1299. *
  1300. * @access public
  1301. * @param string
  1302. * @return string
  1303. */
  1304. function display_errors($open = '<p>', $close = '</p>')
  1305. {
  1306. $str = '';
  1307. foreach ($this->error_msg as $val)
  1308. {
  1309. $str .= $open.$val.$close;
  1310. }
  1311. return $str;
  1312. }
  1313. }
  1314. // END Image_lib Class
  1315. /* End of file Image_lib.php */
  1316. /* Location: ./system/libraries/Image_lib.php */