
Resize Image to Fixed Height while Maintaining Aspect Ratio Pass the specified new width, calculated new height as well as the old image data to the resize_image() function you created in the previous section. This ratio is then multiplied by original image height to get the new height. For example, if the original image was 2880*1800 in size and you want the new image to be 576px wide, the resize ratio would be 576/2880 = 0.2. This ratio needs to be maintained when calculating the new height of the image. It calculates the resize ratio based on the new width and original width. The resize_to_width() function takes the new width and the original image with its width and height as parameters. $image_width_fixed = resize_image_to_width(560, $old_image, $width, $height) Return resize_image($new_width, $new_height, $image, $width, $height) function resize_to_width($new_width, $image, $width, $height)

If you want images to have a specific width while preserving the aspect ratio of the original image, you should first get the height of the image based on given width and aspect ratio. The height is calculated automatically in order to maintain the aspect ratio. This means that if you use the above resize_image() function to resize them, some of them will look distorted. The images that you are planning to resize can have different aspect ratios. Resize Image to Fixed Width while Maintaining Aspect Ratio The values passed to the resize_image() function are the ones that you obtained in the previous section while loading the old image. The resize_image() function returns a new image resource identifier which contains the resized image. Since you want to copy and resize the whole image, the x and y coordinates for the source and destination have been set to zero. $new_image = resize_image(280, 180, $old_image, $width, $height) Imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height) $new_image = imagecreatetruecolor($new_width, $new_height) The syntax of this function looks like this: bool imagecopyresampled (resource $dst_image, resource $src_image, int $dst_x, int $dst_y, int $src_x, int $src_y, int $dst_w, int $dst_h, int $src_w, int $src_h)Įxample: function resize_image($new_width, $new_height, $image, $width, $height) The ninth and tenth parameters determine the width and height of the new image where you are copying the old image. The seventh and eighth parameters determine the width and height of the original image block that you want to copy to the new image. The fifth and sixth parameters determine the x and y coordinates of the source image from where you want to start copying the original image data. The third and fourth parameters specify the x and y coordinates for the destination image where you want to start copying the image. The first two parameters identify the new and old image resource respectively. The imagecopyresampled() function accepts 10 different parameters. The image created by this function will be all black so use the imagecopyresampled() function to copy and resize the original image over the new black one you just created. The imagecreatetruecolor() function creates a new true color image with given width and height. Resize Image to Fixed Width and HeightĪfter you have the image resource identifier, you can use other GD functions to resize the image. It applies an appropriate imagecreatefrom*() function based on the image type and returns the image resource identifier in the end. The load_image() function accepts two parameters, the path or URL of the image file and the image type. The list() function is used to assign values to multiple variables at once. The $filename variable can contain either the path of the image or its URL. $old_image = load_image($filename, $type)

List($width, $height, $type) = getimagesize($filename) Use the value of this returned constant to determine the type of image and appropriate function to use.

The IMAGETYPE_XXX constants are stored at index 2 of the returned array.

The width and height of the image are stored at index 0 and 1 respectively. This function returns an array with up to 7 elements. The function you use will depend on the type of image you are resizing.įirst, use the getimagesize() function to get important information about the image like its width, height and type. Loading the image will require to use different functions like The first step before you resize an image is loading it as an image resource inside the script. GD has all the necessary functions to manipulate images so you won't have to use any other 3rd party library. You can make use of the PHP's GD library to resize images in PHP. Maybe you need to resize images that your users are uploading. Maybe you have to create multiple sizes of the same image to serve on different devices. Many times, you have to programmatically resize images.
