text = $text : 0; isset($style) ? $this->style = $style : 0; isset($displaytext) ? $this->displaytext = $displaytext : 0; isset($gzip) ? $this->gzip = $gzip : 0; $stylesheet = ANYFONT_FONTDIR."/styles.ini"; if($this->style == "admin"){ $this->fontsettings = array(); $this->fontsettings['font-name'] = $this->text; $this->text = $this->displaytext; if($this->displaytext == "charactermap"){ $this->text = self::charMap(); } $this->fontsettings['color'] = "333333"; $this->fontsettings['font-size'] = 27; $this->fontsettings['shadow'] = false; $this->fontsettings['shadow-color'] = "FFFFFF"; $this->fontsettings["limit-width"] = true; $this->fontsettings["max-width"] = 960; $this->fontsettings["line-height"] = 50; $this->fontsettings['is_admin'] = true; $this->fontsettings['image-padding'] = true; $this->fontsettings['image-padding-bottom'] = 10; $this->fontsettings['image-padding-left'] = 10; } else if($this->style == "admin-small"){ $this->fontsettings = array(); $this->fontsettings['font-name'] = $this->text; $this->text = $this->displaytext; $this->fontsettings['color'] = "333333"; $this->fontsettings['font-size'] = 20; $this->fontsettings['shadow'] = false; $this->fontsettings['shadow-color'] = "FFFFFF"; $this->fontsettings['shadow-spread'] = 0; $this->fontsettings['is_admin'] = true; }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')); exit(0); } } if(is_array($this->fontsettings)){ $this->linecount = 1; !isset($this->fontsettings['shadow-distance']) ? $this->fontsettings['shadow-distance'] = 1 : 0; !isset($this->fontsettings['shadow-spread']) ? $this->fontsettings['shadow-spread'] = 1 : 0; !isset($this->fontsettings['image-padding']) ? $this->fontsettings['image-padding'] = 0 : 0; !isset($this->fontsettings['image-padding-top']) ? $this->fontsettings['image-padding-top'] = 0 : 0; !isset($this->fontsettings['image-padding-bottom']) ? $this->fontsettings['image-padding-bottom'] = 0 : 0; !isset($this->fontsettings['image-padding-left']) ? $this->fontsettings['image-padding-left'] = 0 : 0; !isset($this->fontsettings['image-padding-right']) ? $this->fontsettings['image-padding-right'] = 0 : 0; !isset($this->fontsettings['line-height']) ? $this->fontsettings['line-height'] = 0 : 0; $this->fonttype = !file_exists(ANYFONT_FONTDIR."/".$this->fontsettings['font-name'].".ttf") ? ".otf" : ".ttf"; $pre_hash = ""; foreach($this->fontsettings as $value){ $pre_hash.=$value; } $hash = md5($pre_hash.$this->text."imagick"); $this->etag = $hash; if(!isset($this->fontsettings['is_admin'])){ $this->cache_file = ANYFONT_CACHE."/$hash-site.png"; } else { $this->cache_file = ANYFONT_CACHE."/$hash-admin.png"; } if(file_exists($this->cache_file)){ if(isset($_SERVER["HTTP_IF_NONE_MATCH"]) || isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ if($_SERVER["HTTP_IF_NONE_MATCH"] == $this->etag || $_SERVER['HTTP_IF_MODIFIED_SINCE'] == gmdate('D, d M Y H:i:s', filemtime($this->cache_file)).' GMT'){ header("HTTP/1.0 304 Not Modified"); exit(304); } else { self::text2Image(); } } else { self::printImage(); } } else { self::text2Image(); } } } private function text2Image(){ switch(ANYFONT_IMAGE_ENGINE){ case 'imagick': self::textImagick(); break; case 'cairo': self::textImagick(); break; } } private function textCairo(){ // $img = new CairoImageSurface(CairoFormat::ARGB32, 500, 180); // // $context = new CairoContext($img); // // // $context->save (); // // $RGB = self::hexToRgb($this->fontsettings['background-color']); // // $context->setSourceRgb ($RGB[0], $RGB[1], $RGB[2]); /* white */ // // // // $context->paint (); // // // // $context->restore (); // // $FontRGB = self::hexToRgb($this->fontsettings['background-color']); // // $context->setSourceRgb ($FontRGB[0], $FontRGB[1], $FontRGB[2]); /* black */ // // // // $context->selectFontFace (ANYFONT_FONTDIR."/".$this->fontsettings['font-name'].$this->fonttype/*, CairoFontSlant::NORMAL, CairoFontWeight::NORMAL*/); // // $context->setFontSize ($this->fontsettings['font-size']); // // // // $context->moveTo ( 0, $this->fontsettings['font-size']); // // $context->showText ($this->text); // // $img->writeToPng($this->cache_file); // self::printImage(); } /** Description * Creates a png image from a text string using truetype fonts and writes the file to cache */ private function textImagick(){ $image = new Imagick(); $draw = new ImagickDraw(); $draw->setTextEncoding("UTF-8"); $draw->setFillColor(new ImagickPixel("#".$this->fontsettings['color'])); $draw->setFont(ANYFONT_FONTDIR."/".$this->fontsettings['font-name'].$this->fonttype); $draw->setFontSize($this->fontsettings['font-size']); if($this->fontsettings["limit-width"]){ $str = explode(" ", $this->text); $count = count($str); $n = 0; $l = 0; $lines = array(); $max_width = (int)$this->fontsettings["max-width"]; $max = $max_width; while($n < $count){ !isset($lines[$l]) ? $lines[$l] = "" : 0; $test_str = $lines[$l] !== "" ? $lines[$l]." ".$str[$n] : $str[$n]; $metrics = $image->queryFontMetrics($draw, $test_str); if ($metrics['textWidth'] < $max) { $lines[$l] != "" ? $lines[$l].=" ".$str[$n] : $lines[$l] = $str[$n]; $n++; } else if($test_str !== $str[$n]){ $max = $max_width; $l++; } else { $max+=10; } } $this->text = implode("\n", $lines); } $this->linecount = substr_count($this->text, "\n")+1; //get metrics for width $fm = $image->queryFontMetrics($draw, $this->text); //get metrics for first letter $fcm = $image->queryFontMetrics($draw, substr($this->text, 0, 1)); $min_x = min(array($fcm["boundingBox"]["x2"],$fcm["boundingBox"]["x1"])); if($min_x < 0){ $min_x = -($min_x); } //get metrics for font height and baseline $fym = $image->queryFontMetrics($draw, self::charMap()); $min_y = min(array($fym["boundingBox"]["y2"],$fym["boundingBox"]["y1"])); $max_y = max(array($fym["boundingBox"]["y2"],$fym["boundingBox"]["y1"])); // echo "
";echo $this->text; // print_r($fm);print_r($fym);print_r($fcm); // echo ""; // exit(0); $padding_x = !$this->fontsettings['image-padding'] ? 0 : (int)$this->fontsettings['image-padding-left']+(int)$this->fontsettings['image-padding-right']; $padding_y = !$this->fontsettings['image-padding'] ? 0 : (int)$this->fontsettings['image-padding-top']+(int)$this->fontsettings['image-padding-bottom']; $shadowspread = !$this->fontsettings['shadow'] ? 0 : $this->fontsettings['shadow-spread']; $width = $fm["textWidth"]+$padding_x; $height = $max_y+(-($min_y))+$padding_y; if($this->fontsettings['shadow']){ $height += ((int)$this->fontsettings['shadow-distance']+((int)$this->fontsettings['shadow-spread']*2.8)); $width += ((int)$this->fontsettings['shadow-distance']+((int)$this->fontsettings['shadow-spread']*2.8)); } if($this->linecount > 1){ $this->fontsettings["line-height"] > 0 ? $height = $this->fontsettings["line-height"] < $height ? $height-(($height-$this->fontsettings["line-height"])/2) : $this->fontsettings["line-height"] : 0; $height*=$this->linecount; } $x = $min_x+$shadowspread+(!$this->fontsettings['image-padding'] ? 0 : ((int)$this->fontsettings['image-padding-left'])); $y = $max_y+$shadowspread+(!$this->fontsettings['image-padding'] ? 0 : ((int)$this->fontsettings['image-padding-top'])); $image->newImage($width, $height, "transparent", "png"); if($this->fontsettings['shadow']){ if($this->linecount > 1 && $this->fontsettings["line-height"] > 0){ $l = 0; $ly = $y; while($l < $this->linecount){ $lines = explode("\n", $this->text); $l > 0 ? $ly += $this->fontsettings["line-height"] : 0; $image = $this->shadowImage($image, $x, $ly, $lines[$l]); $l++; } }else{ $image = $this->shadowImage($image, $x, $y, $this->text); } $this->fontsettings['shadow-spread'] > 0 ? $image->blurImage(0, (int)$this->fontsettings['shadow-spread']) : 0; } if($this->linecount > 1 && $this->fontsettings["line-height"] > 0){ $l = 0; while($l < $this->linecount){ $lines = explode("\n", $this->text); $l > 0 ? $y += $this->fontsettings["line-height"] : 0; $image->annotateImage($draw, $x, $y, 0, $lines[$l]); $l++; } }else{ $image->annotateImage($draw, $x, $y, 0, $this->text); } $image->writeImage($this->cache_file); self::printImage($image); } private function shadowImage($image, $x, $y, $text){ $shadow = new ImagickDraw(); $shadow->setFillColor(new ImagickPixel("#".$this->fontsettings['shadow-color'])); $shadow->setFont(ANYFONT_FONTDIR."/".$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, $text); return $image; } /** Description * Fetches the image from cache and delivers it to the browser along with the required headers. * @prints PNG image */ private function printImage($img=false){ !$img ? $img = file_get_contents($this->cache_file) : 0; header('Content-type: image/png'); $this->gzip ? $img = gzencode($img) : 0; $this->gzip ? header('Content-encoding: gzip') : 0; header('X-Generated-By: AnyFont v'.ANYFONT_VERSION.' for WordPress'); header('Content-length: '.strlen($img)); header('Last-modified: '.gmdate('D, d M Y H:i:s', filemtime($this->cache_file)).' GMT'); header('Expires: '.gmdate("D, d M Y H:i:s", mktime(0, 0, 0, date("m") , date("d")-1, date("Y") )).' GMT'); //FORMAT: Wed, 11 Jan 1984 05:00:00 GMT header('Etag: "'.$this->etag.'"'); print($img); } private function hexToRgb($hex){ substr($hex,0,1) == '#' ? $hex = substr($hex,1) : 0; if(strlen($hex) == 3){ $hex = substr($hex,0,1) . substr($hex,0,1). substr($hex,1,1) . substr($hex,1,1). substr($hex,2,1) . substr($hex,2,1); } strlen($hex) != 6 ? $error = 'Error: Invalid color "'.$hex.'"' : 0 ; $rgb[] = hexdec(substr($hex,0,2)); $rgb[] = hexdec(substr($hex,2,2)); $rgb[] = hexdec(substr($hex,4,2)); return $rgb; } private function charMap(){ $i = 33; $txt = ""; while($i <= 126){ $txt .= "$i; "; $i++; } $i = 188; while($i <= 255){ $txt .= "$i; "; $i++; } return html_entity_decode(urldecode($txt), ENT_QUOTES); } } ?>