php 动态处理图片后输出显示的完整代码

前端之家收集整理的这篇文章主要介绍了php 动态处理图片后输出显示的完整代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP动态改变图片尺寸后输出输出图片时使用下面的地址: image_resize.PHP?img=image.jpg&w=150&h=150&constrain=1
w和h为要显示的尺寸,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:
  1. <?PHP
  2. /**
  3. * 动态处理图片显示
  4. *
  5. * @param
  6. * @arrange (512.笔记) jb51.cc
  7. **/
  8. header ("Content-type: image/jpeg");
  9. /*
  10. JPEG / PNG Image Resizer
  11. Parameters (passed via URL):
  12. img = path / url of jpeg or png image file
  13. percent = if this is defined,image is resized by it's
  14. value in percent (i.e. 50 to divide by 50 percent)
  15. w = image width
  16. h = image height
  17. constrain = if this is parameter is passed and w and h are set
  18. to a size value then the size of the resulting image
  19. is constrained by whichever dimension is smaller
  20. Requires the PHP GD Extension
  21. Outputs the resulting image in JPEG Format
  22. By: Michael John G. Lopez
  23. Filename : imgsize.PHP
  24. */
  25. $img = $_GET['img'];
  26. $percent = $_GET['percent'];
  27. $constrain = $_GET['constrain'];
  28. $w = $_GET['w'];
  29. $h = $_GET['h'];
  30. // get image size of img
  31. $x = @getimagesize($img);
  32. // image width
  33. $sw = $x[0];
  34. // image height
  35. $sh = $x[1];
  36. if ($percent > 0) {
  37. // calculate resized height and width if percent is defined
  38. $percent = $percent * 0.01;
  39. $w = $sw * $percent;
  40. $h = $sh * $percent;
  41. } else {
  42. if (isset ($w) AND !isset ($h)) {
  43. // autocompute height if only width is set
  44. $h = (100 / ($sw / $w)) * .01;
  45. $h = @round ($sh * $h);
  46. } elseif (isset ($h) AND !isset ($w)) {
  47. // autocompute width if only height is set
  48. $w = (100 / ($sh / $h)) * .01;
  49. $w = @round ($sw * $w);
  50. } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) {
  51. // get the smaller resulting image dimension if both height
  52. // and width are set and $constrain is also set
  53. $hx = (100 / ($sw / $w)) * .01;
  54. $hx = @round ($sh * $hx);
  55. $wx = (100 / ($sh / $h)) * .01;
  56. $wx = @round ($sw * $wx);
  57. if ($hx < $h) {
  58. $h = (100 / ($sw / $w)) * .01;
  59. $h = @round ($sh * $h);
  60. } else {
  61. $w = (100 / ($sh / $h)) * .01;
  62. $w = @round ($sw * $w);
  63. }
  64. }
  65. }
  66. $im = @ImageCreateFromJPEG ($img) or // Read JPEG Image
  67. $im = @ImageCreateFromPNG ($img) or // or PNG Image
  68. $im = @ImageCreateFromGIF ($img) or // or GIF Image
  69. $im = false; // If image is not JPEG,PNG,or GIF
  70. if (!$im) {
  71. // We get errors from PHP's ImageCreate functions...
  72. // So let's echo back the contents of the actual image.
  73. readfile ($img);
  74. } else {
  75. // Create the resized image destination
  76. $thumb = @ImageCreateTrueColor ($w,$h);
  77. // Copy from image source,resize it,and paste to image destination
  78. @ImageCopyResampled ($thumb,$im,$w,$h,$sw,$sh);
  79. // Output resized image
  80. @ImageJPEG ($thumb);
  81. }
  82. /*** 来自:编程之家 jb51.cc(jb51.cc) ***/
  83. ?>

猜你在找的PHP相关文章