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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
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 | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2016, 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 |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | 125ef47 | 2016-01-11 12:33:00 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 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 |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/image_lib.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 395 | log_message('info', '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 | { |
Andrey Andreev | eac4adf | 2016-03-22 11:24:14 +0200 | [diff] [blame^] | 459 | if (in_array($key, array('wm_font_color', 'wm_shadow_color'), TRUE)) |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 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 | } |
Andrey Andreev | eac4adf | 2016-03-22 11:24:14 +0200 | [diff] [blame^] | 481 | elseif (in_array($key, array('width', 'height'), TRUE) && ! ctype_digit((string) $val)) |
| 482 | { |
| 483 | continue; |
| 484 | } |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 485 | |
| 486 | $this->$key = $val; |
| 487 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 491 | // 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] | 492 | if ($this->source_image === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | { |
| 494 | $this->set_error('imglib_source_image_required'); |
Eric Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 495 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 498 | /* Is getimagesize() available? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | * |
| 500 | * We use it to determine the image properties (width/height). |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 501 | * Note: We need to figure out how to determine image |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | * properties using ImageMagick and NetPBM |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | */ |
| 504 | if ( ! function_exists('getimagesize')) |
| 505 | { |
| 506 | $this->set_error('imglib_gd_required_for_props'); |
| 507 | return FALSE; |
| 508 | } |
| 509 | |
| 510 | $this->image_library = strtolower($this->image_library); |
| 511 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 512 | /* Set the full server path |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 513 | * |
| 514 | * The source image may or may not contain a path. |
| 515 | * Either way, we'll try use realpath to generate the |
| 516 | * full server path in order to more reliably read it. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | */ |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 518 | if (($full_source_path = realpath($this->source_image)) !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 519 | { |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 520 | $full_source_path = str_replace('\\', '/', $full_source_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 521 | } |
| 522 | else |
| 523 | { |
| 524 | $full_source_path = $this->source_image; |
| 525 | } |
| 526 | |
| 527 | $x = explode('/', $full_source_path); |
| 528 | $this->source_image = end($x); |
| 529 | $this->source_folder = str_replace($this->source_image, '', $full_source_path); |
| 530 | |
| 531 | // Set the Image Properties |
| 532 | if ( ! $this->get_image_properties($this->source_folder.$this->source_image)) |
| 533 | { |
Eric Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 534 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Assign the "new" image name/path |
| 539 | * |
| 540 | * If the user has set a "new_image" name it means |
| 541 | * we are making a copy of the source image. If not |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 542 | * it means we are altering the original. We'll |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | * set the destination filename and path accordingly. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | */ |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 545 | if ($this->new_image === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 546 | { |
| 547 | $this->dest_image = $this->source_image; |
| 548 | $this->dest_folder = $this->source_folder; |
| 549 | } |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 550 | elseif (strpos($this->new_image, '/') === FALSE) |
| 551 | { |
| 552 | $this->dest_folder = $this->source_folder; |
| 553 | $this->dest_image = $this->new_image; |
| 554 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 555 | else |
| 556 | { |
Andrey Andreev | 1b81553 | 2012-04-03 16:06:03 +0300 | [diff] [blame] | 557 | if (strpos($this->new_image, '/') === FALSE && strpos($this->new_image, '\\') === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 558 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 559 | $full_dest_path = str_replace('\\', '/', realpath($this->new_image)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | } |
| 561 | else |
| 562 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 563 | $full_dest_path = $this->new_image; |
| 564 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 566 | // Is there a file name? |
| 567 | if ( ! preg_match('#\.(jpg|jpeg|gif|png)$#i', $full_dest_path)) |
| 568 | { |
| 569 | $this->dest_folder = $full_dest_path.'/'; |
| 570 | $this->dest_image = $this->source_image; |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | $x = explode('/', $full_dest_path); |
| 575 | $this->dest_image = end($x); |
| 576 | $this->dest_folder = str_replace($this->dest_image, '', $full_dest_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 580 | /* Compile the finalized filenames/paths |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 581 | * |
| 582 | * We'll create two master strings containing the |
| 583 | * full server path to the source image and the |
| 584 | * full server path to the destination image. |
| 585 | * We'll also split the destination image name |
| 586 | * so we can insert the thumbnail marker if needed. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 587 | */ |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 588 | if ($this->create_thumb === FALSE OR $this->thumb_marker === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 589 | { |
| 590 | $this->thumb_marker = ''; |
| 591 | } |
| 592 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 593 | $xp = $this->explode_name($this->dest_image); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 594 | |
| 595 | $filename = $xp['name']; |
| 596 | $file_ext = $xp['ext']; |
| 597 | |
| 598 | $this->full_src_path = $this->source_folder.$this->source_image; |
| 599 | $this->full_dst_path = $this->dest_folder.$filename.$this->thumb_marker.$file_ext; |
| 600 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 601 | /* Should we maintain image proportions? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 602 | * |
| 603 | * When creating thumbs or copies, the target width/height |
| 604 | * might not be in correct proportion with the source |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 605 | * image's width/height. We'll recalculate it here. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 606 | */ |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 607 | if ($this->maintain_ratio === TRUE && ($this->width !== 0 OR $this->height !== 0)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 608 | { |
| 609 | $this->image_reproportion(); |
| 610 | } |
| 611 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 612 | /* Was a width and height specified? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 613 | * |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 614 | * If the destination width/height was not submitted we |
| 615 | * will use the values from the actual file |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 616 | */ |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 617 | if ($this->width === '') |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 618 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 619 | $this->width = $this->orig_width; |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 620 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 621 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 622 | if ($this->height === '') |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 623 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | $this->height = $this->orig_height; |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 625 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 626 | |
| 627 | // Set the quality |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 628 | $this->quality = trim(str_replace('%', '', $this->quality)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 629 | |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 630 | if ($this->quality === '' OR $this->quality === 0 OR ! ctype_digit($this->quality)) |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 631 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | $this->quality = 90; |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 633 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 634 | |
| 635 | // Set the x/y coordinates |
Andrey Andreev | 5036c9c | 2012-06-04 15:34:56 +0300 | [diff] [blame] | 636 | is_numeric($this->x_axis) OR $this->x_axis = 0; |
| 637 | is_numeric($this->y_axis) OR $this->y_axis = 0; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 638 | |
| 639 | // Watermark-related Stuff... |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 640 | if ($this->wm_overlay_path !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 641 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 642 | $this->wm_overlay_path = str_replace('\\', '/', realpath($this->wm_overlay_path)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 645 | if ($this->wm_shadow_color !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 646 | { |
| 647 | $this->wm_use_drop_shadow = TRUE; |
| 648 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 649 | elseif ($this->wm_use_drop_shadow === TRUE && $this->wm_shadow_color === '') |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 650 | { |
| 651 | $this->wm_use_drop_shadow = FALSE; |
| 652 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 653 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 654 | if ($this->wm_font_path !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | { |
| 656 | $this->wm_use_truetype = TRUE; |
| 657 | } |
| 658 | |
| 659 | return TRUE; |
| 660 | } |
| 661 | |
| 662 | // -------------------------------------------------------------------- |
| 663 | |
| 664 | /** |
| 665 | * Image Resize |
| 666 | * |
| 667 | * This is a wrapper function that chooses the proper |
| 668 | * resize function based on the protocol specified |
| 669 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 670 | * @return bool |
| 671 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 672 | public function resize() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 673 | { |
Andrey Andreev | 1a9f52c | 2012-01-07 01:06:34 +0200 | [diff] [blame] | 674 | $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] | 675 | return $this->$protocol('resize'); |
| 676 | } |
| 677 | |
| 678 | // -------------------------------------------------------------------- |
| 679 | |
| 680 | /** |
| 681 | * Image Crop |
| 682 | * |
| 683 | * This is a wrapper function that chooses the proper |
| 684 | * cropping function based on the protocol specified |
| 685 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | * @return bool |
| 687 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 688 | public function crop() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 689 | { |
Andrey Andreev | 5a67e3d | 2012-01-07 01:21:09 +0200 | [diff] [blame] | 690 | $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] | 691 | return $this->$protocol('crop'); |
| 692 | } |
| 693 | |
| 694 | // -------------------------------------------------------------------- |
| 695 | |
| 696 | /** |
| 697 | * Image Rotate |
| 698 | * |
| 699 | * This is a wrapper function that chooses the proper |
| 700 | * rotation function based on the protocol specified |
| 701 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 702 | * @return bool |
| 703 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 704 | public function rotate() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 705 | { |
| 706 | // Allowed rotation values |
| 707 | $degs = array(90, 180, 270, 'vrt', 'hor'); |
| 708 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 709 | if ($this->rotation_angle === '' OR ! in_array($this->rotation_angle, $degs)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 710 | { |
| 711 | $this->set_error('imglib_rotation_angle_required'); |
Eric Barnes | b167336 | 2011-12-05 22:05:38 -0500 | [diff] [blame] | 712 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // Reassign the width and height |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 716 | if ($this->rotation_angle === 90 OR $this->rotation_angle === 270) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 717 | { |
| 718 | $this->width = $this->orig_height; |
| 719 | $this->height = $this->orig_width; |
| 720 | } |
| 721 | else |
| 722 | { |
| 723 | $this->width = $this->orig_width; |
| 724 | $this->height = $this->orig_height; |
| 725 | } |
| 726 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 727 | // Choose resizing function |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 728 | if ($this->image_library === 'imagemagick' OR $this->image_library === 'netpbm') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 729 | { |
| 730 | $protocol = 'image_process_'.$this->image_library; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 731 | return $this->$protocol('rotate'); |
| 732 | } |
| 733 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 734 | return ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt') |
| 735 | ? $this->image_mirror_gd() |
| 736 | : $this->image_rotate_gd(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // -------------------------------------------------------------------- |
| 740 | |
| 741 | /** |
| 742 | * Image Process Using GD/GD2 |
| 743 | * |
| 744 | * This function will resize or crop |
| 745 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 746 | * @param string |
| 747 | * @return bool |
| 748 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 749 | public function image_process_gd($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 750 | { |
| 751 | $v2_override = FALSE; |
| 752 | |
| 753 | // If the target width/height match the source, AND if the new file name is not equal to the old file name |
| 754 | // 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] | 755 | 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] | 756 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 757 | 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] | 758 | { |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 759 | chmod($this->full_dst_path, $this->file_permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 760 | } |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 761 | |
| 762 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | // Let's set up our values based on the action |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 766 | if ($action === 'crop') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 767 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 768 | // Reassign the source width/height if cropping |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 769 | $this->orig_width = $this->width; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 770 | $this->orig_height = $this->height; |
| 771 | |
| 772 | // GD 2.0 has a cropping bug so we'll test for it |
| 773 | if ($this->gd_version() !== FALSE) |
| 774 | { |
| 775 | $gd_version = str_replace('0', '', $this->gd_version()); |
Andrey Andreev | d4818b7 | 2014-10-23 17:15:32 +0300 | [diff] [blame] | 776 | $v2_override = ($gd_version == 2); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | else |
| 780 | { |
| 781 | // If resizing the x/y axis must be zero |
| 782 | $this->x_axis = 0; |
| 783 | $this->y_axis = 0; |
| 784 | } |
| 785 | |
Andrey Andreev | 10fb7d1 | 2015-08-03 10:05:29 +0300 | [diff] [blame] | 786 | // Create the image handle |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 787 | if ( ! ($src_img = $this->image_create_gd())) |
| 788 | { |
| 789 | return FALSE; |
| 790 | } |
| 791 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 792 | /* Create the image |
| 793 | * |
| 794 | * Old conditional which users report cause problems with shared GD libs who report themselves as "2.0 or greater" |
| 795 | * it appears that this is no longer the issue that it was in 2004, so we've removed it, retaining it in the comment |
| 796 | * below should that ever prove inaccurate. |
| 797 | * |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 798 | * if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor') && $v2_override === FALSE) |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 799 | */ |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 800 | if ($this->image_library === 'gd2' && function_exists('imagecreatetruecolor')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 801 | { |
| 802 | $create = 'imagecreatetruecolor'; |
| 803 | $copy = 'imagecopyresampled'; |
| 804 | } |
| 805 | else |
| 806 | { |
| 807 | $create = 'imagecreate'; |
| 808 | $copy = 'imagecopyresized'; |
| 809 | } |
| 810 | |
| 811 | $dst_img = $create($this->width, $this->height); |
Derek Jones | 595bfd1 | 2010-08-20 10:28:22 -0500 | [diff] [blame] | 812 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 813 | if ($this->image_type === 3) // png we can actually preserve transparency |
Derek Jones | 595bfd1 | 2010-08-20 10:28:22 -0500 | [diff] [blame] | 814 | { |
| 815 | imagealphablending($dst_img, FALSE); |
| 816 | imagesavealpha($dst_img, TRUE); |
| 817 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 818 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 819 | $copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height); |
| 820 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 821 | // Show the image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 822 | if ($this->dynamic_output === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 823 | { |
| 824 | $this->image_display_gd($dst_img); |
| 825 | } |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 826 | elseif ( ! $this->image_save_gd($dst_img)) // Or save it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 827 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 828 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 831 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 832 | imagedestroy($dst_img); |
| 833 | imagedestroy($src_img); |
| 834 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 835 | chmod($this->full_dst_path, $this->file_permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 836 | |
| 837 | return TRUE; |
| 838 | } |
| 839 | |
| 840 | // -------------------------------------------------------------------- |
| 841 | |
| 842 | /** |
| 843 | * Image Process Using ImageMagick |
| 844 | * |
| 845 | * This function will resize, crop or rotate |
| 846 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 847 | * @param string |
| 848 | * @return bool |
| 849 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 850 | public function image_process_imagemagick($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 851 | { |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 852 | // Do we have a vaild library path? |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 853 | if ($this->library_path === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 854 | { |
| 855 | $this->set_error('imglib_libpath_invalid'); |
| 856 | return FALSE; |
| 857 | } |
| 858 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 859 | if ( ! preg_match('/convert$/i', $this->library_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 860 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 861 | $this->library_path = rtrim($this->library_path, '/').'/convert'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | // Execute the command |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 865 | $cmd = $this->library_path.' -quality '.$this->quality; |
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 | if ($action === 'crop') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 868 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 869 | $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] | 870 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 871 | elseif ($action === 'rotate') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 872 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 873 | $angle = ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt') |
| 874 | ? '-flop' : '-rotate '.$this->rotation_angle; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 875 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 876 | $cmd .= ' '.$angle.' "'.$this->full_src_path.'" "'.$this->full_dst_path.'" 2>&1'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 877 | } |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 878 | else // Resize |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 879 | { |
Omar | 24063af | 2012-07-02 13:50:17 -0300 | [diff] [blame] | 880 | if($this->maintain_ratio === TRUE) |
| 881 | { |
| 882 | $cmd .= ' -resize '.$this->width.'x'.$this->height.' "'.$this->full_src_path.'" "'.$this->full_dst_path.'" 2>&1'; |
| 883 | } |
Omar | bb531d6 | 2012-06-29 10:48:28 -0300 | [diff] [blame] | 884 | else |
Omar | 24063af | 2012-07-02 13:50:17 -0300 | [diff] [blame] | 885 | { |
| 886 | $cmd .= ' -resize '.$this->width.'x'.$this->height.'\! "'.$this->full_src_path.'" "'.$this->full_dst_path.'" 2>&1'; |
| 887 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | $retval = 1; |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 891 | // exec() might be disabled |
| 892 | if (function_usable('exec')) |
| 893 | { |
| 894 | @exec($cmd, $output, $retval); |
| 895 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 896 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 897 | // Did it work? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 898 | if ($retval > 0) |
| 899 | { |
| 900 | $this->set_error('imglib_image_process_failed'); |
| 901 | return FALSE; |
| 902 | } |
| 903 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 904 | chmod($this->full_dst_path, $this->file_permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 905 | |
| 906 | return TRUE; |
| 907 | } |
| 908 | |
| 909 | // -------------------------------------------------------------------- |
| 910 | |
| 911 | /** |
| 912 | * Image Process Using NetPBM |
| 913 | * |
| 914 | * This function will resize, crop or rotate |
| 915 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 916 | * @param string |
| 917 | * @return bool |
| 918 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 919 | public function image_process_netpbm($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 920 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 921 | if ($this->library_path === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 922 | { |
| 923 | $this->set_error('imglib_libpath_invalid'); |
| 924 | return FALSE; |
| 925 | } |
| 926 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 927 | // Build the resizing command |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 928 | switch ($this->image_type) |
| 929 | { |
| 930 | case 1 : |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 931 | $cmd_in = 'giftopnm'; |
| 932 | $cmd_out = 'ppmtogif'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 933 | break; |
| 934 | case 2 : |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 935 | $cmd_in = 'jpegtopnm'; |
| 936 | $cmd_out = 'ppmtojpeg'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 937 | break; |
| 938 | case 3 : |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 939 | $cmd_in = 'pngtopnm'; |
| 940 | $cmd_out = 'ppmtopng'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 941 | break; |
| 942 | } |
| 943 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 944 | if ($action === 'crop') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 945 | { |
| 946 | $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height; |
| 947 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 948 | elseif ($action === 'rotate') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 949 | { |
| 950 | switch ($this->rotation_angle) |
| 951 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 952 | case 90: $angle = 'r270'; |
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 180: $angle = 'r180'; |
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 270: $angle = 'r90'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 957 | break; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 958 | case 'vrt': $angle = 'tb'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 959 | break; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 960 | case 'hor': $angle = 'lr'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 961 | break; |
| 962 | } |
| 963 | |
| 964 | $cmd_inner = 'pnmflip -'.$angle.' '; |
| 965 | } |
| 966 | else // Resize |
| 967 | { |
| 968 | $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height; |
| 969 | } |
| 970 | |
| 971 | $cmd = $this->library_path.$cmd_in.' '.$this->full_src_path.' | '.$cmd_inner.' | '.$cmd_out.' > '.$this->dest_folder.'netpbm.tmp'; |
| 972 | |
| 973 | $retval = 1; |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 974 | // exec() might be disabled |
| 975 | if (function_usable('exec')) |
| 976 | { |
| 977 | @exec($cmd, $output, $retval); |
| 978 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 979 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 980 | // Did it work? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 981 | if ($retval > 0) |
| 982 | { |
| 983 | $this->set_error('imglib_image_process_failed'); |
| 984 | return FALSE; |
| 985 | } |
| 986 | |
| 987 | // With NetPBM we have to create a temporary image. |
| 988 | // If you try manipulating the original it fails so |
| 989 | // we have to rename the temp file. |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 990 | copy($this->dest_folder.'netpbm.tmp', $this->full_dst_path); |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 991 | unlink($this->dest_folder.'netpbm.tmp'); |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 992 | chmod($this->full_dst_path, $this->file_permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 993 | |
| 994 | return TRUE; |
| 995 | } |
| 996 | |
| 997 | // -------------------------------------------------------------------- |
| 998 | |
| 999 | /** |
| 1000 | * Image Rotate Using GD |
| 1001 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1002 | * @return bool |
| 1003 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1004 | public function image_rotate_gd() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1005 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1006 | // Create the image handle |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1007 | if ( ! ($src_img = $this->image_create_gd())) |
| 1008 | { |
| 1009 | return FALSE; |
| 1010 | } |
| 1011 | |
| 1012 | // Set the background color |
| 1013 | // This won't work with transparent PNG files so we are |
| 1014 | // going to have to figure out how to determine the color |
| 1015 | // of the alpha channel in a future release. |
| 1016 | |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1017 | $white = imagecolorallocate($src_img, 255, 255, 255); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1018 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1019 | // Rotate it! |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1020 | $dst_img = imagerotate($src_img, $this->rotation_angle, $white); |
| 1021 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1022 | // Show the image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1023 | if ($this->dynamic_output === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1024 | { |
| 1025 | $this->image_display_gd($dst_img); |
| 1026 | } |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1027 | elseif ( ! $this->image_save_gd($dst_img)) // ... or save it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1028 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1029 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1032 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1033 | imagedestroy($dst_img); |
| 1034 | imagedestroy($src_img); |
| 1035 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 1036 | chmod($this->full_dst_path, $this->file_permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1037 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 1038 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | // -------------------------------------------------------------------- |
| 1042 | |
| 1043 | /** |
| 1044 | * Create Mirror Image using GD |
| 1045 | * |
| 1046 | * This function will flip horizontal or vertical |
| 1047 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1048 | * @return bool |
| 1049 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1050 | public function image_mirror_gd() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1051 | { |
| 1052 | if ( ! $src_img = $this->image_create_gd()) |
| 1053 | { |
| 1054 | return FALSE; |
| 1055 | } |
| 1056 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1057 | $width = $this->orig_width; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1058 | $height = $this->orig_height; |
| 1059 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1060 | if ($this->rotation_angle === 'hor') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1061 | { |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1062 | for ($i = 0; $i < $height; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1063 | { |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1064 | $left = 0; |
| 1065 | $right = $width - 1; |
| 1066 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1067 | while ($left < $right) |
| 1068 | { |
| 1069 | $cl = imagecolorat($src_img, $left, $i); |
| 1070 | $cr = imagecolorat($src_img, $right, $i); |
| 1071 | |
| 1072 | imagesetpixel($src_img, $left, $i, $cr); |
| 1073 | imagesetpixel($src_img, $right, $i, $cl); |
| 1074 | |
| 1075 | $left++; |
| 1076 | $right--; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | else |
| 1081 | { |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1082 | for ($i = 0; $i < $width; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1083 | { |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1084 | $top = 0; |
| 1085 | $bottom = $height - 1; |
| 1086 | |
Andrey Andreev | 5d78fd8 | 2015-05-11 18:19:01 +0300 | [diff] [blame] | 1087 | while ($top < $bottom) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1088 | { |
| 1089 | $ct = imagecolorat($src_img, $i, $top); |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1090 | $cb = imagecolorat($src_img, $i, $bottom); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1091 | |
| 1092 | imagesetpixel($src_img, $i, $top, $cb); |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1093 | imagesetpixel($src_img, $i, $bottom, $ct); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1094 | |
| 1095 | $top++; |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1096 | $bottom--; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1101 | // Show the image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1102 | if ($this->dynamic_output === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1103 | { |
| 1104 | $this->image_display_gd($src_img); |
| 1105 | } |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1106 | elseif ( ! $this->image_save_gd($src_img)) // ... or save it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1107 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1108 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1111 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1112 | imagedestroy($src_img); |
| 1113 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 1114 | chmod($this->full_dst_path, $this->file_permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1115 | |
| 1116 | return TRUE; |
| 1117 | } |
| 1118 | |
| 1119 | // -------------------------------------------------------------------- |
| 1120 | |
| 1121 | /** |
| 1122 | * Image Watermark |
| 1123 | * |
| 1124 | * This is a wrapper function that chooses the type |
| 1125 | * of watermarking based on the specified preference. |
| 1126 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1127 | * @return bool |
| 1128 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1129 | public function watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1130 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1131 | return ($this->wm_type === 'overlay') ? $this->overlay_watermark() : $this->text_watermark(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | // -------------------------------------------------------------------- |
| 1135 | |
| 1136 | /** |
| 1137 | * Watermark - Graphic Version |
| 1138 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1139 | * @return bool |
| 1140 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1141 | public function overlay_watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1142 | { |
| 1143 | if ( ! function_exists('imagecolortransparent')) |
| 1144 | { |
| 1145 | $this->set_error('imglib_gd_required'); |
| 1146 | return FALSE; |
| 1147 | } |
| 1148 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1149 | // Fetch source image properties |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1150 | $this->get_image_properties(); |
| 1151 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1152 | // Fetch watermark image properties |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1153 | $props = $this->get_image_properties($this->wm_overlay_path, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1154 | $wm_img_type = $props['image_type']; |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1155 | $wm_width = $props['width']; |
| 1156 | $wm_height = $props['height']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1157 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1158 | // Create two image resources |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1159 | $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] | 1160 | $src_img = $this->image_create_gd($this->full_src_path); |
| 1161 | |
| 1162 | // Reverse the offset if necessary |
| 1163 | // When the image is positioned at the bottom |
| 1164 | // we don't want the vertical offset to push it |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1165 | // further down. We want the reverse, so we'll |
| 1166 | // invert the offset. Same with the horizontal |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1167 | // offset when the image is at the right |
| 1168 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1169 | $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]); |
| 1170 | $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1171 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1172 | if ($this->wm_vrt_alignment === 'B') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1173 | $this->wm_vrt_offset = $this->wm_vrt_offset * -1; |
| 1174 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1175 | if ($this->wm_hor_alignment === 'R') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1176 | $this->wm_hor_offset = $this->wm_hor_offset * -1; |
| 1177 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1178 | // Set the base x and y axis values |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1179 | $x_axis = $this->wm_hor_offset + $this->wm_padding; |
| 1180 | $y_axis = $this->wm_vrt_offset + $this->wm_padding; |
| 1181 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1182 | // Set the vertical position |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1183 | if ($this->wm_vrt_alignment === 'M') |
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 | $y_axis += ($this->orig_height / 2) - ($wm_height / 2); |
| 1186 | } |
| 1187 | elseif ($this->wm_vrt_alignment === 'B') |
| 1188 | { |
| 1189 | $y_axis += $this->orig_height - $wm_height; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1192 | // Set the horizontal position |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1193 | if ($this->wm_hor_alignment === 'C') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1194 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1195 | $x_axis += ($this->orig_width / 2) - ($wm_width / 2); |
| 1196 | } |
| 1197 | elseif ($this->wm_hor_alignment === 'R') |
| 1198 | { |
| 1199 | $x_axis += $this->orig_width - $wm_width; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Andrey Andreev | 10fb7d1 | 2015-08-03 10:05:29 +0300 | [diff] [blame] | 1202 | // Build the finalized image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1203 | if ($wm_img_type === 3 && function_exists('imagealphablending')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1204 | { |
| 1205 | @imagealphablending($src_img, TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1206 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1207 | |
| 1208 | // Set RGB values for text and shadow |
| 1209 | $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp); |
| 1210 | $alpha = ($rgba & 0x7F000000) >> 24; |
| 1211 | |
| 1212 | // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency |
| 1213 | if ($alpha > 0) |
| 1214 | { |
| 1215 | // copy the image directly, the image's alpha transparency being the sole determinant of blending |
| 1216 | imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height); |
| 1217 | } |
| 1218 | else |
| 1219 | { |
| 1220 | // set our RGB value from above to be transparent and merge the images with the specified opacity |
| 1221 | imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp)); |
| 1222 | imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity); |
| 1223 | } |
| 1224 | |
Andrey Andreev | db037db | 2015-01-12 13:45:12 +0200 | [diff] [blame] | 1225 | // We can preserve transparency for PNG images |
Дмитрий | b23b8fc | 2014-10-20 00:36:55 +0400 | [diff] [blame] | 1226 | if ($this->image_type === 3) |
| 1227 | { |
| 1228 | imagealphablending($src_img, FALSE); |
| 1229 | imagesavealpha($src_img, TRUE); |
| 1230 | } |
| 1231 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1232 | // Output the image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1233 | if ($this->dynamic_output === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1234 | { |
| 1235 | $this->image_display_gd($src_img); |
| 1236 | } |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1237 | elseif ( ! $this->image_save_gd($src_img)) // ... or save it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1238 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1239 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | imagedestroy($src_img); |
| 1243 | imagedestroy($wm_img); |
| 1244 | |
| 1245 | return TRUE; |
| 1246 | } |
| 1247 | |
| 1248 | // -------------------------------------------------------------------- |
| 1249 | |
| 1250 | /** |
| 1251 | * Watermark - Text Version |
| 1252 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1253 | * @return bool |
| 1254 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1255 | public function text_watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1256 | { |
| 1257 | if ( ! ($src_img = $this->image_create_gd())) |
| 1258 | { |
| 1259 | return FALSE; |
| 1260 | } |
| 1261 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1262 | if ($this->wm_use_truetype === TRUE && ! file_exists($this->wm_font_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1263 | { |
| 1264 | $this->set_error('imglib_missing_font'); |
| 1265 | return FALSE; |
| 1266 | } |
| 1267 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1268 | // Fetch source image properties |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1269 | $this->get_image_properties(); |
| 1270 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1271 | // Reverse the vertical offset |
| 1272 | // When the image is positioned at the bottom |
| 1273 | // we don't want the vertical offset to push it |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1274 | // further down. We want the reverse, so we'll |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1275 | // invert the offset. Note: The horizontal |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1276 | // offset flips itself automatically |
| 1277 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1278 | if ($this->wm_vrt_alignment === 'B') |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1279 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1280 | $this->wm_vrt_offset = $this->wm_vrt_offset * -1; |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1281 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1282 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1283 | if ($this->wm_hor_alignment === 'R') |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1284 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1285 | $this->wm_hor_offset = $this->wm_hor_offset * -1; |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1286 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1287 | |
| 1288 | // Set font width and height |
| 1289 | // These are calculated differently depending on |
| 1290 | // whether we are using the true type font or not |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1291 | if ($this->wm_use_truetype === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1292 | { |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1293 | if (empty($this->wm_font_size)) |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1294 | { |
| 1295 | $this->wm_font_size = 17; |
| 1296 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1297 | |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1298 | if (function_exists('imagettfbbox')) |
| 1299 | { |
| 1300 | $temp = imagettfbbox($this->wm_font_size, 0, $this->wm_font_path, $this->wm_text); |
| 1301 | $temp = $temp[2] - $temp[0]; |
| 1302 | |
| 1303 | $fontwidth = $temp / strlen($this->wm_text); |
| 1304 | } |
| 1305 | else |
| 1306 | { |
| 1307 | $fontwidth = $this->wm_font_size - ($this->wm_font_size / 4); |
| 1308 | } |
| 1309 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1310 | $fontheight = $this->wm_font_size; |
| 1311 | $this->wm_vrt_offset += $this->wm_font_size; |
| 1312 | } |
| 1313 | else |
| 1314 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1315 | $fontwidth = imagefontwidth($this->wm_font_size); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1316 | $fontheight = imagefontheight($this->wm_font_size); |
| 1317 | } |
| 1318 | |
| 1319 | // Set base X and Y axis values |
| 1320 | $x_axis = $this->wm_hor_offset + $this->wm_padding; |
| 1321 | $y_axis = $this->wm_vrt_offset + $this->wm_padding; |
| 1322 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1323 | if ($this->wm_use_drop_shadow === FALSE) |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1324 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1325 | $this->wm_shadow_distance = 0; |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1326 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1327 | |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 1328 | $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]); |
| 1329 | $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1330 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1331 | // Set vertical alignment |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1332 | if ($this->wm_vrt_alignment === 'M') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1333 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1334 | $y_axis += ($this->orig_height / 2) + ($fontheight / 2); |
| 1335 | } |
| 1336 | elseif ($this->wm_vrt_alignment === 'B') |
| 1337 | { |
| 1338 | $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1339 | } |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1340 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1341 | // Set horizontal alignment |
| 1342 | if ($this->wm_hor_alignment === 'R') |
| 1343 | { |
omar | 9c85532 | 2015-01-31 11:58:03 -0700 | [diff] [blame] | 1344 | $x_axis += $this->orig_width - ($fontwidth * strlen($this->wm_text)) - $this->wm_shadow_distance; |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1345 | } |
| 1346 | elseif ($this->wm_hor_alignment === 'C') |
| 1347 | { |
| 1348 | $x_axis += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2); |
| 1349 | } |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1350 | |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 1351 | if ($this->wm_use_drop_shadow) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1352 | { |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1353 | // Offset from text |
| 1354 | $x_shad = $x_axis + $this->wm_shadow_distance; |
| 1355 | $y_shad = $y_axis + $this->wm_shadow_distance; |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1356 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1357 | /* Set RGB values for shadow |
Andrey Andreev | 8323ae6 | 2011-12-31 18:39:10 +0200 | [diff] [blame] | 1358 | * |
| 1359 | * First character is #, so we don't really need it. |
| 1360 | * Get the rest of the string and split it into 2-length |
| 1361 | * hex values: |
| 1362 | */ |
Andrey Andreev | 8323ae6 | 2011-12-31 18:39:10 +0200 | [diff] [blame] | 1363 | $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2); |
Ronald Beilsma | 8f80bc4 | 2012-01-12 16:41:49 +0100 | [diff] [blame] | 1364 | $drp_color = imagecolorclosest($src_img, hexdec($drp_color[0]), hexdec($drp_color[1]), hexdec($drp_color[2])); |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1365 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1366 | // Add the shadow to the source image |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 1367 | if ($this->wm_use_truetype) |
| 1368 | { |
| 1369 | imagettftext($src_img, $this->wm_font_size, 0, $x_shad, $y_shad, $drp_color, $this->wm_font_path, $this->wm_text); |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 1370 | } |
| 1371 | else |
| 1372 | { |
| 1373 | imagestring($src_img, $this->wm_font_size, $x_shad, $y_shad, $this->wm_text, $drp_color); |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 1374 | } |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1375 | } |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1376 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1377 | /* Set RGB values for text |
| 1378 | * |
| 1379 | * First character is #, so we don't really need it. |
| 1380 | * Get the rest of the string and split it into 2-length |
| 1381 | * hex values: |
| 1382 | */ |
| 1383 | $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2); |
| 1384 | $txt_color = imagecolorclosest($src_img, hexdec($txt_color[0]), hexdec($txt_color[1]), hexdec($txt_color[2])); |
Andrey Andreev | 99ae226 | 2012-10-05 15:14:30 +0300 | [diff] [blame] | 1385 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1386 | // Add the text to the source image |
| 1387 | if ($this->wm_use_truetype) |
| 1388 | { |
| 1389 | imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text); |
| 1390 | } |
| 1391 | else |
| 1392 | { |
| 1393 | imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); |
| 1394 | } |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1395 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1396 | // We can preserve transparency for PNG images |
| 1397 | if ($this->image_type === 3) |
| 1398 | { |
| 1399 | imagealphablending($src_img, FALSE); |
| 1400 | imagesavealpha($src_img, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1403 | // Output the final image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1404 | if ($this->dynamic_output === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1405 | { |
| 1406 | $this->image_display_gd($src_img); |
| 1407 | } |
| 1408 | else |
| 1409 | { |
| 1410 | $this->image_save_gd($src_img); |
| 1411 | } |
| 1412 | |
| 1413 | imagedestroy($src_img); |
| 1414 | |
| 1415 | return TRUE; |
| 1416 | } |
| 1417 | |
| 1418 | // -------------------------------------------------------------------- |
| 1419 | |
| 1420 | /** |
| 1421 | * Create Image - GD |
| 1422 | * |
| 1423 | * This simply creates an image resource handle |
| 1424 | * based on the type of image being processed |
| 1425 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1426 | * @param string |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 1427 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1428 | * @return resource |
| 1429 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1430 | public function image_create_gd($path = '', $image_type = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1431 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1432 | if ($path === '') |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1433 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1434 | $path = $this->full_src_path; |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1435 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1436 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1437 | if ($image_type === '') |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1438 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1439 | $image_type = $this->image_type; |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1440 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1441 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1442 | switch ($image_type) |
| 1443 | { |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1444 | case 1: |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1445 | if ( ! function_exists('imagecreatefromgif')) |
| 1446 | { |
| 1447 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported')); |
| 1448 | return FALSE; |
| 1449 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1450 | |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1451 | return imagecreatefromgif($path); |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1452 | case 2: |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1453 | if ( ! function_exists('imagecreatefromjpeg')) |
| 1454 | { |
| 1455 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported')); |
| 1456 | return FALSE; |
| 1457 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1458 | |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1459 | return imagecreatefromjpeg($path); |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1460 | case 3: |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1461 | if ( ! function_exists('imagecreatefrompng')) |
| 1462 | { |
| 1463 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported')); |
| 1464 | return FALSE; |
| 1465 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1466 | |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1467 | return imagecreatefrompng($path); |
| 1468 | default: |
| 1469 | $this->set_error(array('imglib_unsupported_imagecreate')); |
| 1470 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1471 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | // -------------------------------------------------------------------- |
| 1475 | |
| 1476 | /** |
| 1477 | * Write image file to disk - GD |
| 1478 | * |
| 1479 | * Takes an image resource as input and writes the file |
| 1480 | * to the specified destination |
| 1481 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1482 | * @param resource |
| 1483 | * @return bool |
| 1484 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1485 | public function image_save_gd($resource) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1486 | { |
| 1487 | switch ($this->image_type) |
| 1488 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1489 | case 1: |
| 1490 | if ( ! function_exists('imagegif')) |
| 1491 | { |
| 1492 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported')); |
| 1493 | return FALSE; |
| 1494 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1495 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1496 | if ( ! @imagegif($resource, $this->full_dst_path)) |
| 1497 | { |
| 1498 | $this->set_error('imglib_save_failed'); |
| 1499 | return FALSE; |
| 1500 | } |
| 1501 | break; |
| 1502 | case 2: |
| 1503 | if ( ! function_exists('imagejpeg')) |
| 1504 | { |
| 1505 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported')); |
| 1506 | return FALSE; |
| 1507 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1508 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1509 | if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality)) |
| 1510 | { |
| 1511 | $this->set_error('imglib_save_failed'); |
| 1512 | return FALSE; |
| 1513 | } |
| 1514 | break; |
| 1515 | case 3: |
| 1516 | if ( ! function_exists('imagepng')) |
| 1517 | { |
| 1518 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported')); |
| 1519 | return FALSE; |
| 1520 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1521 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1522 | if ( ! @imagepng($resource, $this->full_dst_path)) |
| 1523 | { |
| 1524 | $this->set_error('imglib_save_failed'); |
| 1525 | return FALSE; |
| 1526 | } |
| 1527 | break; |
| 1528 | default: |
| 1529 | $this->set_error(array('imglib_unsupported_imagecreate')); |
| 1530 | return FALSE; |
| 1531 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | return TRUE; |
| 1535 | } |
| 1536 | |
| 1537 | // -------------------------------------------------------------------- |
| 1538 | |
| 1539 | /** |
| 1540 | * Dynamically outputs an image |
| 1541 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1542 | * @param resource |
| 1543 | * @return void |
| 1544 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1545 | public function image_display_gd($resource) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1546 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1547 | header('Content-Disposition: filename='.$this->source_image.';'); |
| 1548 | header('Content-Type: '.$this->mime_type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1549 | header('Content-Transfer-Encoding: binary'); |
| 1550 | header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT'); |
| 1551 | |
| 1552 | switch ($this->image_type) |
| 1553 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1554 | case 1 : imagegif($resource); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1555 | break; |
Ted Wood | 9cc707b | 2013-01-08 19:41:45 -0800 | [diff] [blame] | 1556 | case 2 : imagejpeg($resource, NULL, $this->quality); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1557 | break; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1558 | case 3 : imagepng($resource); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1559 | break; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1560 | default: echo 'Unable to display the image'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1561 | break; |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | // -------------------------------------------------------------------- |
| 1566 | |
| 1567 | /** |
| 1568 | * Re-proportion Image Width/Height |
| 1569 | * |
| 1570 | * When creating thumbs, the desired width/height |
| 1571 | * can end up warping the image due to an incorrect |
| 1572 | * ratio between the full-sized image and the thumb. |
| 1573 | * |
| 1574 | * This function lets us re-proportion the width/height |
| 1575 | * if users choose to maintain the aspect ratio when resizing. |
| 1576 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1577 | * @return void |
| 1578 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1579 | public function image_reproportion() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1580 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1581 | 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] | 1582 | OR ( ! ctype_digit((string) $this->width) && ! ctype_digit((string) $this->height)) |
| 1583 | 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] | 1584 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1585 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 1588 | // Sanitize |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1589 | $this->width = (int) $this->width; |
| 1590 | $this->height = (int) $this->height; |
| 1591 | |
| 1592 | if ($this->master_dim !== 'width' && $this->master_dim !== 'height') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1593 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1594 | if ($this->width > 0 && $this->height > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1595 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1596 | $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0) |
| 1597 | ? 'width' : 'height'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1598 | } |
| 1599 | else |
| 1600 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1601 | $this->master_dim = ($this->height === 0) ? 'width' : 'height'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1602 | } |
| 1603 | } |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1604 | elseif (($this->master_dim === 'width' && $this->width === 0) |
| 1605 | OR ($this->master_dim === 'height' && $this->height === 0)) |
| 1606 | { |
| 1607 | return; |
| 1608 | } |
| 1609 | |
| 1610 | if ($this->master_dim === 'width') |
| 1611 | { |
| 1612 | $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width); |
| 1613 | } |
| 1614 | else |
| 1615 | { |
| 1616 | $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height); |
| 1617 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | // -------------------------------------------------------------------- |
| 1621 | |
| 1622 | /** |
| 1623 | * Get image properties |
| 1624 | * |
| 1625 | * A helper function that gets info about the file |
| 1626 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1627 | * @param string |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 1628 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1629 | * @return mixed |
| 1630 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1631 | public function get_image_properties($path = '', $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1632 | { |
| 1633 | // For now we require GD but we should |
| 1634 | // find a way to determine this using IM or NetPBM |
| 1635 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1636 | if ($path === '') |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1637 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1638 | $path = $this->full_src_path; |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1639 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1640 | |
| 1641 | if ( ! file_exists($path)) |
| 1642 | { |
| 1643 | $this->set_error('imglib_invalid_path'); |
| 1644 | return FALSE; |
| 1645 | } |
| 1646 | |
Phil Sturgeon | 901998a | 2011-08-26 10:03:33 +0100 | [diff] [blame] | 1647 | $vals = getimagesize($path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1648 | $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1649 | $mime = (isset($types[$vals[2]])) ? 'image/'.$types[$vals[2]] : 'image/jpg'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1650 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1651 | if ($return === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1652 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1653 | return array( |
| 1654 | 'width' => $vals[0], |
| 1655 | 'height' => $vals[1], |
| 1656 | 'image_type' => $vals[2], |
| 1657 | 'size_str' => $vals[3], |
| 1658 | 'mime_type' => $mime |
| 1659 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1662 | $this->orig_width = $vals[0]; |
| 1663 | $this->orig_height = $vals[1]; |
| 1664 | $this->image_type = $vals[2]; |
| 1665 | $this->size_str = $vals[3]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1666 | $this->mime_type = $mime; |
| 1667 | |
| 1668 | return TRUE; |
| 1669 | } |
| 1670 | |
| 1671 | // -------------------------------------------------------------------- |
| 1672 | |
| 1673 | /** |
| 1674 | * Size calculator |
| 1675 | * |
| 1676 | * This function takes a known width x height and |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1677 | * recalculates it to a new size. Only one |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1678 | * new variable needs to be known |
| 1679 | * |
| 1680 | * $props = array( |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1681 | * 'width' => $width, |
| 1682 | * 'height' => $height, |
| 1683 | * 'new_width' => 40, |
| 1684 | * 'new_height' => '' |
| 1685 | * ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1686 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1687 | * @param array |
| 1688 | * @return array |
| 1689 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1690 | public function size_calculator($vals) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1691 | { |
| 1692 | if ( ! is_array($vals)) |
| 1693 | { |
| 1694 | return; |
| 1695 | } |
| 1696 | |
| 1697 | $allowed = array('new_width', 'new_height', 'width', 'height'); |
| 1698 | |
| 1699 | foreach ($allowed as $item) |
| 1700 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1701 | if (empty($vals[$item])) |
| 1702 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1703 | $vals[$item] = 0; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1704 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1707 | if ($vals['width'] === 0 OR $vals['height'] === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1708 | { |
| 1709 | return $vals; |
| 1710 | } |
| 1711 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1712 | if ($vals['new_width'] === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1713 | { |
| 1714 | $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']); |
| 1715 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1716 | elseif ($vals['new_height'] === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1717 | { |
| 1718 | $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']); |
| 1719 | } |
| 1720 | |
| 1721 | return $vals; |
| 1722 | } |
| 1723 | |
| 1724 | // -------------------------------------------------------------------- |
| 1725 | |
| 1726 | /** |
| 1727 | * Explode source_image |
| 1728 | * |
| 1729 | * This is a helper function that extracts the extension |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1730 | * from the source_image. This function lets us deal with |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1731 | * source_images with multiple periods, like: my.cool.jpg |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1732 | * It returns an associative array with two elements: |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1733 | * $array['ext'] = '.jpg'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1734 | * $array['name'] = 'my.cool'; |
| 1735 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1736 | * @param array |
| 1737 | * @return array |
| 1738 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1739 | public function explode_name($source_image) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1740 | { |
Derek Jones | 08cae63 | 2009-02-10 20:03:29 +0000 | [diff] [blame] | 1741 | $ext = strrchr($source_image, '.'); |
| 1742 | $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1743 | |
Derek Jones | 08cae63 | 2009-02-10 20:03:29 +0000 | [diff] [blame] | 1744 | return array('ext' => $ext, 'name' => $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | // -------------------------------------------------------------------- |
| 1748 | |
| 1749 | /** |
| 1750 | * Is GD Installed? |
| 1751 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1752 | * @return bool |
| 1753 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1754 | public function gd_loaded() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1755 | { |
| 1756 | if ( ! extension_loaded('gd')) |
| 1757 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1758 | /* As it is stated in the PHP manual, dl() is not always available |
| 1759 | * and even if so - it could generate an E_WARNING message on failure |
| 1760 | */ |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1761 | return (function_exists('dl') && @dl('gd.so')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | return TRUE; |
| 1765 | } |
| 1766 | |
| 1767 | // -------------------------------------------------------------------- |
| 1768 | |
| 1769 | /** |
| 1770 | * Get GD version |
| 1771 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1772 | * @return mixed |
| 1773 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1774 | public function gd_version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1775 | { |
| 1776 | if (function_exists('gd_info')) |
| 1777 | { |
| 1778 | $gd_version = @gd_info(); |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1779 | return preg_replace('/\D/', '', $gd_version['GD Version']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | return FALSE; |
| 1783 | } |
| 1784 | |
| 1785 | // -------------------------------------------------------------------- |
| 1786 | |
| 1787 | /** |
| 1788 | * Set error message |
| 1789 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1790 | * @param string |
| 1791 | * @return void |
| 1792 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1793 | public function set_error($msg) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1794 | { |
| 1795 | $CI =& get_instance(); |
| 1796 | $CI->lang->load('imglib'); |
| 1797 | |
| 1798 | if (is_array($msg)) |
| 1799 | { |
| 1800 | foreach ($msg as $val) |
| 1801 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1802 | $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1803 | $this->error_msg[] = $msg; |
| 1804 | log_message('error', $msg); |
| 1805 | } |
| 1806 | } |
| 1807 | else |
| 1808 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1809 | $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1810 | $this->error_msg[] = $msg; |
| 1811 | log_message('error', $msg); |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | // -------------------------------------------------------------------- |
| 1816 | |
| 1817 | /** |
| 1818 | * Show error messages |
| 1819 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1820 | * @param string |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 1821 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1822 | * @return string |
| 1823 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1824 | public function display_errors($open = '<p>', $close = '</p>') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1825 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1826 | 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] | 1827 | } |
| 1828 | |
| 1829 | } |