Most often we try to upload and resize an image with PHP on our web projects. That time we use PHP resize image with upload image PHP which uploaded by PHP file upload.
If you have no idea about it or you need to create this project for your web-based software then you can create PHP image upload and resize it by following this post. In this post I will share with you how can you make a PHP file uploader first and then PHP image uploader for resizing image. You can make this project by following some simple codes.
This free photo resizer with PHP can save your hosting space and make your website first. So, why I make you late..!
Let’s see to do this work.
Relevant Article:
1. How To Run Computer Management MSC & Some Useful Run Commands
2. Make Auto Load And jQuery Refresh Page Div Every 10 Seconds
3. Facebook Widget For Website: Place Your Facebook Page Widget
4. Login Page In PHP: How To Create PHP Login Form
5. How To Check if WiFi is connected Or Disconnected
Upload and Resize an image with PHP
<?php
define (“MAX_SIZE”,”400″);
$errors=0;if($_SERVER[“REQUEST_METHOD”] == “POST”)
{
$image =$_FILES[“file”][“name”];
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
if ($image)
{
$filename = stripslashes($_FILES[‘file’][‘name’]);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != “jpg”) && ($extension != “jpeg”)
&& ($extension != “png”) && ($extension != “gif”))
{
echo ‘ Unknown Image extension ‘;
$errors=1;
}
else
{
$size=filesize($_FILES[‘file’][‘tmp_name’]);if ($size > MAX_SIZE*1024)
{
echo “You have exceeded the size limit”;
$errors=1;
}if($extension==”jpg” || $extension==”jpeg” )
{
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension==”png”)
{
$uploadedfile = $_FILES[‘file’][‘tmp_name’];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}list($width,$height)=getimagesize($uploadedfile);
$newwidth=60;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=25;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,
$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,
$width,$height);
$filename = “images/”. $_FILES[‘file’][‘name’];
$filename1 = “images/small”. $_FILES[‘file’][‘name’];
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
//If no errors registred, print the success message
if(isset($_POST[‘Submit’]) && !$errors)
{
// mysql_query(“update SQL statement “);
echo “Image Uploaded Successfully!”;
}
?>
Extention PHP function
Now you just find file extensions.
function getExtension($str) {
$i = strrpos($str,”.”);
if (!$i) { return “”; }
$l = strlen($str) – $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
Hopefully, you can make your PHP project for upload image with PHP and Resize it. If you have to any ask then don’t avoid the comment section. I am ready for your answer always. Share this post on social media with your own buddies.