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