[thelist] php on-the-fly image and text generation question

Andy Warwick mailing.lists at creed.co.uk
Sun Oct 13 16:49:01 CDT 2002


On Sunday, October 13, 2002, at 09:34  pm, Dunstan Orchard wrote:

> Does anyone have any idea why some of the letters on this page are all
> blurry?
>
> http://www.1976design.com/testarea/photo/image.php

Dunstan

I had a similar issue with some code I was using to generate text on
the fly for headers, and it seemed to be a rounding/rendering error,
where the 'hinting' in the font file wasn't very good at certain point
sizes.

I got round it by generating the text at double size, then shrinking it
down. I also set up a caching mechanism, so that the generated type
would be saved to disk the first time it was created, so second and
subsequent times it was requested the file simply had to be loaded
rather than generated.

My code is given below, called using the following rewrite in .htaccess:

# Rewrite URIs of the form http://www.domain.co.uk/gfx/titles/h1/... to
script
#
------------------------------------------------------------------------
-----

	RewriteCond %{REQUEST_URI} ^/_gfx/titles/(.*)/(.*)$   [NC]
	RewriteRule ^.*$ /_gfx/titles/_create_header.php?style=%1&file=%2
[R]

And thus in the HTML:

	<h1><img src="_gfx/titles/h1/accessible_design.jpg" alt="accessible
design" /></h1>

Hopefully you should be able to work out what it's doing and grab the
bits you need.

<php code>
	ob_end_clean();

	$cache_path = dirname ( $_SERVER['PATH_TRANSLATED'] ) ;

	$file = ( isset ( $_GET['file'] ) ) ? $_GET['file'] : 'NULL' ;
	$vetted_file = basename ( realpath ( $_GET['file'] ) ) ;

	if ( $file !== $vetted_file ) exit () ;

	header ( 'Content-Type: image/jpeg' ) ;

	if ( $bytes = @filesize ( "$cache_path/$vetted_file" ) ) {
		header ( "Content-Length: $bytes" ) ;
		readfile ( "$cache_path/$vetted_file" ) ;
	} else {

		$style = ( isset ( $_GET['style'] ) ) ? $_GET['style'] : 'default' ;

		switch ( $style ) {
			case 'h1' :
				$size = '54' ;
				$height = '76' ;
				$width = '22' ;
				$baseline = '56' ;
				$text_color = 'blue' ;
				break ;
			case 'h2' :
				$size = '32' ;
				$height = '50' ;
				$width = '20' ;
				$baseline = '38' ;
				$text_color = 'grey' ;
				break ;
			case 'braille' :
				$size = '35' ;
				$height = '60' ;
				$width = '42' ;
				$baseline = '55' ;
				$text_color = 'grey' ;
				break ;
			default ;
				$size = '27' ;
				$height = '40' ;
				$width = '16' ;
				$baseline = '28' ;
				$text_color = 'grey' ;
				break ;
		}

		// Trim whitespace, swap underscores for spaces and lowercase text.
		$text = trim ( strtolower ( str_replace ( '_' , ' ', $_GET['file'] )
) ) ;

		$text = str_replace ( '.jpg' , '' , $text ) ; // Trim '.jpg' from end

		$image_width = ( strlen ( $text ) * $width ) ;
		$image_height = $height ;

		// Create 2 x size image
		$im = ImageCreate ( $image_width , $image_height ) ;

		// Setup colors
		$background = ImageColorAllocate ( $im , 253,253,255 ) ;
		$blue 		= ImageColorAllocate ( $im , 102,115,127 ) ;
		$grey 		= ImageColorAllocate ( $im , 146,149,153 ) ;

		ImageColorTransparent ( $im , $background ) ;

		switch ( $style ) {
			case 'h1' : $font_path = dirname ( $_SERVER['DOCUMENT_ROOT']
).'/fonts/Interstate-LightComp.ttf' ; break ;
			case 'h2' : $font_path = dirname ( $_SERVER['DOCUMENT_ROOT']
).'/fonts/Interstate-LightCond.ttf' ; break ;
			case 'braille' : $font_path = dirname ( $_SERVER['DOCUMENT_ROOT']
).'/fonts/braille.ttf' ; break ;
			default : $font_path = dirname ( $_SERVER['DOCUMENT_ROOT']
).'/fonts/Interstate-LightCond.ttf' ; break ;
		}

		ImageTTFText ( $im , $size , 0 , 0 , $baseline , $$text_color ,
$font_path , $text ) ;

		// Create destination image
		$header = ImageCreateTrueColor ( $image_width/2 , $image_height/2 ) ;

		// Resize image
		ImageCopyResampled ( $header , $im , 0 , 0 , 0 , 0 , $image_width/2,
$image_height/2 ,
							 $image_width , $image_height ) ;

		ImageJPEG ( $header , '' , 100 ) ;
		ImageJPEG ( $header , "$cache_path/".$vetted_file , 100 ) ;
	}
</php code>

If you have any questions let me know.

HTH

--
Andy Warwick
Creed New Media Design
<http://www.creed.co.uk/src/evolt/index.htm>




More information about the thelist mailing list