JavaScript - Simple Resize Image

In this tutorial we will create a Simple Resize Image using JavaScript. This code can resize the image properties when the user input a value in the form. The code itself use onclick() function to resize the image by tweaking the image style properties when called in the form. This is helpful for the student that needed to know the amount of words in their project. We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="eb">
  3. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  5. <nav class="navbar navbar-default">
  6. <div class="container-fluid">
  7. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  8. </div>
  9. </nav>
  10. <div class="col-md-3"></div>
  11. <div class="col-md-6 well">
  12. <h3 class="text-primary">JavaScript - Simple Resize Image</h3>
  13. <hr style="border-top:1px dotted #ccc;"/>
  14. <div class="col-md-2"></div>
  15. <div class="col-md-8">
  16. <center><img id="image" src="image.jpg" height="100%" width="100%"></center>
  17. <br />
  18. <div class="form-inline">
  19. <label>Width</label>
  20. <input class="form-control" type="number" id="width"/>
  21. <br /><br />
  22. <label>Height</label>
  23. <input class="form-control" type="number" id="height"/>
  24. </div>
  25. <br />
  26. <button class="btn btn-primary" onclick="resize();">Resize</buttom>
  27. </div>
  28. </div>
  29. <script src="js/script.js"></script>
  30. </body>
  31. </html>

Creating the Script

This code contains the script of the application. This code will immediately resize the image when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. function resize(){
  2. var height=document.getElementById('height').value;
  3. var width=document.getElementById('width').value;
  4.  
  5. if(height=="" || width==""){
  6. alert("Please complete the required field!");
  7. }else{
  8. document.getElementById('image').style.width=width+"px";
  9. document.getElementById('image').style.height=height+"px";
  10. }
  11. }
There you have it we successfully created a Simple Resize Image using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment