Mapper, model and images


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.

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.


Disclaimer: I did post this on another popular programming forum but got little response so apologies if you see this post elsewhere.

When you mention image manipulation, are you meaning thumbnail generation or processing with some sort of business logic? If its the former, then I think that the presentation logic is probably best left to a view helper. I don’t think models should be aware of browsers or file locations.

For thumbnails I use a view helper in the following method

<img src=“<?=$this->thumbnail($object->objectImage,105,105,“resize”);?>” alt=“<?=$object->objectName;?>” />

$object->objectImage is the name of the original image e.g object-1.jpg The helper then sees if an existing thumbnail with the dimensions supplied exists, and if not creates the thumb in a specified directory, read from an ini file. It then returns the location to the (existing or newly created) image rather than the whole tag (something like /public/thumbs/object-1-105-105-resize.jpg). Thumbnail generation is handled by wrapped phpthumb class which is part of my own library, with the last argument changing the resize method,(default is crop) This lets me not have to worry about changes to image dimensions etc from fickle designers :slight_smile:

I am indeed referring to the creation of thumbnails. I might have say 3 or 4 different size of image that I need. If I have a large system with a lot of images, it would be quite a hit on the server to have to resize various images as people browse around the site.

Have you used this on any very large sites? Did you hit any problems?


Well the resizing/generation only happens once (if the image doesn’t exist), so after the very first page view by a user, the rest of the visitors will be presented with the pre-resized image.

I’ve used this technique on an e-learning web app accessed by large multinational corporations (15k+ users) daily, and not its not had a detrimental effect, as 99.9% of the time its going to simply check for the existence of a file and return the path. The one thing you have to remember if using something similar is to clear all thumbs relating to the image when you update the source image.

Anyway, if you’d like the source, let me know!