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