티스토리 뷰

imagecreate 대신 imagecreatetruecolor사용 하고 imagecopyresized 대신 imagecopyresampled를 사용합니다.

-------------------

세 번째 매개 변수는 Dominic이 지적한대로 포함 할 가치가 있습니다. jpeg 품질을 지정합니다.

"그리고 찌그러진 것처럼 보임"문제에 대해, 당신은 그 자체가 정사각형 일 수도 있고 아닐 수도있는 소스 이미지에서 정사각형 썸네일을 만들고 있다는 것을 기억하십시오.

이 문제를 해결하는 한 가지 방법은 원본 크기를 사용하여 원본에서 복사 할 전체 너비 또는 전체 높이 (이미지가 세로인지 가로인지에 따라 다름) 정사각형을 만드는 것입니다. 이것은 imagecopyresized ()에 대한 인수의 "0,0,0,0"을 동적으로 계산 된 것으로 대체하는 것을 의미합니다.

(편집 : 예)

function makeSquareThumb($srcImage, $destSize, $destImage = null) {
    //I'm sure there's a better way than this, but it works...
        //I don't like my folder and file checking in the middle, but need to illustrate the need for this. 
        
            $srcFolder = dirname($srcImage); //source folder
                $srcName = basename($srcImage); //original image filename
                
                    //the IF ELSEIF ELSE below is NOT comprehensive - eg: what if the dest folder is the same as the source?
                        //writeable nature of the destination is not checked!
                            if(!destImage) {
                                    $destFolder = $srcFolder.'/thumbs/';
                                            if(!is_dir($destFolder)) {
                                                        //make the thumbs folder if there isn't one!
                                                                    mkdir($destFolder);
                                                                            }
                                                                                    $destImage = $destFolder.$srcName;
                                                                                        } elseif (is_dir($destImage)) {
                                                                                                $destFolder = $destImage;
                                                                                                        $destImage = $destFolder.'/'.$srcName;
                                                                                                            } else {
                                                                                                                    $destFolder = dirname($destImage);
                                                                                                                        }
                                                                                                                        
                                                                                                                        
                                                                                                                            //Now make it!
                                                                                                                                $srcCanvas = imagecreatefromjpeg($srcImage);
                                                                                                                                    $srcWidth = imagesx($srcCanvas);
                                                                                                                                        $srcHeight = imagesy($srcCanvas);
                                                                                                                                        
                                                                                                                                            //this let's us easily sample a square from the middle, regardless of apsect ratio.
                                                                                                                                                $shortSide = array($srcWidth,$srcHeight);
                                                                                                                                                    sort($shortSide);
                                                                                                                                                    
                                                                                                                                                        $src_x = $srcWidth/2 - $shortSide[0]/2;
                                                                                                                                                            $src_y = $srcHeight/2 - $shortSide[0]/2;
                                                                                                                                                            
                                                                                                                                                                //do it!
                                                                                                                                                                    $destCanvas = imagecreatetruecolor($destSize, $destSize);
                                                                                                                                                                        imagecopyresampled($destCanvas,$srcCanvas,0,0,$src_x,$src_y,$destSize,$destSize,$shortSide[0],$shortSide[0]);
                                                                                                                                                                            imagejpeg($destCanvas, $destImage);
                                                                                                                                                                            }
                                                                                                                                                                            
-------------------

시험:

imagejpeg($targetImage, "$thumbPath/$thumbName", 100);


출처
https://stackoverflow.com/questions/1904943
댓글
공지사항
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31