Dreevoo.com | Online Learning and Knowledge Sharing
 
Home | Programs | Programming languages | PHP & MySQL | Image Resize in PHP
Guest
Click to view your profile
Topics
Programs
Languages
Recipes
Home
Shortcuts
 
 

Image Resize in PHP

In this tutorial we will learn how to handle file uploads in PHP and how to resize and save the image with PHP code.

 
  Author: podtalje | Version: PHP | 20th October 2013 |  
 
 
1.
 

First of all we will create a simple form in HTML so that we will be able to upload images.

<form method="post" action="index.php" enctype="multipart/form-data">
    <input type="file" name="uploaded_file" />
    <input type="submit" value="Upload" />
</form>


Please note that we have used enctype="multipart/form-data" to correctly support file uploads.

 
 
2.
 

Now we will write some PHP code to handle file upload.

We must first check if there was an upload and if the file was successfully stored to the temporary location on the server.

if ( isset($_FILES['uploaded_file']) && is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {


 
 
3.
 

Next we save the location of the temporary filename to variable $f

$f = $_FILES['uploaded_file']['tmp_name'];


and in variable $fname we save the name of original file

$fname=$_FILES['uploaded_file']['name'];

 
 
4.
 

We will also need original image width and height.

$x = getimagesize($f);
$sw = $x[0];
$sh = $x[1];

 
 
5.
 

Based on the format of the image uploaded we now load the image into $im variable.

if (strpos($fname,'.jpg')>0)
    $im=ImageCreateFromJPEG($f);
elseif (strpos($fname,'.gif')>0)
    $im=ImageCreateFromGIF($f);
elseif (strpos($fname,'.png')>0)
    $im=ImageCreateFromPNG($f);




 
 
6.
 

We create a new empty image $thumb where we specify the new size of the image.

$thumb = ImageCreateTrueColor(100, 100); 

In this example the size of the image will be 100px x 100px.


Now we copy the original image stored in variable $im to the newly created image $thumb.

ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, 100, 100, $sw, $sh);

 
 
7.
 

And in the end we must of course save the image to a file.

if (strpos($fname,'.jpg')>0)
    imagejpeg($thumb,$fname);
elseif (strpos($fname,'.gif')>0)
    imagegif($thumb,$fname);
elseif (strpos($fname,'.png')>0)
    imagepng($thumb,$fname);


We have used IF statement so that output format of the image will be the same as the original format.

 
 
8.
 

For verification purposes we also include code to show the newly created image in the browser.

echo '<img src="'.$fname.'"></img>';

 
 
9.
 

You can now open your page in browser and if you upload an image, your image will be resized to 100px x 100px.


Complete code from this lesson can be found below.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP image upload</title>
</head>
<body>
<p>Upload picture: </p>
<form method="post" action="index.php" enctype="multipart/form-data">
    <input type="file" name="uploaded_file" />
    <input type="submit" value="Upload" />
</form>

<?php
if ( isset($_FILES['uploaded_file']) && is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {

  $f=$_FILES['uploaded_file']['tmp_name'];
  $fname=$_FILES['uploaded_file']['name'];
    
  $x = getimagesize($f);
  $sw = $x[0];
  $sh = $x[1]; 
   
  if (strpos($fname,'.jpg')>0) $im=ImageCreateFromJPEG($f);
  elseif (strpos($fname,'.gif')>0) $im=ImageCreateFromGIF($f);
  elseif (strpos($fname,'.png')>0) $im=ImageCreateFromPNG($f);
  else echo "False";
 
  $thumb = ImageCreateTrueColor(100, 100); 
  ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, 100, 100, $sw, $sh);
 
  if (strpos($fname,'.jpg')>0) imagejpeg($thumb,$fname);
  elseif (strpos($fname,'.gif')>0) imagegif($thumb,$fname);
  elseif (strpos($fname,'.png')>0) imagepng($thumb,$fname); 
 
  echo '<img src="'.$fname.'"></img>';
 
}
?>
</body>
</html>

 
 
 
   
  Please login to post a comment
  Click to open user profile  
thefansbuzz, 19th Feb 2023, 7:32 PM
Cheap SMM Panel Social Media Marketing is a huge field with a lot of competition. If you want to stand out from the crowd, you need to know how to find ways to make your content go viral. If you are interested in using social media to market your product, this service is perfect for you. The panel gives you access to hundreds of different social media sites, so that you can find new ways to market your business. Visit our website at: https://thefansbuzz.com/
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO