image resize dynamically


/ Published in: PHP
Save to your folder(s)

get the image size and reduces it dynamically


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. function imageResize($width, $height, $target) {
  4.  
  5. //takes the larger size of the width and height and applies the
  6. formula accordingly...this is so this script will work
  7. dynamically with any size image
  8.  
  9. if ($width > $height) {
  10. $percentage = ($target / $width);
  11. } else {
  12. $percentage = ($target / $height);
  13. }
  14.  
  15. //gets the new value and applies the percentage, then rounds the value
  16. $width = round($width * $percentage);
  17. $height = round($height * $percentage);
  18.  
  19. //returns the new sizes in html image tag format...this is so you
  20. can plug this function inside an image tag and just get the
  21.  
  22. return "width=\"$width\" height=\"$height\"";
  23.  
  24. }
  25.  
  26. ?>
  27. //Then in the HTML:
  28. <?php
  29.  
  30. //get the image size of the picture and load it into an array
  31. $mysock = getimagesize("images/sock001.jpg");
  32.  
  33. ?>
  34.  
  35. <!-using a standard html image tag, where you would have the
  36. width and height, insert your new imageResize() function with
  37. the correct attributes -->
  38.  
  39. <img src="images/sock001.jpg" <?php imageResize($mysock[0],
  40. $mysock[1], 150); ?>>

URL: http://www.sitepoint.com/article/image-resizing-php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.