动态调整图像大小

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态调整图像大小相关的知识,希望对你有一定的参考价值。

get the image size and reduces it dynamically
  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); ?>>

以上是关于动态调整图像大小的主要内容,如果未能解决你的问题,请参考以下文章

调整图像大小后检测公差颜色

如何使背景图像跨越每个表格行并使用 HTML 和/或 CSS 动态调整大小?

jQuery动态比例元素/图像调整大小

如何在android中动态地使形状可拖动和调整大小

上传前调整图像大小 - 将画布转换为文件对象

PHP图像动态调整大小和圆角图像