Re: [PHP] How can I call GD's imagepng() directly from my class?

2010-12-07 Thread Richard Quadling
On 7 December 2010 07:19, Nathan Nobbe  wrote:
> On Mon, Dec 6, 2010 at 5:26 PM, Daevid Vincent  wrote:
>
>>
>> I got a little 'hack' further, but not loving it. Maybe I'll move the image
>> to a $_SESSION variable and then have the "gdtest.php" pull it and echo it
>> that way
>>
>
> well i think you may be in the right direction, however, id be interested to
> see what others on the list think about this particular approach.
>  personally, i wouldnt store an image in the session.. but i would certainly
> provision a temporary directory inside the webroot where you could put these
> images.
>
> perhaps if the images are tied to sessions, throw cleanup in a custom
> session cleanup cron, or queue the images for deletion when the user logs
> out.  seems to me like the session is an area of abuse in many php apps ive
> seen.  even though the standard store is file based, i would still recommend
> a separate store for images.
>
>
>>
>>        public function render_image()
>>        {
>>                $my_img = imagecreate( 200, 80 );
>>                $background = imagecolorallocate( $my_img, 0, 0, 255 );
>>                $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
>>                $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
>>                imagestring( $my_img, 4, 30, 25, "Test Image", $text_colour
>> );
>>                imagesetthickness ( $my_img, 5 );
>>                imageline( $my_img, 30, 45, 165, 45, $line_colour );
>>
>>                 ob_start();
>>                 header( "Content-type: image/png" );
>>                header('Content-Length: ' . strlen($my_img));
>>                imagepng( $my_img );
>>                 $final_image_data = ob_get_contents();
>>                ob_end_clean();
>>
>>                imagecolordeallocate( $line_color );
>>                imagecolordeallocate( $text_color );
>>                imagecolordeallocate( $background );
>>                imagedestroy( $my_img );
>>
>>                 echo
>> 'data:image/png;base64,'.base64_encode($final_image_data);
>>        }
>>
>> 
>>
>
> i dunno, why not just have something a bit simpler, where you generate an
> image and store it to disk.  the function could return the path where the
> image was saved.  then when the browser loads that resource php doesnt even
> fire up, the webserver just sends back the image directly.
>
> not sure on whether you would pass in the location or if you would have
> internal logic cook up the path, but im sure you could determine that based
> on the rest of the code .. lets suppose your class computes it internally,
>
> something like
>
>  class LOPA
> {
>  private function get_temp_path()
>  {
>     // some session specific logic to cook up a temporary path
>     // something relative to the web root ...
>  }
>
>  public function render_image()
>  {
>    $my_img = imagecreate( 200, 80 );
>
>    $background = imagecolorallocate( $my_img, 0, 0, 255 );
>    $text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
>    $line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
>
>    imagestring( $my_img, 4, 30, 25, "Test Image", $text_colour);
>    imagesetthickness ( $my_img, 5 );
>    imageline( $my_img, 30, 45, 165, 45, $line_colour );
>
>    // now saving to a temp location inside the webroot
>    $temp_path = $this->get_temp_path();
>    imagepng( $my_img, $temp_path );
>
>    imagecolordeallocate( $line_color );
>    imagecolordeallocate( $text_color );
>    imagecolordeallocate( $background );
>    imagedestroy( $my_img );
>
>    // returning path to temp location
>    return $temp_path;
>  }
> }
> ?>
>
> 
> 
>
> -nathan
>
I had a similar issue in generating a badge based upon various stats.

I found that there were many individuals who shared the same stats, so
the badge was the same. Even more advanced users often shared stats.

So, rather than creating an image per individual, I based the name of
the image on $md5BadgeData = md5(serialize($badgeData)).

So, main page loads and gathers the stats for person.

If the badge doesn't exist, then creates the PNG file.

Write out the HTML using 

If a user's stats change, they'll have a new badge. Sure, there will
be orphaned badges, but they are tiny little things and I don't have
gazillion users.




-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] new keyword combined with other things...

