imagejpeg($thumb, $newthumbname); imagedestroy($largeimage); imagedestroy($thumb); //**END OF INSERT You
imagejpeg($thumb, $newthumbname); imagedestroy($largeimage); imagedestroy($thumb); //**END OF INSERT You first give your thumbnail its own directory, and you re using the same naming scheme for simplicity s sake. You then decide to make your thumbnails equal to 10% of the size of the original pictures. By using percentages instead of hard integers, you ensure that the proportions are kept equal and no skewing of your image occurs. Of course, you can make this smaller or larger depending on your users preferences and typical file uploads. You then create the thumbnail using the 5-step process as before: 1. Create a GD-friendly image from your source. 2. Create a blank GD-friendly image, with your smaller dimensions. 3. Copy the source image into the smaller blank image. 4. Save the newly created small image in its proper directory with its proper name. 5. Destroy the temporary images. Just like before, easy as pie, right? You may notice a broken image in the screenshot above; do you know why it is broken? If you said because we uploaded that photo before we implemented the thumbnail process, then you get 100 points and you get to take a break. Not a long one, mind you, but a break nonetheless. Summary This chapter covered a lot, and has only scratched the surface on image manipulation using the PHP/GD combination. Hopefully by now, you can upload images, resize them, change their coloring, create an automatic thumbnail, create new images, and merge two images together. In this chapter, you used a form to get the image from the user. What if the user tried to upload a file that wasn t an image at all, either by mistake or out of malicious intent? In this chapter, such a file would have been caught by the code that checked for the image type. Not all forms are so straightforward to check, though. In the next chapter, you ll learn about how to check that users enter information in your form in the proper format, and how to give them appropriate feedback when they don t. Exercises 1. Create a site called a virtual vacation. Offer different backgrounds for people to superimpose photos of themselves in, and let them send virtual postcards to their friends and family. 225 Manipulating and Creating Images with PHP
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.