Upload and Resize an Image with PHP | Lisenme

Upload and Resize an Image with PHP

Image Upload and Resize Using PHP

Trimming picture before transfer and converter, dependably an extraordinary thought for server space enhancement. Yield include resizes the picture according to the required size. You can likewise lessen the page size and load time by demonstrating the correct size picture. On the off chance that your web application has picture transfer usefulness, picture trim and resize are exceptionally valuable.

You can without much of a stretch transfer picture and make thumbnail utilizing PHP. In any case, on the off chance that you need live picture transfer and harvest, jQuery should be utilized. In this instructional exercise, we will demonstrate to you proper methodologies to transfer, trim, and resize picture utilizing jQuery and PHP. There are numerous jQuery modules accessible for trimming picture, imgAreaSelect is one of them. The imgAreaSelect module chooses a zone of a picture and actualize picture editing and resizing usefulness.

Live Demo   Download Source Code

index.html

<form action="" method="post" enctype="multipart/form-data">
	<div class="form-group col-md-3">
		<label class="required">Width</label>
		<input type="number" name="new_width" required />
	</div>
	<div class="form-group col-md-3">
		<label class="required">Height</label>
		<input type="number" name="new_height" required />
	</div>
	<div class="form-group col-md-6">
		<label class="required">Choose Image</label>
		<input type="file" name="upload_image" class="custom-file-input" required />
	</div>
	<input type="submit" name="form_submit" class="btn btn-primary" value="Submit"  style="
    margin-top: 25px;
    width: 100%;
    background-color: #1d91d2;
"/>
</form>

resize.php

<?php
function resizeImage($resourceType,$image_width,$image_height,$resizeWidth,$resizeHeight) {
    // $resizeWidth = 100;
    // $resizeHeight = 100;
    $imageLayer = imagecreatetruecolor($resizeWidth,$resizeHeight);
    imagecopyresampled($imageLayer,$resourceType,0,0,0,0,$resizeWidth,$resizeHeight, $image_width,$image_height);
    return $imageLayer;
}

if(isset($_POST["form_submit"])) {
	$imageProcess = 0;
    if(is_array($_FILES)) {
        $new_width = $_POST['new_width'];
        $new_height = $_POST['new_height'];
        $fileName = $_FILES['upload_image']['tmp_name'];
        $sourceProperties = getimagesize($fileName);
        $resizeFileName = time();
        $uploadPath = "./uploads/";
        $fileExt = pathinfo($_FILES['upload_image']['name'], PATHINFO_EXTENSION);
        $uploadImageType = $sourceProperties[2];
        $sourceImageWidth = $sourceProperties[0];
        $sourceImageHeight = $sourceProperties[1];
        switch ($uploadImageType) {
            case IMAGETYPE_JPEG:
                $resourceType = imagecreatefromjpeg($fileName); 
                $imageLayer = resizeImage($resourceType,$sourceImageWidth,$sourceImageHeight,$new_width,$new_height);
                imagejpeg($imageLayer,$uploadPath."thump_".$resizeFileName.'.'. $fileExt);
                break;

            case IMAGETYPE_GIF:
                $resourceType = imagecreatefromgif($fileName); 
                $imageLayer = resizeImage($resourceType,$sourceImageWidth,$sourceImageHeight,$new_width,$new_height);
                imagegif($imageLayer,$uploadPath."thump_".$resizeFileName.'.'. $fileExt);
                break;

            case IMAGETYPE_PNG:
                $resourceType = imagecreatefrompng($fileName); 
                $imageLayer = resizeImage($resourceType,$sourceImageWidth,$sourceImageHeight,$new_width,$new_height);
                imagepng($imageLayer,$uploadPath."thump_".$resizeFileName.'.'. $fileExt);
                break;

            default:
                $imageProcess = 0;
                break;
        }
        move_uploaded_file($fileName, $uploadPath. $resizeFileName. ".". $fileExt);
        $imageProcess = 1;
    }

	if($imageProcess == 1){
	?>

download.php

<div class="row">
			
			<div class="col-md-4">
				<img class="img-rounded img-responsive" src="<?php echo $uploadPath."thump_".$resizeFileName.'.'. $fileExt; ?>" width="<?php echo $new_width; ?>" height="<?php echo $new_height; ?>" >

				<h4><b>Thump Image</b></h4>

				<a href="<?php echo $uploadPath."thump_".$resizeFileName.'.'. $fileExt; ?>" download class="btn btn-danger"><i class="fa fa-download"></i> Download </a href="">
			</div>
			<div class="col-md-8">
				<img class="img-rounded img-responsive" src="<?php echo $uploadPath.$resizeFileName.'.'. $fileExt; ?>" >

				<h4><b>Original Image</b></h4>
			</div>
		</div>

	<?php
	}else{
	?>
		<div class="alert icon-alert with-arrow alert-danger form-alter" role="alert">
			<i class="fa fa-fw fa-times-circle"></i>
			<strong> Note !</strong> <span class="warning-message">Invalid Image </span>
		</div>
	<?php
	}
	$imageProcess = 0;
}
?>

 

You May Also Like

About the Author: Ranjith Kumar A

0 0 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ivan arias
2 years ago

nice and clean thank you
https://www.fpc-security.com/

Alminaz
Alminaz
1 year ago

Jpeg and Jpg are the same?

emmy smith
emmy smith
1 year ago

dear sir, i tell u , no one like u. good works and love ur works . keep it up Thank u

3
0
Would love your thoughts, please comment.x
()
x