Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
add image background option
Browse files Browse the repository at this point in the history
  • Loading branch information
mrahmadt committed Sep 16, 2011
1 parent a2d5e2a commit c61d12a
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions word_cloud.php
Expand Up @@ -15,12 +15,14 @@ class WordCloud {
private $mask;
private $table;
private $image;
private $imagecolor;

public function __construct($width, $height, $font, $text=null, $vertical_freq = FrequencyTable::WORDS_MAINLY_HORIZONTAL) {
public function __construct($width, $height, $font, $text=null, $imagecolor=array( 0,0, 0, 127), $vertical_freq = FrequencyTable::WORDS_MAINLY_HORIZONTAL) {
$this->width = $width;
$this->height = $height;
$this->font = $font;

$this->imagecolor = $imagecolor;

$this->mask = new Mask();
if(is_array($text)){
$this->table = new FrequencyTable($font,'',$vertical_freq);//, $text);
Expand All @@ -40,7 +42,14 @@ public function __construct($width, $height, $font, $text=null, $vertical_freq =
// $this->table->add_word('word4', 4);
// $this->table->add_word('word5');
// for($i = 6; $i <= 20; $i++) $this->table->add_word('word'.$i, $i % 5);

$this->image = imagecreatetruecolor($width, $height);
//Set the flag to save full alpha channel information (as opposed to single-color transparency) when saving PNG images
imagesavealpha($this->image, true);
//behaves identically to imagecolorallocate() with the addition of the transparency parameter alpha
$trans_colour = imagecolorallocatealpha($this->image, $imagecolor[0],$imagecolor[1], $imagecolor[2], $imagecolor[3]);
imagefill($this->image, 0, 0, $trans_colour);

}

public function get_image() {
Expand Down Expand Up @@ -84,6 +93,14 @@ public function render($palette) {
list($x1, $y1, $x2, $y2) = $this->mask->get_bounding_box();
$image2 = imagecreatetruecolor(abs($x2 - $x1), abs($y2 - $y1));
imagecopy($image2 ,$this->image, 0, 0, $x1, $y1, abs($x2 - $x1), abs($y2 - $y1));

//Set the flag to save full alpha channel information (as opposed to single-color transparency) when saving PNG images
imagesavealpha($image2, true);
//behaves identically to imagecolorallocate() with the addition of the transparency parameter alpha
$trans_colour = imagecolorallocatealpha($image2, $this->imagecolor[0],$this->imagecolor[1], $this->imagecolor[2], $this->imagecolor[3]);
imagefill($image2, 0, 0, $trans_colour);


imagedestroy($this->image);
$this->image = $image2;

Expand Down

0 comments on commit c61d12a

Please sign in to comment.