免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2985 | 回复: 1
打印 上一主题 下一主题

php 截取固定图片大小 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-15 15:56 |只看该作者 |倒序浏览
php 截取固定图片大小








1.创建iamge处理类
  1. <?php
  2. include_once 'ImageResize.class.php';
  3. date_default_timezone_set('PRC');
  4. class Image {   
  5.   
  6.     protected $nameinfo;   
  7.     protected $InputImageFileExtension;   
  8.   
  9.     public static function getInstance() {   
  10.         static $instance;   
  11.         if (!isset ($instance)) {   
  12.             $class = __CLASS__;   
  13.             $instance = new $class ();   
  14.         }   
  15.   
  16.         return $instance;   
  17.     }   
  18.   
  19.     function uploadresize($fileparam, $imageparam) {   
  20.   $newW = $imageparam['imageW'];
  21.   $newH = $imageparam['imageH'];
  22.         $this->nameinfo = explode('.',$fileparam['name']);   
  23.   
  24.         //取得图片格式   
  25.         $this->InputImageFileExtension = $this->nameinfo[1];   
  26.   
  27.         //判断是不是给了新的文件名   
  28.         if(empty($imageparam['imagename'])){   
  29.             $outputFileName = $this->nameinfo[0];   
  30.         }else{   
  31.             $outputFileName = $imageparam['imagename'];   
  32.         }   
  33.   
  34.         //判断路径是否正确   
  35.         if (!file_exists($imageparam['imagepath'])) {   
  36.             if(!mkdir($imageparam['imagepath'])){   
  37.                 throw new Exception('image path is wrong');   
  38.                 exit;   
  39.             }   
  40.         }   
  41.   
  42.        $file_src = $imageparam['imagepath']."/". $outputFileName . "_." . $this->InputImageFileExtension;   
  43.        //  temporary safe image storage   
  44.        if(file_exists($file_src)){   
  45.         unlink($file_src);   
  46.        }   
  47.   
  48.        move_uploaded_file($fileparam['tmp_name'], $file_src);   
  49.        // set image size
  50.    $imageResize = new ImageResize();
  51.    $imageResize->my_image_resize($file_src,$file_src,$newW,$newH,$this->InputImageFileExtension);  
  52.     }   
  53. }   
  54. ?>  

  55. 2.设置图片大小处理类:
  56. <?php
  57. class ImageResize {
  58. /*
  59. * 说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形
  60. * 参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高
  61. * written by smallchicken
  62. * time 2008-12-18
  63. */
  64. // 获得任意大小图像,不足地方拉伸,不产生变形,不留下空白
  65. function my_image_resize($src_file, $dst_file, $new_width, $new_height, $type) {
  66. if ($new_width < 1 || $new_height < 1) {
  67. echo "params width or height error !";
  68. exit ();
  69. }
  70. if (! file_exists ( $src_file )) {
  71. echo $src_file . " is not exists !";
  72. exit ();
  73. }
  74. // 图像类型
  75. $support_type = array ("jpg", "png", "gif" );

  76. if (! in_array ( $type, $support_type, true )) {
  77. echo "this type of image does not support! only support jpg , gif or png";
  78. exit ();
  79. }
  80. //Load image
  81. switch ($type) {
  82. case "jpg" :
  83. $src_img = imagecreatefromjpeg ( $src_file );
  84. break;
  85. case "png" :
  86. $src_img = imagecreatefrompng ( $src_file );
  87. break;
  88. case "gif" :
  89. $src_img = imagecreatefromgif ( $src_file );
  90. break;
  91. default :
  92. echo "Load image error!";
  93. exit ();
  94. }
  95. $w = imagesx ( $src_img );
  96. $h = imagesy ( $src_img );
  97. ////
  98. if($w < $new_width && $h < $new_height){
  99. // 定义一个中间的临时图像,该图像的宽高比 正好满足目标要求
  100. $inter_w = $w;
  101. $inter_h = $h;
  102. $inter_img = imagecreatetruecolor ( $inter_w, $inter_h );
  103. imagecopy ( $inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h );
  104. // 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像
  105. // 定义一个新的图像
  106. $new_img = imagecreatetruecolor ( $inter_w, $inter_h );
  107. imagecopyresampled ( $new_img, $inter_img, 0, 0, 0, 0, $inter_w, $inter_w, $inter_h, $inter_h );
  108. switch ($type) {
  109. case "jpg" :
  110. imagejpeg ( $new_img, $dst_file, 100 ); // 存储图像
  111. break;
  112. case "png" :
  113. imagepng ( $new_img, $dst_file, 100 );
  114. break;
  115. case "gif" :
  116. imagegif ( $new_img, $dst_file, 100 );
  117. break;
  118. default :
  119. break;
  120. }
  121. die();

  122. }
  123. ////
  124. $ratio_w = 1.0 * $new_width / $w;
  125. $ratio_h = 1.0 * $new_height / $h;
  126. $ratio = 1.0;
  127. // 生成的图像的高宽比原来的都小,或都大 ,原则是 取大比例放大,取大比例缩小(缩小的比例就比较小了)
  128. if (($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) {
  129. if ($ratio_w < $ratio_h) {
  130. $ratio = $ratio_h; // 情况一,宽度的比例比高度方向的小,按照高度的比例标准来裁剪或放大
  131. } else {
  132. $ratio = $ratio_w;
  133. }
  134. // 定义一个中间的临时图像,该图像的宽高比 正好满足目标要求
  135. $inter_w = ( int ) ($new_width / $ratio);
  136. $inter_h = ( int ) ($new_height / $ratio);
  137. $inter_img = imagecreatetruecolor ( $inter_w, $inter_h );
  138. imagecopy ( $inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h );
  139. // 生成一个以最大边长度为大小的是目标图像$ratio比例的临时图像
  140. // 定义一个新的图像
  141. $new_img = imagecreatetruecolor ( $new_width, $new_height );
  142. imagecopyresampled ( $new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height, $inter_w, $inter_h );
  143. switch ($type) {
  144. case "jpg" :
  145. imagejpeg ( $new_img, $dst_file, 100 ); // 存储图像
  146. break;
  147. case "png" :
  148. imagepng ( $new_img, $dst_file, 100 );
  149. break;
  150. case "gif" :
  151. imagegif ( $new_img, $dst_file, 100 );
  152. break;
  153. default :
  154. break;
  155. }
  156. } else {
  157. // end if 1
  158. // 2 目标图像 的一个边大于原图,一个边小于原图 ,先放大平普图像,然后裁剪
  159. // =if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h <1) )
  160. $ratio = $ratio_h > $ratio_w ? $ratio_h : $ratio_w; //取比例大的那个值
  161. // 定义一个中间的大图像,该图像的高或宽和目标图像相等,然后对原图放大
  162. $inter_w = ( int ) ($w * $ratio);
  163. $inter_h = ( int ) ($h * $ratio);
  164. $inter_img = imagecreatetruecolor ( $inter_w, $inter_h );
  165. //将原图缩放比例后裁剪
  166. imagecopyresampled ( $inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h, $w, $h );
  167. // 定义一个新的图像
  168. $new_img = imagecreatetruecolor ( $new_width, $new_height );
  169. imagecopy ( $new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height );
  170. switch ($type) {
  171. case "jpg" :
  172. imagejpeg ( $new_img, $dst_file, 100 ); // 存储图像
  173. break;
  174. case "png" :
  175. imagepng ( $new_img, $dst_file, 100 );
  176. break;
  177. case "gif" :
  178. imagegif ( $new_img, $dst_file, 100 );
  179. break;
  180. default :
  181. break;
  182. }
  183. } // if3
  184. } // end function
  185. }
  186. ?>
复制代码
3.图片固定大小及路径设置
  1. <?php  
  2. include "../../tools/Image/Image.class.php";  
  3. $array = $_FILES['photo'];
复制代码
//等比例缩放参数
  1. $resizeParam = array (  
  2.         'imagepath' => 'd:/xampp/www/', //图片存储路径  
  3. 'imageW' => 200, //图片存储路径  
  4. 'imageH' => 200 //图片存储路径  
  5. );  
  6. if (!empty ($array)) {
  7.    Image :: getInstance()->uploadresize($array, $resizeParam);  
  8. }
  9. ?>  
复制代码
4.简单form表单
  1. <html>  
  2. <body>  
  3. <form action='ImageTools.php' method='post' name='form' enctype="multipart/form-data">  
  4. <input type='file' name='photo'>  
  5. <input type=submit value='submit'>  
  6. </form>  
  7. </body>  
  8. </html>  
复制代码
over

相关参考:

http://blog.51yip.com/php/645.html

论坛徽章:
0
2 [报告]
发表于 2012-03-15 15:56 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP