채택완료

썸네일 파일명 생성시 중복 처리 질문드립니다

2019-06-19 (수) 03:53:16 3,883
Copy
public static function makeThumb($src_path, $thumb_dir, $w, $h, $options=array()){

        $temp  = BFiledir::parseFilename($src_path);
        $fname = $temp["name"];
        $ext   = $temp["extension"];

        //원본파일이 없으면
        if($fname!="" && is_file($src_path)){
            //애니메이션 GIF이면 원본그대로
            if(BImageutil::isAnimatedGif($src_path) && $options['is_anigif_thumb']==false){
                list($t_w, $t_h) = BImageutil::rateLimit($src_path, $w, $h);
                $filename = $fname.'.'.$ext;
                $arr = array(
                    $fname.'.'.$ext,
                    $t_w,
                    $t_h,
                    'filename'    => $filename,
                    'width'        => $t_w,
                    'height'    => $t_h
                );

                
            //애니메이션이 아니면
            }else{
                if($options['keep_filename']){
                    $filename = $fname.".".$ext;
                }else{
                    $str= "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //중복방지문자
                    $temp= str_shuffle($str); 
                    $ret = substr($temp, 0, 1); 
                    $filename = $fname."".$ret.".".$ext;                
                }else{
                    $temp1 = "l";
                    $filename = $fname."".$temp1.".".$ext;        
                }

                $thumb_path = $thumb_dir."/".$filename;
                
                //썸네일이 존재하지 않으면
                if(!is_file($thumb_path)){
                    
                    BFiledir::autoMkDir($thumb_dir, 0755);
            
                    $t = new BThumbnail($src_path, $thumb_path, $w, $h, $options);
                    $t->save();
                    list($t_w, $t_h) = $t->getThumbSize();
                
                //썸네일이 존재하면
                }else{
                    list($t_w, $t_h) = BImageutil::getImageSize($thumb_path);
                }

                $arr = array(
                    $filename,
                    $t_w,
                    $t_h,
                    "filename" => $filename,
                    "width" => $t_w,
                    "height" => $t_h
                );
            }
        }

        return $arr;
    }
}

이렇게 했는데 썸네일파일명이 중복처리를 하고 싶습니다

일단 셔플로 돌려서 하나씩 짤라서 넣는방법을 했지만 아닌거 같아서 잘 알려주시분 계신가요?

예)

썸네일 파일명이 apple.jpg 이라고 저장이되고 만약에 같은 파일명이 생기다면 뒷에 미리정해놓은 숫자 또는 영어를 마지막에 하나 더 붙혀서 appleA.jpg 이런식으로 처리하고 싶습니다 

답변 1개 / 댓글 2개

채택된 답변
+20 포인트

아래 소스를 참조해서 수정해보세요..

Copy
$ret=0;

do {

  $ret++;

  $filename = $fname."".$ret.".".$ext;

  $thumb_path = $thumb_dir."/".$filename;

} while(is_file($thumb_path))

답변에 대한 댓글 2개

2019-06-19 (수) 20:31:56
감사합니다 아이키스님

참고 해서 작업하고있는데 에러나 나는군요 ㅠ

계속 도전해보겠습니다
하다가 안되면 쪽지 주세요..^^

답변을 작성하려면 로그인이 필요합니다.