How to resize and crop image by using PHP and jQuery

Today in modern web application, crop and resize of images is neccessary whether for profile use or general use of resized images as thumbnails. Resize and crop images is best technique to save space and to decrease page execution time and load your webpage faster. This article will cover all the needed techniques for resizing and croping images to build up a modern solution which you can use in every day situations.

Here are the steps to build functionality of croping and resizing images as thubnail.

Step 1. Create a HTML file i.e. index.html

In this file, we have defined markup and script for performing actions.




  
  
  
  




In this step, we have defined HTML code with jquery script for getting croping position of original image. We have created two div i.e. crop_resize_wrapper & crop_block. "crop_resize_wrapper" is the container of the image and "crop_block" is the draggable div. We have created a function crop() which get the dimension of the image. Once dimensions will get choosen, croped dimensions will get posted on php file i.e. get_crop.php.

Step 2. Create file i.e. get_crop.html

In this file, we have performed functionality for resizing & croping image by using PHP methods.


PHP GD library - this is the PHP library which we have used for performing resize & crop functionality. In this step, we loaded the image from folder i.e. images/etutorialz_image.jpg and get croping dimensions from post and applied with PHP methods i.e. imagecopyresized to resize existing image and performed methos i.e. imagejpeg to create the cropped image and save it with name cropped_etutorialz_img.jpg.

Step 3. Create stylesheet i.e. crop_etutorialz.css

body
{
	margin:0px;
	padding:0px;
}
#crop_resize_wrapper
{
	position:relative;
	margin-left:150px;
	margin-top:50px;
	width:600px;
	height:400px;
}
#crop_resize_wrapper img
{
	width:600px;
	height:400px;
}
#crop_block
{
	width:300px;
	height:200px;
	border:1px dashed white;
	position:absolute;
	top:0px;
	box-sizing:border-box;
}

Here you are good to perform croping & resizing functionality on your website. You can customize this code further as per your requirement and feel free to give comments on this tutorial.