1*c609f1dcSAndreas Gohr<?php 2*c609f1dcSAndreas Gohr 3*c609f1dcSAndreas Gohrnamespace splitbrain\RingIcon; 4*c609f1dcSAndreas Gohr 5*c609f1dcSAndreas Gohr 6*c609f1dcSAndreas Gohrclass RingIcon extends AbstractRingIcon 7*c609f1dcSAndreas Gohr{ 8*c609f1dcSAndreas Gohr /** 9*c609f1dcSAndreas Gohr * Generates an ring image 10*c609f1dcSAndreas Gohr * 11*c609f1dcSAndreas Gohr * If a seed is given, the image will be based on that seed 12*c609f1dcSAndreas Gohr * 13*c609f1dcSAndreas Gohr * @param string $seed initialize the genrator with this string 14*c609f1dcSAndreas Gohr * @param string $file if given, the image is saved at that path, otherwise is printed to browser 15*c609f1dcSAndreas Gohr */ 16*c609f1dcSAndreas Gohr public function createImage($seed = '', $file = '') 17*c609f1dcSAndreas Gohr { 18*c609f1dcSAndreas Gohr if (!$seed) { 19*c609f1dcSAndreas Gohr $seed = mt_rand() . time(); 20*c609f1dcSAndreas Gohr } 21*c609f1dcSAndreas Gohr $this->seed = $seed; 22*c609f1dcSAndreas Gohr 23*c609f1dcSAndreas Gohr // monochrome wanted? 24*c609f1dcSAndreas Gohr if($this->ismono) { 25*c609f1dcSAndreas Gohr $this->generateMonoColor(); 26*c609f1dcSAndreas Gohr } 27*c609f1dcSAndreas Gohr 28*c609f1dcSAndreas Gohr // create 29*c609f1dcSAndreas Gohr $image = $this->createTransparentImage($this->fullsize, $this->fullsize); 30*c609f1dcSAndreas Gohr $arcwidth = $this->fullsize; 31*c609f1dcSAndreas Gohr for ($i = $this->rings; $i > 0; $i--) { 32*c609f1dcSAndreas Gohr $this->drawRing($image, $arcwidth); 33*c609f1dcSAndreas Gohr $arcwidth -= $this->ringwidth; 34*c609f1dcSAndreas Gohr } 35*c609f1dcSAndreas Gohr 36*c609f1dcSAndreas Gohr // resample for antialiasing 37*c609f1dcSAndreas Gohr $out = $this->createTransparentImage($this->size, $this->size); 38*c609f1dcSAndreas Gohr imagecopyresampled($out, $image, 0, 0, 0, 0, $this->size, $this->size, $this->fullsize, $this->fullsize); 39*c609f1dcSAndreas Gohr if ($file) { 40*c609f1dcSAndreas Gohr imagepng($out, $file); 41*c609f1dcSAndreas Gohr } else { 42*c609f1dcSAndreas Gohr header("Content-type: image/png"); 43*c609f1dcSAndreas Gohr imagepng($out); 44*c609f1dcSAndreas Gohr } 45*c609f1dcSAndreas Gohr imagedestroy($out); 46*c609f1dcSAndreas Gohr imagedestroy($image); 47*c609f1dcSAndreas Gohr } 48*c609f1dcSAndreas Gohr 49*c609f1dcSAndreas Gohr 50*c609f1dcSAndreas Gohr /** 51*c609f1dcSAndreas Gohr * Drawas a single ring 52*c609f1dcSAndreas Gohr * 53*c609f1dcSAndreas Gohr * @param resource $image 54*c609f1dcSAndreas Gohr * @param int $arcwidth outer width of the ring 55*c609f1dcSAndreas Gohr */ 56*c609f1dcSAndreas Gohr protected function drawRing($image, $arcwidth) 57*c609f1dcSAndreas Gohr { 58*c609f1dcSAndreas Gohr $color = $this->randomColor($image); 59*c609f1dcSAndreas Gohr $transparency = $this->transparentColor($image); 60*c609f1dcSAndreas Gohr 61*c609f1dcSAndreas Gohr $start = $this->rand(20, 360); 62*c609f1dcSAndreas Gohr $stop = $this->rand(20, 360); 63*c609f1dcSAndreas Gohr if($stop < $start) list($start, $stop) = array($stop, $start); 64*c609f1dcSAndreas Gohr 65*c609f1dcSAndreas Gohr imagefilledarc($image, $this->center, $this->center, $arcwidth, $arcwidth, $stop, $start, $color, IMG_ARC_PIE); 66*c609f1dcSAndreas Gohr imagefilledellipse($image, $this->center, $this->center, $arcwidth - $this->ringwidth, 67*c609f1dcSAndreas Gohr $arcwidth - $this->ringwidth, $transparency); 68*c609f1dcSAndreas Gohr 69*c609f1dcSAndreas Gohr imagecolordeallocate($image, $color); 70*c609f1dcSAndreas Gohr imagecolordeallocate($image, $transparency); 71*c609f1dcSAndreas Gohr } 72*c609f1dcSAndreas Gohr 73*c609f1dcSAndreas Gohr /** 74*c609f1dcSAndreas Gohr * Allocate a transparent color 75*c609f1dcSAndreas Gohr * 76*c609f1dcSAndreas Gohr * @param resource $image 77*c609f1dcSAndreas Gohr * @return int 78*c609f1dcSAndreas Gohr */ 79*c609f1dcSAndreas Gohr protected function transparentColor($image) 80*c609f1dcSAndreas Gohr { 81*c609f1dcSAndreas Gohr return imagecolorallocatealpha($image, 0, 0, 0, 127); 82*c609f1dcSAndreas Gohr } 83*c609f1dcSAndreas Gohr 84*c609f1dcSAndreas Gohr /** 85*c609f1dcSAndreas Gohr * Allocate a random color 86*c609f1dcSAndreas Gohr * 87*c609f1dcSAndreas Gohr * @param $image 88*c609f1dcSAndreas Gohr * @return int 89*c609f1dcSAndreas Gohr */ 90*c609f1dcSAndreas Gohr protected function randomColor($image) 91*c609f1dcSAndreas Gohr { 92*c609f1dcSAndreas Gohr if($this->ismono) { 93*c609f1dcSAndreas Gohr return imagecolorallocatealpha($image, $this->monocolor[0], $this->monocolor[1], $this->monocolor[2], $this->rand(0, 96)); 94*c609f1dcSAndreas Gohr } 95*c609f1dcSAndreas Gohr return imagecolorallocate($image, $this->rand(0, 255), $this->rand(0, 255), $this->rand(0, 255)); 96*c609f1dcSAndreas Gohr } 97*c609f1dcSAndreas Gohr 98*c609f1dcSAndreas Gohr /** 99*c609f1dcSAndreas Gohr * Create a transparent image 100*c609f1dcSAndreas Gohr * 101*c609f1dcSAndreas Gohr * @param int $width 102*c609f1dcSAndreas Gohr * @param int $height 103*c609f1dcSAndreas Gohr * @return resource 104*c609f1dcSAndreas Gohr * @throws \Exception 105*c609f1dcSAndreas Gohr */ 106*c609f1dcSAndreas Gohr protected function createTransparentImage($width, $height) 107*c609f1dcSAndreas Gohr { 108*c609f1dcSAndreas Gohr $image = @imagecreatetruecolor($width, $height); 109*c609f1dcSAndreas Gohr if (!$image) { 110*c609f1dcSAndreas Gohr throw new \Exception('Missing libgd support'); 111*c609f1dcSAndreas Gohr } 112*c609f1dcSAndreas Gohr imagealphablending($image, false); 113*c609f1dcSAndreas Gohr $transparency = $this->transparentColor($image); 114*c609f1dcSAndreas Gohr imagefill($image, 0, 0, $transparency); 115*c609f1dcSAndreas Gohr imagecolordeallocate($image, $transparency); 116*c609f1dcSAndreas Gohr imagesavealpha($image, true); 117*c609f1dcSAndreas Gohr return $image; 118*c609f1dcSAndreas Gohr } 119*c609f1dcSAndreas Gohr} 120