2010-12-07 Thread Alexandru Patranescu
In many other languages this will work:

*$result = new Object() -> method();*

But in php, it fails at parsing.
I've tried with parenthesis around new but nothing. Anyhow, as I saw later,
*new* operator has precedence over others so this couldn't be a solution.
I know a static function can be defined and the above statement can
transform into

*$result = Object::getNewInstance() -> method()*

but is there any way to write it directly? and if not, why isn't this
implemented yet and when will it be?

best regards,
Patranescu Alexandru


Re: [PHP] new keyword combined with other things...

2010-12-07 Thread Daniel P. Brown
On Tue, Dec 7, 2010 at 10:40, Alexandru Patranescu  wrote:
>
> but is there any way to write it directly? and if not, why isn't this
> implemented yet and when will it be?

That kind of chaining has not yet been implemented in PHP --- but
it's being discussed and voted now, as a matter of fact.  There's no
guarantee if or when it will be implemented.  It's looking positive
that it will, but whether it will be soon or not, it's hard to tell
right now.

-- 

Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] new keyword combined with other things...

2010-12-07 Thread Gerardo Benitez
Try with



$result = Object::method();

this is a static method in a class.

the operator -> is access to a member in an object.

Gerardo.





On Tue, Dec 7, 2010 at 12:40 PM, Alexandru Patranescu wrote:

> In many other languages this will work:
>
> *$result = new Object() -> method();*
>
> But in php, it fails at parsing.
> I've tried with parenthesis around new but nothing. Anyhow, as I saw later,
> *new* operator has precedence over others so this couldn't be a solution.
> I know a static function can be defined and the above statement can
> transform into
>
> *$result = Object::getNewInstance() -> method()*
>
> but is there any way to write it directly? and if not, why isn't this
> implemented yet and when will it be?
>
> best regards,
> Patranescu Alexandru
>



-- 
Gerardo Benitez
-
www.webseficientes.com.ar
Diseño web, programación, Seo


Re: [PHP] new keyword combined with other things...

2010-12-07 Thread Jim Lucas
On 12/7/2010 7:40 AM, Alexandru Patranescu wrote:
> In many other languages this will work:
> 
> *$result = new Object() -> method();*
> 
> But in php, it fails at parsing.
> I've tried with parenthesis around new but nothing. Anyhow, as I saw later,
> *new* operator has precedence over others so this couldn't be a solution.
> I know a static function can be defined and the above statement can
> transform into
> 
> *$result = Object::getNewInstance() -> method()*
> 
> but is there any way to write it directly? and if not, why isn't this
> implemented yet and when will it be?
> 
> best regards,
> Patranescu Alexandru
> 


Here is what I do if I want to make it a one liner...

PrintString();

echo myNew('CustomClass')->ReturnString('Something Else');

$var = myNew('CustomClass')->ReturnString('And again...');

echo $var;

?>

I also use the following if I want to use the Singleton method of getting my 
data.

PrintString();

echo myClass::run()->ReturnString();

$var = myClass::run()->ReturnString();

echo $var;

?>

YMMV

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] new keyword combined with other things...

2010-12-07 Thread Alexandru Patranescu
I know how to do it in other ways.
I was just wondering why the simple new Object() -> method won't work. new
operator has precedence over...
That must be the problem.  -> is not an operator. Is not in this list:
http://php.net/manual/en/language.operators.precedence.php
That must be done. -> should be an operator!

Alex



On Tue, Dec 7, 2010 at 10:49 PM, Jim Lucas  wrote:

