PHP dynamic processing picture after output display

Source: Internet
Author: User
PHP dynamically change the image size after output, the output image using the following address:
Image_resize.php?img=image.jpg&w=150&h=150&constrain=1
W and H are the dimensions to be displayed
  1. Header ("Content-type:image/jpeg");
  2. /*
  3. Jpeg/png Image Resizer
  4. Parameters (passed via URL):
  5. img = Path/url of JPEG or PNG image file
  6. Percent = If this is defined, image was resized by it ' s
  7. Value in percent (i.e. divide by percent)
  8. w = Image Width
  9. H = Image Height
  10. Constrain = If this is parameter was passed and W and H are set
  11. To a size value then the size of the resulting image
  12. is constrained by whichever dimension is smaller
  13. Requires the PHP GD Extension
  14. Outputs the resulting image in JPEG Format
  15. By:michael John G. lopez-www.sydel.net
  16. Filename:imgsize.php
  17. */
  18. $img = $_get[' img '];
  19. $percent = $_get[' percent ');
  20. $constrain = $_get[' constrain ');
  21. $w = $_get[' W '];
  22. $h = $_get[' h '];
  23. Get Image size of img
  24. $x = @getimagesize ($img);
  25. Image width
  26. $SW = $x [0];
  27. Image Height
  28. $sh = $x [1];
  29. if ($percent > 0) {
  30. Calculate resized height and width if percent is defined
  31. $percent = $percent * 0.01;
  32. $w = $SW * $percent;
  33. $h = $sh * $percent;
  34. } else {
  35. if (Isset ($w) and!isset ($h)) {
  36. Autocompute height If only width is set
  37. $h = (+/($SW/$w)) *. 01;
  38. $h = @round ($sh * $h);
  39. } elseif (Isset ($h) and!isset ($w)) {
  40. Autocompute width If only height is set
  41. $w = (+/($sh/$h)) *. 01;
  42. $w = @round ($SW * $w);
  43. } elseif (Isset ($h) and Isset ($w) and Isset ($constrain)) {
  44. Get the smaller resulting image dimension if both height
  45. and width is set and $constrain is also set
  46. $HX = (+/($SW/$w)) *. 01;
  47. $HX = @round ($sh * $hx);
  48. $WX = (+/($sh/$h)) *. 01;
  49. $WX = @round ($SW * $wx);
  50. if ($hx < $h) {
  51. $h = (+/($SW/$w)) *. 01;
  52. $h = @round ($sh * $h);
  53. } else {
  54. $w = (+/($sh/$h)) *. 01;
  55. $w = @round ($SW * $w);
  56. }
  57. }
  58. }
  59. $im = @ImageCreateFromJPEG ($img) or//Read JPEG Image
  60. $im = @ImageCreateFromPNG ($img) or//or PNG Image
  61. $im = @ImageCreateFromGIF ($img) or//or GIF Image
  62. $im = false; If image is not JPEG, PNG, or GIF
  63. if (! $im) {
  64. We get errors from PHP ' s imagecreate functions ...
  65. So let's echo back the contents of the actual image.
  66. ReadFile ($IMG);
  67. } else {
  68. Create the resized image destination
  69. $thumb = @ImageCreateTrueColor ($w, $h);
  70. Copy from image source, resize it, and paste to image destination
  71. @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $SW, $sh);
  72. Output resized image
  73. @ImageJPEG ($THUMB);
  74. }
  75. ?>
Copy Code
Php
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.