一直想在网上找个图片所略图程序,但是一直没有找到无变形的,一般都是对原图进行简单的缩放,我想也许已经有了,只是我没有找到,所以没办法自己写了一个,其实也不是全是自己写的,只是在已有的基础上改的.
版权说明:该版本是在“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缩略图生成程序源码如下:
- <?php
-
-
-
-
- class ieb_upload{
- var $FormName;
- var $Directroy;
- var $MaxSize;
- var $CanUpload;
- var $doUpFile;
- var $sm_File;
- var $Error;
-
- function ieb_upload($formName='', $dirPath='', $maxSize=2097152)
- {
- global $FormName, $Directroy, $MaxSize, $CanUpload, $Error, $doUpFile, $sm_File;
-
- $FormName = $formName;
- $MaxSize = $maxSize;
- $CanUpload = true;
- $doUpFile = '';
- $sm_File = '';
- $Error = 0;
-
- if ($formName == ''){
- $CanUpload = false;
- $Error = 1;
- break;
- }
-
- if ($dirPath == ''){
- $Directroy = $dirPath;
- }else{
- $Directroy = $dirPath.'/';
- }
- }
-
-
- function scanFile()
- {
- global $FormName, $Error, $CanUpload;
-
- if ($CanUpload){
-
- $scan = is_readable($_FILES[$FormName]['name']);
-
- if ($scan){
- $Error = 2;
- }
-
- return $scan;
- }
- }
-
-
-
- function getSize($format = 'B')
- {
- global $FormName, $Error, $CanUpload;
-
- if ($CanUpload){
-
- if ($_FILES[$FormName]['size'] == 0){
- $Error = 3;
- $CanUpload = false;
- }
-
- switch ($format){
- case 'B':
- return $_FILES[$FormName]['size'];
- break;
-
- case 'M':
- return ($_FILES[$FormName]['size'])/(1024*1024);
- }
-
- }
- }
-
-
- function getExt()
- {
- global $FormName, $Error, $CanUpload;
-
- if ($CanUpload){
- $ext=$_FILES[$FormName]['name'];
- $extStr=explode('.',$ext);
- $count=count($extStr)-1;
- }
- return $extStr[$count];
- }
-
-
- function getName()
- {
- global $FormName, $CanUpload;
-
- if ($CanUpload){
- return $_FILES[$FormName]['name'];
- }
- }
-
-
- function newName()
- {
- global $CanUpload, $FormName;
-
- if ($CanUpload){
- $FullName=$_FILES[$FormName]['name'];
- $extStr=explode('.',$FullName);
- $count=count($extStr)-1;
- $ext = $extStr[$count];
-
- return date('YmdHis').rand(0,9).'.'.$ext;
- }
- }
-
-
- function upload($fileName = '')
- {
- global $FormName, $Directroy, $CanUpload, $Error, $doUpFile;
-
- if ($CanUpload){
- if ($_FILES[$FormName]['size'] == 0){
- $Error = 3;
- $CanUpload = false;
- return $Error;
- break;
- }
- }
-
- if($CanUpload){
-
- if ($fileName == ''){
- $fileName = $_FILES[$FormName]['name'];
- }
-
- $doUpload=@copy($_FILES[$FormName]['tmp_name'], $Directroy.$fileName);
-
- if($doUpload)
- {
- $doUpFile = $fileName;
- chmod($Directroy.$fileName, 0777);
- return true;
- }else{
- $Error = 4;
- return $Error;
- }
- }
- }
-
-
- function thumb($dscChar='',$width=160,$height=120)
- {
- global $CanUpload, $Error, $Directroy, $doUpFile, $sm_File;
-
- if ($CanUpload && $doUpFile != ''){
- $srcFile = $doUpFile;
-
- if ($dscChar == ''){
- $dscChar = 'sm_';
- }
-
- $dscFile = $Directroy.$dscChar.$srcFile;
- $data = getimagesize($Directroy.$srcFile,&$info);
-
- switch ($data[2]) {
- case 1:
- $im = @imagecreatefromgif($Directroy.$srcFile);
- break;
-
- case 2:
- $im = @imagecreatefromjpeg($Directroy.$srcFile);
- break;
-
- case 3:
- $im = @imagecreatefrompng($Directroy.$srcFile);
- break;
- }
-
- $srcW=imagesx($im);
- $srcH=imagesy($im);
-
- if(($srcW/$width)>=($srcH/$height)){
- $temp_height=$height;
- $temp_width=$srcW/($srcH/$height);
- $src_X=abs(($width-$temp_width)/2);
- $src_Y=0;
- }
- else{
- $temp_width=$width;
- $temp_height=$srcH/($srcW/$width);
- $src_X=0;
- $src_Y=abs(($height-$temp_height)/2);
- }
- $temp_img=imagecreatetruecolor($temp_width,$temp_height);
- imagecopyresized($temp_img,$im,0,0,0,0,$temp_width,$temp_height,$srcW,$srcH);
-
-
- $ni=imagecreatetruecolor($width,$height);
- imagecopyresized($ni,$temp_img,0,0,$src_X,$src_Y,$width,$height,$width,$height);
- $cr = imagejpeg($ni,$dscFile);
- chmod($dscFile, 0777);
-
-
- if ($cr){
- $sm_File = $dscFile;
- return true;
- }else{
- $Error = 5;
- return $Error;
- }
- }
- }
-
-
- function Err(){
- global $Error;
- return $Error;
- }
-
-
- function UpFile(){
- global $doUpFile, $Error;
- if ($doUpFile != ''){
- return $doUpFile;
- }else{
- $Error = 6;
- }
- }
-
-
- function filePath(){
- global $Directroy, $doUpFile, $Error;
- if ($doUpFile != ''){
- return $Directroy.$doUpFile;
- }else{
- $Error = 6;
- }
- }
-
-
- function thumbMap(){
- global $sm_File, $Error;
- if ($sm_File != ''){
- return $sm_File;
- }else{
- $Error = 6;
- }
- }
-
-
- function ieb_version(){
- return 'swin img class Ver 0.1';
- }
- }
- ?>
|