Image resize or image cropping is one of the important part during any website building. To make our uploaded image fix exactly in same diamention in front end we need to resize or crop that image.
In this tutorial of Php code for beginners we will show you the example of code which resize the uploaded image using GD library.
This is the html code for uploading the image for resizing. After selecting image you will click on submit button. When submit button will click our next code will check for image and prosess them accordingly.
Now when you click on submit our php code check whether submit button is clicked or not? As soon as he find submit is clicked the following code run to prosses the image for resizing.
Here is the Complete code for you to resize you image according to the height width set by you.
In this tutorial of Php code for beginners we will show you the example of code which resize the uploaded image using GD library.
This is the html code for uploading the image for resizing. After selecting image you will click on submit button. When submit button will click our next code will check for image and prosess them accordingly.
<html>
<head>
<title>Resize image in php</title>
</head>
<body>
<form name = "form" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" enctype="multipart/form-data">
<table>
<tr>
<td><label>Insert Image to resize:-</label></td>
<td><input type = "file" name = "file" id = "file"></td>
</tr>
<tr>
<td colspan = 2>
<input type = "submit" value = "submit" name = "submit">
</td>
</tr>
</table>
</form>
</body>
</html>
<head>
<title>Resize image in php</title>
</head>
<body>
<form name = "form" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" enctype="multipart/form-data">
<table>
<tr>
<td><label>Insert Image to resize:-</label></td>
<td><input type = "file" name = "file" id = "file"></td>
</tr>
<tr>
<td colspan = 2>
<input type = "submit" value = "submit" name = "submit">
</td>
</tr>
</table>
</form>
</body>
</html>
Now when you click on submit our php code check whether submit button is clicked or not? As soon as he find submit is clicked the following code run to prosses the image for resizing.
<?php
if(isset($_POST['submit'])){
$error = 0;
$uploadfile = 'images/'.basename($_FILES['file']['name']);
// $uploadfile is directory name where original image will save. You can unlink it after resizing the image.
$resize = 'resize/';
//It is you resize image directory.
$file_name = basename($_FILES['file']['name']);
$filecheck = basename($_FILES['file']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));
if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < 2120000))){
echo "File Not Supported";
$error = 1;
die();
}
// In above code we are checking the type and size of image. If it is not image file the error messege will appear.
if($_FILES['file']['name']!="")
{
ini_set('memory_limit','32M');
//Here we set the memory limit.
if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
echo $message= "Error in uploading a file due to the size of image.\n";
$error = 1;
}
else
{
$info = pathinfo($uploadfile);
$img = imagecreatefromjpeg( $uploadfile );
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = 100; //New Width for Image.
$new_height = 100; //New Height for Image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
imagejpeg( $tmp_img, "{$resize}{$file_name}" );
// All processing is done and your resized image will store from temporary directory to your resize image directory
}
}
if($error==0)
{
echo "Image Uploaded Successfully.";
exit;
}
}
?>
if(isset($_POST['submit'])){
$error = 0;
$uploadfile = 'images/'.basename($_FILES['file']['name']);
// $uploadfile is directory name where original image will save. You can unlink it after resizing the image.
$resize = 'resize/';
//It is you resize image directory.
$file_name = basename($_FILES['file']['name']);
$filecheck = basename($_FILES['file']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));
if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < 2120000))){
echo "File Not Supported";
$error = 1;
die();
}
// In above code we are checking the type and size of image. If it is not image file the error messege will appear.
if($_FILES['file']['name']!="")
{
ini_set('memory_limit','32M');
//Here we set the memory limit.
if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
echo $message= "Error in uploading a file due to the size of image.\n";
$error = 1;
}
else
{
$info = pathinfo($uploadfile);
$img = imagecreatefromjpeg( $uploadfile );
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = 100; //New Width for Image.
$new_height = 100; //New Height for Image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
imagejpeg( $tmp_img, "{$resize}{$file_name}" );
// All processing is done and your resized image will store from temporary directory to your resize image directory
}
}
if($error==0)
{
echo "Image Uploaded Successfully.";
exit;
}
}
?>
Here is the Complete code for you to resize you image according to the height width set by you.
<?php
if(isset($_POST['submit'])){
$error = 0;
$uploadfile = 'images/'.basename($_FILES['file']['name']);
$resize = 'resize/';
$file_name = basename($_FILES['file']['name']);
$filecheck = basename($_FILES['file']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));
if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < 2120000))){
echo "File Not Supported";
$error = 1;
die();
}
if($_FILES['file']['name']!="")
{
ini_set('memory_limit','32M');
if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
echo $message= "Error in uploading a file due to the size of image.\n";
$error = 1;
}
else
{
$info = pathinfo($uploadfile);
$img = imagecreatefromjpeg( $uploadfile );
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = 100;
$new_height = 100;
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
imagejpeg( $tmp_img, "{$resize}{$file_name}" );
}
}
if($error==0)
{
echo "Image Uploaded Successfully.";
exit;
}
}
?>
<html>
<head>
<title>Resize image in php</title>
</head>
<body>
<form name = "form" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" enctype="multipart/form-data">
<table>
<tr>
<td><label>Insert Image to resize:-</label></td>
<td><input type = "file" name = "file" id = "file"></td>
</tr>
<tr>
<td colspan = 2>
<input type = "submit" value = "submit" name = "submit">
</td>
</tr>
</table>
</form>
</body>
</html>
if(isset($_POST['submit'])){
$error = 0;
$uploadfile = 'images/'.basename($_FILES['file']['name']);
$resize = 'resize/';
$file_name = basename($_FILES['file']['name']);
$filecheck = basename($_FILES['file']['name']);
$ext = strtolower(substr($filecheck, strrpos($filecheck, '.') + 1));
if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < 2120000))){
echo "File Not Supported";
$error = 1;
die();
}
if($_FILES['file']['name']!="")
{
ini_set('memory_limit','32M');
if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
{
echo $message= "Error in uploading a file due to the size of image.\n";
$error = 1;
}
else
{
$info = pathinfo($uploadfile);
$img = imagecreatefromjpeg( $uploadfile );
$width = imagesx( $img );
$height = imagesy( $img );
$new_width = 100;
$new_height = 100;
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
imagejpeg( $tmp_img, "{$resize}{$file_name}" );
}
}
if($error==0)
{
echo "Image Uploaded Successfully.";
exit;
}
}
?>
<html>
<head>
<title>Resize image in php</title>
</head>
<body>
<form name = "form" action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "POST" enctype="multipart/form-data">
<table>
<tr>
<td><label>Insert Image to resize:-</label></td>
<td><input type = "file" name = "file" id = "file"></td>
</tr>
<tr>
<td colspan = 2>
<input type = "submit" value = "submit" name = "submit">
</td>
</tr>
</table>
</form>
</body>
</html>


