These days everyone wants to upload their images to a web application. Whether it’s a free public site like Photobucket, or an eCommerce Web Site like Shutterstock or Flickr, the concepts are the same. The intent is to provide a system for web users with no web programming skills, which lets them upload a single file, and do nothing else. The result is the automated manipulation of image uploads into thumbnails, various intermediate sizes, perhaps retaining the original image upload, and tracking all components with a MySQL Database.
The first step is to enable the user for image uploads. Here is more information on Modifying Image Uploads With PHP Programming and Using mime_content_type() In PHP Programming as supplemental reading. Beware the caveats of allowing file uploads, as the first step of uploading a file is a generic file upload. You must assume that the web user could be a hacker uploading a virus or other malicious program. In the scope of image uploads, your user may be uploading pornographic images or images with other potentially offensive content, depending on your target audience. A medical or other strict professional business would not appreciate images appearing in their site with defaming themes.
So, your file upload has been cleansed and validated, and you are ready to move forward. You should determine the MIME-Type of the image file to know which commands to use. JPEG, GIF, PNG, and BMP, etc, require slightly different flavors of command structure. For simplicity, we’ll handle a JPEG file that has rather large dimensions. You must understand the difference between Image Dimensions and Image Resolution, and that PHP will interpret a high resolution image at 72 dpi, therefore making the original dimensions effectively larger than one might expect. Please read more about Defining Image Resolution And Image Dimension if necessary, or contact The PHP Kemist for more information.
Evaluate the uploaded image for height and width values, and calculate the Aspect Ratio. The aspect ratio the calculation of the width divided by the height, carried to some reasonable number of significant figures. Width and height values applied to the image must obviously be round numbers, not fractions, so use number_format and/or ceil or round on your resulting values. The aspect ratio of your image will be used to determine the new width and height for images smaller than the original upload. You will define a set width or height, then use the aspect ratio to calculate the second dimension, to retain the original aspect ratio for our reduced image. Aspect ratio would not be used if we were cropping our image, but that’s another discussion.
Our PHP Program must first create a palette and file to start manipulating the uploaded image. We can open a new image file using imagecreatefromjpeg, since we’re using JPEG as our example upload. If the upload were detected to be a GIF, we’d use imagecreatefromgif and so on. Once the image file has been created in the buffer with our uploaded image data, we are free to manipulate the image however we want. Making a JPEG larger is obviously a bad idea, as we would increase the compression artifacts and decrease the image quality. A PHP Programmer must observe standard graphic design and image manipulation caveats when using PHP Programs to manipulate an image. The results can be equally as devastating when misusing image manipulation code as they can be fantastic when using them wisely.
Using your preference of PHP image reduction language options, the PHP Program will require you to define the new width and height values. Most likely, your PHP Website will be displaying a predefined image size, so we know the width and/or height. Since the uploaded image may be a standard 2:3 or 3:4 aspect ration, we cannot assume a one size fits all approach. Some people crop their own images without using standard aspect ratio and may upload a very wide or tall image, which can cause deformation is we make invalid aspect ratio assumptions.
It is typical to allow a range of aspect ratios once we have determined the orientation as either landscape or portrait. Portrait images simply have the inverse aspect ratio of landscape images, so the calculations are almost the same. Don’t assume the user will upload only landscape images, and be prepared to error handle images that don’t meet your requirements. Inform the user with sufficient information to know if the image was too small, didn’t meet landscape/portrait requirements, was an incorrect image format, was not an image at all, and was not within tolerable aspect ratio ranges.
PHP Programs store the image data and the manipulated image file in the buffer, so you must receive the image, verify the file, retrieve specifications from the image, manipulate the image, and export the data either to a file on the system, into a database as raw code, and/or push the image data straight out to the browser, deending on your PHP Program definitions… all before the script ends. If your image has not been saved or served before the script ends, the PHP Server will automatically cleanse the buffer of your image data by default when the script ends.
Saving image data directly into a database is a waste of your database. Saving your image data to the file system is most common, and the data can be saved to a secure location to avoid prying eyes that are not validated by the PHP Program. You can set the quality of compression on the resulting JPEG files as well as the names and locations. Don’t spend to much time with color palette handling unless it’s really mission critical or you are handling transparent GIF or GIF89a file types.
You can overlay another image on top of your uploaded image to watermark your resulting images. If you need t tag images with your company logo and web address, like a dog peeing on a tree (peemarking), there are numerous methods that result in images that carry your signature if it is parted from your website.
Multiple image uploads can be managed by PHP simultaneously, which can save headaches for your users. In the front-end form for the user, create more than one file upload field and name each one incrementally, but differently. You can use array tags for field names if you want to iterate the upload name structures and check for a valid file upload for each field name. Inside the iteration, you will need to check for a valid file upload and carry the field name through that iteration. The field name associted with the image file upload will likely have significance for defining the resulting file names and what you put into your MySQL Database for tracking the images in the file system.
Tracking image file uploads using PHP Programming can be done using user names, timestamps, iteration values, and other repeating and non-repeating values, with unique strings. The essence of successful automation of image file uploading and processing is in the error handling and file validation code. If your goal is clearly defined and your website definitions are clear for image display requirements, you will have a nice path to defining your PHP Program for Image File Uploads and Automated Image Manipulation. In association with user login and session tracking, you can associate your image uploads and resulting image files with each unique user and specific sections of your website.