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.

index.php

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
                $this->Fetchresource($source_imgs);
                // 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);
                else
                    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')
                    imagegif($result_imgfile_tmp);
                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
                if(isset($result_imgfile))
                    imagedestroy($result_imgfile); 
                // Destroy image
                if(isset($result_imgfile_tmp))
                    imagedestroy($result_imgfile_tmp);
            }
    }


$ImageMaker =   new CustomImgManipulation();


$source_imgs   =   'profile.jpg';

$destination    =   'result_ext_img.jpg';
$ImageMaker->ConvertThumbs($source_imgs,120,120,$destination);

?>
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

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 Pakainfo.com 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.

Leave a Comment