| 1: | <?php | 
| 2: |  | 
| 3: |  | 
| 4: |  | 
| 5: |  | 
| 6: |  | 
| 7: |  | 
| 8: |  | 
| 9: |  | 
| 10: |  | 
| 11: |  | 
| 12: |  | 
| 13: |  | 
| 14: |  | 
| 15: |  | 
| 16: |  | 
| 17: |  | 
| 18: |  | 
| 19: |  | 
| 20: |  | 
| 21: |  | 
| 22: |  | 
| 23: |  | 
| 24: |  | 
| 25: | namespace WideImage; | 
| 26: |  | 
| 27: | use WideImage\MapperFactory; | 
| 28: | use WideImage\Exception\Exception; | 
| 29: | use WideImage\Exception\UnsupportedFormatException; | 
| 30: | use WideImage\Exception\InvalidImageSourceException; | 
| 31: | use WideImage\Exception\InvalidImageHandleException; | 
| 32: |  | 
| 33: |  | 
| 34: |  | 
| 35: |  | 
| 36: |  | 
| 37: |  | 
| 38: | class WideImage | 
| 39: | { | 
| 40: | const SIDE_TOP_LEFT = 1; | 
| 41: | const SIDE_TOP = 2; | 
| 42: | const SIDE_TOP_RIGHT = 4; | 
| 43: | const SIDE_RIGHT = 8; | 
| 44: | const SIDE_BOTTOM_RIGHT = 16; | 
| 45: | const SIDE_BOTTOM = 32; | 
| 46: | const SIDE_BOTTOM_LEFT = 64; | 
| 47: | const SIDE_LEFT = 128; | 
| 48: | const SIDE_ALL = 255; | 
| 49: |  | 
| 50: |  | 
| 51: |  | 
| 52: |  | 
| 53: | protected static $path = null; | 
| 54: |  | 
| 55: |  | 
| 56: |  | 
| 57: |  | 
| 58: |  | 
| 59: | public static function version() | 
| 60: | { | 
| 61: | return '##DEV##'; | 
| 62: | } | 
| 63: |  | 
| 64: |  | 
| 65: |  | 
| 66: |  | 
| 67: |  | 
| 68: | public static function path() | 
| 69: | { | 
| 70: | if (static::$path === null) { | 
| 71: | static::$path = __DIR__ . DIRECTORY_SEPARATOR; | 
| 72: | } | 
| 73: |  | 
| 74: | return static::$path; | 
| 75: | } | 
| 76: |  | 
| 77: |  | 
| 78: |  | 
| 79: |  | 
| 80: | public static function checkGD() | 
| 81: | { | 
| 82: | if (!extension_loaded('gd')) { | 
| 83: | throw new Exception("WideImage requires the GD extension, but it's apparently not loaded."); | 
| 84: | } | 
| 85: | } | 
| 86: |  | 
| 87: |  | 
| 88: |  | 
| 89: |  | 
| 90: |  | 
| 91: |  | 
| 92: |  | 
| 93: |  | 
| 94: |  | 
| 95: |  | 
| 96: |  | 
| 97: |  | 
| 98: |  | 
| 99: | public static function registerCustomMapper($mapper_class_name, $mime_type, $extension) | 
| 100: | { | 
| 101: | MapperFactory::registerMapper($mapper_class_name, $mime_type, strtoupper($extension)); | 
| 102: | } | 
| 103: |  | 
| 104: |  | 
| 105: |  | 
| 106: |  | 
| 107: |  | 
| 108: |  | 
| 109: |  | 
| 110: |  | 
| 111: |  | 
| 112: |  | 
| 113: |  | 
| 114: |  | 
| 115: |  | 
| 116: |  | 
| 117: |  | 
| 118: |  | 
| 119: |  | 
| 120: |  | 
| 121: |  | 
| 122: |  | 
| 123: |  | 
| 124: |  | 
| 125: |  | 
| 126: |  | 
| 127: |  | 
| 128: |  | 
| 129: | public static function load($source) | 
| 130: | { | 
| 131: | $predictedSourceType = ''; | 
| 132: |  | 
| 133: | if ($source == '') { | 
| 134: | $predictedSourceType = 'String'; | 
| 135: | } | 
| 136: |  | 
| 137: |  | 
| 138: | if (!$predictedSourceType && static::isValidImageHandle($source)) { | 
| 139: | $predictedSourceType = 'Handle'; | 
| 140: | } | 
| 141: |  | 
| 142: |  | 
| 143: | if (!$predictedSourceType) { | 
| 144: |  | 
| 145: | $binLength    = 64; | 
| 146: | $sourceLength = strlen($source); | 
| 147: | $maxlen       = ($sourceLength > $binLength) ? $binLength : $sourceLength; | 
| 148: |  | 
| 149: | for ($i = 0; $i < $maxlen; $i++) { | 
| 150: | if (ord($source[$i]) < 32) { | 
| 151: | $predictedSourceType = 'String'; | 
| 152: | break; | 
| 153: | } | 
| 154: | } | 
| 155: | } | 
| 156: |  | 
| 157: |  | 
| 158: | if (isset($_FILES[$source]) && isset($_FILES[$source]['tmp_name'])) { | 
| 159: | $predictedSourceType = 'Upload'; | 
| 160: | } | 
| 161: |  | 
| 162: |  | 
| 163: | if (!$predictedSourceType) { | 
| 164: | $predictedSourceType = 'File'; | 
| 165: | } | 
| 166: |  | 
| 167: | return call_user_func(array(__CLASS__, 'loadFrom' . $predictedSourceType), $source); | 
| 168: | } | 
| 169: |  | 
| 170: |  | 
| 171: |  | 
| 172: |  | 
| 173: |  | 
| 174: |  | 
| 175: |  | 
| 176: | public static function loadFromFile($uri) | 
| 177: | { | 
| 178: | $data   = file_get_contents($uri); | 
| 179: | $handle = @imagecreatefromstring($data); | 
| 180: |  | 
| 181: | if (!static::isValidImageHandle($handle)) { | 
| 182: | try { | 
| 183: |  | 
| 184: | $mapper = MapperFactory::selectMapper($uri); | 
| 185: |  | 
| 186: | if ($mapper) { | 
| 187: | $handle = $mapper->load($uri); | 
| 188: | } | 
| 189: | } catch (UnsupportedFormatException $e) { | 
| 190: |  | 
| 191: | } | 
| 192: |  | 
| 193: |  | 
| 194: | if (!static::isValidImageHandle($handle)) { | 
| 195: | $custom_mappers = MapperFactory::getCustomMappers(); | 
| 196: |  | 
| 197: | foreach ($custom_mappers as $mime_type => $mapper_class) { | 
| 198: | $mapper = MapperFactory::selectMapper(null, $mime_type); | 
| 199: | $handle = $mapper->loadFromString($data); | 
| 200: |  | 
| 201: | if (static::isValidImageHandle($handle)) { | 
| 202: | break; | 
| 203: | } | 
| 204: | } | 
| 205: | } | 
| 206: | } | 
| 207: |  | 
| 208: | if (!static::isValidImageHandle($handle)) { | 
| 209: | throw new InvalidImageSourceException("File '{$uri}' appears to be an invalid image source."); | 
| 210: | } | 
| 211: |  | 
| 212: | return static::loadFromHandle($handle); | 
| 213: | } | 
| 214: |  | 
| 215: |  | 
| 216: |  | 
| 217: |  | 
| 218: |  | 
| 219: |  | 
| 220: |  | 
| 221: | public static function loadFromString($string) | 
| 222: | { | 
| 223: | if (strlen($string) < 128) { | 
| 224: | throw new InvalidImageSourceException("String doesn't contain image data."); | 
| 225: | } | 
| 226: |  | 
| 227: | $handle = @imagecreatefromstring($string); | 
| 228: |  | 
| 229: | if (!static::isValidImageHandle($handle)) { | 
| 230: | $custom_mappers = MapperFactory::getCustomMappers(); | 
| 231: |  | 
| 232: | foreach ($custom_mappers as $mime_type => $mapper_class) { | 
| 233: | $mapper = MapperFactory::selectMapper(null, $mime_type); | 
| 234: | $handle = $mapper->loadFromString($string); | 
| 235: |  | 
| 236: | if (static::isValidImageHandle($handle)) { | 
| 237: | break; | 
| 238: | } | 
| 239: | } | 
| 240: | } | 
| 241: |  | 
| 242: | if (!static::isValidImageHandle($handle)) { | 
| 243: | throw new InvalidImageSourceException("String doesn't contain valid image data."); | 
| 244: | } | 
| 245: |  | 
| 246: | return static::loadFromHandle($handle); | 
| 247: | } | 
| 248: |  | 
| 249: |  | 
| 250: |  | 
| 251: |  | 
| 252: |  | 
| 253: |  | 
| 254: |  | 
| 255: |  | 
| 256: |  | 
| 257: |  | 
| 258: |  | 
| 259: |  | 
| 260: |  | 
| 261: |  | 
| 262: |  | 
| 263: |  | 
| 264: |  | 
| 265: |  | 
| 266: |  | 
| 267: | public static function loadFromHandle($handle) | 
| 268: | { | 
| 269: | if (!static::isValidImageHandle($handle)) { | 
| 270: | throw new InvalidImageSourceException("Handle is not a valid GD image resource."); | 
| 271: | } | 
| 272: |  | 
| 273: | if (imageistruecolor($handle)) { | 
| 274: | return new TrueColorImage($handle); | 
| 275: | } | 
| 276: |  | 
| 277: | return new PaletteImage($handle); | 
| 278: | } | 
| 279: |  | 
| 280: |  | 
| 281: |  | 
| 282: |  | 
| 283: |  | 
| 284: |  | 
| 285: |  | 
| 286: |  | 
| 287: |  | 
| 288: |  | 
| 289: |  | 
| 290: |  | 
| 291: | public static function loadFromUpload($field_name, $index = null) | 
| 292: | { | 
| 293: | if (!array_key_exists($field_name, $_FILES)) { | 
| 294: | throw new InvalidImageSourceException("Upload field '{$field_name}' doesn't exist."); | 
| 295: | } | 
| 296: |  | 
| 297: | if (is_array($_FILES[$field_name]['tmp_name'])) { | 
| 298: | if (isset($_FILES[$field_name]['tmp_name'][$index])) { | 
| 299: | $filename = $_FILES[$field_name]['tmp_name'][$index]; | 
| 300: | } else { | 
| 301: | $result = array(); | 
| 302: |  | 
| 303: | foreach ($_FILES[$field_name]['tmp_name'] as $idx => $tmp_name) { | 
| 304: | $result[$idx] = static::loadFromFile($tmp_name); | 
| 305: | } | 
| 306: |  | 
| 307: | return $result; | 
| 308: | } | 
| 309: | } else { | 
| 310: | $filename = $_FILES[$field_name]['tmp_name']; | 
| 311: | } | 
| 312: |  | 
| 313: | if (!file_exists($filename)) { | 
| 314: | throw new InvalidImageSourceException("Uploaded file doesn't exist."); | 
| 315: | } | 
| 316: |  | 
| 317: | return static::loadFromFile($filename); | 
| 318: | } | 
| 319: |  | 
| 320: |  | 
| 321: |  | 
| 322: |  | 
| 323: |  | 
| 324: |  | 
| 325: |  | 
| 326: |  | 
| 327: | public static function createPaletteImage($width, $height) | 
| 328: | { | 
| 329: | return PaletteImage::create($width, $height); | 
| 330: | } | 
| 331: |  | 
| 332: |  | 
| 333: |  | 
| 334: |  | 
| 335: |  | 
| 336: |  | 
| 337: |  | 
| 338: |  | 
| 339: | public static function createTrueColorImage($width, $height) | 
| 340: | { | 
| 341: | return TrueColorImage::create($width, $height); | 
| 342: | } | 
| 343: |  | 
| 344: |  | 
| 345: |  | 
| 346: |  | 
| 347: |  | 
| 348: |  | 
| 349: |  | 
| 350: | public static function isValidImageHandle($handle) | 
| 351: | { | 
| 352: | return ($handle instanceof \GdImage || (is_resource($handle) && get_resource_type($handle) == 'gd')); | 
| 353: | } | 
| 354: |  | 
| 355: |  | 
| 356: |  | 
| 357: |  | 
| 358: |  | 
| 359: |  | 
| 360: | public static function assertValidImageHandle($handle) | 
| 361: | { | 
| 362: | if (!static::isValidImageHandle($handle)) { | 
| 363: | throw new InvalidImageHandleException("{$handle} is not a valid image handle."); | 
| 364: | } | 
| 365: | } | 
| 366: | } | 
| 367: |  | 
| 368: | WideImage::checkGD(); | 
| 369: |  | 
| 370: | WideImage::registerCustomMapper('\\WideImage\\Mapper\\BMP', 'image/bmp', 'bmp'); | 
| 371: | WideImage::registerCustomMapper('\\WideImage\\Mapper\\TGA', 'image/tga', 'tga'); | 
| 372: |  |