Php Image Resize Example




Php Image Resize Example

Hi guys,

Today i will explained to the How To image resize in php. This example is so easy to use in php. This example to i am uploaded to the image file to uploaded into system.

Image and other file resize in very easy in php tutorial. THis example to uploaded in jpg,png and gif file to uploaded and resize to file. This Example to i am use in two file is created.

sSo let's start to the example and follow to the my all step.

Example :


<!DOCTYPE html>



<title>Image Upload</title>

<script src=""></script>

<link rel="stylesheet" href="" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="" type="text/javascript"></script>

<link href="" rel="stylesheet" type="text/css" />



<div class="container">

<h2>Image File</h2>

<form name="frmImageResize" action="upload_file.php" method="post" enctype="multipart/form-data">

<div class="row">

<div class="col-md-6">

<div class="form-group">

<input type="file" name="myImage" class="form-control">




<div class="form-group">

<button class="btn btn-success" value="submit" name="submit">Submit</button>








if(isset($_POST["submit"])) {

if(is_array($_FILES)) {

$file = $_FILES['myImage']['tmp_name'];

$source_properties = getimagesize($file);

$image_type = $source_properties[2];

if( $image_type == IMAGETYPE_JPEG ) {

$image_resource_id = imagecreatefromjpeg($file);

$target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);

imagejpeg($target_layer,$_FILES['myImage']['name'] . "_thump.jpg");

}elseif( $image_type == IMAGETYPE_GIF ) {

$image_resource_id = imagecreatefromgif($file);

$target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);

imagegif($target_layer,$_FILES['myImage']['name'] . "_thump.gif");

}elseif( $image_type == IMAGETYPE_PNG ) {

$image_resource_id = imagecreatefrompng($file);

$target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);

imagepng($target_layer,$_FILES['myImage']['name'] . "_thump.png");




function fn_resize($image_resource_id,$width,$height) {

$target_width =200;

$target_height =200;


imagecopyresampled($target_layer,$image_resource_id,0,0,0,0,$target_width,$target_height, $width,$height);

return $target_layer;



finally resize to the your file in your folder.

So, finally we are done with our code we can get below output.

#PHP 8