首页 > 语言 > PHP > 正文

PHP图片裁剪与缩放示例(无损裁剪图片)

2024-09-04 11:44:40
字体:大 中 小
来源:转载
供稿:网友
exif_imagetype -- 判断一个图像的类型。

功能说明:函数功能是把一个图像裁剪为任意大小的图像,并保持图像不变形。

参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高。

获得任意大小图像,不足地方拉伸,不产生变形,不留下空白。

  1.  
  2. functionimage_resize($src_file,$dst_file,$new_width,$new_height) 
  3.   
  4. { 
  5.   
  6.   $new_width=intval($new_width); 
  7.   
  8.      
  9.   
  10.   $new_height=intval($new_width); 
  11.   
  12.      
  13.   
  14.   if($new_width< 1 ||$new_height< 1) 
  15.   
  16.   { 
  17.   
  18.     echo"params width or height error !"; 
  19.   
  20.     exit(); 
  21.   
  22.   } 
  23.   
  24.      
  25.   
  26.   if(!file_exists($src_file)) 
  27.   
  28.   { 
  29.   
  30.     echo$src_file." is not exists !"; 
  31.   
  32.     exit(); 
  33.   
  34.   } 
  35.   
  36.      
  37.   
  38.   // 图像类型 
  39.   
  40.   $type= exif_imagetype($src_file); 
  41.   
  42.      
  43.   
  44.   $support_type=array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF); 
  45.   
  46.      
  47.   
  48.   if(!in_array($type,$support_type, true)) 
  49.   
  50.   { 
  51.   
  52.        
  53.   
  54.     echo"this type of image does not support! only support jpg , gif or png"; 
  55.   
  56.     exit(); 
  57.   
  58.   } 
  59.   
  60.      
  61.   
  62.   //Load image 
  63.   
  64.      
  65.   
  66.   switch($type) 
  67.   
  68.   { 
  69.   
  70.     caseIMAGETYPE_JPEG : 
  71.   
  72.          
  73.   
  74.       $src_img= imagecreatefromjpeg($src_file); 
  75.   
  76.          
  77.   
  78.       break; 
  79.   
  80.          
  81.   
  82.     caseIMAGETYPE_PNG : 
  83.   
  84.          
  85.   
  86.       $src_img= imagecreatefrompng($src_file); 
  87.   
  88.          
  89.   
  90.       break; 
  91.   
  92.          
  93.   
  94.     caseIMAGETYPE_GIF : 
  95.   
  96.          
  97.   
  98.       $src_img= imagecreatefromgif($src_file); 
  99.   
  100.          
  101.   
  102.       break; 
  103.   
  104.          
  105.   
  106.     default: 
  107.   
  108.       echo"Load image error!"; 
  109.   
  110.          
  111.   
  112.       exit(); 
  113.   
  114.   } 
  115.   
  116.      
  117.   
  118.   $w= imagesx($src_img); 
  119.   
  120.      
  121.   
  122.   $h= imagesy($src_img); 
  123.   
  124.      
  125.   
  126.   $ratio_w= 1.0 *$new_width/$w; 
  127.   
  128.      
  129.   
  130.   $ratio_h= 1.0 *$new_height/$h; 
  131.   
  132.      
  133.   
  134.   $ratio= 1.0; 
  135.   
  136.      
  137.   
  138.   // 生成的图像的高宽比原来的都小,或都大 ,原则是 取大比例放大,取大比例缩小(缩小的比例就比较小了) 
  139.   
  140.      
  141.   
  142.   if(($ratio_w< 1 &&$ratio_h< 1) || ($ratio_w> 1 &&$ratio_h> 1)) 
  143.   
  144.   { 
  145.   
  146.        
  147.   
  148.     if($ratio_w<$ratio_h) { 
  149.   
  150.          
  151.   
  152.       $ratio=$ratio_h; 
  153.   
  154.          
  155.   
  156.       // 情况一,宽度的比例比高度方向的小,按照高度的比例标准来裁剪或放大 
  157.   
  158.          
  159.   
  160.     }else{ 
  161.   
  162.          
  163.   
  164.       $ratio=$ratio_w; 
  165.   
  166.          
  167.   
  168.     } 
  169.   
  170.        
  171.   
  172.     // 定义一个中间的临时图像,该图像的宽高比 正好满足目标要求 
  173.   
  174.        
  175.   
  176.     $inter_w= (int)($new_width/$ratio); 
  177.   
  178.        
  179.   
  180.     $inter_h= (int)($new_height/$ratio); 
  181.   
  182.        
  183.   
  184.     $inter_img= imagecreatetruecolor($inter_w,$inter_h); 
  185.   
  186.        
  187.   
  188.     //var_dump($inter_img); 
  189.   
  190.        
  191.   
  192.     imagecopy($inter_img,$src_img, 0, 0, 0, 0,$inter_w,$inter_h); 
  193.   
  194.        
  195.   
  196.     // 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像 
  197.   
  198.     // 定义一个新的图像 
  199.   
  200.        
  201.   
  202.     $new_img= imagecreatetruecolor($new_width,$new_height); 
  203.   
  204.        
  205.   
  206.     //var_dump($new_img);exit(); 
  207.   
  208.     imagecopyresampled($new_img,$inter_img, 0, 0, 0, 0,$new_width,$new_height,$inter_w,$inter_h); 
  209.   
  210.        
  211.   
  212.     switch($type) 
  213.   
  214.     { 
  215.   
  216.       caseIMAGETYPE_JPEG : 
  217.   
  218.            
  219.   
  220.         // 存储图像 
  221.   
  222.         imagejpeg($new_img,$dst_file, 100); 
  223.   
  224.            
  225.   
  226.         break; 
  227.   
  228.            
  229.   
  230.       caseIMAGETYPE_PNG : 
  231.   
  232.            
  233.   
  234.         imagepng($new_img,$dst_file, 100); 
  235.   
  236.            
  237.   
  238.         break; 
  239.   
  240.            
  241.   
  242.       caseIMAGETYPE_GIF : 
  243.   
  244.            
  245.   
  246.         imagegif($new_img,$dst_file, 100); 
  247.   
  248.            
  249.   
  250.         break; 
  251.   
  252.            
  253.   
  254.       default: 
  255.   
  256.            
  257.   
  258.         break; 
  259.   
  260.     } 
  261.   
  262.        
  263.   
  264.   }// end if 1 
  265.   
  266.      
  267.   
  268.   // 2 目标图像 的一个边大于原图,一个边小于原图 ,先放大平普图像,然后裁剪 
  269.   
  270.      
  271.   
  272.   // =if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h<1) ) 
  273.   
  274.      
  275.   
  276.   else{ 
  277.   
  278.        
  279.   
  280.     $ratio=$ratio_h>$ratio_w?$ratio_h:$ratio_w; 
  281.   
  282.        
  283.   
  284.     //取比例大的那个值 
  285.   
  286.        
  287.   
  288.     // 定义一个中间的大图像,该图像的高或宽和目标图像相等,然后对原图放大 
  289.   
  290.        
  291.   
  292.     $inter_w= (int)($w*$ratio); 
  293.   
  294.        
  295.   
  296.     $inter_h= (int)($h*$ratio); 
  297.   
  298.        
  299.   
  300.     $inter_img= imagecreatetruecolor($inter_w,$inter_h); 
  301.   
  302.        
  303.   
  304.     //将原图缩放比例后裁剪 
  305.   
  306.        
  307.   
  308.     imagecopyresampled($inter_img,$src_img, 0, 0, 0, 0,$inter_w,$inter_h,$w,$h); 
  309.   
  310.        
  311.   
  312.     // 定义一个新的图像 
  313.   
  314.        
  315.   
  316.     $new_img= imagecreatetruecolor($new_width,$new_height); 
  317.   
  318.        
  319.   
  320.     imagecopy($new_img,$inter_img, 0, 0, 0, 0,$new_width,$new_height); 
  321.   
  322.        
  323.   
  324.     switch($type) 
  325.   
  326.     { 
  327.   
  328.       caseIMAGETYPE_JPEG : 
  329.   
  330.            
  331.   
  332.         // 存储图像 
  333.   
  334.         imagejpeg($new_img,$dst_file, 100); 
  335.   
  336.            
  337.   
  338.         break; 
  339.   
  340.            
  341.   
  342.       caseIMAGETYPE_PNG : 
  343.   
  344.            
  345.   
  346.         imagepng($new_img,$dst_file, 100); 
  347.   
  348.         break; 
  349.   
  350.            
  351.   
  352.       caseIMAGETYPE_GIF : 
  353.   
  354.            
  355.   
  356.         imagegif($new_img,$dst_file, 100); 
  357.   
  358.            
  359.   
  360.         break; 
  361.   
  362.            
  363.   
  364.       default: 
  365.   
  366.            
  367.   
  368.         break; 
  369.   
  370.     } 
  371.   
  372.        
  373.   
  374.   }// if3 
  375.   
  376.      
  377.   
  378. }// end function 
  379.   
  380. //输出新图片 
  381.   
  382. image_resize('test.jpg','demo.jpg',
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表