Upload and resize images with Zend_Form_Element_File
• Stefan Koch • English, PHP
The Zend Framework includes a class called Zend_Form_Element_File for creating a file upload within a form. It uses Zend_File_Transfer for receiving the file, but the whole process lacks some methods for the often required image upload.
What is easily possible
Let’s begin with the things that are possible. You can specify several validators for your file to check for file extensions or maximum file size. That’s stuff which is required for all files and therefore it is included. You may also specify a target directory.
Renaming a file according to your needs
Renaming a file according to your needs is also possible, even though (often) not as easily as the other stuff. You need to add a filter after initialising, because you usually do not know the filename at runtime. At least when you upload a profile picture you often want to give it a name like the username or the user’s id.
Therefore, you have to add the Rename-filter in your controller when a file is uploaded
This filter will rename the upload (only one file is allowed) according to the rule in target.
Resizing an image
The difficult part with Zend is resizing the image. Of course you can do this in your controller after you received the upload, but this is not very nice style. As Zend supports filters, we better program a new filter for this task. I called it Skoch_Filter_File_Resize:
As you might see this file also requires an adapter to ensure you can use the filter with both GD and Imagick. Thus, we need an abstract class and the implementation classes:
Using the filter
This filter can now be attached to your Zend_Form_Element_File instance and will then resize the image to produce a thumbnail:
You may specify several options invoking the filter. As you see in my code, I used with, height and keepRatio resulting in two maximum sizes. The image will then be resized so that it fits both of the lengths, but the aspect ratio will be kept. The whole list of options:
width: The maximum width of the resized image
height: The maximum height of the resized image
keepRatio: Keep the aspect ratio and do not resize to both width and height (usually expected)
keepSmaller: Do not resize if the image is already smaller than the given sizes
directory: Set a directory to store the thumbnail in. If nothing is given, the normal image will be overwritten. This will usually be used when you produce thumbnails in different sizes.
adapter: The adapter to use for resizing. You may specify a string or an instance of an adapter.
Now it’s easily possible to resize an uploaded image. To automatically load the classes, you need to add an option to your application.ini.
Often you want to create several thumbnails in different sizes. This can be done by using a so called filter chain and the directory option of the Skoch_Filter_File_Resize.
If you specify directory, the value of setDestination() will not be considered anymore. Thus, you have to pass the full path to the directory option.
If you want to use the directory option together with renaming, make sure to add the Resize-filter after the Rename-filter to ensure that Resize gets the new filename and will save the thumbnail with the new filename. Otherwise you might get this structure: