Join David Powers for an in-depth discussion in this video Setting up the class definition, part of Resizing and Watermarking Images with PHP.
- [Voiceover] Creating a custom class…to resize images involves a lot of code.…I've already started the class definition…in ResizeImages.php which you can find in the chapter…two 02_02 folder of the exercise files.…It begins on line two by declaring a namespace…to avoid potential clashes with classes from other sources.…I've chosen Foundationphp as the namespace…because that's the domain name of my website.…Then there's a list of protected properties.…I'll explain what most of them are for when they're used.…
Here on line eight, mimeTypes is an array…of image formats supported on the web.…The next two lines, webpSupported and useImageScale…are Boolean properties that assume the server supports…working with webp images and the image scale function.…The class constructor method detects the version…of php running on the server…and resets these to false if they're not supported.…jpegQuality is set to 75 and pngCompression is set to zero.…
These are the same as the php defaults,…but we need to specify them explicitly…
Chapter 2 pulls everything together, using a custom PHP class that can batch process multiple images, automatically detect the correct MIME type, generate custom sizes, and add an optional watermark.
- Scaling an image with imagescale()
- Resizing an image with imagecopyresampled()
- Adding watermarks
- Correcting orientation
- Batch processing images