Skip to content

Commit

Permalink
Add alpha property to RGBA
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysalvat committed Oct 5, 2012
1 parent 881e07e commit 4e473f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
46 changes: 28 additions & 18 deletions PointlessImageToCssConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,16 @@ public function getImageHeight() {
public function resize($width) {
$ratio = $width / $this->getImageWidth();
$height = $this->getImageHeight() * $ratio;

$new_image = imagecreatetruecolor($width, $height);
imagealphablending($new_image, false);
imagesavealpha($new_image,true);

$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);

imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getImageWidth(), $this->getImageHeight());

$this->image = $new_image;
}

Expand Down Expand Up @@ -146,25 +153,28 @@ public function computeStyle() {
$style .= " height:0;\n";
$style .= " box-shadow:\n";
foreach($pixels as $row => $cols) {
$style .= "\n";
foreach($cols as $col => $colors) {
$style .= ' ';
$style .= sprintf("%4s", $col * $step)."px ";
$style .= sprintf("%2s", $row * $step)."px ";
$style .= $this->getBlur() ? $this->getBlur()."px " : "0 ";
$style .= $step."px ";

if ($this->color_type === PointlessImageToCssConverter::RGBA) {
$style .= "rgba(".$colors["red"].",".$colors["green"].",".$colors["blue"].",1)";
} else {
$style .= strtoupper(
'#'.
$this->rgb2hexa($colors['red']).
$this->rgb2hexa($colors['green']).
$this->rgb2hexa($colors['blue'])
);
$alpha = round(($colors["alpha"] / -127) + 1, 1);

if ($alpha) {
$style .= ' ';
$style .= sprintf("%4s", $col * $step)."px ";
$style .= sprintf("%2s", $row * $step)."px ";
$style .= $this->getBlur() ? $this->getBlur()."px " : "0 ";
$style .= $step."px ";

if ($this->color_type === PointlessImageToCssConverter::RGBA) {
$style .= "rgba(".$colors["red"].",".$colors["green"].",".$colors["blue"].",".$alpha.")";
} else {
$style .= strtoupper(
'#'.
$this->rgb2hexa($colors['red']).
$this->rgb2hexa($colors['green']).
$this->rgb2hexa($colors['blue'])
);
}
$style .=",\n";
}
$style .=",\n";
}
}
return preg_replace('/,$/', ';', $style);
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<!doctype html>
<html>
<head>
Expand All @@ -10,6 +9,7 @@
$img = new PointlessImageToCssConverter("monalisa.jpg");
$img->setWidth(200);
$img->setPixelSize(4);
$img->setColorType(PointlessImageToCssConverter::HEXA);
$img->setBlur(4);

echo $img->computeStyle();
Expand Down

0 comments on commit 4e473f4

Please sign in to comment.