Resize image in PHP
You need to use either PHP's ImageMagick or GD functions to work with images.
With GD, for example, it's as simple as...
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
And you could call this function, like so...
$img = resize_image(‘/path/to/some/image.jpg’, 200, 200);
From personal experience, GD's image resampling does dramatically reduce file size too, especially when resampling raw digital camera images.
Simply use PHP's GD functions (like imagescale):
Syntax:
imagescale ( $image , $new_width , $new_height )
Example:
Step: 1 Read the file
$image_name = 'path_of_Image/Name_of_Image.jpg|png|gif';
Step: 2: Load the Image File
$image = imagecreatefromjpeg($image_name); // For JPEG
//or
$image = imagecreatefrompng($image_name); // For PNG
//or
$image = imagecreatefromgif($image_name); // For GIF
Step: 3: Our Life-saver comes in '_' | Scale the image
$imgResized = imagescale($image , 500, 400); // width=500 and height = 400
// $imgResized is our final product
Note: imagescale will work for (PHP 5 >= 5.5.0, PHP 7)
Step: 4: Save the Resized image to your desired directory.
imagejpeg($imgResized, 'path_of_Image/Name_of_Image_resized.jpg'); //for jpeg
imagepng($imgResized, 'path_of_Image/Name_of_Image_resized.png'); //for png
Source : Click to Read more