circle.ch weblog by Urs Gehrig

 Search
A weblog about libre software, law, technology, politics and the like.
Academic, Android, Apache, Apple, Art, Best Of, Biotech, Civil Society, Content Management, Cooking, Copyright, Creative Commons, Crosspost, Culture, Database, Deutsch, DRM, Economy, Education, Event, Gadget, General, Geodata, Government, Health, Howto, Humor, Innovation, Intellectual Property, Java, Language, LaTeX, Law, Linux, Media, Moblog, Mozilla, Music, Office, Open Content, Open Source, P2P, PHP, Podcast, Politics, Privacy, Projects, Random Thought, Rant, Science, Search, Social Network, Software, Sport, Talks, Technology, Technology Transfer, Travelling, Weblog, Wiki, Wireless and Mobile, XML

23. April 2002

Converting an Image to standalone SVG
@ 20:36:29

Abstract

SVG offers the possibility to display images within an SVG viewer by either referencing to the image file or - wich makes the SVG file standalone - embeding the image source within the SVG file [1]. To have a standalone file one needs to encde the image by base64 [2]. The code below is reading the test.jpg file binary safe, which is one of the important parts here, while it is later converted directly by the PHP built-in base64_encode()function [3].
Finally the encoded string and the image size parameters will be applied to an SVG template file. Have a look at the sample script too [4].

Happy SVGin!

Script: image2svg.php

<?php
/**
* Topic: Converting an image to standalone SVG
* Author: Urs Gehrig
* Date: 2002-04-22
* License: GNU General Public License (GPL)
* http://www.gnu.org/licenses/gpl.html
*
* Thanks: Vincent Hardy
*/

// One never knows, how long it will last...
ini_set("max_execution_time", 10000);

// Get them from cvs.php.net
require_once("HTML/ITX.php" );

// Load a test image
$filename = "test.jpg";
$fd = fopen ($filename, "rb" ) or
die ("File $file cannot be opened." );

// Read binary-safe
$buffer = fread($fd, filesize ($filename) );
fclose ($fd);

// Get parameters of the image
$par = getimagesize($filename );

// Handle the template file
$tpl = new IntegratedTemplate("." );
$tpl->loadTemplatefile("tpl.image.svg", true, true);
$content = array
(
"img_information" => base64_encode($buffer ),
"img_width" => $par[0],
"img_height" => $par[1],
"script_name" => basename($SCRIPT_NAME)
);
$tpl->setVariable($content);

// Ouput the SVG file
header("Content-type: image/svg+xml");
$tpl->show();
?>

Script: tpl.image.svg

<?xml version="1.0" encoding="iso-8859-1" ?>
<!--
// *****************************************
// Image To SVG Conversion
// Copyright (c) 2002, 2003 by Urs Gehrig
// *****************************************
//-->
<svg width="{img_width}" height="{img_height}"
viewBox="0 0 {img_width} {img_height}"
xml:space="preserve">

<g id="image_1">
<image width="{img_width}" height="{img_height}"
xlink:href="data:image/jpeg;base64,{img_information}"
transform="matrix(1 0 0 1 0 0)"/>
</g>

<a xlink:href="{script_name}" target="_self">
<text style="font-size:10; stroke:black; stroke-width:0.5" x="5" y="12">circle.ch</text></a>
</svg>

Links

[1] http://www.w3.org/Graphics/SVG/ - Scalable Vector Graphics (SVG)
[2] http://www.ietf.org/rfc/rfc2045.txt - Multipurpose Internet Mail Extensions (MIME)
[3] http://www.php.net/base64_encode - Base 64 encoding
[4] http://circle.ch/projects/image2svg/ - Convert your Image online

Comments (1) Permalink del.icio.us

15. April 2002

Authentication with PEAR Auth
@ 20:52:14

