If you are working on a website or project which deals with images, there will most probably come a time when you will have to programmatically resize those images. Maybe you have to create three different sizes of the same image to serve on different devices. Maybe you need to resize images that your users are uploading. Loading a 1920*1080 image when 96*54 image will do is just a waste of resources. In this article, we will write
different functions that will make use of the PHP’s GD library to resize images in PHP. GD has all the necessary functions to manipulate images so you won’t have to use any other 3rd party library. The first step before we resize an image is loading it as an image resource
inside the script. This is very different than using functions like file_get_contents() to get the content of the image file. Loading the image will require us to use different functions like imagecreatefromjpeg(),
imagecreatefrompng() and imagecreatefromgif() etc. The function we use will depend on the type of image we are resizing. We will use the getimagesize() function to get important information
about the image like its PHP The The Resize Image to Fixed Width and HeightOnce we have the image resource identifier, we can use other GD functions to resize the image. The
imagecreatetruecolor() function creates a new true color image with given width and height. The image created by this function will be all black so we will use the imagecopyresampled() function to copy and resize the original image over the new black one we just created using The
PHP
Here is the example code that resamples the original image and resizes it to have the given dimensions. PHP
Since we want to copy and resize the whole image, the x and y coordinates for the source and destination have been set to zero. The Resize Image to Fixed Width While Maintaining the Aspect RatioThe images that you are planning to resize can have different
aspect ratios. This means that if you use the above The example code below resizes the original image to have a specific width. The height is calculated automatically in order to maintain the aspect ratio. PHP
The Resize Image to Fixed Height While Maintaining the Aspect RatioIf you plan to resize an image to a given height and want its width to be calculated automatically, you can use the example code in this section. The logic would be similar to calculating the height of the image for a specific width. The only difference is that we will be calculating the width of the image based on the given height. Here is the example code that we will use to resize the image to given height. PHP
In this case, Scale Image By a Given FactorThis is also a very common requirement when resizing images. This time, instead of specifying the width or height of new image, you specify the scale. If you want the new image size to be half the original image, you set the scale to 0.5. Here is the example code to scale an image by given factor while preserving the aspect ratio. PHP
Inside the Save the Resized ImageSo far in the tutorial, we have learned how to resize an image to a given width and/or height or how to scale the image. Now it is time to save the resized image. We will use three GD library functions named imagejpeg(), imagepng() and imagegif() to save the image based on the specified type. JPEG images can be saved with different amount of compression applied to them. This can help us reduce the image size significantly. The example code below will save the resized image based on specified parameters. PHP
The Complete PHP Code for Loading, Resizing and Saving ImagesHere is the complete code for loading, resizing and saving JPEG, PNG or GIF images in PHP. I have reorganized the code and placed all the functions that we defined at the top. PHP
In the above example, I have resized the same image to have fixed width and height, only have fixed width and only have fixed height. A fourth version of the image has also been scaled down to 0.8 times the original size. You can use the same code to resize images uploaded by a user and resize all images in a directory by iterating over them one by one using the glob() function. Quick SummaryLet’s recap everything that we have covered in this tutorial.
Let me know if there is anything that you would like me to clarify. Also, you are more than welcome to comment if you know other techniques to resize an image programmatically in PHP. Rate this post — Loading...How to scale an image in PHP explain with example?The imagescale() function is an inbuilt function in PHP which is used to scale an image using the given new width and height. Parameters: This function accepts four parameters as mentioned above and described below: $image: It is returned by one of the image creation functions, such as imagecreatetruecolor().
How to resize image in PHP?Images can be resized using ImageMagick or GD functions. If GD's functions are used, the size of the image file is also reduced when raw digital camera images are sampled.
Which function is used to resize the image?Resize the image, using the imresize function.
|