Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

A Monday Exercise - "Graphical Pseudofont"

General discussions related to php

Moderators: macek, egami, gesf

A Monday Exercise - "Graphical Pseudofont"

Postby swirlee » Mon Jul 14, 2003 4:01 pm

For those of you who feel your skills are languishing, I thought I'd post a challenging little exercise. The idea is this:

You have an image (PNG, GIF, or BMP) which contains all of the basic English characters: a-z, A-Z, 0-9, some punctuation. The characters are monospaced (all have the same width and also height) and in a specific (predictable) order. This is the "pseudofont". The challenge is this: Create a function that the user may pass a string to, which will create an image of the string using the letters in the pseudofont image. You'll probably want to use the GD library, but there are alternatives.

If you wish to participate in this exercise, there really aren't any rules. I'd like to see whatever comes out of this, even if you end up going in a completely different direction. There also aren't any prizes or rewards except pride and perhaps bragging rights. Some features you might want to consider adding if you're feeling especially ambitious:

  • If the pseudofont is black-and-white, allow the user to specify a foreground and background color or transparency (replace black with foreground, white with background).
  • Allow different-sized pseudofonts. Note: With a predictable character order and monospaced characters, you can derive the character width and height.
  • Allow the user to specify attributes like spacing (extra space between characters) or even scaling (i.e. double the height or width)
  • Allow the user to specify the output file format.
  • Allow the user to specify a maximum image width and create a word-wrapping scheme for long texts.
  • If a character is encountered that doesn't exist in the pseudofont, allow the option to grab it from an existing system font.
  • I'm sure you can think of dozens others.


I'd really like to see others participate in this challenge. If you intend to participate, let me know below, just so I'm not sitting here waiting to see if anybody's interested. Also, if you think this is a lame challenge, I'd love to see others post their challenges.

Update: For the purpose of the exercise, I've created a pseudofont image. It contains the following characters in the following order:

Code: Select all
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?"',:;-()_


The last is intended to be a space, not an underscore. Each character is 7px wide with one pixel of padding to the right of each character. You may include the padding as part of each character or you may trim it off. Here it is:

Image

(I think the spacing is right for all characters, if but anybody notices any discrepancies, let me know.)
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby swirlee » Mon Jul 14, 2003 8:32 pm

I'm disappointed that I've got a working script before anybody else has elected to participate. Nonetheless, below is a link to my source:

http://www.swirlee.org/pseudofont/pseudofont.class.phps

This is the source to the Pseudofont class. The source is relatively descriptive, so I won't try to explain it all here. To see the source of a sample implementation with random text (funny headlines I grabbed from the web), see here:

http://www.swirlee.org/pseudofont/index.phps

That version of the script expects a PNG and pukes at anything else. I plan on fixing this tomorrow to accept more image formats. Unfortunately, the only space I've got online doesn't have PNG support turned on, so I don't have a nifty demonstration to show you (index.php as above just pukes), but here is a sample "funny headline" generated by the script:

Image

Additionally, I implore you to download the scripts and the pseudofont file and give it a go. Here's the pseudofont file:

http://www.swirlee.org/pseudofont/myalphabet.png

Some feedback would be nice, and I'd love to see if anybody else tried this little exercise. Tomorrow I'm going to try to implement some of the features I mentioned.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby WiZARD » Mon Jul 14, 2003 10:08 pm

he-he-he.......
http://www.swirlee.org/pseudofont/
too many errors
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby Oleg Butuzov » Mon Jul 14, 2003 10:12 pm

