PHP Crop/Resize Image Function using GD Library

Today, We want to share with you PHP Crop/Resize Image Function using GD Library.In this post we will show you crop and save image using jquery and php, hear for php resize image on upload we will give you demo and example for implement.In this post, we will learn about php Fit image background pixels in PHP with an example.

PHP Crop/Resize Image Function using GD Library

There are the Following The simple About PHP Crop/Resize Image Function using GD Library Full Information With Example and source code.

As I will cover this Post with live Working example to develop image upload and cropping with php and jquery, so the crop image in php source code for this example is following below.


class CustomImgManipulation
        protected   $resource;
        public      $destination;

        public  function Fetchresource($file)
                $size                       =   getimagesize($file);
                $this->resource['width']    =   $size[0];
                $this->resource['height']   =   $size[1];
                $this->resource['type']     =   $size['mime'];
                return $this;

        public  function ConvertThumbs($source_imgs = '', $width = 60,$height = 60,$scaleImgName = false, $quality = 80)
                // Set resource file settings
                // Determine kind to extract from
                if($this->resource['type'] == 'image/gif')
                    $result_imgfile  =   imagecreatefromgif($source_imgs);
                elseif($this->resource['type'] == 'image/png') {
                        $result_imgfile  =   imagecreatefrompng($source_imgs);
                        $quality    =   7;
                elseif($this->resource['type'] == 'image/jpeg')
                        $result_imgfile  =   imagecreatefromjpeg($source_imgs);
                    return false;
                // Assign variables for calculations
                $w  =   $this->resource['width'];
                $h  =   $this->resource['height'];
                // Calculate proportional height/width
                if($w > $h) {
                        $new_height =   $height;
                        $new_width  =   floor($w * ($new_height / $h));
                        $crop_x     =   ceil(($w - $h) / 2);
                        $crop_y     =   0;
                else {
                        $new_width  =   $width;
                        $new_height =   floor( $h * ( $new_width / $w ));
                        $crop_x     =   0;
                        $crop_y     =   ceil(($h - $w) / 2);
                // New image
                $result_imgfile_tmp = imagecreatetruecolor($width,$height);
                // Copy/crop action
                imagecopyresampled($result_imgfile_tmp, $result_imgfile, 0, 0, $crop_x, $crop_y, $new_width, $new_height, $w, $h);
                // If false, send browser header for output to browser window
                if($scaleImgName == false)
                    header('Content-Type: '.$this->resource['type']);
                // Output proper image type
                if($this->resource['type'] == 'image/gif')
                elseif($this->resource['type'] == 'image/png')
                    ($scaleImgName !== false)? imagepng($result_imgfile_tmp, $scaleImgName, $quality) : imagepng($result_imgfile_tmp);
                elseif($this->resource['type'] == 'image/jpeg')
                    ($scaleImgName !== false)? imagejpeg($result_imgfile_tmp, $scaleImgName, $quality) : imagejpeg($result_imgfile_tmp);
                // Destroy set images
                // Destroy image

$ImageMaker =   new CustomImgManipulation();

$source_imgs   =   'profile.jpg';

$destination    =   'result_ext_img.jpg';


Angular 6 CRUD Operations Application Tutorials

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Crop/Resize Image Function using GD Library.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1.   Send PHP variable to bootstrap modal popup via Ajax
  2.   jQuery AJAX Multiple Files Upload using PHP
  3.   How to Get the length of an array in PHP?
  4.   PHP Upload and Crop Image using jQuery Ajax
  5.   How to Post Into Facebook Group with PHP using Graph API
  6.   PHP Automatic Flexify image resizing
  7.   Remove Special characters from String php
  8.   How to Create a File Type Validation using PHP
  9.   PHP Image Resize, Crop, Thumbnail, Watermark
  10.   jquery get checkboxes value if checked/selected