2012-08-21 5 views
0

나는 두 개의 이미지를 생성하는이 코드를 가지고 있습니다. 그것은 무엇을이미지 이름 바꾸기 기능 : PHP에서

는 이름 small_test.jpg.JPG

내가 필요로하는 test_s.jpg으로 생성 된 이름과 단지 test.jpg 될 수있는 두 번째입니다 test.jpg.JPG 이미지를 생성합니다.

이렇게하면 도움이 필요한 기능이 변경되어야합니다.

function setFile($src = null) { 
    $this->ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION)); 
    if(is_file($src) && ($this->ext == "JPG" OR $this->ext == "JPEG")) { 
    $this->img_r = ImageCreateFromJPEG($src); 
    } elseif(is_file($src) && $this->ext == "PNG") { 
    $this->img_r = ImageCreateFromPNG($src); 
    } elseif(is_file($src) && $this->ext == "GIF") { 
    $this->img_r = ImageCreateFromGIF($src); 
    } 
    $this->img_w = imagesx($this->img_r); 
    $this->img_h = imagesy($this->img_r); 
} 

function resize($largestSide = 100) { 
    $width = imagesx($this->img_r); 
    $height = imagesy($this->img_r); 
    $newWidth = 0; 
    $newHeight = 0; 

    if($width > $height){ 
    $newWidth = $largestSide; 
    $newHeight = $height * ($newWidth/$width); 
    } else { 
    $newHeight = $largestSide; 
    $newWidth = $width * ($newHeight/$height); 
    } 

    $this->dst_r = ImageCreateTrueColor($newWidth, $newHeight); 
    imagecopyresampled($this->dst_r, 
         $this->img_r, 
         0, 0, 0, 0, 
         $newWidth, $newHeight, 
         $width, $height); 
    $this->img_r = $this->dst_r; 
    $this->img_h = $newHeight; 
    $this->img_w = $newWidth; 
} 

function createFile($output_filename = null) { 
    if($this->ext == "JPG" OR $this->ext == "JPEG") { 
    imageJPEG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext, $this->quality); 
    } elseif($this->ext == "PNG") { 
    imagePNG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext); 
    } elseif($this->ext == "GIF") { 
    imageGIF($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext); 
    } 
    return $output_filename; 
} 

function setUploadDir($dirname) { 
    $this->uploaddir = $dirname; 
} 

function flush() { 
    $tempFile = $_FILES['Filedata']['tmp_name']; 
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/'; 
    $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; 
    $filename = $_FILES['Filedata']['name']; 
    $ext = pathinfo($filename, PATHINFO_EXTENSION); 
    $thumbnail = basename($filename,'.' .$ext) . '_s.' . $ext;  

    imagedestroy($this->dst_r); 
    unlink($targetFile); 
    imagedestroy($this->img_r); 
} 

} 

$tempFile = $_FILES['Filedata']['tmp_name']; 
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/'; 
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name']; 
$filename = $_FILES['Filedata']['name']; 
$ext = pathinfo($FileName, PATHINFO_EXTENSION); 
$thumbnail = basename($FileName,'.' .$ext) . '_s.' . $ext; 
move_uploaded_file ($tempFile, $targetFile); 

$image = new Image(); 
$image->setFile($targetFile); 
$image->setUploadDir($targetPath); 
$image->resize(640); 
$small_file = $image->createFile('small_'.$filename); 
$image->resize(100); 
$large_file = $image->createFile($filename); 
$image->flush(); 
} 
+0

이것은 많은 코드입니다. 정확히 작동하지 않는 곳은 어디입니까? – Tchoupi

+1

어떤 오류가 발생합니까? 어느 선 에서요? 입력은 무엇입니까? [* this *] (http://stackoverflow.com/faq#howtoask)를 읽어주십시오. – alfasin

+0

은 변경하려는 이름이거나 업로드하지 않는 이름입니까? – Stu

답변

0

코드가 너무 잘못되었습니다. 코드를 완전히 기록하지 않고 수정할 수 없습니다.

다음과 같이 알려주십시오. http://phpthumb.gxdlabs.com 그것은 제가 사용하는 것으로 성능이 뛰어나고 버그가없고 안전합니다.

많은 것들을 할 수 있으며 자신의 라이브러리를 만드는 대신이 방법을 사용하는 것이 좋습니다.

+0

예 .. 나는 그것을보고 있었다! 나는 uploadify에서 찾은 무언가를 사용했다. 나는 당신이 나에게 준 코드를 본다. 실제로는 출력을 내주지 않을 것이다. ... –

+0

@ Al_12 그래, 코드의 90 %가 잘못된 것 같다 .... – Sammaye

+0

@ Al_12 그 편집에 가까워 지려고 시도합니다. – Sammaye

관련 문제