views:

59

answers:

1

Hi,

I've seen and read plenty of blog posts and forum topics talking about and giving examples of Data Mapper / Model implementations in PHP, but I've not seen any that also deal with saving files/images.

I'm currently working on a Zend Framework based project and I'm doing some image manipulation in the model (which is being passed a file path), and then I'm leaving it to the mapper to save that file to the appropriate location - is this common practise?

But then, how do you deal with creating say 3 different size images from the one passed in? At the moment I have a "setImage($path_to_tmp_name)" which checks the image type, resizes and then saves back to the original filename. A call to "getImagePath()" then returns the current file path which the data mapper can use and then change with a call to "setImagePath($path)" once it's saved it to the appropriate location, say "/content/my_images". Does this sound practical to you?

Also, how would you deal with getting the URL to that image? Do you see that as being something that the model should be providing? It seems to me like that model should worry about where the images are being stored or ultimately how they're accessed through a browser and so I'm inclined to put that in the ini file and just pass the URL prefix to the view through the controller. Does that sound reasonable?

I'm using GD for image manipulation - not that that's of any relevance.

UPDATE: I've been wondering if the image resizing should be done in the model at all. The model could require that it's provided a "main" image and a "thumb" image, both of certain dimensions. I've thought about creating a "getImageSpecs()" function in the model that would return something that defines the required sizes, then a separate image manipulation class could carry out the resizing and (perhaps in the controller?) and just pass the final paths in to the model using something like "setImagePaths($images)".

Any thoughts much appreciated :)

James.

A: 

I would 1st tell you that having a mapper save the path to the file is common practice as that is exactly what a service layer (mapper in your case) should do. The best thing in your case would be to create a path for each file generated. I would suggest doing:

$path = '/path/to/root/' . substr(chunk_split(md5(<unique id for file>), 2, '/'), 0, 5);

By doing the above your path would be something like: /path/to/root/ab/23/. Since you need different sizes for each image, you now save each image with the name of the id followed by the size. ex:

$fileName = <uniquie file id from above> . '_50_100' . $ext;

All you need to do in the end is save the path in the database. Your model can then go and retrieve the path form the service layer and build the URLs you need.

MANCHUCK
Thanks very much for the response. Sorry if I'm being ignorant, but why not just use the ID as the filename? Is the chunk_spit just to aid categorisation of files?
James