2013-03-06 2 views
1

업로드 한 파일 업로드 스크립트가 업로드 될 때 흐릿한 이미지를 업로드합니다! 이것은 내가 현재 잘못하고있는 것을 알아 내려고하는 현재 스크립트입니다. 스크립트는 이미지를 .png로 업로드하며 사용자 이름은 현재 로그인 한 사용자의 실제 사용자 이름입니다.PHP 파일 업로드 스크립트로 인해 흐릿한 이미지 생성

원본 이미지는 17x22이므로 흐릿하게 보이지 않습니다. 17px로

<?php 
include('../class/resize.php'); 
//error_reporting(0); 
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ 
$path = "../files/cloaks/"; //set your folder path 
$filename = $_FILES['photoimg']['tmp_name']; //get the temporary uploaded image name 
$valid_formats = array("jpg", "png", "gif", "bmp", "jpeg","GIF","JPG","PNG", "JPEG"); //add the formats you want to upload 

     $name = $_FILES['photoimg']['name']; //get the name of the image 
     $size = $_FILES['photoimg']['size']; //get the size of the image 
     if(strlen($name)) //check if the file is selected or cancelled after pressing the browse button. 
     { 
      list($txt, $ext) = explode(".", $name); //extract the name and extension of the image 
      if(in_array($ext,$valid_formats)) //if the file is valid go on. 
      { 
      if($size < 2098888) // check if the file size is more than 2 mb 
      { 
      $actual_image_name = $_POST['fname']; //actual image name going to store in your folder 
      $tmp = $_FILES['photoimg']['tmp_name']; 
      if(move_uploaded_file($tmp, $path.$actual_image_name)) //check the path if it is fine 
       { 
        move_uploaded_file($tmp, $path.$actual_image_name); //move the file to the folder 
        $dburl = ('../files/cloaks/'.$actual_image_name.''); 
        $image = new ZiResize(); 
        $image->load($dburl); 
        $image->resize(22,17); 
        $image->save($path.$actual_image_name); 
        //display the image after successfully upload 
        echo "<img src='files/cloaks/".$actual_image_name."' class='preview'> <input type='hidden' name='actual_image_name' id='actual_image_name' value='$actual_image_name' />"; 

       } 
      else 
       { 
       echo "failed"; 
       } 
      } 
      else 
      { 
       echo "Error! Max image size is 2 MB!";     
      } 
      } 
      else 
      { 
       echo "Error! Invalid image format!";  
      } 
     } 
     else 
     {  
     echo "Error! No file selected!"; 
     }  
    exit; 
    } 
?> 

resize.php 코드

<?php 
class ZiResize { 

    var $image; 
    var $image_type; 

    function load($filename) { 

     $image_info = getimagesize($filename); 
     $this->image_type = $image_info[2]; 
     if($this->image_type == IMAGETYPE_JPEG) { 

     $this->image = imagecreatefromjpeg($filename); 
     } elseif($this->image_type == IMAGETYPE_GIF) { 

     $this->image = imagecreatefromgif($filename); 
     } elseif($this->image_type == IMAGETYPE_PNG) { 

     $this->image = imagecreatefrompng($filename); 
     } 
    } 
    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { 

     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image,$filename,$compression); 
     } elseif($image_type == IMAGETYPE_GIF) { 

     imagegif($this->image,$filename); 
     } elseif($image_type == IMAGETYPE_PNG) { 

     imagepng($this->image,$filename); 
     } 
     if($permissions != null) { 

     chmod($filename,$permissions); 
     } 
    } 
    function output($image_type=IMAGETYPE_JPEG) { 

     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image); 
     } elseif($image_type == IMAGETYPE_GIF) { 

     imagegif($this->image); 
     } elseif($image_type == IMAGETYPE_PNG) { 

     imagepng($this->image); 
     } 
    } 
    function getWidth() { 

     return imagesx($this->image); 
    } 
    function getHeight() { 

     return imagesy($this->image); 
    } 
    function resizeToHeight($height) { 

     $ratio = $height/$this->getHeight(); 
     $width = $this->getWidth() * $ratio; 
     $this->resize($width,$height); 
    } 

    function resizeToWidth($width) { 
     $ratio = $width/$this->getWidth(); 
     $height = $this->getheight() * $ratio; 
     $this->resize($width,$height); 
    } 

    function scale($scale) { 
     $width = $this->getWidth() * $scale/100; 
     $height = $this->getheight() * $scale/100; 
     $this->resize($width,$height); 
    } 

    function resize($width,$height) { 
     $new_image = imagecreate($width, $height); 
     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
     $this->image = $new_image; 
    }  

} 
?> 
+0

문제는 'ZiResize' 클래스에 있습니다. 아마 그것의 문서를 확인하십시오. 스크린 샷의 예를 보여줄 수 있습니까? –

+0

예제 스크린 샷은 정확히 무엇입니까? 크기 조정 코드? – user2140088

+0

이게 뭐야? 크기를 조정하여 이미지가 흐려질 수 있습니다. $ image-> resize (22,17)' – chriz

답변

1

변경 compression 인수를 볼 수있는 또 다른 이유 인 그 색상으로 제한됩니다, 당신은 설정할 수 있습니다 결과 이미지의 품질, 품질이 원래 이미지보다 낮 으면 크기가 조정되지 않은 이미지는 "흐리게 보임"됩니다.

할 수 있습니다 :

  1. 이미지를 크기를 조정하지 않는 것처럼 "압축"

을 지원하지 않습니다 변경 압축 값 (다른 형식으로 이미지의 $image->save($path.$actual_image_name, NULL, 100);

  • 변경 형식, 다음을 대체 할 수 있습니다 :

    $image->resize(22,17); 
    $image->save($path.$actual_image_name); 
    

    with this :

    $image->save($path.$actual_image_name, NULL, 100); 
    
  • +0

    jpg가 아닌 .png 파일을 업로드하고 있습니다. – user2140088

    +0

    예,하지만 당신은'jpg' 파일을 저장하고 있습니다. 필자가 작성한 코드 만 바꾸면 도움이 될 것입니다. 그렇지 않다면 알려주세요. – Buksy

    +0

    그게 효과가 있습니다! 고맙습니다! :디 – user2140088

    0

    당신의 크기 조정을 22 픽셀입니다. 이미지를 그 크기로 줄이면 항상 흐릿 해 보일 것입니다. 풀 컬러 스펙트럼을 허용하므로 imagecreatetruecolor도 사용해야합니다. 사용 ImageCreate 당신이 PHP에서,

    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null)

    당신은 JPG 형식을 사용하여 이미지를 저장할 때 이미지가 blurY의

    +0

    아니요, 원본 이미지는 22x17 픽셀입니다. 그것의 크기가 조정되지 않는 경우, 누군가가 나를 위해 크기 조정 부분을 제거 할 수 있다면, 심지어 나 자신이하는 것이 어려워서 좋을 것이다. – user2140088

    +0

    답이 업데이트되었습니다. 크기 변경 기능에서'imagecreate'를'imagecreatetruecolor'로 바꾸십시오. – bizzehdee

    +0

    모든 "imagecreate"에 대해이 작업을 수행해야합니까? – user2140088