To set the images, we always need to find the images of the right dimensions to use it in our products. Sometimes it becomes very difficult to find the right images and it is also a very time taking process. Each place needs a different size and a different type of image. The choice of the right image also attracts the users, in e-commerce the right image plays an important role in user interaction.
Here we have some methods in PHP to alter the images. It saves our time to search for the image of the right dimensions.
Here are some steps to follow to resize an image with the PHP in-built method:
Step 1:
imagecreatetruecolor($dest_width,$dest_height)
The method creates a blank image of the given height and width. That means the height and width in which we want to resize our image.
Step 2 :
imagecopyresampled($dest_img,$src,0,0,0,0,$dest_width,$dest_height, $src_width,$src_height)
In the next step, we simply copy and resize our source image to our destination image, which means we resize the image from the original dimensions to the required dimensions.
$image_info = getimagesize($src_img)
Get the information of the image like, width, height, mime,size.
$dest_img is the image in size we need. $src is the original image resource.$dest_width is our required width $dest_height is our required height. $src_width is the original image’s height and $src_height is the original image’s height.
Step 3 :
imagejpeg($dest_img,$src_img);
It creates the image in our required image extension.
Please check the given code snippet for the above explanation:
This is the way we can alter the dimension of an image.
In case you have any queries then feel free to ask in the comment section below.
Be the first to comment.