php-general Digest 16 Oct 2011 08:45:35 -0000 Issue 7522

Topics (messages 315314 through 315318):

Image Rotation Script
        315314 by: dev.nkmo.com
        315315 by: Stuart Dallas

Re: Extending an instantiated class
        315316 by: Alain Williams
        315318 by: David Harkness

Seeking strategy/algorithm for maintaining order of records
        315317 by: Stephen

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
We have a simple script which rotates and image to a random value, saves
it to a cache directory and displays it. For some reason when I move the
script from a Debian box over to the production CentOS machine, it no
longer caches any of the images. the rest works, but not the cache. If you
could look at it and see if anything jumps out at you, please let me know.

install the code below to the directory /angles


.htaccess:
RewriteEngine on
RewriteRule ^rotate_(\d+)(?:_(?:\d+))?.png$ rotate.php?im=$1

rotate.php:
<?php
// Setup
if(isset($_GET['im']) && file_exists($_GET['im'].'.png')) {
header('Content-type: image/png');
$im = $_GET['im'].'.png';
$degrees = rand(0, 360);
$save = 'cache/'.$_GET['im'].'_'.$degrees.'.png';
if(!file_exists($save)) {
// Rotate via "command line" and cache it
exec('convert '.$im.' -filter \'Lanczos\' -resize \'150x150\' -rotate
'.$degrees.' -black-threshold 40% '.$save, $out);
}
// Output out (newly?) cached file
echo file_get_contents($save);
} else {
die("Image not found");
}
?>

Use it by url:
http://www.servername.com/angles/rotate_019.png
Each time you reload page the angle should rotate to a new position.

--- End Message ---
--- Begin Message ---
On 15 Oct 2011, at 15:50, d...@nkmo.com wrote:

> We have a simple script which rotates and image to a random value, saves
> it to a cache directory and displays it. For some reason when I move the
> script from a Debian box over to the production CentOS machine, it no
> longer caches any of the images. the rest works, but not the cache. If you
> could look at it and see if anything jumps out at you, please let me know.
> 
> install the code below to the directory /angles
> 
> 
> .htaccess:
> RewriteEngine on
> RewriteRule ^rotate_(\d+)(?:_(?:\d+))?.png$ rotate.php?im=$1
> 
> rotate.php:
> <?php
> // Setup
> if(isset($_GET['im']) && file_exists($_GET['im'].'.png')) {
> header('Content-type: image/png');
> $im = $_GET['im'].'.png';
> $degrees = rand(0, 360);
> $save = 'cache/'.$_GET['im'].'_'.$degrees.'.png';
> if(!file_exists($save)) {
> // Rotate via "command line" and cache it
> exec('convert '.$im.' -filter \'Lanczos\' -resize \'150x150\' -rotate
> '.$degrees.' -black-threshold 40% '.$save, $out);
> }
> // Output out (newly?) cached file
> echo file_get_contents($save);
> } else {
> die("Image not found");
> }
> ?>
> 
> Use it by url:
> http://www.servername.com/angles/rotate_019.png
> Each time you reload page the angle should rotate to a new position.

My first thought was that the current working directory is probably set 
differently. However, you say that the script works and presents the rotated 
images, it's just the cache that isn't right. I still think I'm probably 
correct, so try these changes...

> $im = dirname(__FILE__).'/'.$_GET['im'].'.png';


and...

> $save = dirname(__FILE__).'/cache/'.$_GET['im'].'_'.$degrees.'.png';

You also have a pretty major hole here because you're taking a querystring 
parameter and putting it straight into a command line. What happens if I pass 
the value of $_GET['im'] as "../../../../../../../../../../../etc/passwd" ? Use 
escapeshellarg when putting variables into command lines to protect against 
this type of hack.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/


--- End Message ---
--- Begin Message ---
On Sat, Oct 15, 2011 at 03:01:44PM +0100, Alain Williams wrote:
> Well, that is what I think that I need. Please let me explain what I am 
> trying to do
> and tell me how to do it or a better way of doing it.
> ......

I have solved it. The problem is basically one of ensuring that properties are 
the same
value when accessed via the parent or child class ... be that to get or set a 
value.
So I solved the problem by making the properties in the child class references 
to the
ones in the parent class.

Methods are not a problem - inheritance just works fine.

To do this, pass the instance of the parent class to the child class 
constructor and use
a reflection class to determine the properties in the parent class. This will 
not work
properly if the child class overrides a property of the parent class ... this 
does not
worry me.

class Screen {
    public $screen;
    ....
}

class Form extends Screen {
    function __construct(&$s) {
      $sr = new ReflectionClass($s);

      foreach($sr->getProperties(ReflectionProperty::IS_PUBLIC | 
ReflectionProperty::IS_PROTECTED) as $p)
        $this->{$p->name} = &$s->{$p->name};
    }
}

$s = new Screen(....);
$s->AddJsVar('Date', '15 Oct 2011');

$f1 = new Form($s);

$f1->screen = 'something'; // this assigns the property in $s.


Comments ?

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT 
Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: 
http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h>

--- End Message ---
--- Begin Message ---
On Sat, Oct 15, 2011 at 7:01 AM, Alain Williams <a...@phcomp.co.uk> wrote:

> I have an application where a Screen (web page) may contain several Forms.
> The Forms
> will want to access properties, etc, from their Screen. So what I want is
> to do
> something like:
>

You're using an is-a relationship between Form and Screen, but I think has-a
would be better since a Screen has one or more Forms. Keep the $screen
property in Form, but break the inheritance relationship. Define methods in
Form to allow you to set properties on its containing Screen.

    class Form
    {
        private $screen;

        public function __construct(Screen $screen) {
            $this->screen = $screen;
        }

        public function AddJsVar($name, $value) {
            $this->screen->AddJsVar($name, $value);
        }

        public function GetJsVar($name) {
            return $this->screen->GetJsVar($name);
        }
    }

Now adding a var to a Form passes it on to its owning Screen. It's also a
lot simpler than using reflection.

Peace,
David

--- End Message ---
--- Begin Message ---
I am building a site for my photography.

The photographs are displayed by category.

The category table has a field for "order"

In my control panel I want to be able to change the order of the categories by changing the values in the category field.

I can dynamically build a form of all categories and have a field for new order number, Then loop through the POST and update the values.

The field is not unique, so I am not worried about DB errors.

But I wonder if there is a better way.

Thoughts please?

Thank you
Stephen

--- End Message ---

Reply via email to