CMS MADE SIMPLE FORGE

TruetypeText

 

[#4780] Feature request: support user-supplied TruetypeText images

avatar
Created By: Rick Beton (rickb)
Date Submitted: Sun Apr 11 11:59:35 -0400 2010

Assigned To: Jonathan Schmid (Foaly*)
Version: None
CMSMS Version: None
Severity: Minor
Resolution: Awaiting Response
State: Open
Summary:
Feature request: support user-supplied TruetypeText images
Detailed Description:
TruetypeText is an excellent module for generating replacement images on the
fly. Sometimes, however, it is necessary to tweak the automcatically-generated
images.  There is an easy way to do this if the class.style.php code is tweaked
to allow it.

The idea is that an extra step is added when an image is required:

1. check whether a manually-supplied image exists
2. if not, then check whether a cached image exists
3. if not, then generate a new image and store it in the cache
4. otherwise return an error message.

So, step 1 is the new step I propose. I shall add a patch file indicating how I
achieved this.


History

Comments
avatar
Date: 2010-04-11 12:00
Posted By: Rick Beton (rickb)

Index: TruetypeText/class.style.php
===================================================================
--- TruetypeText/class.style.php	(revision 332)
+++ TruetypeText/class.style.php	(revision 333)
@@ -33,7 +33,7 @@
 		$result = false;
 		return $result;
 	}
-	
+
 	function getstylesbyfont($fontfile)	{
 		$result = array();
 		for ($i=0 ; $i<count($this->styles); $i++)
@@ -43,7 +43,7 @@
 		}
 		return $result;
 	}
-	
+
 	function & stylearray()	{
 		return $this->styles;
 	}
@@ -60,8 +60,8 @@
 	var $pictureperword; // one picture per word (boolean)
 	var $transparent;
 	var $elements;
-		
-		
+
+
function TTTstyle($name = '', $backgroundcolor = "#FFFFFF",$format = 'png',
$fulllineheight = true, $maxwidth = 0, $pictureperword = false, $transparent =
true)	{
 		global $gCms;
 		$this->db = & $gCms->getDb();
@@ -83,11 +83,11 @@
 			return false;
$query = 'SELECT * FROM '.cms_db_prefix().'module_truetypetext_styles WHERE
id = ?';
 		$row = $this->db->GetRow($query, array($id));
-		if ($row)		
+		if ($row)
 			$this->loadrow($row);
 		return ($row !== false);
 	}
-	
+
 	function loadDbByName($name="")
 	{
 		if ($name != '')
@@ -100,7 +100,7 @@
 			$this->loadrow($row);
 		return ($row !== false);
 	}
-	
+
 	function loadrow($row)	{
 		$this->id = $row['id'];
 		$this->name = $row['name'];
@@ -126,7 +126,7 @@
 			$elements[] = implode(';',$element);
 		return implode('\n', $elements);
 	}
-	
+
 	function saveDbNew()
 	{
 		$newid = $this->db->GenID(cms_db_prefix()."module_truetypetext_styles_seq");
@@ -145,7 +145,7 @@
$dbresult = $this->db->Execute($query,
array($this->name,$this->format,$this->backgroundcolor,$this->fulllineheight,$this->maxwidth,$this->pictureperword,
$this->transparent, $this->getelements(), $this->id));
 		return ($dbresult !== false);
 	}
-	
+
 	function deleteDb()
 	{
 		if ($this->id == -1) return false;
@@ -153,7 +153,7 @@
 		$dbresult = $this->db->Execute($query, array($this->id));
 		return ($dbresult !== false);
 	}
-	
+
function addTextElement($fontfile, $size = 20, $color = "#000000", $x = 0, $y
= 0, $angle = 0)	{
 		$this->elements[] = array('text',$fontfile,$size,$color,$x,$y,$angle);
 	}
@@ -169,7 +169,7 @@
 	{
 		$this->elements[] = array('transformation',$type);
 	}
