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