I'm on the fence about this, while more customization is good, I have a feeling this is a "in-progress" update, it just feels incomplete and half-way there.
ReplyDeleteWe use badge layout for apps on design approvals (visual projects), so the image being displayed is important. Old layout "feels like" it had larger images,
maybe because the images were cropped more loosely so it's easier to tell which project it was at quick glance. Now the image is cropped closer, making it
harder to scan thru at quick glance. I find myself needing to click into the project more often than usual. Which makes the whole user experience less
efficient.
I have a couple suggestions that might make it work better:
1. Increase the height of the window the cover image is being displayed.
2. Let us to choose which image to be displayed as "cover" (like how Pinterest handles cover images of each board, was hoping for this for a long time)
3. Let us adjust which part of the image to show and how tight or loose the crop is (with a fixed window, let us move the image around and maybe enlarge or
shrink it to control what shows thru the window. Pinterest does a limited form of this, which is very useful in making the cover image relevant)
4. Allow Cover Image to be ordered in different hierarchy (currently every element can be ordered differently except the Cover Image, it seems to be stuck
in the 2nd spot, would like the option to set it on another spot in the layout. This one seems like an easy fix, since you guys allow that for every other
element already)
I'm on the fence about this, while more customization is good, I have a feeling this is a "in-progress" update, it just feels incomplete and half-way there.
ReplyDeleteWe use badge layout for apps on design approvals (visual projects), so the image being displayed is important. Old layout "feels like" it had larger images,
maybe because the images were cropped more loosely so it's easier to tell which project it was at quick glance. Now the image is cropped closer, making it
harder to scan thru at quick glance. I find myself needing to click into the project more often than usual. Which makes the whole user experience less
efficient.
I have a couple suggestions that might make it work better:
1. Increase the height of the window the cover image is being displayed.
2. Let us to choose which image to be displayed as "cover" (like how Pinterest handles cover images of each board, was hoping for this for a long time)
3. Let us adjust which part of the image to show and how tight or loose the crop is (with a fixed window, let us move the image around and maybe enlarge or
shrink it to control what shows thru the window. Pinterest does a limited form of this, which is very useful in making the cover image relevant)
4. Allow Cover Image to be ordered in different hierarchy (currently every element can be ordered differently except the Cover Image, it seems to be stuck
in the 2nd spot, would like the option to set it on another spot in the layout. This one seems like an easy fix, since you guys allow that for every other
element already)