How to upload and resize image in php with example – onlinecode

How to upload and resize image in php with example – onlinecode

In this post we will give you information about How to upload and resize image in php with example – onlinecode. Hear we will give you detail about How to upload and resize image in php with example – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, We will tell you how to upload and resize image in PHP. upload images are the most important part of the website.

In this example, we will upload the image and create a resize image in PHP. if we will upload a large image on our website then our website runs slow. so we need to resize image width and height. the image resize will help our website improve SEO(search engine optimization).

Sometimes, we need to create thumbnails of images. so, we can easily create a thumbnail using the below example. we can also create thumbnails of images in different formate like png, jpg, jpeg, and gif.

<?php
	if(isset($_FILES['image'])){
		$file_name = $_FILES['image']['name']; // The file name  
		$file_temp = $_FILES['image']['tmp_name']; // File in the PHP tmp folder
		$getext = explode(".", $file_name); // Split file name into an array using the dot
        $fileExt = end($getext); // Now target the last array element to get the file extension
		
		$wmax = 300;
		$hmax = 300;
        $new_file_path ='./image/thumb/';
		img_resize($file_temp,$wmax, $hmax, $fileExt,$new_file_path);


	}
	
	
	function img_resize($target,$w, $h, $ext,$new_file_path) {
		list($w_orig, $h_orig) = getimagesize($target);
		$scale_ratio = $w_orig / $h_orig;
		if (($w / $h) > $scale_ratio) {
			   $w = $h * $scale_ratio;
		} else {
			   $h = $w / $scale_ratio;
		}
		
		
		$img = "";
		$ext = strtolower($ext);
		if ($ext == "gif"){ 
			$img = imagecreatefromgif($target);
		} else if($ext =="png"){ 
			$img = imagecreatefrompng($target);
		} else { 
			$img = imagecreatefromjpeg($target);
		}
		
		$tci = imagecreatetruecolor($w, $h);
		imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
		if ($ext == "gif"){ 
			$res=imagegif($tci, $new_file_path.time(). "_thump.gif");
		} else if($ext =="png"){ 
			$res=imagepng($tci, $new_file_path.time(). "_thump.png");
		} else { 
			$res=imagejpeg($tci, $new_file_path.time(). "_thump.jpg");
		}
}
?>

<form action="<?= $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">         
	<input type="file" name="image" />
	<input type="submit"/>
</form>   

Please follow and like us:

Hope this code and post will helped you for implement How to upload and resize image in php with example – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

7  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US