> On 12/7/2010 7:40 AM, Alexandru Patranescu wrote:
> > In many other languages this will work:
> >
> > *$result = new Object() -> method();*
> >
> > But in php, it fails at parsing.
> > I've tried with parenthesis around new but nothing. Anyhow, as I saw
> later,
> > *new* operator has precedence over others so this couldn't be a solution.
> > I know a static function can be defined and the above statement can
> > transform into
> >
> > *$result = Object::getNewInstance() -> method()*
> >
> > but is there any way to write it directly? and if not, why isn't this
> > implemented yet and when will it be?
> >
> > best regards,
> > Patranescu Alexandru
> >
>
>
> Here is what I do if I want to make it a one liner...
>
> 
> function myNew($obj='stdClass') {
>return new $obj();
> }
>
> class CustomClass {
>function PrintString($str='Something')
>{
>print($str);
>}
>function ReturnString($str='Something')
>{
>return $str;
>}
> }
>
>
> myNew('CustomClass')->PrintString();
>
> echo myNew('CustomClass')->ReturnString('Something Else');
>
> $var = myNew('CustomClass')->ReturnString('And again...');
>
> echo $var;
>
> ?>
>
> I also use the following if I want to use the Singleton method of getting
> my data.
>
> 
> class myClass {
>static $_instance;
>static function run($DefaultValues=null)
>{
>if(self::$_instance === null)
>{
>//First and only construction.
>self::$_instance = new self($DefaultValues);
>}
>return self::$_instance;
>}
>function PrintString($str='Default Value')
>{
>print $str;
>}
>function ReturnString($str='Something')
>{
>return $str;
>}
> }
>
> myClass::run()->PrintString();
>
> echo myClass::run()->ReturnString();
>
> $var = myClass::run()->ReturnString();
>
> echo $var;
>
> ?>
>
> YMMV
>


Re: [PHP] new keyword combined with other things...

2010-12-07 Thread Paul M Foster
On Tue, Dec 07, 2010 at 11:01:09PM +0200, Alexandru Patranescu wrote:

> I know how to do it in other ways.
> I was just wondering why the simple new Object() -> method won't work. new
> operator has precedence over...
> That must be the problem.  -> is not an operator. Is not in this list:
> http://php.net/manual/en/language.operators.precedence.php
> That must be done. -> should be an operator!

I haven't followed this thread closely, but I see code like this in
CodeIgniter a lot. I don't code like this, but it appears that they only
write this kind of code using already instantiated objects. Here is an
example from their user manual:

$this->db->select('title')->from('mytable')->where('id', $id)->limit(10,
20);

What kind of internal magic they use to make this work, I don't know. I
haven't examined their internals.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] new keyword combined with other things...

2010-12-07 Thread David Harkness
On Tue, Dec 7, 2010 at 1:55 PM, Paul M Foster wrote:

> Here is an example from their [CodeIgniter] user manual:
>
> $this->db->select('title')->from('mytable')->where('id', $id)->limit(10,
> 20);
>

This is known as a "fluent interface" because it attempts to make your code
read more naturally--i.e. fluently. Each method returns either a new object
or the same object ($this) so it can flow into the next method call. In the
latter case, the initial object stored in $this->db implements all the above
methods. I first saw this in mock object libraries for Java, but it was
invented years earlier.

$mock = $this->getMock('Calculator');
$mock->expect('add')->once()->with(3, 5)->willReturn(8);

David


[PHP] newbie basic realm protection - why don't the input usr/pass stick?

2010-12-07 Thread Govinda

Hi everyone

I am hacking my way through something unrelated to this post.. but  
needed to stop and (real quick) pass-protect a page to use it to run  
some quick (*admin-only*) scripts on a shared host.
..and I see now how poor is my understanding of what seems like basic  
stuff.  As a start for my quick understanding to pass protect a page,  
I used the "Example #6 Basic HTTP Authentication example", from here:

http://www.php.net/manual/en/features.http-auth.php
..which is just this:

Hello {$_SERVER['PHP_AUTH_USER']}.";
echo "You entered {$_SERVER['PHP_AUTH_PW']} as your password.p>";

}
?>

..and no matter what i type in the authentication dialogue that pops  
up.. then after I submit it.. it just keeps looping forever popping up  
the dialogue..  I.e. I never get past the authenticate dialogue to see  
this line executed:

echo "Hello {$_SERVER['PHP_AUTH_USER']}.";

What am I missing?

Thanks for your bothering to help with this,


