text = $text : 0; isset($style) ? $this->style = $style : 0; isset($gzip) ? $this->gzip = $gzip : 0; $stylesheet = FONT_DIR."/styles.ini"; if($this->style == "admin"){ $this->fontsettings = array(); $this->fontsettings['font-name'] = $this->text; $this->fontsettings['color'] = "333333"; $this->fontsettings['font-size'] = 24; $this->fontsettings['shadow'] = false; $this->fontsettings['shadow-color'] = "FFFFFF"; } else if($this->style == "admin-small"){ $this->fontsettings = array(); $this->fontsettings['font-name'] = $this->text; $this->fontsettings['color'] = "333333"; $this->fontsettings['font-size'] = 20; $this->fontsettings['shadow'] = true; $this->fontsettings['shadow-color'] = "888888"; $this->fontsettings['shadow-spread'] = 1; }else if(file_exists($stylesheet)){ $styles = parse_ini_file($stylesheet, true); if(isset($styles[$this->style])){ $this->fontsettings = $styles[$this->style]; } else { wp_redirect(get_option('siteurl')); } } if(is_array($this->fontsettings)){ $this->linecount = 1; if ($this->fontsettings["limit-width"]){ $this->text = wordwrap($this->text, (int)$this->fontsettings["max-width"], "\n", true); } !isset($this->fontsettings['shadow-distance']) ? $this->fontsettings['shadow-distance'] = 1 : 0; !isset($this->fontsettings['shadow-spread']) ? $this->fontsettings['shadow-spread'] = 1 : 0; $this->fonttype = !file_exists(FONT_DIR."/".$this->fontsettings['font-name'].".ttf") ? ".otf" : ".ttf"; $hash = md5(basename($this->fontsettings['font-name']).$this->fontsettings['color'].$this->fontsettings['font-size'].$this->fontsettings['shadow-spread'].$this->fontsettings['shadow-distance'].$this->fontsettings['shadow'].$this->fontsettings['shadow-color'].$this->fontsettings['limit-width'].$this->fontsettings['max-width'].$this->text); $this->etag = $hash; $this->file = CACHE_DIR."/$hash.png"; if(file_exists($this->file)){ if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == gmdate('D, d M Y H:i:s', filemtime($this->file)).' GMT'){ header("HTTP/1.0 304 Not Modified"); } else{ self::fetchImage(); } } else { self::text2Image(); } } else { wp_redirect(get_option('siteurl')); } } /** Description * Creates a png image from a text string using truetype fonts and writes the file to cache */ private function text2Image(){ $image = new Imagick(); $draw = new ImagickDraw(); $draw->setFillColor(new ImagickPixel("#".$this->fontsettings['color'])); $draw->setFont(FONT_DIR."/".$this->fontsettings['font-name'].$this->fonttype); $draw->setFontSize($this->fontsettings['font-size']); $lines = substr_count($this->text, "\n"); $lines+=1; $fm = $image->queryFontMetrics($draw, $this->text, true); $width = $fm["textWidth"]+($this->fontsettings['shadow-distance']+(int)$this->fontsettings['shadow-spread']); $height = $fm["boundingBox"]["y2"]+(-($fm["boundingBox"]["y1"]))+($this->fontsettings['shadow-distance']+(int)$this->fontsettings['shadow-spread']); $height < $fm["textHeight"] ? $height = $fm["textHeight"]+($this->fontsettings['shadow-distance']+(int)$this->fontsettings['shadow-spread']) : 0; $firstchar = substr($this->text, 0, 1); $fcm = $image->queryFontMetrics($draw, $firstchar); $x = -($fcm["boundingBox"]["x1"])+((int)$this->fontsettings['shadow-spread']-1); $y = (($height/$lines)+$fm["boundingBox"]["y1"])-($this->fontsettings['shadow-distance']+(int)$this->fontsettings['shadow-spread']); $image->newImage($width, $height, "transparent", "png"); if($this->fontsettings['shadow']){ $shadow = new ImagickDraw(); $shadow->setFillColor(new ImagickPixel("#".$this->fontsettings['shadow-color'])); $shadow->setFont(FONT_DIR."/".$this->fontsettings['font-name'].$this->fonttype); $shadow->setFontSize($this->fontsettings['font-size']); $image->annotateImage($shadow, ($x+$this->fontsettings['shadow-distance']),($y+$this->fontsettings['shadow-distance']), 0, $this->text); $image->blurImage(0, (int)$this->fontsettings['shadow-spread']); } $image->annotateImage($draw, $x, $y, 0, $this->text); $image->writeImage($this->file); if(file_exists($this->file)){ self::fetchImage(); } } /** Description * Fetches the image from cache and delivers it to the browser along with the required headers. * @return PNG image */ private function fetchImage(){ $image = !$this->gzip ? file_get_contents($this->file) : gzencode(file_get_contents($this->file)); header('X-Generated-By: AnyFont for WordPress'); $this->gzip ? header('Content-encoding: gzip') : 0; header('Content-type: image/png'); header('Content-length: '.strlen($image)); header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($this->file)).' GMT'); header('Etag: '.$this->etag); header('Cache:'); header('Pragma:'); header('Cache-Control:'); print($image); exit(0); } } ?>