Forum Moderators: coopster
I'm currently writing a little PHP lib for image manipulation. Just the basic stuff, loading images, resizing, cropping, outputting, etc.
I'm currently stuck on how to structure/implement this bad boy though.
So far I have an Image class. This class loads an image and stores it as a GD resource. It allows you to access properties such as width, height, whether it is portrait or not, etc.
I then started adding methods to this class for outputting the image to the browser, and it occurred to me that might not be the best place to put them.
The way I see it I have three ways I can do this,
1) Place the methods within the Image class, so all I need to do is $image->outputAsJPEG();
2) Place the methods in an output class, so I would need to do something like ImageOutputter::outputAsJPEG($image);, or even instantiate the output class to set settings, etc
3) I'm going to use the Decorator pattern for the image transformations and stack them. My third option is to add the output action as part of this pattern so it just gets added to the stack.
All thoughts on this are welcome. Personally I'm stuck between 2 and 3 as the Image class with the output methods in it seems bloated and big at the moment, and just doesn't feel right.
Cheers,
Steve
[edited by: eelixduppy at 4:23 pm (utc) on Jan. 13, 2010]
[edit reason] disabled smileys [/edit]
The process I chose in the end was to create output classes that you instantiate and pass the Image object to.
This made sense and fit in with how the rest of the Image classes worked.
The reason I didn't make those methods are part of the Image class is because outputting the image is nothing to do with the image itself. The image is just an image, how it can used should be defined externally to that.
Thanks for help anyway. Here's an example of what I made (some method names are WIP),
// Load image
$image = SM_Image::createFromPath('image1.jpg');
// Add actions
$action = new SM_Image_Action_Resize($image);
$action->setTargetWidth(400);
$action->setTargetHeight(400);
$action->setResizeFit(true);
$action = new SM_Image_Action_Crop($action);
$action->setCropWidth(300);
$action->setCropHeight(300);
$action->setAnchorPoint(SM_Image_Action_Crop::ANCHOR_POINT_BOTTOM);
// Get image from action stack, aka cascade actions
$image = $action->getImage();
// Output
$output = new SM_Image_Output_Response($image);
$output->output();