php缩略图生成程序

一直想在网上找个图片所略图程序,但是一直没有找到无变形的,一般都是对原图进行简单的缩放,我想也许已经有了,只是我没有找到,所以没办法自己写了一个,其实也不是全是自己写的,只是在已有的基础上改的.

版权说明:该版本是在“IEB_UPLOAD CLASS Ver 1.1”的基础上二次开发的,原程序对图片的裁剪将使图片变形、失真!本人在该程序的基础上进行了更改,用数据参数与原图片文件参数(主要是指宽和高)进行对比,得出比例值,先生成与原图片同比例缩放的图片,然后再以该中间图中心开始截取,从而获得缩略图,当然,图片会被裁剪,但是是最小限度的裁剪.

$upfos = new ieb_upload('photo_file','./up_img'); photo_file 表单文件域名称,不要加$; ./up_img,上传位置

$old_file_name=$upfos -> getName();

$old_file_fooder=$upfos -> getExt();

$file_size=$upfos -> getSize();

$file_name=$upfos -> newName();

$upfos -> upload($file_name);

$upfos -> thumb("small_",125,120);

$new_file_name=$upfos -> UpFile(); $new_file_name 上传后大图名

$small_file_name="small_".$new_file_name; $small_file_name 略图名

php缩略图生成程序源码如下:

  1. <?php
  2. //程序设计:swin.wang Email: php_in_china@yahoo.com.cn QQ:592298893
  3. //该程序你可无限制使用,但请保留版权信息.
  4. class ieb_upload{
  5. var $FormName; //文件域名称
  6. var $Directroy; //上传至目录
  7. var $MaxSize; //最大上传大小
  8. var $CanUpload; //是否可以上传
  9. var $doUpFile; //上传的文件名
  10. var $sm_File; //缩略图名称
  11. var $Error; //错误参数
  12. function ieb_upload($formName='', $dirPath='', $maxSize=2097152) //(1024*2)*1024=2097152 就是 2M
  13. {
  14. global $FormName, $Directroy, $MaxSize, $CanUpload, $Error, $doUpFile, $sm_File;
  15. //初始化各种参数
  16. $FormName = $formName;
  17. $MaxSize = $maxSize;
  18. $CanUpload = true;
  19. $doUpFile = '';
  20. $sm_File = '';
  21. $Error = 0;
  22. if ($formName == ''){
  23. $CanUpload = false;
  24. $Error = 1;
  25. break;
  26. }
  27. if ($dirPath == ''){
  28. $Directroy = $dirPath;
  29. }else{
  30. $Directroy = $dirPath.'/';
  31. }
  32. }
  33. //检查文件是否存在
  34. function scanFile()
  35. {
  36. global $FormName, $Error, $CanUpload;
  37. if ($CanUpload){
  38. $scan = is_readable($_FILES[$FormName]['name']);
  39. if ($scan){
  40. $Error = 2;
  41. }
  42. return $scan;
  43. }
  44. }
  45. //获取文件大小
  46. function getSize($format = 'B')
  47. {
  48. global $FormName, $Error, $CanUpload;
  49. if ($CanUpload){
  50. if ($_FILES[$FormName]['size'] == 0){
  51. $Error = 3;
  52. $CanUpload = false;
  53. }
  54. switch ($format){
  55. case 'B':
  56. return $_FILES[$FormName]['size'];
  57. break;
  58. case 'M':
  59. return ($_FILES[$FormName]['size'])/(1024*1024);
  60. }
  61. }
  62. }
  63. //获取文件类型
  64. function getExt()
  65. {
  66. global $FormName, $Error, $CanUpload;
  67. if ($CanUpload){
  68. $ext=$_FILES[$FormName]['name'];
  69. $extStr=explode('.',$ext);
  70. $count=count($extStr)-1;
  71. }
  72. return $extStr[$count];
  73. }
  74. //获取文件名称
  75. function getName()
  76. {
  77. global $FormName, $CanUpload;
  78. if ($CanUpload){
  79. return $_FILES[$FormName]['name'];
  80. }
  81. }
  82. //新建文件名
  83. function newName()
  84. {
  85. global $CanUpload, $FormName;
  86. if ($CanUpload){
  87. $FullName=$_FILES[$FormName]['name'];
  88. $extStr=explode('.',$FullName);
  89. $count=count($extStr)-1;
  90. $ext = $extStr[$count];
  91. return date('YmdHis').rand(0,9).'.'.$ext;
  92. }
  93. }
  94. //上传文件
  95. function upload($fileName = '')
  96. {
  97. global $FormName, $Directroy, $CanUpload, $Error, $doUpFile;
  98. if ($CanUpload){
  99. if ($_FILES[$FormName]['size'] == 0){
  100. $Error = 3;
  101. $CanUpload = false;
  102. return $Error;
  103. break;
  104. }
  105. }
  106. if($CanUpload){
  107. if ($fileName == ''){
  108. $fileName = $_FILES[$FormName]['name'];
  109. }
  110. $doUpload=@copy($_FILES[$FormName]['tmp_name'], $Directroy.$fileName);
  111. if($doUpload)
  112. {
  113. $doUpFile = $fileName;
  114. chmod($Directroy.$fileName, 0777);
  115. return true;
  116. }else{
  117. $Error = 4;
  118. return $Error;
  119. }
  120. }
  121. }
  122. //创建图片缩略图
  123. function thumb($dscChar='',$width=160,$height=120)
  124. {
  125. global $CanUpload, $Error, $Directroy, $doUpFile, $sm_File;
  126. if ($CanUpload && $doUpFile != ''){
  127. $srcFile = $doUpFile;
  128. if ($dscChar == ''){
  129. $dscChar = 'sm_';
  130. }
  131. $dscFile = $Directroy.$dscChar.$srcFile;
  132. $data = getimagesize($Directroy.$srcFile,&$info);
  133. switch ($data[2]) {
  134. case 1:
  135. $im = @imagecreatefromgif($Directroy.$srcFile);
  136. break;
  137. case 2:
  138. $im = @imagecreatefromjpeg($Directroy.$srcFile);
  139. break;
  140. case 3:
  141. $im = @imagecreatefrompng($Directroy.$srcFile);
  142. break;
  143. }
  144. $srcW=imagesx($im);
  145. $srcH=imagesy($im);
  146. if(($srcW/$width)>=($srcH/$height)){
  147. $temp_height=$height;
  148. $temp_width=$srcW/($srcH/$height);
  149. $src_X=abs(($width-$temp_width)/2);
  150. $src_Y=0;
  151. }
  152. else{
  153. $temp_width=$width;
  154. $temp_height=$srcH/($srcW/$width);
  155. $src_X=0;
  156. $src_Y=abs(($height-$temp_height)/2);
  157. }
  158. $temp_img=imagecreatetruecolor($temp_width,$temp_height);
  159. imagecopyresized($temp_img,$im,0,0,0,0,$temp_width,$temp_height,$srcW,$srcH);
  160. $ni=imagecreatetruecolor($width,$height);
  161. imagecopyresized($ni,$temp_img,0,0,$src_X,$src_Y,$width,$height,$width,$height);
  162. $cr = imagejpeg($ni,$dscFile);
  163. chmod($dscFile, 0777);
  164. if ($cr){
  165. $sm_File = $dscFile;
  166. return true;
  167. }else{
  168. $Error = 5;
  169. return $Error;
  170. }
  171. }
  172. }
  173. //显示错误参数
  174. function Err(){
  175. global $Error;
  176. return $Error;
  177. }
  178. //上传后的文件名
  179. function UpFile(){
  180. global $doUpFile, $Error;
  181. if ($doUpFile != ''){
  182. return $doUpFile;
  183. }else{
  184. $Error = 6;
  185. }
  186. }
  187. //上传文件的路径
  188. function filePath(){
  189. global $Directroy, $doUpFile, $Error;
  190. if ($doUpFile != ''){
  191. return $Directroy.$doUpFile;
  192. }else{
  193. $Error = 6;
  194. }
  195. }
  196. //缩略图文件名称
  197. function thumbMap(){
  198. global $sm_File, $Error;
  199. if ($sm_File != ''){
  200. return $sm_File;
  201. }else{
  202. $Error = 6;
  203. }
  204. }
  205. //开源代码phpfensi.com
  206. //显示版本信息
  207. function ieb_version(){
  208. return 'swin img class Ver 0.1';
  209. }
  210. }
  211. ?>