| 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: | namespace WideImage;
|
| 25: |
|
| 26: | use WideImage\Exception\InvalidImageDimensionException;
|
| 27: | use WideImage\Exception\GDFunctionResultException;
|
| 28: |
|
| 29: | |
| 30: | |
| 31: |
|
| 32: | class PaletteImage extends Image
|
| 33: | {
|
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: |
|
| 41: | public static function create($width, $height)
|
| 42: | {
|
| 43: | if ($width * $height <= 0 || $width < 0) {
|
| 44: | throw new InvalidImageDimensionException("Can't create an image with dimensions [$width, $height].");
|
| 45: | }
|
| 46: |
|
| 47: | return new PaletteImage(imagecreate($width, $height));
|
| 48: | }
|
| 49: |
|
| 50: | public function doCreate($width, $height)
|
| 51: | {
|
| 52: | return static::create($width, $height);
|
| 53: | }
|
| 54: |
|
| 55: | |
| 56: | |
| 57: | |
| 58: |
|
| 59: | public function isTrueColor()
|
| 60: | {
|
| 61: | return false;
|
| 62: | }
|
| 63: |
|
| 64: | |
| 65: | |
| 66: | |
| 67: |
|
| 68: | public function asPalette($nColors = 255, $dither = null, $matchPalette = true)
|
| 69: | {
|
| 70: | return $this->copy();
|
| 71: | }
|
| 72: |
|
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: |
|
| 79: | protected function copyAsNew($trueColor = false)
|
| 80: | {
|
| 81: | $width = $this->getWidth();
|
| 82: | $height = $this->getHeight();
|
| 83: |
|
| 84: | if ($trueColor) {
|
| 85: | $new = TrueColorImage::create($width, $height);
|
| 86: | } else {
|
| 87: | $new = PaletteImage::create($width, $height);
|
| 88: | }
|
| 89: |
|
| 90: |
|
| 91: | if ($this->isTransparent()) {
|
| 92: | $rgb = $this->getTransparentColorRGB();
|
| 93: |
|
| 94: | if (is_array($rgb)) {
|
| 95: | $tci = $new->allocateColor($rgb['red'], $rgb['green'], $rgb['blue']);
|
| 96: | $new->fill(0, 0, $tci);
|
| 97: | $new->setTransparentColor($tci);
|
| 98: | }
|
| 99: | }
|
| 100: |
|
| 101: | imageCopy($new->getHandle(), $this->handle, 0, 0, 0, 0, $width, $height);
|
| 102: |
|
| 103: | return $new;
|
| 104: | }
|
| 105: |
|
| 106: | |
| 107: | |
| 108: | |
| 109: |
|
| 110: | public function asTrueColor()
|
| 111: | {
|
| 112: | $width = $this->getWidth();
|
| 113: | $height = $this->getHeight();
|
| 114: | $new = WideImage::createTrueColorImage($width, $height);
|
| 115: |
|
| 116: | if ($this->isTransparent()) {
|
| 117: | $new->copyTransparencyFrom($this);
|
| 118: | }
|
| 119: |
|
| 120: | if (!imageCopy($new->getHandle(), $this->handle, 0, 0, 0, 0, $width, $height)) {
|
| 121: | throw new GDFunctionResultException("imagecopy() returned false");
|
| 122: | }
|
| 123: |
|
| 124: | return $new;
|
| 125: | }
|
| 126: |
|
| 127: | |
| 128: | |
| 129: | |
| 130: |
|
| 131: | public function getChannels()
|
| 132: | {
|
| 133: | $args = func_get_args();
|
| 134: |
|
| 135: | if (count($args) == 1 && is_array($args[0])) {
|
| 136: | $args = $args[0];
|
| 137: | }
|
| 138: |
|
| 139: | return OperationFactory::get('CopyChannelsPalette')->execute($this, $args);
|
| 140: | }
|
| 141: |
|
| 142: | |
| 143: | |
| 144: | |
| 145: |
|
| 146: | public function copyNoAlpha()
|
| 147: | {
|
| 148: | return WideImage::loadFromString($this->asString('png'));
|
| 149: | }
|
| 150: | }
|
| 151: | |