| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfImageAlphaMaskGD extends sfImageTransformAbstract |
|---|
| 22 |
{ |
|---|
| 23 |
public function __construct($mask, $color = false) |
|---|
| 24 |
{ |
|---|
| 25 |
$this->mask = $mask; |
|---|
| 26 |
$this->color = $color; |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
protected function transform(sfImage $image) |
|---|
| 30 |
{ |
|---|
| 31 |
$this->image = $image; |
|---|
| 32 |
$this->mimeType = $image->getMIMEType(); |
|---|
| 33 |
$resource = $image->getAdapter()->getHolder(); |
|---|
| 34 |
|
|---|
| 35 |
switch ($this->mimeType) |
|---|
| 36 |
{ |
|---|
| 37 |
case 'image/png': |
|---|
| 38 |
$this->transformAlpha($resource); |
|---|
| 39 |
break; |
|---|
| 40 |
case 'image/gif': |
|---|
| 41 |
case 'image/jpg': |
|---|
| 42 |
default: |
|---|
| 43 |
$this->transformDefault($resource); |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
return $image; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
private function transformAlpha($resource) |
|---|
| 50 |
{ |
|---|
| 51 |
$w = imagesx($resource); |
|---|
| 52 |
$h = imagesy($resource); |
|---|
| 53 |
|
|---|
| 54 |
$mask = $this->mask->getAdapter()->getHolder(); |
|---|
| 55 |
$canvas = imagecreatetruecolor($w, $h); |
|---|
| 56 |
|
|---|
| 57 |
$color_background = imagecolorallocate($canvas, 0, 0, 0); |
|---|
| 58 |
imagefilledrectangle($canvas, 0, 0, $w, $h, $color_background); |
|---|
| 59 |
imagealphablending($canvas, false); |
|---|
| 60 |
imagesavealpha($canvas, true); |
|---|
| 61 |
|
|---|
| 62 |
for ($x = 0;$x < $w;$x++) |
|---|
| 63 |
{ |
|---|
| 64 |
for ($y = 0;$y < $h;$y++) |
|---|
| 65 |
{ |
|---|
| 66 |
$RealPixel = @imagecolorsforindex($resource, @imagecolorat($resource, $x, $y)); |
|---|
| 67 |
$MaskPixel = @imagecolorsforindex($mask, @imagecolorat($mask, $x, $y)); |
|---|
| 68 |
$MaskAlpha = 127 - (floor($MaskPixel['red'] / 2) * (1 - ($RealPixel['alpha'] / 127))); |
|---|
| 69 |
|
|---|
| 70 |
if (false === $this->color) |
|---|
| 71 |
{ |
|---|
| 72 |
$newcolor = imagecolorallocatealpha($canvas, $RealPixel['red'], $RealPixel['green'], $RealPixel['blue'], intval($MaskAlpha)); |
|---|
| 73 |
} |
|---|
| 74 |
else |
|---|
| 75 |
{ |
|---|
| 76 |
$newcolorPixel = sscanf($this->color, '#%2x%2x%2x'); |
|---|
| 77 |
$newcolorPixel[0] = ($newcolorPixel[0] * $MaskAlpha + $RealPixel['red'] * (127 - $MaskAlpha)) / 127; |
|---|
| 78 |
$newcolorPixel[1] = ($newcolorPixel[1] * $MaskAlpha + $RealPixel['green'] * (127 - $MaskAlpha)) / 127; |
|---|
| 79 |
$newcolorPixel[2] = ($newcolorPixel[2] * $MaskAlpha + $RealPixel['blue'] * (127 - $MaskAlpha)) / 127; |
|---|
| 80 |
$newcolor = imagecolorallocate($canvas, $newcolorPixel[0], $newcolorPixel[1], $newcolorPixel[2]); |
|---|
| 81 |
} |
|---|
| 82 |
imagesetpixel($canvas, $x, $y, $newcolor); |
|---|
| 83 |
} |
|---|
| 84 |
} |
|---|
| 85 |
imagealphablending($resource, false); |
|---|
| 86 |
imagesavealpha($resource, true); |
|---|
| 87 |
imagecopy($resource, $canvas, 0, 0, 0, 0, $w, $h); |
|---|
| 88 |
|
|---|
| 89 |
imagedestroy($mask); |
|---|
| 90 |
imagedestroy($canvas); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
* Callback function to extend/alter parameters as given in your thumbnailing.yml. |
|---|
| 95 |
* |
|---|
| 96 |
* This callback adds the resources path to a mask image |
|---|
| 97 |
* |
|---|
| 98 |
* @throws InvalidArgumentException |
|---|
| 99 |
* @param sfImage $sourceImage The original image |
|---|
| 100 |
* @param array $parameters Configured parameters for this transformation |
|---|
| 101 |
* @return array $parameters Extended/altered parameters |
|---|
| 102 |
*/ |
|---|
| 103 |
public static function prepareParameters($sourceImage, $parameters) |
|---|
| 104 |
{ |
|---|
| 105 |
if (!array_key_exists('mask', $parameters)) |
|---|
| 106 |
{ |
|---|
| 107 |
return $parameters; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
$user_resources_dir = sfConfig::get('sf_data_dir') . '/resources'; |
|---|
| 111 |
$plugin_paths = ProjectConfiguration::getActive()->getAllPluginPaths(); |
|---|
| 112 |
$plugin_resources_dir = $plugin_paths['sfImageTransformExtraPlugin'].'/data/example-resources'; |
|---|
| 113 |
if (file_exists($user_resources_dir . '/' . $parameters['mask'])) |
|---|
| 114 |
{ |
|---|
| 115 |
$parameters['mask'] = new sfImage($user_resources_dir . '/' . $parameters['mask']); |
|---|
| 116 |
} |
|---|
| 117 |
else if (file_exists($plugin_resources_dir . '/' . $parameters['mask'])) |
|---|
| 118 |
{ |
|---|
| 119 |
$parameters['mask'] = new sfImage($plugin_resources_dir . '/' . $parameters['mask']); |
|---|
| 120 |
} |
|---|
| 121 |
else |
|---|
| 122 |
{ |
|---|
| 123 |
throw new InvalidArgumentException('Mask "'.$parameters['mask'].'" could not be found!'); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
return $parameters; |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|