PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Image_lib.php

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