-	
+
 	function checkfont($fontfile)	{
 		foreach ($this->elements as $element)
 		{
@@ -190,11 +190,11 @@
 		$name = md5($name);
 		return $name;
 	}
-	
+
 	function getImages ($text) {
 		if ($text == '') return '';
 		$text = html_entity_decode($text);
-		
+
 		$images = array();
 
 		if ($this->pictureperword)
@@ -212,29 +212,42 @@
 			$this->pictureperword = true;
 			return $images;
 		}
-		
-		
+
+
 		if (count($this->elements)==0)
 			return '<!-- TruetypeText error : empty style -->'.$text;
-		$filepath = cms_join_path('cache',
$this->getCacheName($text).'.'.$this->format);
-		$fileabsolutepath = cms_join_path(dirname(__FILE__), $filepath);
-		if (!file_exists($fileabsolutepath))
-			$result = $this->createImage($text, $fileabsolutepath);
+
+		$filename = $this->getCacheName($text).'.'.$this->format;
+  	    $relpath = 'uploads/TruetypeText/';
+  	    $manualpath = dirname(__FILE__).'/../../'.$relpath.$filename;
+		if (file_exists($manualpath))
+		{
+		    // image was prepared by user
+		    $fileabsolutepath = $manualpath;
+			$result = true;
+		}
 		else
-			$result = true;
+		{
+		    // normal automatic image handling
+		    $fileabsolutepath = dirname(__FILE__).'/cache/'.$filename;
+  	        $relpath = 'modules/TruetypeText/cache/';
+		    if (!file_exists($fileabsolutepath))
+			    $result = $this->createImage($text, $fileabsolutepath);
+		    else
+			    $result = true;
+		}
+
 		if ($result)
 		{
 			$size = @getimagesize($fileabsolutepath);
 			global $CMS_ADMIN_PAGE;
 			if (isset($CMS_ADMIN_PAGE))
-				$filepath = cms_join_path('..','modules','TruetypeText',$filepath);
+				$fileurl = '../'.$relpath.$filename;
 			else
-				$filepath = cms_join_path('modules','TruetypeText',$filepath);
-			return array(array("text"=>$text, "src"=>$filepath, "size"=>$size[3]));
+				$fileurl = $relpath.$filename;
+			return array(array("text"=>$text, "src"=>$fileurl, "size"=>$size[3]));
 		}
 		else return '<!-- Truetypetext error -->'.$text;
-		
-		
 	}
 
 	function getImage($text, $alt=true, $moretext="")
@@ -249,23 +262,23 @@
 			else
$result .= '<img src="'.$image["src"].'"'.($alt=true ? '
alt="'.$image["text"].'"' : '').' '.$moretext.' '.$image["size"].'/>';
 		}
-		
+
 		return $result;
 	}
-	
+
 	function getImageSrc($text)
 	{
 		if ($text == '') return '';
 		$this->pictureperword = false;
 		$images = $this->getImages($text);
-		return $images[0]["src"];		
+		return $images[0]["src"];
 	}
-	
-	
+
+
 /*	function getImage($text, $alt=true, $moretext="")
 	{
 		if ($moretext=="test") return $this->getImage2($text,$alt,$moretext);
-		
+
 		if ($text == '') return '';
 		$text = html_entity_decode($text);
 		if ($this->pictureperword)
@@ -304,17 +317,17 @@
 
 		}
 		else return '<!-- Truetypetext error -->'.$text;
-		
-		
+
+
 	}*/
-	
+
 	function getImageClean($text)
 	{
 		$text = str_replace('<br />',"\n",$text);
 		$text = preg_replace('@<[\/\!]*?[^<>]*?>@si', '', $text);
 		return $this->getimage($text);
 	}
-	
+
 	function createImage($text, $file)
 	{
 		if ($text == '') return true;
@@ -326,7 +339,7 @@
 		$TTTimage->destroy();
 		return $TTTimage->getsize();
 	}
-	
+
 	function wrap($text)
 	{
 		if ($text == '') return '';
@@ -351,7 +364,7 @@
 			$newtext .= ($newtext=='' ? '' : '\n').$line;
 		return $newtext;
 	}
-	
+
 	function getTTTimage($text)	{
 		include_once dirname(__FILE__)."/class.ttt.php";
 		$TTTimage = new TTTimage($this->backgroundcolor, $this->transparent);
      
avatar
Date: 2010-04-11 12:03
Posted By: Rick Beton (rickb)

Note that some usage of cms_join_path has been replaced by simple string
concatenation.  This works because '/' always works as a file path separator -
even on Windows!
(provided you disregard Windows 98 and earlier.)

Not many people seem to know this.
      
avatar
Date: 2010-04-11 12:30
Posted By: Rick Beton (rickb)

I forgot to explain that it is necessary to create the directory

uploads/TruetypeText/

This is the place where the manually-tweaked images are to be uploaded. Images
there have the same name as the automatically-generated images.
      
avatar
Date: 2011-07-27 08:27
Posted By: Jonathan Schmid (Foaly*)

please post your feature request in the tab "feature requests". please attach a
diff or patch file.
      
Updates

Updated: 2011-07-27 08:27
resolution_id: 5 => 10
assigned_to_id: 100 => 12994

Updated: 2010-04-11 12:00
resolution_id: => 5