Abstract
PEAR offers now a session based authentication using a database container. The sample below should help you to start up with it.
Script
Links http://pear.php.net - PEAR Repository
01  <?php
02  
/**
03  * Authentication using PEAR Auth and MySQL
04  *
05  * -----------------------------------------------------------------------
06  * # Add this table to a database called 'auth':
07  * # Table structure for table `auth`
08  *
09  * CREATE TABLE auth (
10  * username varchar(50) default NULL,
11  * password varchar(50) default NULL
12  * ) TYPE=MyISAM COMMENT='PEAR, Auth';
13  *
14  * # Data for table `auth`, user:test, pwd:test
15  * INSERT INTO auth VALUES ('test', '098f6bcd4621d373cade4e832627b4f6');
16  * INSERT INTO auth VALUES ('demo', 'fe01ce2a7fbac8fafaed7c982a04e229');
17  *
18  * -----------------------------------------------------------------------
19  * Author: Urs Gehrig <urs@NOSPAMcircle.ch>
20  * Date: 2002-04-15
21  *
22  * Infos: http://pear.php.net/manual/en/packages.auth.auth.php
23  * $Id$
24  */
25  
26  
require_once("PEAR.php");
27  require_once(
"DB.php");
28  require_once(
"DB/mysql.php");
29  require_once(
"Auth/Auth.php");
30  
31  
// Initialization of database related information
32  
$params = array(
33                
"dsn" => "mysql://root:@localhost/auth",
34                
"table" => "auth",
35                
"usernamecol" => "username",
36                
"passwordcol" => "password"
37            
);
38  
39  
// A database is used as storage container in this example
40  
$a = &new Auth("DB", $params );
41  
42  
// Detection, if user is logged in. Otherwise the login form
43  // is being displayed.
44  
$a->start();
45  
46  if (
$a->getAuth())
47  {
48      if(
$_GET[act] == "logout")
49      {
50          
// Log user out
51          
$a->logout();
52          echo
"You have been logged out!<br />\n";
53          echo
"<a href=\"$_SERVER['PHP_SELF']?act=login\">Login</a><br />\n";
54      }
55      else
56      {
57          
// Output the content of your site, that is only visible
58          // for users which have been authenticated successfully.
59          
printf("Welcome user <b>%s</b>!<br />\n", $a->getUsername() );
60          echo
"<a href= \"$_SERVER['PHP_SELF']?act=logout\">Logout</a><br />\n";
61      }
62  }
63  else
64  {
65      
// Not authenticated
66      
echo "<center>You are not authenticated.<br />\n";
67      echo
"[Try: demo,demo or test,test]</center>\n";
68  }
69  
?>

Comments (21) Permalink del.icio.us

13. December 2001

Scripts
@ 21:09:37

PHP - scripts {most of them introduced on zend.com}

  • [2001-12-10] updated! automatically created
    image gallery


  • create dynamic button images


  • [2001-03-16] updated! a dynamically cascading
    menu with buttons


  • file upload for WIN32


  • an upload class for your database. currently, images and texts are checked.
    but there are no limits. extend to your own needs!


  • Having contributed code to the PHP PEAR repository, you can download an
    upload script, which is based on the "official" PEAR HTML Form class. Upload
    a single, or multiple files.


  • [2001-06-06] updated! A drag&drop-shop draft
    project from my early PHP days in 2000 ;)


  • [2001-03-10] updated! a filebased guestbook with administrator-mode


  • [2001-01-24] updated! a news management tool
    based on files. use "test" for login and password on my testsite. or see,
    how one implements the web front-end: list
    all news
    . I added an RDF/RSS format compliant file export of the data.
    Read about RDF/RSS at netscape.com.
    The tool features now WAP/WML export!



THIS SOFTWARE IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.

Comments (2) Permalink del.icio.us

27. March 2001

Removing obsolete session from the DB
@ 20:58:50

Abstract

Somewhere you may have a table with active_session of visitors using your site. From time to time you would like to remove obsolete session. The script below is using mktime() to fit the timestamp stored in the DB. I use to remove session in this example that are older than two days.

Script

<?php
/**
* Author: Urs Gehrig <urs_at_circle_ch>
* Date: 27-3-2001
*
* Subject: how you could remove your sessions older
* than _x_ days from your database.
*/
require_once("prepend.php");
page_open(array( "sess" => "ac_session",
"auth" => "ac_default_auth",
"perm" => "ac_perm",
"user" => "ac_user" ));
?>
<?php
/**
* delete sessions from database older than 2 days
* delete ac_user and ac_session
*/
$db = &new db_yourdb();
$cfg->active_sessions = "ac_active_sessions";

/**
* building the reference date
* $expired contains something like "20010327231519"
*/
$expired = strftime("%Y%m%d%H%M%S",
mktime(
date("H"),
date("i"),
date("s"),
date("m"),
date("d")-2,
date("Y")
) );
/**
* building the sql query
*/
$sql = "DELETE FROM
$cfg->active_sessions
WHERE
changed < '$expired'
AND
name
LIKE
'%ac_%'";

