Is this too cumbersome?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Is this too cumbersome?

Post by pickle »

Theory? wrote:I used those initially, but I realized that if I wanted to have a variable header, should one be needed, I can still rely on the method from the Image class to simply provide that instead of me having to do the translation later. Is there any substantial performance gains to using the IMAGETYPE constants?
The Image class can still send a string. I was referring to your switch statement.

There technically would be a performance increase, as PHP wouldn't have to parse those strings every time. However, unless you're getting millions of hits a day, the increase would be negligible. I just like using pre-defined constants wherever I can.

@~inghamn: Does your setup prevent the 404 header from being sent to the browser? If not, that could wreak havoc on bots (ie: Google, Yahoo!).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: Is this too cumbersome?

Post by inghamn »

pickle wrote: Does your setup prevent the 404 header from being sent to the browser? If not, that could wreak havoc on bots (ie: Google, Yahoo!).
Yes - unless, for some reason, the thumbnail cannot be created. Then, you really do want to send a 404.

Apache's ErrorDocument defers sending any Headers to whatever script you choose. So, it's up to your custom 404 script to send whatever headers are appropriate. I took this idea several years ago from a talk Rasmus gave.

Here's teh 404 script I use. It tries to create and save an image file, then serves the file as an image. Otherwise, it sends the 404 Headers, and renders a custom error page.

Code: Select all

 
if (preg_match('|/([a-z]+)/([0-9]+)\.[a-z]{3}$|',$_SERVER['REQUEST_URI'],$matches))
{
    $_GET['size'] = $matches[1];
    $_GET['media_id'] = $matches[2];
 
    try { $media = new Media($_GET['media_id']); }
    catch (Exception $e) { $_SESSION['errorMessages'][] = $e; }
 
    include APPLICATION_HOME.'/html/media/source.php';
}
else { $_SESSION['errorMessages'][] = new Exception('media/unknownMedia'); }
 
Header('http/1.1 404 Not Found');
Header('Status: 404 Not Found');
$template = new Template();
echo $template->render();
 
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: Is this too cumbersome?

Post by inghamn »

That /media/source.php is there to stream whatever file to the browser. It sets headers as necessary:

Code: Select all

 
$media = new Media($_GET['media_id']);
$size = isset($_GET['size']) ? $_GET['size'] : '';
 
switch($size)
{
    case 'thumbnail':
        Header('Content-type: image/gif');
        $media->output('thumbnail');
        break;
 
    default:
        Header("Content-type: image/jpg");
        $media->output('medium');
}
 
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Is this too cumbersome?

Post by Theory? »

Couldn't I technically just build something like that into the Exception model? If the thumbnail doesn't exist, it throws an exception with a custom errorID and the exception simply activates the script to build a thumbnail and then forwards them back to the script to try again?

Or would that be even more unnecessary complexity?
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: Is this too cumbersome?

Post by inghamn »

Theory? wrote: Couldn't I technically just build something like that into the Exception model? If the thumbnail doesn't exist, it throws an exception with a custom errorID and the exception simply activates the script to build a thumbnail and then forwards them back to the script to try again?
I was originally doing something like that. Doing that requires serving all your thumbnails through a PHP script. The script would open the file, and stream it to the browser.

What I ran into on my shared host was that I was spending 95% of my CPU time streaming image data in PHP. Siwtching to URLs that pointed directly to a thumbnail file (that should be there) lets Apache serve the file directly. And Apache is way more effecient at streaming data to the browser. My page load times went from taking to 6 seconds to only taking about .03 seconds. And this was on pages with only around 15 images.

I realize the dangers of early optimization, but this needed it, and it didn't seem like an optimization at all. It just felt like a better architecture begin with.
Post Reply