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