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 | * |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2019, 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/) |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | * @license https://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 | 0768a40 | 2018-03-10 02:08:32 +0200 | [diff] [blame] | 838 | if ($this->dynamic_output !== TRUE) |
| 839 | { |
| 840 | chmod($this->full_dst_path, $this->file_permissions); |
| 841 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 842 | |
| 843 | return TRUE; |
| 844 | } |
| 845 | |
| 846 | // -------------------------------------------------------------------- |
| 847 | |
| 848 | /** |
| 849 | * Image Process Using ImageMagick |
| 850 | * |
| 851 | * This function will resize, crop or rotate |
| 852 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 853 | * @param string |
| 854 | * @return bool |
| 855 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 856 | public function image_process_imagemagick($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 857 | { |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 858 | // Do we have a vaild library path? |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 859 | if ($this->library_path === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 860 | { |
| 861 | $this->set_error('imglib_libpath_invalid'); |
| 862 | return FALSE; |
| 863 | } |
| 864 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 865 | if ( ! preg_match('/convert$/i', $this->library_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 866 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 867 | $this->library_path = rtrim($this->library_path, '/').'/convert'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | // Execute the command |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 871 | $cmd = $this->library_path.' -quality '.$this->quality; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 872 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 873 | if ($action === 'crop') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 874 | { |
Andrey Andreev | b3f6934 | 2016-03-22 11:31:58 +0200 | [diff] [blame] | 875 | $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] | 876 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 877 | elseif ($action === 'rotate') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 878 | { |
Andrey Andreev | b3f6934 | 2016-03-22 11:31:58 +0200 | [diff] [blame] | 879 | $cmd .= ($this->rotation_angle === 'hor' OR $this->rotation_angle === 'vrt') |
| 880 | ? ' -flop' |
| 881 | : ' -rotate '.$this->rotation_angle; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 882 | } |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 883 | else // Resize |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 884 | { |
Omar | 24063af | 2012-07-02 13:50:17 -0300 | [diff] [blame] | 885 | if($this->maintain_ratio === TRUE) |
| 886 | { |
Andrey Andreev | b3f6934 | 2016-03-22 11:31:58 +0200 | [diff] [blame] | 887 | $cmd .= ' -resize '.$this->width.'x'.$this->height; |
Omar | 24063af | 2012-07-02 13:50:17 -0300 | [diff] [blame] | 888 | } |
Omar | bb531d6 | 2012-06-29 10:48:28 -0300 | [diff] [blame] | 889 | else |
Omar | 24063af | 2012-07-02 13:50:17 -0300 | [diff] [blame] | 890 | { |
Andrey Andreev | b3f6934 | 2016-03-22 11:31:58 +0200 | [diff] [blame] | 891 | $cmd .= ' -resize '.$this->width.'x'.$this->height.'\!'; |
Omar | 24063af | 2012-07-02 13:50:17 -0300 | [diff] [blame] | 892 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Andrey Andreev | be8bd92 | 2016-11-07 12:31:31 +0200 | [diff] [blame] | 895 | $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] | 896 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 897 | $retval = 1; |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 898 | // exec() might be disabled |
| 899 | if (function_usable('exec')) |
| 900 | { |
| 901 | @exec($cmd, $output, $retval); |
| 902 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 903 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 904 | // Did it work? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 905 | if ($retval > 0) |
| 906 | { |
| 907 | $this->set_error('imglib_image_process_failed'); |
| 908 | return FALSE; |
| 909 | } |
| 910 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 911 | chmod($this->full_dst_path, $this->file_permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 912 | |
| 913 | return TRUE; |
| 914 | } |
| 915 | |
| 916 | // -------------------------------------------------------------------- |
| 917 | |
| 918 | /** |
| 919 | * Image Process Using NetPBM |
| 920 | * |
| 921 | * This function will resize, crop or rotate |
| 922 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 923 | * @param string |
| 924 | * @return bool |
| 925 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 926 | public function image_process_netpbm($action = 'resize') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 927 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 928 | if ($this->library_path === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 929 | { |
| 930 | $this->set_error('imglib_libpath_invalid'); |
| 931 | return FALSE; |
| 932 | } |
| 933 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 934 | // Build the resizing command |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 935 | switch ($this->image_type) |
| 936 | { |
| 937 | case 1 : |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 938 | $cmd_in = 'giftopnm'; |
| 939 | $cmd_out = 'ppmtogif'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 940 | break; |
| 941 | case 2 : |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 942 | $cmd_in = 'jpegtopnm'; |
| 943 | $cmd_out = 'ppmtojpeg'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 944 | break; |
| 945 | case 3 : |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 946 | $cmd_in = 'pngtopnm'; |
| 947 | $cmd_out = 'ppmtopng'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 948 | break; |
| 949 | } |
| 950 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 951 | if ($action === 'crop') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 952 | { |
| 953 | $cmd_inner = 'pnmcut -left '.$this->x_axis.' -top '.$this->y_axis.' -width '.$this->width.' -height '.$this->height; |
| 954 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 955 | elseif ($action === 'rotate') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 956 | { |
| 957 | switch ($this->rotation_angle) |
| 958 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 959 | case 90: $angle = 'r270'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 960 | break; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 961 | case 180: $angle = 'r180'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 962 | break; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 963 | case 270: $angle = 'r90'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 964 | break; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 965 | case 'vrt': $angle = 'tb'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 966 | break; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 967 | case 'hor': $angle = 'lr'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 968 | break; |
| 969 | } |
| 970 | |
| 971 | $cmd_inner = 'pnmflip -'.$angle.' '; |
| 972 | } |
| 973 | else // Resize |
| 974 | { |
| 975 | $cmd_inner = 'pnmscale -xysize '.$this->width.' '.$this->height; |
| 976 | } |
| 977 | |
Andrey Andreev | 6cab789 | 2017-07-17 13:14:00 +0300 | [diff] [blame] | 978 | $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] | 979 | |
| 980 | $retval = 1; |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 981 | // exec() might be disabled |
| 982 | if (function_usable('exec')) |
| 983 | { |
| 984 | @exec($cmd, $output, $retval); |
| 985 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 986 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 987 | // Did it work? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 988 | if ($retval > 0) |
| 989 | { |
| 990 | $this->set_error('imglib_image_process_failed'); |
| 991 | return FALSE; |
| 992 | } |
| 993 | |
| 994 | // With NetPBM we have to create a temporary image. |
| 995 | // If you try manipulating the original it fails so |
| 996 | // we have to rename the temp file. |
Andrey Andreev | e9d2dc8 | 2012-11-07 14:23:29 +0200 | [diff] [blame] | 997 | copy($this->dest_folder.'netpbm.tmp', $this->full_dst_path); |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 998 | unlink($this->dest_folder.'netpbm.tmp'); |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 999 | chmod($this->full_dst_path, $this->file_permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1000 | |
| 1001 | return TRUE; |
| 1002 | } |
| 1003 | |
| 1004 | // -------------------------------------------------------------------- |
| 1005 | |
| 1006 | /** |
| 1007 | * Image Rotate Using GD |
| 1008 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1009 | * @return bool |
| 1010 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1011 | public function image_rotate_gd() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1012 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1013 | // Create the image handle |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1014 | if ( ! ($src_img = $this->image_create_gd())) |
| 1015 | { |
| 1016 | return FALSE; |
| 1017 | } |
| 1018 | |
| 1019 | // Set the background color |
| 1020 | // This won't work with transparent PNG files so we are |
| 1021 | // going to have to figure out how to determine the color |
| 1022 | // of the alpha channel in a future release. |
| 1023 | |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1024 | $white = imagecolorallocate($src_img, 255, 255, 255); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1025 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1026 | // Rotate it! |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1027 | $dst_img = imagerotate($src_img, $this->rotation_angle, $white); |
| 1028 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1029 | // Show the image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1030 | if ($this->dynamic_output === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1031 | { |
| 1032 | $this->image_display_gd($dst_img); |
| 1033 | } |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1034 | elseif ( ! $this->image_save_gd($dst_img)) // ... or save it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1035 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1036 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1039 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1040 | imagedestroy($dst_img); |
| 1041 | imagedestroy($src_img); |
| 1042 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 1043 | chmod($this->full_dst_path, $this->file_permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1044 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 1045 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | // -------------------------------------------------------------------- |
| 1049 | |
| 1050 | /** |
| 1051 | * Create Mirror Image using GD |
| 1052 | * |
| 1053 | * This function will flip horizontal or vertical |
| 1054 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1055 | * @return bool |
| 1056 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1057 | public function image_mirror_gd() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1058 | { |
| 1059 | if ( ! $src_img = $this->image_create_gd()) |
| 1060 | { |
| 1061 | return FALSE; |
| 1062 | } |
| 1063 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1064 | $width = $this->orig_width; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1065 | $height = $this->orig_height; |
| 1066 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1067 | if ($this->rotation_angle === 'hor') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1068 | { |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1069 | for ($i = 0; $i < $height; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1070 | { |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1071 | $left = 0; |
| 1072 | $right = $width - 1; |
| 1073 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1074 | while ($left < $right) |
| 1075 | { |
| 1076 | $cl = imagecolorat($src_img, $left, $i); |
| 1077 | $cr = imagecolorat($src_img, $right, $i); |
| 1078 | |
| 1079 | imagesetpixel($src_img, $left, $i, $cr); |
| 1080 | imagesetpixel($src_img, $right, $i, $cl); |
| 1081 | |
| 1082 | $left++; |
| 1083 | $right--; |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | else |
| 1088 | { |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1089 | for ($i = 0; $i < $width; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1090 | { |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1091 | $top = 0; |
| 1092 | $bottom = $height - 1; |
| 1093 | |
Andrey Andreev | 5d78fd8 | 2015-05-11 18:19:01 +0300 | [diff] [blame] | 1094 | while ($top < $bottom) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1095 | { |
| 1096 | $ct = imagecolorat($src_img, $i, $top); |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1097 | $cb = imagecolorat($src_img, $i, $bottom); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1098 | |
| 1099 | imagesetpixel($src_img, $i, $top, $cb); |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1100 | imagesetpixel($src_img, $i, $bottom, $ct); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1101 | |
| 1102 | $top++; |
Andrey Andreev | fbde279 | 2015-05-11 11:03:06 +0300 | [diff] [blame] | 1103 | $bottom--; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1108 | // Show the image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1109 | if ($this->dynamic_output === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1110 | { |
| 1111 | $this->image_display_gd($src_img); |
| 1112 | } |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1113 | elseif ( ! $this->image_save_gd($src_img)) // ... or save it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1114 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1115 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1118 | // Kill the file handles |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1119 | imagedestroy($src_img); |
| 1120 | |
Andrey Andreev | 4596574 | 2014-08-27 20:40:11 +0300 | [diff] [blame] | 1121 | chmod($this->full_dst_path, $this->file_permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1122 | |
| 1123 | return TRUE; |
| 1124 | } |
| 1125 | |
| 1126 | // -------------------------------------------------------------------- |
| 1127 | |
| 1128 | /** |
| 1129 | * Image Watermark |
| 1130 | * |
| 1131 | * This is a wrapper function that chooses the type |
| 1132 | * of watermarking based on the specified preference. |
| 1133 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1134 | * @return bool |
| 1135 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1136 | public function watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1137 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1138 | return ($this->wm_type === 'overlay') ? $this->overlay_watermark() : $this->text_watermark(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | // -------------------------------------------------------------------- |
| 1142 | |
| 1143 | /** |
| 1144 | * Watermark - Graphic Version |
| 1145 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1146 | * @return bool |
| 1147 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1148 | public function overlay_watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1149 | { |
| 1150 | if ( ! function_exists('imagecolortransparent')) |
| 1151 | { |
| 1152 | $this->set_error('imglib_gd_required'); |
| 1153 | return FALSE; |
| 1154 | } |
| 1155 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1156 | // Fetch source image properties |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1157 | $this->get_image_properties(); |
| 1158 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1159 | // Fetch watermark image properties |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1160 | $props = $this->get_image_properties($this->wm_overlay_path, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1161 | $wm_img_type = $props['image_type']; |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1162 | $wm_width = $props['width']; |
| 1163 | $wm_height = $props['height']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1164 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1165 | // Create two image resources |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1166 | $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] | 1167 | $src_img = $this->image_create_gd($this->full_src_path); |
| 1168 | |
| 1169 | // Reverse the offset if necessary |
| 1170 | // When the image is positioned at the bottom |
| 1171 | // we don't want the vertical offset to push it |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1172 | // further down. We want the reverse, so we'll |
| 1173 | // invert the offset. Same with the horizontal |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1174 | // offset when the image is at the right |
| 1175 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1176 | $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]); |
| 1177 | $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1178 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1179 | if ($this->wm_vrt_alignment === 'B') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1180 | $this->wm_vrt_offset = $this->wm_vrt_offset * -1; |
| 1181 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1182 | if ($this->wm_hor_alignment === 'R') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1183 | $this->wm_hor_offset = $this->wm_hor_offset * -1; |
| 1184 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1185 | // Set the base x and y axis values |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1186 | $x_axis = $this->wm_hor_offset + $this->wm_padding; |
| 1187 | $y_axis = $this->wm_vrt_offset + $this->wm_padding; |
| 1188 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1189 | // Set the vertical position |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1190 | if ($this->wm_vrt_alignment === 'M') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1191 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1192 | $y_axis += ($this->orig_height / 2) - ($wm_height / 2); |
| 1193 | } |
| 1194 | elseif ($this->wm_vrt_alignment === 'B') |
| 1195 | { |
| 1196 | $y_axis += $this->orig_height - $wm_height; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1199 | // Set the horizontal position |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1200 | if ($this->wm_hor_alignment === 'C') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1201 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1202 | $x_axis += ($this->orig_width / 2) - ($wm_width / 2); |
| 1203 | } |
| 1204 | elseif ($this->wm_hor_alignment === 'R') |
| 1205 | { |
| 1206 | $x_axis += $this->orig_width - $wm_width; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Andrey Andreev | 10fb7d1 | 2015-08-03 10:05:29 +0300 | [diff] [blame] | 1209 | // Build the finalized image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1210 | if ($wm_img_type === 3 && function_exists('imagealphablending')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1211 | { |
| 1212 | @imagealphablending($src_img, TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1213 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1214 | |
| 1215 | // Set RGB values for text and shadow |
| 1216 | $rgba = imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp); |
| 1217 | $alpha = ($rgba & 0x7F000000) >> 24; |
| 1218 | |
| 1219 | // make a best guess as to whether we're dealing with an image with alpha transparency or no/binary transparency |
| 1220 | if ($alpha > 0) |
| 1221 | { |
| 1222 | // copy the image directly, the image's alpha transparency being the sole determinant of blending |
| 1223 | imagecopy($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height); |
| 1224 | } |
| 1225 | else |
| 1226 | { |
| 1227 | // set our RGB value from above to be transparent and merge the images with the specified opacity |
| 1228 | imagecolortransparent($wm_img, imagecolorat($wm_img, $this->wm_x_transp, $this->wm_y_transp)); |
| 1229 | imagecopymerge($src_img, $wm_img, $x_axis, $y_axis, 0, 0, $wm_width, $wm_height, $this->wm_opacity); |
| 1230 | } |
| 1231 | |
Andrey Andreev | db037db | 2015-01-12 13:45:12 +0200 | [diff] [blame] | 1232 | // We can preserve transparency for PNG images |
Дмитрий | b23b8fc | 2014-10-20 00:36:55 +0400 | [diff] [blame] | 1233 | if ($this->image_type === 3) |
| 1234 | { |
| 1235 | imagealphablending($src_img, FALSE); |
| 1236 | imagesavealpha($src_img, TRUE); |
| 1237 | } |
| 1238 | |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1239 | // Output the image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1240 | if ($this->dynamic_output === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1241 | { |
| 1242 | $this->image_display_gd($src_img); |
| 1243 | } |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1244 | elseif ( ! $this->image_save_gd($src_img)) // ... or save it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1245 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1246 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | imagedestroy($src_img); |
| 1250 | imagedestroy($wm_img); |
| 1251 | |
| 1252 | return TRUE; |
| 1253 | } |
| 1254 | |
| 1255 | // -------------------------------------------------------------------- |
| 1256 | |
| 1257 | /** |
| 1258 | * Watermark - Text Version |
| 1259 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1260 | * @return bool |
| 1261 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1262 | public function text_watermark() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1263 | { |
| 1264 | if ( ! ($src_img = $this->image_create_gd())) |
| 1265 | { |
| 1266 | return FALSE; |
| 1267 | } |
| 1268 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1269 | if ($this->wm_use_truetype === TRUE && ! file_exists($this->wm_font_path)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1270 | { |
| 1271 | $this->set_error('imglib_missing_font'); |
| 1272 | return FALSE; |
| 1273 | } |
| 1274 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1275 | // Fetch source image properties |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1276 | $this->get_image_properties(); |
| 1277 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1278 | // Reverse the vertical offset |
| 1279 | // When the image is positioned at the bottom |
| 1280 | // we don't want the vertical offset to push it |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1281 | // further down. We want the reverse, so we'll |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1282 | // invert the offset. Note: The horizontal |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1283 | // offset flips itself automatically |
| 1284 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1285 | if ($this->wm_vrt_alignment === 'B') |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1286 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1287 | $this->wm_vrt_offset = $this->wm_vrt_offset * -1; |
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 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1290 | if ($this->wm_hor_alignment === 'R') |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1291 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1292 | $this->wm_hor_offset = $this->wm_hor_offset * -1; |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1293 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1294 | |
| 1295 | // Set font width and height |
| 1296 | // These are calculated differently depending on |
| 1297 | // whether we are using the true type font or not |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1298 | if ($this->wm_use_truetype === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1299 | { |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1300 | if (empty($this->wm_font_size)) |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1301 | { |
| 1302 | $this->wm_font_size = 17; |
| 1303 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1304 | |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1305 | if (function_exists('imagettfbbox')) |
| 1306 | { |
| 1307 | $temp = imagettfbbox($this->wm_font_size, 0, $this->wm_font_path, $this->wm_text); |
| 1308 | $temp = $temp[2] - $temp[0]; |
| 1309 | |
| 1310 | $fontwidth = $temp / strlen($this->wm_text); |
| 1311 | } |
| 1312 | else |
| 1313 | { |
| 1314 | $fontwidth = $this->wm_font_size - ($this->wm_font_size / 4); |
| 1315 | } |
| 1316 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1317 | $fontheight = $this->wm_font_size; |
| 1318 | $this->wm_vrt_offset += $this->wm_font_size; |
| 1319 | } |
| 1320 | else |
| 1321 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1322 | $fontwidth = imagefontwidth($this->wm_font_size); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1323 | $fontheight = imagefontheight($this->wm_font_size); |
| 1324 | } |
| 1325 | |
| 1326 | // Set base X and Y axis values |
| 1327 | $x_axis = $this->wm_hor_offset + $this->wm_padding; |
| 1328 | $y_axis = $this->wm_vrt_offset + $this->wm_padding; |
| 1329 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1330 | if ($this->wm_use_drop_shadow === FALSE) |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1331 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1332 | $this->wm_shadow_distance = 0; |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1333 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1334 | |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 1335 | $this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]); |
| 1336 | $this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1337 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1338 | // Set vertical alignment |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1339 | if ($this->wm_vrt_alignment === 'M') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1340 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1341 | $y_axis += ($this->orig_height / 2) + ($fontheight / 2); |
| 1342 | } |
| 1343 | elseif ($this->wm_vrt_alignment === 'B') |
| 1344 | { |
| 1345 | $y_axis += $this->orig_height - $fontheight - $this->wm_shadow_distance - ($fontheight / 2); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1346 | } |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1347 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1348 | // Set horizontal alignment |
| 1349 | if ($this->wm_hor_alignment === 'R') |
| 1350 | { |
omar | 9c85532 | 2015-01-31 11:58:03 -0700 | [diff] [blame] | 1351 | $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] | 1352 | } |
| 1353 | elseif ($this->wm_hor_alignment === 'C') |
| 1354 | { |
| 1355 | $x_axis += floor(($this->orig_width - ($fontwidth * strlen($this->wm_text))) / 2); |
| 1356 | } |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1357 | |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 1358 | if ($this->wm_use_drop_shadow) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1359 | { |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1360 | // Offset from text |
| 1361 | $x_shad = $x_axis + $this->wm_shadow_distance; |
| 1362 | $y_shad = $y_axis + $this->wm_shadow_distance; |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1363 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1364 | /* Set RGB values for shadow |
Andrey Andreev | 8323ae6 | 2011-12-31 18:39:10 +0200 | [diff] [blame] | 1365 | * |
| 1366 | * First character is #, so we don't really need it. |
| 1367 | * Get the rest of the string and split it into 2-length |
| 1368 | * hex values: |
| 1369 | */ |
Andrey Andreev | 8323ae6 | 2011-12-31 18:39:10 +0200 | [diff] [blame] | 1370 | $drp_color = str_split(substr($this->wm_shadow_color, 1, 6), 2); |
Ronald Beilsma | 8f80bc4 | 2012-01-12 16:41:49 +0100 | [diff] [blame] | 1371 | $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] | 1372 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1373 | // Add the shadow to the source image |
Andrey Andreev | 64dbdfb | 2011-12-30 14:14:07 +0200 | [diff] [blame] | 1374 | if ($this->wm_use_truetype) |
| 1375 | { |
| 1376 | 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] | 1377 | } |
| 1378 | else |
| 1379 | { |
| 1380 | 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] | 1381 | } |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1382 | } |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1383 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1384 | /* Set RGB values for text |
| 1385 | * |
| 1386 | * First character is #, so we don't really need it. |
| 1387 | * Get the rest of the string and split it into 2-length |
| 1388 | * hex values: |
| 1389 | */ |
| 1390 | $txt_color = str_split(substr($this->wm_font_color, 1, 6), 2); |
| 1391 | $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] | 1392 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1393 | // Add the text to the source image |
| 1394 | if ($this->wm_use_truetype) |
| 1395 | { |
| 1396 | imagettftext($src_img, $this->wm_font_size, 0, $x_axis, $y_axis, $txt_color, $this->wm_font_path, $this->wm_text); |
| 1397 | } |
| 1398 | else |
| 1399 | { |
| 1400 | imagestring($src_img, $this->wm_font_size, $x_axis, $y_axis, $this->wm_text, $txt_color); |
| 1401 | } |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1402 | |
omar | 0779e99 | 2015-01-31 11:57:07 -0700 | [diff] [blame] | 1403 | // We can preserve transparency for PNG images |
| 1404 | if ($this->image_type === 3) |
| 1405 | { |
| 1406 | imagealphablending($src_img, FALSE); |
| 1407 | imagesavealpha($src_img, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1410 | // Output the final image |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1411 | if ($this->dynamic_output === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1412 | { |
| 1413 | $this->image_display_gd($src_img); |
| 1414 | } |
| 1415 | else |
| 1416 | { |
| 1417 | $this->image_save_gd($src_img); |
| 1418 | } |
| 1419 | |
| 1420 | imagedestroy($src_img); |
| 1421 | |
| 1422 | return TRUE; |
| 1423 | } |
| 1424 | |
| 1425 | // -------------------------------------------------------------------- |
| 1426 | |
| 1427 | /** |
| 1428 | * Create Image - GD |
| 1429 | * |
| 1430 | * This simply creates an image resource handle |
| 1431 | * based on the type of image being processed |
| 1432 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1433 | * @param string |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 1434 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1435 | * @return resource |
| 1436 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1437 | public function image_create_gd($path = '', $image_type = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1438 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1439 | if ($path === '') |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1440 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1441 | $path = $this->full_src_path; |
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 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1444 | if ($image_type === '') |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1445 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1446 | $image_type = $this->image_type; |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1447 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1448 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1449 | switch ($image_type) |
| 1450 | { |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1451 | case 1: |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1452 | if ( ! function_exists('imagecreatefromgif')) |
| 1453 | { |
| 1454 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported')); |
| 1455 | return FALSE; |
| 1456 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1457 | |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1458 | return imagecreatefromgif($path); |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1459 | case 2: |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1460 | if ( ! function_exists('imagecreatefromjpeg')) |
| 1461 | { |
| 1462 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported')); |
| 1463 | return FALSE; |
| 1464 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1465 | |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1466 | return imagecreatefromjpeg($path); |
Andrey Andreev | 3a19759 | 2015-05-21 01:05:06 +0300 | [diff] [blame] | 1467 | case 3: |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1468 | if ( ! function_exists('imagecreatefrompng')) |
| 1469 | { |
| 1470 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported')); |
| 1471 | return FALSE; |
| 1472 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1473 | |
Andrey Andreev | e52fc26 | 2014-02-11 13:27:01 +0200 | [diff] [blame] | 1474 | return imagecreatefrompng($path); |
| 1475 | default: |
| 1476 | $this->set_error(array('imglib_unsupported_imagecreate')); |
| 1477 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1478 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | // -------------------------------------------------------------------- |
| 1482 | |
| 1483 | /** |
| 1484 | * Write image file to disk - GD |
| 1485 | * |
| 1486 | * Takes an image resource as input and writes the file |
| 1487 | * to the specified destination |
| 1488 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1489 | * @param resource |
| 1490 | * @return bool |
| 1491 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1492 | public function image_save_gd($resource) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1493 | { |
| 1494 | switch ($this->image_type) |
| 1495 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1496 | case 1: |
| 1497 | if ( ! function_exists('imagegif')) |
| 1498 | { |
| 1499 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported')); |
| 1500 | return FALSE; |
| 1501 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1502 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1503 | if ( ! @imagegif($resource, $this->full_dst_path)) |
| 1504 | { |
| 1505 | $this->set_error('imglib_save_failed'); |
| 1506 | return FALSE; |
| 1507 | } |
| 1508 | break; |
| 1509 | case 2: |
| 1510 | if ( ! function_exists('imagejpeg')) |
| 1511 | { |
| 1512 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_jpg_not_supported')); |
| 1513 | return FALSE; |
| 1514 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1515 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1516 | if ( ! @imagejpeg($resource, $this->full_dst_path, $this->quality)) |
| 1517 | { |
| 1518 | $this->set_error('imglib_save_failed'); |
| 1519 | return FALSE; |
| 1520 | } |
| 1521 | break; |
| 1522 | case 3: |
| 1523 | if ( ! function_exists('imagepng')) |
| 1524 | { |
| 1525 | $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported')); |
| 1526 | return FALSE; |
| 1527 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1528 | |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1529 | if ( ! @imagepng($resource, $this->full_dst_path)) |
| 1530 | { |
| 1531 | $this->set_error('imglib_save_failed'); |
| 1532 | return FALSE; |
| 1533 | } |
| 1534 | break; |
| 1535 | default: |
| 1536 | $this->set_error(array('imglib_unsupported_imagecreate')); |
| 1537 | return FALSE; |
| 1538 | break; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | return TRUE; |
| 1542 | } |
| 1543 | |
| 1544 | // -------------------------------------------------------------------- |
| 1545 | |
| 1546 | /** |
| 1547 | * Dynamically outputs an image |
| 1548 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1549 | * @param resource |
| 1550 | * @return void |
| 1551 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1552 | public function image_display_gd($resource) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1553 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1554 | header('Content-Disposition: filename='.$this->source_image.';'); |
| 1555 | header('Content-Type: '.$this->mime_type); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1556 | header('Content-Transfer-Encoding: binary'); |
| 1557 | header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT'); |
| 1558 | |
| 1559 | switch ($this->image_type) |
| 1560 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1561 | case 1 : imagegif($resource); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1562 | break; |
Ted Wood | 9cc707b | 2013-01-08 19:41:45 -0800 | [diff] [blame] | 1563 | case 2 : imagejpeg($resource, NULL, $this->quality); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1564 | break; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1565 | case 3 : imagepng($resource); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1566 | break; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1567 | default: echo 'Unable to display the image'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1568 | break; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | // -------------------------------------------------------------------- |
| 1573 | |
| 1574 | /** |
| 1575 | * Re-proportion Image Width/Height |
| 1576 | * |
| 1577 | * When creating thumbs, the desired width/height |
| 1578 | * can end up warping the image due to an incorrect |
| 1579 | * ratio between the full-sized image and the thumb. |
| 1580 | * |
| 1581 | * This function lets us re-proportion the width/height |
| 1582 | * if users choose to maintain the aspect ratio when resizing. |
| 1583 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1584 | * @return void |
| 1585 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1586 | public function image_reproportion() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1587 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1588 | 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] | 1589 | OR ( ! ctype_digit((string) $this->width) && ! ctype_digit((string) $this->height)) |
| 1590 | 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] | 1591 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1592 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Andrey Andreev | 7a7ad78 | 2012-11-12 17:21:01 +0200 | [diff] [blame] | 1595 | // Sanitize |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1596 | $this->width = (int) $this->width; |
| 1597 | $this->height = (int) $this->height; |
| 1598 | |
| 1599 | if ($this->master_dim !== 'width' && $this->master_dim !== 'height') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1600 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1601 | if ($this->width > 0 && $this->height > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1602 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1603 | $this->master_dim = ((($this->orig_height/$this->orig_width) - ($this->height/$this->width)) < 0) |
| 1604 | ? 'width' : 'height'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1605 | } |
| 1606 | else |
| 1607 | { |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1608 | $this->master_dim = ($this->height === 0) ? 'width' : 'height'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1609 | } |
| 1610 | } |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1611 | elseif (($this->master_dim === 'width' && $this->width === 0) |
| 1612 | OR ($this->master_dim === 'height' && $this->height === 0)) |
| 1613 | { |
| 1614 | return; |
| 1615 | } |
| 1616 | |
| 1617 | if ($this->master_dim === 'width') |
| 1618 | { |
| 1619 | $this->height = (int) ceil($this->width*$this->orig_height/$this->orig_width); |
| 1620 | } |
| 1621 | else |
| 1622 | { |
| 1623 | $this->width = (int) ceil($this->orig_width*$this->height/$this->orig_height); |
| 1624 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | // -------------------------------------------------------------------- |
| 1628 | |
| 1629 | /** |
| 1630 | * Get image properties |
| 1631 | * |
| 1632 | * A helper function that gets info about the file |
| 1633 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1634 | * @param string |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 1635 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1636 | * @return mixed |
| 1637 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1638 | public function get_image_properties($path = '', $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1639 | { |
| 1640 | // For now we require GD but we should |
| 1641 | // find a way to determine this using IM or NetPBM |
| 1642 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1643 | if ($path === '') |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1644 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1645 | $path = $this->full_src_path; |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1646 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1647 | |
| 1648 | if ( ! file_exists($path)) |
| 1649 | { |
| 1650 | $this->set_error('imglib_invalid_path'); |
| 1651 | return FALSE; |
| 1652 | } |
| 1653 | |
Phil Sturgeon | 901998a | 2011-08-26 10:03:33 +0100 | [diff] [blame] | 1654 | $vals = getimagesize($path); |
Andrey Andreev | 747deff | 2017-01-06 13:22:16 +0200 | [diff] [blame] | 1655 | if ($vals === FALSE) |
| 1656 | { |
| 1657 | $this->set_error('imglib_invalid_image'); |
| 1658 | return FALSE; |
| 1659 | } |
Andrey Andreev | 5f7302c | 2017-01-06 13:26:08 +0200 | [diff] [blame] | 1660 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1661 | $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); |
Andrey Andreev | 5f7302c | 2017-01-06 13:26:08 +0200 | [diff] [blame] | 1662 | $mime = isset($types[$vals[2]]) ? 'image/'.$types[$vals[2]] : 'image/jpg'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1663 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1664 | if ($return === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1665 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1666 | return array( |
Andrey Andreev | 5f7302c | 2017-01-06 13:26:08 +0200 | [diff] [blame] | 1667 | 'width' => $vals[0], |
| 1668 | 'height' => $vals[1], |
| 1669 | 'image_type' => $vals[2], |
| 1670 | 'size_str' => $vals[3], |
| 1671 | 'mime_type' => $mime |
| 1672 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Andrey Andreev | 5f7302c | 2017-01-06 13:26:08 +0200 | [diff] [blame] | 1675 | $this->orig_width = $vals[0]; |
| 1676 | $this->orig_height = $vals[1]; |
| 1677 | $this->image_type = $vals[2]; |
| 1678 | $this->size_str = $vals[3]; |
| 1679 | $this->mime_type = $mime; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1680 | |
| 1681 | return TRUE; |
| 1682 | } |
| 1683 | |
| 1684 | // -------------------------------------------------------------------- |
| 1685 | |
| 1686 | /** |
| 1687 | * Size calculator |
| 1688 | * |
| 1689 | * This function takes a known width x height and |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1690 | * recalculates it to a new size. Only one |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1691 | * new variable needs to be known |
| 1692 | * |
| 1693 | * $props = array( |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1694 | * 'width' => $width, |
| 1695 | * 'height' => $height, |
| 1696 | * 'new_width' => 40, |
| 1697 | * 'new_height' => '' |
| 1698 | * ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1699 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1700 | * @param array |
| 1701 | * @return array |
| 1702 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1703 | public function size_calculator($vals) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1704 | { |
| 1705 | if ( ! is_array($vals)) |
| 1706 | { |
| 1707 | return; |
| 1708 | } |
| 1709 | |
| 1710 | $allowed = array('new_width', 'new_height', 'width', 'height'); |
| 1711 | |
| 1712 | foreach ($allowed as $item) |
| 1713 | { |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1714 | if (empty($vals[$item])) |
| 1715 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1716 | $vals[$item] = 0; |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 1717 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1720 | if ($vals['width'] === 0 OR $vals['height'] === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1721 | { |
| 1722 | return $vals; |
| 1723 | } |
| 1724 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1725 | if ($vals['new_width'] === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1726 | { |
| 1727 | $vals['new_width'] = ceil($vals['width']*$vals['new_height']/$vals['height']); |
| 1728 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1729 | elseif ($vals['new_height'] === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1730 | { |
| 1731 | $vals['new_height'] = ceil($vals['new_width']*$vals['height']/$vals['width']); |
| 1732 | } |
| 1733 | |
| 1734 | return $vals; |
| 1735 | } |
| 1736 | |
| 1737 | // -------------------------------------------------------------------- |
| 1738 | |
| 1739 | /** |
| 1740 | * Explode source_image |
| 1741 | * |
| 1742 | * This is a helper function that extracts the extension |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1743 | * from the source_image. This function lets us deal with |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1744 | * source_images with multiple periods, like: my.cool.jpg |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1745 | * It returns an associative array with two elements: |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1746 | * $array['ext'] = '.jpg'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1747 | * $array['name'] = 'my.cool'; |
| 1748 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1749 | * @param array |
| 1750 | * @return array |
| 1751 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1752 | public function explode_name($source_image) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1753 | { |
Derek Jones | 08cae63 | 2009-02-10 20:03:29 +0000 | [diff] [blame] | 1754 | $ext = strrchr($source_image, '.'); |
| 1755 | $name = ($ext === FALSE) ? $source_image : substr($source_image, 0, -strlen($ext)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 1756 | |
Derek Jones | 08cae63 | 2009-02-10 20:03:29 +0000 | [diff] [blame] | 1757 | return array('ext' => $ext, 'name' => $name); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | // -------------------------------------------------------------------- |
| 1761 | |
| 1762 | /** |
| 1763 | * Is GD Installed? |
| 1764 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1765 | * @return bool |
| 1766 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1767 | public function gd_loaded() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1768 | { |
| 1769 | if ( ! extension_loaded('gd')) |
| 1770 | { |
Andrey Andreev | a92b903 | 2011-12-24 19:05:58 +0200 | [diff] [blame] | 1771 | /* As it is stated in the PHP manual, dl() is not always available |
| 1772 | * and even if so - it could generate an E_WARNING message on failure |
| 1773 | */ |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1774 | return (function_exists('dl') && @dl('gd.so')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | return TRUE; |
| 1778 | } |
| 1779 | |
| 1780 | // -------------------------------------------------------------------- |
| 1781 | |
| 1782 | /** |
| 1783 | * Get GD version |
| 1784 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1785 | * @return mixed |
| 1786 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1787 | public function gd_version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1788 | { |
| 1789 | if (function_exists('gd_info')) |
| 1790 | { |
| 1791 | $gd_version = @gd_info(); |
Andrey Andreev | 8e70b79 | 2012-01-12 20:19:24 +0200 | [diff] [blame] | 1792 | return preg_replace('/\D/', '', $gd_version['GD Version']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | return FALSE; |
| 1796 | } |
| 1797 | |
| 1798 | // -------------------------------------------------------------------- |
| 1799 | |
| 1800 | /** |
| 1801 | * Set error message |
| 1802 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1803 | * @param string |
| 1804 | * @return void |
| 1805 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1806 | public function set_error($msg) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1807 | { |
| 1808 | $CI =& get_instance(); |
| 1809 | $CI->lang->load('imglib'); |
| 1810 | |
| 1811 | if (is_array($msg)) |
| 1812 | { |
| 1813 | foreach ($msg as $val) |
| 1814 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1815 | $msg = ($CI->lang->line($val) === FALSE) ? $val : $CI->lang->line($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1816 | $this->error_msg[] = $msg; |
| 1817 | log_message('error', $msg); |
| 1818 | } |
| 1819 | } |
| 1820 | else |
| 1821 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 1822 | $msg = ($CI->lang->line($msg) === FALSE) ? $msg : $CI->lang->line($msg); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1823 | $this->error_msg[] = $msg; |
| 1824 | log_message('error', $msg); |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | // -------------------------------------------------------------------- |
| 1829 | |
| 1830 | /** |
| 1831 | * Show error messages |
| 1832 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1833 | * @param string |
Timothy Warren | b82bc3a | 2012-04-27 09:12:58 -0400 | [diff] [blame] | 1834 | * @param string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1835 | * @return string |
| 1836 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 1837 | public function display_errors($open = '<p>', $close = '</p>') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1838 | { |
Andrey Andreev | 2c8baeb | 2012-01-19 15:45:28 +0200 | [diff] [blame] | 1839 | 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] | 1840 | } |
| 1841 | |
| 1842 | } |