-
-
/** - *
- * @author zhao jinhan
- * @date 2014년 1월 13일 11:54:30
- * @email xb_zjh@126.com
- *
- */
- header('콘텐츠 유형:텍스트/html ; 문자세트=utf-8');
- //정의된 크기
- define('THUMB_WIDTH',300);
- define('THUMB_HEIGHT',300);
- /**
- * 업로드된 파일명 재생성
- * @return string
- * @author zhao jinhan
- *
- */
- function _file_type($filetype = null){
- switch($filetype)
- {
- case "image/jpeg":
- $fileextname = "jpg";
- 휴식;
- case "image/gif":
- $fileextname = "gif";
- 휴식;
- case "image/png":
- $fileextname = "png";
- 휴식;
- 기본값:
- $fileextname = false;
- 휴식;
- }
- return $fileextname?date('YmdHis',time()).'.'.$fileextname:false;
- }
- /**
- *
- * @param string $filename
- * @param string $width
- * @param string $height
- * @param string $quality
- * @param string $savepath
- * @return 부울
- */
- 함수 _make_thumb($filename='', $width=THUMB_WIDTH, $height=THUMB_HEIGHT, $savepath='./upload'){
- if(file_exists($filename)){
- //상위 이미지 크기
- $imagesize=getimagesize($filename);
- $imagewidth=$imagesize[0];
- $imageheight=$imagesize[1];
- $mime = $imagesize['mime'];
- //宽高比例
- $ratio = $imagewidth/$imageheight;
- //새로운建一个背景이미지
- $bgimg = imagecreatetruecolor($width, $height);
- $white = imagecolorallocate($bgimg, 255, 255, 255);
- //填充背景color为白color
- imagefill($bgimg,0,0,$white);
- if($mime == 'image/gif'){
- $im = @imagecreatefromgif($filename); /* 열기 시도 */
- $outfun = 'imagegif';
- }elseif($mime == 'image/png'){
- $im = @imagecreatefrompng($filename); /* 열기 시도 */
- $outfun = 'imagepng';
- }else{
- $im = @imagecreatefromjpeg($filename); /* 열기 시도 */
- $outfun = 'imagejpeg';
- }
- if($ratio > 1){
- //宽島较大
- if($imagewidth > $width){
- //缩放图文到背景图文上
- $new_width = $너비;
- $new_height = ($width*$imageheight)/$imagewidth;
- $bg_y = ceil(abs(($height-$new_height)/2));
- imagecopyresampled($bgimg, $im, 0, $bg_y, 0, 0, $new_width, $new_height, $imagewidth, $imageheight);
- }else{
- //复复复图文到背景图文上
- $copy = true;
- }
- }else{
- //고도 높이
- if($imageheight > $height){
- //缩放图文
- $new_height = $height;
- $new_width = ($height*$imagewidth)/$imageheight;
- $bg_x = ceil(($width-$new_width)/2);
- imagecopyresampled($bgimg, $im, $bg_x, 0, 0, 0, $new_width, $new_height, $imagewidth, $imageheight);
- }else{
- //复复复图文到背景图文上
- $copy = true;
- }
- }
- if($copy){
- //复主图文到背景图文上
- $bg_x = ceil(($width-$imagewidth)/2);
- $bg_y = ceil(($height-$imageheight)/2);
- imagecopy($bgimg, $im, $bg_x, $bg_y, 0, 0, $imagewidth, $imageheight);
- }
- $ext = _file_type($mime);
- $outfun($bgimg, $savepath.'/'.$ext);
- imagedestroy($bgimg);
- $savepath.'/'.$ext를 반환합니다.
- }else{
- false를 반환합니다.
- }
- }
- if($_POST){
- $size = $_POST['size']?strtoupper(trim($_POST['size'])):'2M';
- $imgsize = $_FILES['img']['크기']?$_FILES['img']['크기']/(1024*1024):0;
- $imgwidth = $imgheight = $_POST['너비-높이']?intval($_POST['너비-높이']):300;
- //자정결정义文件上传大小
- ini_set('upload_max_filesize',$size);
- $mathsize = str_replace('M','',$size);
- if($imgsize>$mathsize){
- echo "图文大小不得超过{$size}!";
- 반환;
- }
- if($file_name = _file_type($_FILES['img']['type'])){
- if($_FILES['img']['error'] == UPLOAD_ERR_OK) {
- $savepath = '업로드/';
- if(!is_dir($savepath)){
- mkdir($savepath,0644);
- }
- //生成缩略图
- $thumb_file = _make_thumb($_FILES['img']['tmp_name'], $imgwidth, $imgheight, $savepath);
- //move_uploaded_file($_FILES['img']['tmp_name'],$savepath.$file_name);
- echo "生成后的图文为:";
- }else{
- echo $_FILES['img']['error'];
- 반환;
- }
- }else{
- echo "图文格式不正确,请上传jpg,gif,png적格式!";
- 반환;
- }
}else{
- echo << ;head>
-
- 이미지를 확대하여 정사각형으로 저장하세요< /title>
-
-
-
;
form>
|