We used GD Library function in class methods, for detailed usage of these functions refer to GD and Image Functions. It accepts two parameters, $width and $height. Get_image_height_ratio: Calculates image height maintaining the aspect ratio. Accept two parameters, $width and $height. Get_image_width_ratio: Calculates image width maintaining the aspect ratio. Show_image: This method will only print out the image without saving new image.įill_transparent: This method will fill transparent background to given image. It accepts two parameters, $path (where we save the new image) and $quality (the quality of new image). Save_image: This method saves the image assigned to $new_image property. Then we create new image of new width and new height, We use imagecopyresampled to copy the original image to new image object. Default case is image is square then use new width and new height (No need to calculate width or height). If uploaded image height is greater than its width (Portrait image) then we keep the new height and calculate new width to maintain the aspect ratio. It checks if uploaded image width is greater than its height (Landscape image) then it keeps the new width and calculates the new height for image to maintain the aspect ratio. We use a switch statement for conditional check. It accepts three parameters, $new_width, $new_height, $cropped (if image should be cropped or not). Resize_image:The method that resizes the uploaded image and assigns it to relevant class property. We first get the width and height of uploaded image, then its extension and assign them to relevant class properties. _construct: Its the constructor method which called when the object is initialized. Other methods are private methods used as supporting methods for above mentioned public methods. The main methods used in image class are its constructor, resize_image, save_image and show_image. $new_height: Height of new/resized image.$new_width: Width of new/resized image.$new_image: The new resized image object, initially its set to original image.$image_height: Height of original image.$extension: The extension of original image.$image: Will hold the original image object.In image class we declare following properties for use in different methods: style.css: Contains all the css styles for html page and form.index.php: which will be used for html form to upload an image.image.php: An image class with different methods which will resize image and save it.To achieve this we will create following files: Save the resized image and show resized image at the end.Open an image class object and resize it to a desired size.but they should experience temporary slowness loading images rather than long waits for page loads or PHP execution time limit errors. Supports the same options for file attachment objects and any storage path. We will walk through following steps to create different sizes of uploaded image in php: Offloads thumbnail generation and image resizing to queue or image load.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |