PHP Picture zoom function: Zoom picture according to width ratio

Source: Internet
Author: User
  1. /*

  2. The program does not overwrite the picture's original image by limiting the width of the image to a simple function that scales the picture.
  3. Picture equal to zoom function parameter description:
  4. $IMGSRC The original image address can be a remote picture or a path on the server.
  5. $newimgname the name of the picture after shrinking
  6. $kuan Limit the width of the picture, if it is larger than the width of the image to narrow down the operation.

  7. Example:

  8. $IMGSRC = "http://www.xingzuo51.com/upload/20141116/20141116010041-0.jpg";//Can be a remote picture.
  9. $newimgname = "Upload/20141116/000.jpg"; Do not add "/" in front of local path
  10. $kuan = 600;
  11. Img_suofang ($IMGSRC, $newimgname, $kuan); Call Picture shrink function
  12. */
  13. function Img_suofang ($IMGSRC, $newimgname, $kuan) {
  14. $info = getimagesize ($IMGSRC); Get picture information
  15. List ($w, $h) = $info;
  16. $BL = ($h/$w);
  17. Print_r ($info);
  18. Die ();
  19. $type = $info [2];
  20. Die ($type);
  21. if ($w > $kuan) {
  22. $k = $kuan;
  23. $g = ($k * $bl);
  24. Switch ($info [2]) {
  25. Case 1:
  26. $im = Imagecreatefromgif ($IMGSRC);
  27. $n = Imagecreatetruecolor ($k, $g);
  28. Imagecopyresampled ($n, $im, 0,0,0,0, $k, $g, $w, $h);
  29. $type = ". gif";
  30. Imagegif ($n, $newimgname. " $type ");
  31. Break
  32. Case 2:
  33. $im = Imagecreatefromjpeg ($IMGSRC);
  34. $n = Imagecreatetruecolor ($k, $g);
  35. Imagecopyresampled ($n, $im, 0,0,0,0, $k, $g, $w, $h);
  36. $type = ". jpg";
  37. Imagejpeg ($n, $newimgname. $type);
  38. Break
  39. Case 3:
  40. $im = Imagecreatefrompng ($IMGSRC);
  41. $n = Imagecreatetruecolor ($k, $g);
  42. Imagecopyresampled ($n, $im, 0,0,0,0, $k, $g, $w, $h);
  43. $type = ". png";
  44. Imagepng ($n, $newimgname. $type);
  45. Break
  46. Default
  47. Die ("No jpg Image");
  48. Break
  49. }

  50. if ($im && $n) {

  51. echo "generated thumbnails successfully.
    ";
  52. }else{
  53. echo "failed to generate thumbnails. ";
  54. }
  55. Imagedestroy ($im);
  56. Imagedestroy ($n);
  57. }else{
  58. echo "Picture does not exceed the specified width and does not need to be scaled. ";
  59. }
  60. }

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.