ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to Resize and Upload an Image in PHP

uploadresizePicImage

In this tutorial, I will teach you how to resize and upload an image in php. .  This method can control the size of the images when uploading it on your website. It also optimize your web pages and lessen the load of time for a faster browsing experience.

Lets begin:

  1. Create the landing page of your project and name it “index.php”.  Copy  this following code and paste it into file.
  1. <!-- extension bootstrap css -->
  2. <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
  3.  
  4. <h1 align="center">Upload and Resize an Image</h1>
  5.  
  6. <form action="upload.php" method="POST" enctype="multipart/form-data" >
  7. <div class="container">
  8. <div class="row">
  9. <div class="col-md-6">
  10. <div class="col-md-12">
  11. <label class="col-md-3">Upload Image</label>
  12. <input id="photo" name="photo" type="file">
  13. </div>
  14. <div class="col-md-12">
  15. <label class="col-md-3">Choose size</label>
  16. <select name="size_option">
  17. <option>Small</option>
  18. <option>Medium</option>
  19. <option>Large</option>
  20. </select>
  21. <input type="submit" id="submit" name="submit" value="upload">
  22. </div>
  23.  
  24. </div>
  25. </div>
  26. <hr/>
  27. <div class="row">
  28. <div class="col-md-12">
  29. <?php
  30. if (isset($_GET['image'])) {
  31. /* displaying images */
  32.  
  33. $small_path = "images/small/".$_GET['image'];
  34. $medium_path ="images/medium/".$_GET['image'];
  35. $large_path = "images/large/".$_GET['image'];
  36.  
  37. echo "<img src='".$small_path."' alt='small'/> ";
  38. echo "<img src='".$medium_path ."' alt='medium'/> ";
  39. echo "<img src='".$large_path."' alt='large'/>";
  40. }
  41. ?>
  42. </div>
  43. </div>
  44. </div>
  45.  
  46. </form>
  1. Create a method that will resize and upload the image. Name it “upload.php”. Copy  this following code and paste it into file.
  1. <?php
  2.  
  3. if(isset($_POST['submit'])){
  4.  
  5. /* set the name of the file */
  6. $file_name = $_FILES['photo']['name'];
  7. /* set the size of the file */
  8. $file_type = $_FILES['photo']['type'];
  9.  
  10. /* Getting the original file size */
  11. list($orig_width, $orig_height) = getimagesize($_FILES['photo']['tmp_name']);
  12. if ($_POST['size_option']=='Small') {
  13. /* set the image path*/
  14. $path = 'images/small/'.$file_name;
  15. $n_width=100;
  16. $n_height=100;
  17.  
  18. }elseif ($_POST['size_option']=='Medium') {
  19. /* set the image path*/
  20. $path = 'images/medium/'.$file_name;
  21. $n_width=200;
  22. $n_height=200;
  23.  
  24. }elseif ($_POST['size_option']=='Large') {
  25. /* set the image path*/
  26. $path = 'images/large/'.$file_name;
  27. $n_width=300;
  28. $n_height=300;
  29.  
  30. }
  31.  
  32. /* Calculate the new size of an image */
  33.  
  34. if ($orig_height < $orig_width){
  35. // *** Image to be resized is wider (landscape)
  36.  
  37. $orig_ratio = $orig_height / $orig_width;
  38. $n_height = $n_width * $orig_ratio;
  39. }elseif ($orig_height > $orig_width){
  40. // *** Image to be resized is taller (portrait)
  41.  
  42. $orig_ratio = $orig_width / $orig_height ;
  43. $n_width = $n_height * $orig_ratio;
  44. }else {
  45. // *** Image to be resized is a square
  46.  
  47. if ($n_height < $n_width) {
  48.  
  49. $orig_ratio = $orig_height / $orig_width;
  50. $n_height = $n_width * $orig_ratio;
  51. } else if ($n_height > $n_width) {
  52. $orig_ratio = $orig_width / $orig_height ;
  53. $n_width = $n_height * $orig_ratio;
  54. }
  55.  
  56. }
  57.  
  58. /* Getting the binary data from the image */
  59. $img_String = file_get_contents($_FILES['photo']['tmp_name']);
  60. /* create an image from the string */
  61. $create_image = imagecreatefromstring($img_String);
  62.  
  63. $tmp = imagecreatetruecolor($n_width, $n_height);
  64. /* copy and resize the image */
  65. imagecopyresampled($tmp, $create_image, 0, 0, 0, 0, $n_width, $n_height, $orig_width, $orig_height);
  66. /* cheking the file extension*/
  67. switch ($file_type) {
  68.  
  69. case 'image/jpeg':
  70. /* save the image in the corresponding path*/
  71. imagejpeg($tmp, $path , 100);
  72. break;
  73. case 'image/png':
  74. /* save the image in the corresponding path*/
  75. imagepng($tmp, $path ,0);
  76.  
  77. break;
  78. case 'image/gif':
  79. /* save the image in the corresponding path*/
  80. imagegif($tmp, $path ,0);
  81. break;
  82.  
  83. default:
  84. /* save the image in the corresponding path*/
  85. imagejpeg($tmp, $path , 100);
  86. break;
  87. }
  88. /* destroy the raw image */
  89. imagedestroy($create_image);
  90.  
  91. /* return to the index */
  92. header('Location: index.php?image='.$file_name);
  93.  
  94. }
  95. ?>

 

For all students who need programmer for your thesis system or anyone who needs a sourcecode in any programming languages.  You can contact me @ :

Email – jannopalacios@gmail.com

Mobile No. – 09305235027 – tnt

 

Dowload the sourcecode here

Facebook Comments

Check Your Domain Ranking

Leave a Reply

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

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!