PHP thumbnail code, imagecopyresampled function generate thumbnail image

Source: Internet
Author: User
Tags imagejpeg
  1. function CreateImage ($SRCIMAGEURL, $DirImageUrl, $Width, $Height)

  2. {
  3. $img;
  4. $SRCW;
  5. $new _width;
  6. $srch;
  7. $new _height;
  8. Type of picture
  9. $type = substr (STRRCHR ($SrcImageUrl, "."), 1);
  10. Initializing an 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. }

Copy Code

3. Post-Improvement code:

  1. /*

  2. * $o _photo Original path

  3. * $d image path after _photo processing

  4. * $width definition width

  5. * $height defined High

  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); Get the original width

  11. $o _height = Imagesy ($temp _img); Get the original height

  12. Judgment Processing method

  13. if ($width > $o _width | | $height > $o _height) {//Huantuquan or higher than the specified size, compression

  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 image

  25. $new _img = Imagecreatetruecolor ($newwidth, $newheight);

  26. Imagecopyresampled ($new _img, $temp _img, 0, 0, 0, 0, $newwidth, $newheight, $o _width, $o _height);

  27. Imagejpeg ($new _img, $d _photo);

  28. Imagedestroy ($new _img);

  29. }else{//original wide chart and high ratio of the specified size, compression after cutting

  30. if ($o _height* $width/$o _width> $height) {//Make sure width is the same as specified, OK if height is greater than specified

  31. $newwidth = $width;

  32. $newheight = $o _height* $width/$o _width;

  33. $x = 0;

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

  35. }else{//Otherwise determined height is the same as the specified width adaptive

  36. $newwidth = $o _width* $height/$o _height;

  37. $newheight = $height;

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

  39. $y = 0;

  40. }

  41. Thumbnail image

  42. $new _img = Imagecreatetruecolor ($newwidth, $newheight);

  43. Imagecopyresampled ($new _img, $temp _img, 0, 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); Get thumbnail width

  48. $o _height = Imagesy ($temp _img); Get thumbnail height

  49. Crop a picture

  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. ?>

Copy Code
  • Related Article

    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.