PHP image scaling function: scale the image proportionally according to the width

WBOY
Release: 2016-07-25 08:51:34
Original
1066 people have browsed it
  1. /*

  2. A simple function that limits the image width and scales the image proportionally. The program does not overwrite the original image.
  3. Image proportional scaling function parameter description:
  4. $imgsrc The original image address can be a remote image or a path on the server.
  5. $newimgname is the name of the reduced image.
  6. $kuan limits the width of the image. If it exceeds this width, the image will be reduced.

  7. Example:

  8. $imgsrc = "http://www.xingzuo51.com/upload/20141116/20141116010041-0.jpg";//Can be a remote image.
  9. $newimgname = "upload/20141116/000.jpg"; //Do not add "/" in front of the local path
  10. $kuan = 600;
  11. img_suofang($imgsrc,$newimgname,$kuan); //Call the image reduction function
  12. */
  13. function img_suofang($imgsrc,$newimgname,$kuan){
  14. $info = getimagesize($imgsrc); //Get image 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. }< /p>
  50. if ($im && $n) {

  51. echo "Thumbnail generated successfully.
    ";
  52. }else{
  53. echo "Failed to generate thumbnail. ";
  54. }
  55. imagedestroy( $im );
  56. imagedestroy( $n );
  57. }else{
  58. echo "The image does not exceed the specified width and does not need to be scaled. ";
  59. }
  60. }

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template