php-general Digest 16 Mar 2008 11:22:27 -0000 Issue 5351

Topics (messages 271650 through 271655):

GD / Pixel Font Rendering
        271650 by: nihilism machine
        271651 by: nihilism machine

Re: Objects as array key names??
        271652 by: Jeremy Mcentire
        271653 by: Robert Cummings
        271655 by: Richard Heyes

DOM - Question about \0
        271654 by: dav

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message --- I am trying to render an 8 pixel pixel font without anti aliasing to look crisp (silkscreen) in 8pt with gd. the font is huge and ugly:

<?php
// Set the content-type
header("Content-type: image/png");

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'silkscreen.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>


-- any ideas?

--- End Message ---
--- Begin Message --- I am trying to render an 8 pixel pixel font without anti aliasing to look crisp (silkscreen) in 8pt with gd. the font is huge and ugly:

<?php
// Set the content-type
header("Content-type: image/png");

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'silkscreen.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>


-- any ideas?

--- End Message ---
--- Begin Message ---
On Mar 15, 2008, at 7:16 PM, Ray Hauge wrote:
Jim Lucas wrote:
It has been brought to my attention that with Perl and Ruby, you can use Objects as the value of the key within an array. The examples that were shown to me make me think that this would be an awesome ability to have within PHP. $res = query('Select c_id, c_first_name, c_last_name FROM customers');
$customers = array();
while ( $row = fetch_assoc($res) ) {
   $customers[$row]['contact_locations'] = array();
   $SQL = "SELECT *
       FROM contact_locations
       WHERE c_id={$row['c_id']}";
   $loc_res = query($SQL);
   while ( $loc_row = fetch_assoc($loc_res) ) {
       $customers[$row]['contact_locations'][] = $loc_row;
   }
}
Now, contained within one array "$customers" I have all the information that would be needed for displaying any type of information related to a customer or a customers location.


You may have meant the while loop to iterate over $row = fetch_object($res) ...

// START CODE //
$cid = null;
$customers = array();

while ( $row = fetch_assoc($res) ) {

   $cid = $row['cid'];
   $customers[$cid] = $row;
   $customers[$cid]['contact_locations'] = array();

   $SQL = "SELECT *
       FROM contact_locations
       WHERE c_id={$cid}";

   $loc_res = query($SQL);

   while ( $loc_row = fetch_assoc($loc_res) ) {
       $customers[$cid]['contact_locations'][] = $loc_row;
   }

}
// END CODE //



You could also create a data object associated with customer that makes use of overloading functions -- or whatever you kids are calling them these days to allow:

$customer = new Customer(78); // Load customer with ID 78.
$customer->loadLocations(); // Loads related locations.
$customer->location[n]; // Returns nth location.
$customer->location('home'); // Returns the location dubbed "home."
// You can have location loadLocations() if they're not already set.

The data object would have to make use of __get, __set, and __call.

Hereby, the locations aren't indexed by the object, but are elements in a member array.
--- End Message ---
--- Begin Message ---
On Sat, 2008-03-15 at 15:02 -0700, Jim Lucas wrote:
> This question should probably be directed at the PHP Internals list, but 
> I thought I would start by asking here first if anybody would even use 
> this feature.
> 
> It has been brought to my attention that with Perl and Ruby, you can use 
> Objects as the value of the key within an array.  The examples that were 
> shown to me make me think that this would be an awesome ability to have 
> within PHP.
> 
> Here is an example of how I would use such a feature.
> 
> I have a table of "customers".
> I have a table customers "contact_locations".
>       This table has a reference ID back to the "customers" table.
> 
> Now, in my array of information that I build I have this.
> 
> $res = query('Select c_id, c_first_name, c_last_name FROM customers');
> 
> $customers = array();
> while ( $row = fetch_assoc($res) ) {
>       $customers[$row]['contact_locations'] = array();
>       $SQL = "SELECT *
>               FROM contact_locations
>               WHERE c_id={$row['c_id']}";
>       $loc_res = query($SQL);
>       while ( $loc_row = fetch_assoc($loc_res) ) {
>               $customers[$row]['contact_locations'][] = $loc_row;
>       }
> }
> 
> 
> Now, contained within one array "$customers" I have all the information 
> that would be needed for displaying any type of information related to a 
> customer or a customers location.

Specifically for the example above, I'd only use 2 queries to link up
the contact locations to the customer:

<?php

$sql = 
    'SELECT '
   .'    c_id, '
   .'    c_first_name, '
   .'    c_last_name '
   .'FROM '
   .'    customers ';

$customers = array();
if( ($res = some_db_query( $sql )) )
{
    while( ($row = some_db_fetch_assoc( $res )) )
    {
        $row['contact_locations'] = array();
        $customers[$row['c_id']] = $row;
    }
}

$customer_ids = implode( ',', array_keys( $customers ) );

$sql =
    'SELECT '
   .'    * '
   .'FROM '
   .'    contact_locations '
   .'WHERE '
   .'    c_id IN ( '.$customer_ids.' ) ';

if( ($res = some_db_query( $sql )) )
{
    while( ($row = some_db_fetch_assoc( $res )) )
    {
        $customers[$row['c_id']]['contact_locations'][] = $row;
    }
}

?>

Imagine 200 customers in your example... your example will hit the DB
201 times. The above hits the DB twice.

> By doing having this feature, I could build the ability to do xPath 
> searches within the base array.  That would be nice.
> 
> Anyways, what do you all think?  Worth it or not?

I don't see what added advantage you get by having an object or array()
as a key. Why can't you have the contact_locations as a key in the $row?

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


--- End Message ---
--- Begin Message ---
Hi,

Going off the subject alone, you might want to investigate the __tostring() magic method that you can define to handle objects being cast to a string. Also, there's a whole bunch of magic methods that you can use to handle PHP operations on user defined objects:

http://uk.php.net/manual/en/language.oop5.magic.php

--
Richard Heyes
Employ me:
http://www.phpguru.org/cv

--- End Message ---
--- Begin Message ---
Hi,

I have question about \0 character with DOM :

<?php
$cdata = 'foo' . "\0" . 'bar';

$dom = new DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;

$container = $dom->createElement('root');
        $blob = $dom->createElement('blob');
        
                $blob->appendChild($dom->createCDATASection($cdata));

        $container->appendChild($blob);
$dom->appendChild($container);

echo '<pre>' . htmlentities($dom->saveXML());

/*
Result :

<?xml version="1.0" encoding="utf-8"?>
<root>
  <blob><![CDATA[foo]]></blob>
</root>
*/
?>

        
What to do with the character \0 ? encode this character to obtain : 
<![CDATA[foo&00;bar]]> ? or skip the character with str_replace("\0", '', 
$cdata)  ?

What is the best thing to do ? i like to conserve the \0 because is a blob data
        
Jabber is how to transmit binary ?


Sorry for by bad english.


Thank you.

----------------------------------------------------------------------
Free pop3 email with a spam filter.
http://www.bluebottle.com/tag/5


--- End Message ---

Reply via email to