How to resize images on the server

Source: Internet
Author: User
How to resize images on the server
Adjusting the image size on the server side is much better than processing the image on the browser.
This article describes how to adjust the image size on the server side of PHP.
The code consists of two parts:
  • ImageResizer () is used to process the image
  • Loadimage () inserts the image url in a simpler format

  1. Function imageResizer ($ url, $ width, $ height ){
  2. Header ('content-type: image/jpeg ');
  3. List ($ width_orig, $ height_orig) = getimagesize ($ url );
  4. $ Ratio_orig = $ width_orig/$ height_orig;
  5. If ($ width/$ height> $ ratio_orig ){
  6. $ Width = $ height * $ ratio_orig;
  7. } Else {
  8. $ Height = $ width/$ ratio_orig;
  9. }
  10. // This resamples the image
  11. $ Image_p = imagecreatetruecolor ($ width, $ height );
  12. $ Image = imagecreatefromjpeg ($ url );
  13. Imagecopyresampled ($ image_p, $ image, 0, 0, 0, 0, $ width, $ height, $ width_orig, $ height_orig );
  14. // Output the image
  15. Imagejpeg ($ image_p, null, 100 );
  16. }
  17. // Works with both POST and GET
  18. $ Method = $ _ SERVER ['request _ method'];
  19. If ($ method = 'get '){
  20. ImageResize ($ _ GET ['URL'], $ _ GET ['w'], $ _ GET ['H']);
  21. } Elseif ($ method = 'post '){
  22. ImageResize ($ _ POST ['URL'], $ _ POST ['w'], $ _ POST ['H']);
  23. }
  24. // Makes the process simpler
  25. Function loadImage ($ url, $ width, $ height ){
  26. Echo 'image. php? Url = ', urlencode ($ url ),
  27. '& W =', $ width,
  28. '& H =', $ height;
  29. }
  30. ?>

  1. // Above code wocould be in a file called image. php.
  2. // Images wocould be displayed like this:
  3. "Alt =" "/>

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.