Govinda


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] PHP4 to PHP5 migration with E_STRICT

2010-12-07 Thread Tom Robinson
Hi,

I'm migrating a web application written for PHP4 to PHP5. I've turned on
E_STRICT to have a detailed look at all the code thoroughly. I have a
number of 'Notices' and 'Strict Standards' messages appearing now.

I don't consider myself a PHP guru by any means so I'm seeking help with
understanding these messages and how to fix the code.

One of the messages is:

"PHP Strict Standards:  Only variables should be assigned by reference in"

This is for a class SPControlPanel with a method

function getContentTypes(&$db)
{
$tabledata = array();

$sql = "select
contenttype.*
from
contenttype";

return $db->queryAll($sql, true);
}

The warning is for this segment of code below which is another method in
the same class (marked with comment // this line):

function getCategoryTypes(&$db) {
$tabledata = array();

$myContentTypes = &SPControlPanel::getContentTypes($db); // this
line
foreach ($myContentTypes as $key => $data) {
if ($data['iscategory']) $tabledata[] = $data['contenttype'];
}

return $tabledata;
}

There are many more methods making assignments in a similar way in this
class and in other classes throughout the code.

I'm not sure of the best way to re-code this to resolve the E_STRICT
warning.

Any help is much appreciated.

Regards,

Tom


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP4 to PHP5 migration with E_STRICT

2010-12-07 Thread David Harkness
In getCategoryTypes() you're assigning a reference to the return value of
getContentTypes(), and PHP doesn't like that. You can return a reference to
a variable from a function, but taking the reference of a *value* is
meaningless since you can only create references to variables. Just remove
the & on that marked line to get rid of the warning.

Also, I notice that both of those methods (and I assume many more) take a
reference in $db. Note that all objects are assigned by reference in PHP 5,
and thus the & here is unnecessary since you are passing an object to these
methods and aren't changing the reference inside the method. Note that PHP
won't issue a warning for these since it is acceptable and you might want to
point the reference to a new object inside the function. You aren't doing
that here, so the & is superfluous and misleading.

David


[PHP] No errors gets displayed, just a blank page

2010-12-07 Thread Rico Secada
Hi.

What can cause that no parse error gets displayed (blank page/no output
at all) even though error reporting is set to "-1"? 

I have run the script through php lint on the console and it comes up
with no errors.

I have run into this problem the last couple of days making debugging a
nightmare.

Anyone with experience in this behavior?

Thanks and best regards

Rico

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP4 to PHP5 migration with E_STRICT

2010-12-07 Thread Tom Robinson
Thanks David.

If my understanding is correct, then:

SPControlPanel::getContentTypes($db);

is a reference to a static instantiation of the class. If so, then it
must be syntactically something like when using 'new' (which returns a
reference) so there's no need to apply the & operator. Am I on the right
track?

I have some more PHP4 code in the application which caused: "PHP
Notice:  Only variable references should be returned by reference". I
have fixed it like this:

<   function & _getTable($cached=true)
<   {
<   return new TableClass($this->_getTableName());
---
>   function & _getTable($cached=true) {
>   $temp = new TableClass($this->_getTableName());
>   return $temp;

Is this acceptable?

I'll have to come back to the $db issue - it's not issuing a warning so
I'll leave it alone until I've tidied up all the other issues.

Regards,

Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] No errors gets displayed, just a blank page

2010-12-07 Thread Kranthi Krishna
wats the setting of display_errors php.net/display_errors ?

if you are not getting any output it might be because of a simple
parse error (mismatched brackets, misplaced semicolon etc) or an
exit/die command

Kranthi.
http://goo.gl/e6t3

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] newbie basic realm protection - why don't the input usr/pass stick?

2010-12-07 Thread Kranthi Krishna
you script looks (and works) fine. so i dont think the problem is in your script

I found firebug/live http headers firefox addons to be helpful in this situation
see if your client is actually sending "Authorization   Basic" header

Kranthi.
http://goo.gl/e6t3

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php