Php and other proportional scaling image without losing its real implementation code

Source: Internet
Author: User
Php and other proportional scaling image without losing its real implementation code

  1. /**
  2. * Php image cropping and scaling function parameters:
  3. * $ Im Image object. you need to use imagecreatefromjpeg () to read image objects. If php supports png and gif, you can use imagecreatefromgif () and imagecreatefrompng ();
  4. * $ Maxwidth defines the maximum width (in pixels) of the generated image)
  5. * $ Maxheight: the maximum height (in pixels) of the generated image)
  6. * $ Name: image name generated
  7. * $ Filetype: The Final generated Image (.jpg/. png/. gif)
  8. */
  9. Function resizeimage ($ im, $ maxwidth, $ maxheight, $ name, $ filetype ){
  10. // Read the actual width and height of the image to be scaled
  11. $ Pic_width = imagesx ($ im );
  12. $ Pic_height = imagesy ($ im );
  13. // Calculate the actual image width and height and the compression ratio of the image width and height to determine whether the image is scaled based on the width or height. the current program scales the image based on the width.
  14. If ($ maxwidth & $ pic_width> $ maxwidth) | ($ maxheight & $ pic_height> $ maxheight )){
  15. // If the actual image length or width is smaller than the specified length or width, scale the image according to the length or width.
  16. If ($ maxwidth & $ pic_width> $ maxwidth ){
  17. $ Widthratio = $ maxwidth/$ pic_width;
  18. $ Resizewidth_tag = true;
  19. }
  20. If ($ maxheight & $ pic_height> $ maxheight ){
  21. $ Heightratio = $ maxheight/$ pic_height;
  22. $ Resizeheight_tag = true;
  23. }
  24. If ($ resizewidth_tag & $ resizeheight_tag ){
  25. If ($ widthratio <$ heightratio)
  26. $ Ratio = $ widthratio;
  27. Else
  28. $ Ratio = $ heightratio;
  29. }
  30. If ($ resizewidth_tag &&! $ Resizeheight_tag)
  31. $ Ratio = $ widthratio;
  32. If ($ resizeheight_tag &&! $ Resizewidth_tag)
  33. $ Ratio = $ heightratio;
  34. // Calculate the length and width of the scaled image.
  35. $ Newwidth = $ pic_width * $ ratio;
  36. $ Newheight = $ pic_height * $ ratio;
  37. // You can change the image size based on the calculated length and width of the final generated image. There are two ways to change the image size:
  38. // The imagecopyresized () function is valid in all gd versions, but its algorithm for scaling images is rough.
  39. // Imagecopyresamples (). the edge of the image obtained by the pixel interpolation algorithm is smoother, but the speed of this function is slower than that of imagecopyresized.
  40. If (function_exists ("imagecopyresampled ")){
  41. $ Newim = imagecreatetruecolor ($ newwidth, $ newheight );
  42. Imagecopyresampled ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ pic_width, $ pic_height );
  43. } Else {
  44. $ Newim = imagecreate ($ newwidth, $ newheight );
  45. Imagecopyresized ($ newim, $ im, 0, 0, 0, 0, $ newwidth, $ newheight, $ pic_width, $ pic_height );
  46. }
  47. // The processed image is finally generated. to generate a gif or png image, change the imagejpeg () function to imagegif () or imagepng ()
  48. $ Name = $ name. $ filetype;
  49. Imagejpeg ($ newim, $ name );
  50. Imagedestroy ($ newim );
  51. } Else {
  52. $ Name = $ name. $ filetype;
  53. Imagejpeg ($ im, $ name );
  54. }
  55. }

Note: The php gd Library version 1.6.2 previously supports the gif format, but because the gif format uses the lzw algorithm to involve patent rights, the gif format is not supported after gd1.6.2. In a windows environment, you only need to enter php. in the INI file, find extension = php_gd2.dll, remove #, and restart apache. if you are in a linux environment and want to support gif, png, jpeg, you need to download libpng, zlib, and freetype fonts.

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.