Php Image Resize Example

PHP 8 , PHP

Nicesnippets

65

09-06-2021


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 :


image_resize.php

<!DOCTYPE html>

<html>

<head>

<title>Image Upload</title>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://unpkg.com/gijgo@1.9.13/js/gijgo.min.js" type="text/javascript"></script>

<link href="https://unpkg.com/gijgo@1.9.13/css/gijgo.min.css" rel="stylesheet" type="text/css" />

</head>

<body>

<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>

</div>

</div>

<div class="form-group">

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

</div>

</form>

</div>

</body>

</html>

upload_file.php

<?php

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;

$target_layer=imagecreatetruecolor($target_width,$target_height);

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.


Recommended Posts