hehe WiZARD
GD without PNG something new =(((

as for class - thanks.
Oleg Butuzov
Last Samuray
Last Samuray
 
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby WiZARD » Mon Jul 14, 2003 10:59 pm

http://www.swirlee.org/pseudofont/
i see next:
Code: Select all
Warning: ImageCreateFromPng: No PNG support in this PHP build in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 19
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 21
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 22
Warning: Cannot add header information - headers already sent by (output started at /home/poetsmurf/public_html/pseudofont/pseudofont.class.php:19) in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 85
Warning: Cannot add header information - headers already sent by (output started at /home/poetsmurf/public_html/pseudofont/pseudofont.class.php:19) in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 86
Warning: Cannot add header information - headers already sent by (output started at /home/poetsmurf/public_html/pseudofont/pseudofont.class.php:19) in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 87
Warning: Cannot add header information - headers already sent by (output started at /home/poetsmurf/public_html/pseudofont/pseudofont.class.php:19) in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 88
Warning: Cannot add header information - headers already sent by (output started at /home/poetsmurf/public_html/pseudofont/pseudofont.class.php:19) in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 89
Warning: Cannot add header information - headers already sent by (output started at /home/poetsmurf/public_html/pseudofont/pseudofont.class.php:19) in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 90
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 49
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: Supplied argument is not a valid Image resource in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 73
Warning: ImagePng: No PNG support in this PHP build in /home/poetsmurf/public_html/pseudofont/pseudofont.class.php on line 92
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby Oleg Butuzov » Mon Jul 14, 2003 11:12 pm

wierd...=(

Code: Select all
header("Content-type: image/png");
                header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
                header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
                header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
                header("Cache-Control: post-check=0, pre-check=0", false);
                header("Pragma: no-cache");                          // HTTP/1.0


every header of this headers must be sent but!

Code: Select all
Warning: Cannot modify header information - headers already sent by (output started at c:\internet\localhost\www\swirlee\pseudofont.class.php:97) in c:\internet\localhost\www\swirlee\pseudofont.class.php on line 85
Warning: Cannot modify header information - headers already sent by (output started at c:\internet\localhost\www\swirlee\pseudofont.class.php:97) in c:\internet\localhost\www\swirlee\pseudofont.class.php on line 86
Warning: Cannot modify header information - headers already sent by (output started at c:\internet\localhost\www\swirlee\pseudofont.class.php:97) in c:\internet\localhost\www\swirlee\pseudofont.class.php on line 87
Warning: Cannot modify header information - headers already sent by (output started at c:\internet\localhost\www\swirlee\pseudofont.class.php:97) in c:\internet\localhost\www\swirlee\pseudofont.class.php on line 88
Warning: Cannot modify header information - headers already sent by (output started at c:\internet\localhost\www\swirlee\pseudofont.class.php:97) in c:\internet\localhost\www\swirlee\pseudofont.class.php on line 89
Warning: Cannot modify header information - headers already sent by (output started at c:\internet\localhost\www\swirlee\pseudofont.class.php:97) in c:\internet\localhost\www\swirlee\pseudofont.class.php on line 90



and output png image///
[code]‰PNG  IHDRhó­ÇÀPLTEÿÿÿ¥ÙŸÝUIDATxœ
Oleg Butuzov
Last Samuray
Last Samuray
 
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby WiZARD » Mon Jul 14, 2003 11:34 pm

class writed correctly......
maby in code error......
hey swirlee see youre code...... you something send to browser before use class......
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby pootergeist » Tue Jul 15, 2003 3:05 am

If anyone fancies porting that to support .gd2 file format (means GD doesn't need to compile a readable file from the jpg/png/whatever as it is already in the proper format) - then you could use imagecreatefromgd2part which as the name implies only reads a specified part of the file without executing the lot - should save you an easy 50-80% process time.

the logic for the letter grabber and writer would be a tad different though same principle - could add a logic loop to caché $$ref_'letter' pointers so as to bring processing down to minimum - eg
find 'a' as $leta
eval("\$ref_" .$leta. " = imagecreatefromGD2part(\$base, \$x, \$y, \$h, \$w);");
pootergeist
New php-forum User
New php-forum User
 
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK

Postby swirlee » Tue Jul 15, 2003 5:20 am

swirlee wrote:That version of the script expects a PNG and pukes at anything else. I plan on fixing this tomorrow to accept more image formats. Unfortunately, the only space I've got online doesn't have PNG support turned on, so I don't have a nifty demonstration to show you (index.php as above just pukes).


As I stated originally, this is the reason for the errors. The script runs fine on my machine, but the machine where I've uploaded to has an old version of GD without PNG (or, ironically, GIF) support. And since I can't open a PNG image, every other GD function gives an error, and since error messages have been sent, the header-already-sent errors happen. The only reason it's up there is so I could symlink the source for all of you. It won't work if you try to run it where it is. Like I said, if you want to see it in action, just copy the source and run it locally.

pootergeist wrote:If anyone fancies porting that to support .gd2 file format (means GD doesn't need to compile a readable file from the jpg/png/whatever as it is already in the proper format) - then you could use imagecreatefromgd2part which as the name implies only reads a specified part of the file without executing the lot - should save you an easy 50-80% process time.


I'll look into this tonight. The main advantage of reading from a PNG is that I can create the pseudofont file quite painlessly using a run-of-the-mill image editing tool.

Also, if anybody's interested in working this from another angle, PHP/GD has a imageloadfont() function that allows you to load a user-defined bitmap font and use that font with the usual GD font functions. You'd have to make the script take the PNG or whatever and convert it to PHP's binary font format, but it might save a bit of work in the end and allow for a greater range of manipulation. Hmm. Lots to look into after work.

In the meantime, I'm trying to convince my BOFH to get PNG support running.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby bonkers » Thu Jul 17, 2003 8:32 am

I know this is kinda off topic but it came to me from this topic. I see a lot of people putting their php files so that people can view then as .phps, and there seems to be a standard color coding to it. How do you get it to come out like this? When I put my docs as .phps it's all black text... How can I make the pretty colors?
bonkers
New php-forum User
New php-forum User
 
Posts: 46
Joined: Wed Mar 12, 2003 7:17 pm

Postby swirlee » Thu Jul 17, 2003 10:50 am

When you see a .phps file, it's usually because the the author has created a symlink (symbolic link, kind of like a Windows shortcut) to the PHP file using the command "ln -s file.php file.phps". "ln" is the *nix command to create a symbolic link to the first file (file.php) from the second file (file.phps). For this to work, and assuming you're using Apache, you'll have to add a line to your apache.conf. Check this thread for more info on that. If you're not using Apache, you'll have to figure out how to do it in whatever server you're using.

This is a *nix-specific command, so if your server is on Windows, this won't work for you. Also, you might need some other behind-the-scenes configuration that I'm unaware of, as my BOFH handles that sort of thing.

If you're not running *nix, you're not out of luck just yet. The PHP command highlight_file() will produce near-identical results. There are ways to go about using it. If you have a file called file.php, you could just create a new files called file.source.php and have it say:

Code: Select all
<?
   highlight_file('file.php');
?>


(Or you could just make Apache send .phps files to PHP.) Or you could make a source.php that will do this universally, so if you requested source.php?script=file.php, it would show the source of file.php like so:

Code: Select all
<?
   highlight_file($_GET['script']);
?>


Of course it would be a little more complex, and you'd have to deal with certain security concerns. You'd just have to make sure that it wouldn't show the source of certain files, like ones that contain passwords.

Also, you could just use Apache's mod_rewrite to automatically convert requests for file.phps to source.php?script=file.php. Once again, beware of security risks.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back


Return to PHP General

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.