Jump to content

Dynamic images and Headers Help


MissionSix

Recommended Posts

Some background notes:

 

So i'm building an OO based media management system for my website and i'm having trouble with the following...

 

example url: /media/image/3252/

 

My mediaHandler can recognize what type of file needs, via the URL, to be downloaded or send, such as an image object for example, or a .zip file. The ID is sent read from the URL as well so it knows where to look in the DB.

 

The mediaHandler then grabs the file and returns and appropriate fileObject. It also checks the referrer (to eliminate leechers) and updates the download count / hit count in the db.

 

In the example URL i'm trying to get an image, so an imageObject would be returned.

 

The image Object has a filepath, filename, width and height, among other things.

 

My mediaHandler also can take a width or a height as an argument and, using the gd library, create a resize & logo branded output image.

 

An image object (which is a child of a file object), has the contents of the file as one of its properties**.

 

If an image is not to be resized, the handler is supposed to just pass the image (i think via headers) to the browser. // This is where i need help, i can't figure this out....

 

If theres a manipulation to be made, the image needs to be manipulated, cached and stored cache in the db, and then sent to the output to the browser.

 

 

**don't know if this is useful but i thought it might be.

 

The question...

I would like to know if there is a way to "pass the file" through my media handler without using GD to create a blank image and using up more memory it should be.

 

I know theres a way with headers or something but I'm struggling to find an answer...

Link to comment
Share on other sites

Use the function file_exists.

 

What you do is use the same technique as you are doing, except save the image to a specific directory, and use something like

 

$filename = md5($params) . '/' . $imagemime;

 

to generate the code for your filename.  Somewhere at the top of the script you create a string that contains the parameters for the picture, and also what type of image it is (png etc... but realistically this can generally be hardwired for your purposes.

 

Then you check for the existence of the file, and if it does exist, just open the file and return it, using the appropriate headers, otherwise, return the constructed image.

 

When you construct the image you make sure to save it.

 

To save a little bit more CPU you could, instead of returning the image itself each time, return the image location and put a <?php include("myimagegenerator.php") ?> in the src of the image, and then have this script echo back the location of the newly generated image.  This way, you won't even need to open up the image with php, and you can get rid of any fussy headers at the beginning of your output.

 

I've done this type of thing many, many times before.

 

Dave

Link to comment
Share on other sites

So when i point my browser to:

 

/media/image/30/  for example,

 

my media handler will take "30" as the imageid, and query the db for an image from the images table.  it will grab the path from there.  So then my mediahandler returns an image object.  this object reads the mime type of the file, and sends that header to the browser.

 

from here it will check to see if using a thumb (a thumb would be if the width or height are smaller than the origional width / height) is required, for example:  /media/image/30/?w=120

 

the mediaHandler class would then read the width, $w and check to see if  a thumbnail exists on the disk for that width  (named something like:  th.120.0.filename.jpg )**.  If it does, it will read the path of the thumbnail and "send the file to the browser".

 

if a thumb check fails for the width, then it will require the gd library to resize the image and output that to the browser.  It will also then store the thumb on the disk and time stamp it in the db as well.

 

a cron job will cleanup all thumbnails after x time expires, and this would probably run nightly.

 

 

 

The problem I am having trouble with at the moment is redirecting the script to the file directly without giving away the path of the file.    If someone could help with an example that'd be great.

 

 

 

 

notes >>

* I would do something like this:

 

$pieces = explode(".", "th.120.0.filename.jpg");

//  which ->

$width = $pieces[1];
$height = $pieces[2];
$ext = $pieces[4];

// or reverse::

if(file_exists("th.". $width .".". $height .".". $filename .".". $ext) { }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.