Php thumbnail code. the imagecopyresampled function generates a thumbnail.

Source: Internet
Author: User
Tags imagejpeg
Php thumbnail code. the imagecopyresampled function generates a thumbnail.

  1. Function CreateImage ($ SrcImageUrl, $ DirImageUrl, $ Width, $ Height)

  2. {
  3. $ Img;
  4. $ Srcw;
  5. $ New_width;
  6. $ Srch;
  7. $ New_height;
  8. // Image type
  9. $ Type = substr (strrchr ($ SrcImageUrl, "."), 1 );
  10. // Initialize the image
  11. If ($ type = "jpg ")
  12. $ Img = imagecreatefromjpeg ($ SrcImageUrl );
  13. If ($ type = "gif ")
  14. $ Img = imagecreatefromgif ($ SrcImageUrl );
  15. If ($ type = "png ")
  16. $ Img = imagecreatefrompng ($ SrcImageUrl );

  17. $ Srcw = imagesx ($ img );

  18. $ Srch = imagesy ($ img );

  19. If ($ srcw/$ srch> $ Width/$ Height)

  20. {
  21. If ($ srcw> $ Width)
  22. {
  23. $ New_width = $ Width;
  24. $ New_height = $ srch * ($ Width/$ srcw );
  25. }
  26. Else
  27. {
  28. $ New_width = $ srcw;
  29. $ New_height = $ srch;
  30. }
  31. }
  32. Else
  33. {
  34. If ($ srch> $ Height)
  35. {
  36. $ New_height = $ Height;
  37. $ New_width = $ srcw * ($ Height/$ srch );
  38. }
  39. Else
  40. {
  41. $ New_width = $ srcw;
  42. $ New_height = $ srch;
  43. }
  44. }

  45. $ New_image = imagecreatetruecolor ($ new_width, $ new_height );

  46. Imagecopyresampled ($ new_image, $ img, 0, 0, 0, 0, $ new_width, $ new_height, $ srcw, $ srch );
  47. Imagejpeg ($ new_image, $ DirImageUrl );

  48. Imagedestroy ($ img );

  49. Imagedestroy ($ new_image );
  50. }

3. improved code:

  1. /*

  2. * $ O_photo source image path

  3. * $ D_photo: Path of the processed image

  4. * $ Width defines the width.

  5. * $ Height defines the height.

  6. * Call method cutphoto ("test.jpg", "temp.jpg", 256,146 );

  7. */

  8. Function cutphoto ($ o_photo, $ d_photo, $ width, $ height ){

  9. $ Temp_img = imagecreatefromjpeg ($ o_photo );

  10. $ O_width = imagesx ($ temp_img); // Obtain the source image width.

  11. $ O_height = imagesy ($ temp_img); // obtain the original image height.

  12. // Determine the processing method

  13. If ($ width> $ o_width | $ height> $ o_height) {// The size of the source image width or height ratio is small and compressed.

  14. $ Newwidth = $ o_width;

  15. $ Newheight = $ o_height;

  16. If ($ o_width> $ width ){

  17. $ Newwidth = $ width;

  18. $ Newheight = $ o_height * $ width/$ o_width;

  19. }

  20. If ($ newheight> $ height ){

  21. $ Newwidth = $ newwidth * $ height/$ newheight;

  22. $ Newheight = $ height;

  23. }

  24. // Thumbnail

  25. $ New_img = imagecreatetruecolor ($ newwidth, $ newheight );

  26. Imagecopyresampled ($ new_img, $ temp_img, 0, 0, 0, $ newwidth, $ newheight, $ o_width, $ o_height );

  27. Imagejpeg ($ new_img, $ d_photo );

  28. Imagedestroy ($ new_img );

  29. } Else {// The original image width and height are larger than the specified size and are cropped after compression.

  30. If ($ o_height * $ width/$ o_width> $ height) {// first, make sure that the width is the same as the specified width. if the height is greater than the specified height, OK

  31. $ Newwidth = $ width;

  32. $ Newheight = $ o_height * $ width/$ o_width;

  33. $ X = 0;

  34. $ Y = ($ newheight-$ height)/2;

  35. } Else {// otherwise, the specified height is the same and the width is adaptive.

  36. $ Newwidth = $ o_width * $ height/$ o_height;

  37. $ Newheight = $ height;

  38. $ X = ($ newwidth-$ width)/2;

  39. $ Y = 0;

  40. }

  41. // Thumbnail

  42. $ New_img = imagecreatetruecolor ($ newwidth, $ newheight );

  43. Imagecopyresampled ($ new_img, $ temp_img, 0, 0, 0, $ newwidth, $ newheight, $ o_width, $ o_height );

  44. Imagejpeg ($ new_img, $ d_photo );

  45. Imagedestroy ($ new_img );

  46. $ Temp_img = imagecreatefromjpeg ($ d_photo );

  47. $ O_width = imagesx ($ temp_img); // Obtain the thumbnail width.

  48. $ O_height = imagesy ($ temp_img); // Obtain the Thumbnail height.

  49. // Crop an image

  50. $ New_imgx = imagecreatetruecolor ($ width, $ height );

  51. Imagecopyresampled ($ new_imgx, $ temp_img, 0, 0, $ x, $ y, $ width, $ height, $ width, $ height );

  52. Imagejpeg ($ new_imgx, $ d_photo );

  53. Imagedestroy ($ new_imgx );

  54. }
  55. }
  56. ?>

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.