/**
* executing the query
*/
$db->query($sql);

?>
<?php
page_close();
?>

to be cont'd

Links

http://phplib.netuse.de - PHPLIB home
http://ch.php.net/mktime - PHP function reference

Comments (0) Permalink del.icio.us

18. October 2000

Handling nested blocks within Templates
@ 21:02:31

Abstract

The new template class called "Integratedtemplate" by NetUSE is probably, what you have been dreaming of. The author Ulf Wendel <uw@netuse.de> made your dreams come true. Have a look at this introduction sample script. I created two arrays, one containing the section title and another array holding the content. You may of course get your data from a database as well. Pay attention to the parsing of the blocks. Parse an inner block first and then, move to the outer block. Feel free to play around with this example.

Scripts

<!------- main.php ------->
<?php
/**
* Template: Integrated Template by Ulf Wendel
* URL: http://phplib.netuse.de/
*
* Authors: Urs Gehrig <urs@circle.ch>
* Ulf Wendel <uw@netuse.de>
* Date: 18-10-2000
*
* Darstellung im Stil:

club members
muster hans
reuter fritz
...

customers
muster hans
reuter fritz
...

others
muster hans
reuter fritz
...
*/

// ... sollte nicht im webverzeichnis liegen. besser:
// include ("c:/web/phplib/integratedtemplate.inc");
include ("integratedtemplate.inc");

$categories = array
(
"0" => "club members",
"1" => "customers",
"2" => "others"
);

$adresses = array
(
"0" => array ("name" => "muster", "vorname" => "hans"),
"1" => array ("name" => "reuter", "vorname" => "fritz"),
"2" => array ("name" => "gruner", "vorname" => "rudolf"),
"3" => array ("name" => "moser", "vorname" => "björn"),
"4" => array ("name" => "amstutz", "vorname" => "michael")
);

$tpl = new IntegratedTemplate(".");

$tpl->loadTemplatefile("main.tpl.htm", true, true);

for($i=0; $i<count($categories); $i++){

// inneren Block _vor_ dem äußeren parsen
$tpl->setCurrentBlock("block_2");

for ($j=0; $j<count($adresses); $j++) {
$tpl->setVariable( array ( "name" => $adresses[$j]["vorname"] . " " . $adresses[$j]["name"] ) );
$tpl->parseCurrentBlock();
}

// äußerer block parsen
$tpl->setCurrentBlock("block_1");
$tpl->setVariable( array ( "title" => $categories[$i] ) );
$tpl->parseCurrentBlock();
}
$tpl->show();

?>


<!------- main.tpl.htm ------->
<html>
<table border>
<!-- BEGIN block_1 -->
<tr>
<th>{title}</th>
</tr>
<tr>
<td>
<table border>
<!-- BEGIN block_2 -->
<tr>
<td>{name}</td>
</tr>
<!-- END block_2 -->
</table>
</td>
</tr>
<!-- END block_1 -->
</table>
</html>
Download

The code is distributed "AS IS" without any warranty!


Download: http://circle.ch/dl/integratedtemplate.zip (7KB)

Links

http://phplib.netuse.de - The famous PHPLib by NetUSE

published 19.10.2000

Comments (0) Permalink del.icio.us


Werbung:

Beiträge von Dritten:

Nachfolgende Titel verweisen auf von mir gelesene Weblogs.

Feeds:

Blog Content
Blog Comments

WikiAgenda:

Comments:

Good question, but...
Hi, thank you very...
Unter http://www.s...
Ich weiss mir nich...
ThanQ matthias. Th...
in case you just w...
ich liebe dir, urs...
hi there, sorry i...
Hoi Leo. I haven'...
Do you know the si...

Archives:

Blog stack:

Bill Humphries
monorom
Wendy M. Seltzer
Christian Stocker
Roger Fischer
Sandro Zic
Wez Furlong
Ben Hammersley
George Schlossnagle
Joichi Ito
Lawrence Lessig
Derek Slater
Karl-Friedrich Lenz
John Palfrey
Bernhard A.M. Seefeld
Gregor J. Rothfuss
Rainer Langenhan
Elke Engel
Sebastian Bergmann
Simon Willison
Jeremy Zwaodny
Udo Vetter
Axel A. Horns
Miguel de Icaza
Andreas Halter
Silvan Zurbrügg
Hannes Gassert
Markus Koller


$Date: 2005/11/05 11:14:30 $