2012-04-16 4 views
0

내가 크기를 사진과 출력을 다시이 스크립트를 가지고 그것 (크기가 너무 큰?) :PHP는 이미지 미리보기가 항상 작동하지 생성

<?php 
/** 
* Produce a preview of the picture 
* 
*/ 
class CtrlImagePreview { 

    const EXT_DOC = 'doc'; 
    const EXT_FILE = 'file'; 
    const EXT_XLS = 'xls'; 


    /** 
    * Get the appropriate icon in function of the extension 
    * @param string $ext 
    */ 
    public function icon($ext) { 

     //put the extension in lowercase 
     $ext = strtolower($ext); 

     //check if icon exist 
     if(file_exists('picture/icon/'.$ext.'.png')) { 

      //display the approriate icon 
      $url = 'picture/icon/'.$ext.'.png'; 

     } else { 

      //display 
      $url = 'picture/icon/file.png'; 
     } 

     header("Cache-Control: private, max-age=10800, pre-check=10800"); 
     header("Pragma: private"); 
     header("Expires: " . date(DATE_RFC822,time()+60*60*24*30)); 
     header('Content-Type: image/png'); 
     header('Content-Transfer-Encoding: binary'); 
     readfile($url); 
    } 

    /** 
    * Resize and output preview of the image 
    * @param File $file 
    */ 
    public function resize(File $file) { 
     header("Cache-Control: private, max-age=10800, pre-check=10800"); 
     header("Pragma: private"); 
     header("Expires: " . date(DATE_RFC822,time()+60*60*24*30)); 

     header('Content-Type: image/png'); 
     header('Content-Transfer-Encoding: binary'); 

     $this->image('../upload/'.$_SESSION['c']->getId().'/'.$file->getProject()->getId().'/'.$file->getName(), true, 45); 
    } 


    /** 
    * NOTE: this function has been imported for image resizing, output mime: image/jpeg 
    * @param unknown_type $image 
    * @param unknown_type $crop 
    * @param unknown_type $size 
    * @return boolean 
    */ 
    private function image($image, $crop = null, $size = null) { 
     $image = ImageCreateFromString(file_get_contents($image)); 

     if (is_resource($image) === true) { 
      $x = 0; 
      $y = 0; 
      $width = imagesx($image); 
      $height = imagesy($image); 

      /* 
      CROP (Aspect Ratio) Section 
      */ 

      if (is_null($crop) === true) { 
       $crop = array($width, $height); 
      } else { 
       $crop = array_filter(explode(':', $crop)); 

       if (empty($crop) === true) { 
        $crop = array($width, $height); 
       } else { 
        if ((empty($crop[0]) === true) || (is_numeric($crop[0]) === false)) { 
         $crop[0] = $crop[1]; 
        } else if ((empty($crop[1]) === true) || (is_numeric($crop[1]) === false)) { 
         $crop[1] = $crop[0]; 
        } 
       } 

       $ratio = array(0 => $width/$height, 1 => $crop[0]/$crop[1]); 

       if ($ratio[0] > $ratio[1]) { 
        $width = $height * $ratio[1]; 
        $x = (imagesx($image) - $width)/2; 
       } 

       else if ($ratio[0] < $ratio[1]) { 
        $height = $width/$ratio[1]; 
        $y = (imagesy($image) - $height)/2; 
       } 

      } 

      /* 
      Resize Section 
      */ 

      if (is_null($size) === true) { 
       $size = array($width, $height); 
      } 

      else { 
       $size = array_filter(explode('x', $size)); 

       if (empty($size) === true) { 
        $size = array(imagesx($image), imagesy($image)); 
       } else { 
        if ((empty($size[0]) === true) || (is_numeric($size[0]) === false)) { 
         $size[0] = round($size[1] * $width/$height); 
        } else if ((empty($size[1]) === true) || (is_numeric($size[1]) === false)) { 
         $size[1] = round($size[0] * $height/$width); 
        } 
       } 
      } 

      $result = ImageCreateTrueColor($size[0], $size[1]); 

      if (is_resource($result) === true) { 
       ImageSaveAlpha($result, true); 
       ImageAlphaBlending($result, true); 
       ImageFill($result, 0, 0, ImageColorAllocate($result, 255, 255, 255)); 
       ImageCopyResampled($result, $image, 0, 0, $x, $y, $size[0], $size[1], $width, $height); 

       ImageInterlace($result, true); 
       Imagepng($result, null, 0); 
      } 
     } 

     return false; 
    } 
} 

?> 

이 스크립트 때로는 때로는하지 작동하는지 그것의 문제, 그리고 큰 이미지에서 작동하지 않는 것 같습니다 (내 서버에서만, 로컬 모드에서는 괜찮습니다!). 여기

이 스크립트에 의해 생성 된 사진입니다 :

image preview

답변

2

PHP 기능으로 이미지를 조작 할 때 이미지가 메모리에서 압축 해제됩니다. 해상도가 2500 × 1500 (약 4MPix 사진) 인 1MB JPEG 사진이있는 경우 압축되지 않은 크기는 2500 × 1500 (해상도) × 4 (픽셀 당 바이트) = 15MB입니다. 훨씬 더 압축 된 JPEG을 사용하면 압축 된 데이터와 압축되지 않은 데이터의 비율이 1:30 이상이 될 수 있습니다. 올바른 이미지 조작을 위해 번 두 번 사용 가능한 메모리 양 (입력 이미지 및 출력의 경우)이 필요할 수도 있습니다.

일반적인 해결 방법은 메모리 제한을 늘리거나 (PHP 구성 memory_limit) ImageMagick과 같은 외부 라이브러리를 사용하는 것입니다.

+0

예 저는 메모리 제한을 256MB로 늘리려고했지만 여전히 작동하지 않습니다 ...;) 감사합니다. –

1

대형 이미지 크기를 조정하기 위해 많은 메모리를 요구합니다. 종종 취해진 메모리는 (예를 들어) mod_php가 최대로 할당 된 양을 초과합니다. 이전에 이미지 파일 이름을 대기열 (데이터베이스 또는 특정 디렉토리에 저장)을 통해 외부 프로세스로 보낸 다음 명령 줄 기반 PHP 스크립트 (또는 다른 파일 이미지 크기 조정 프로그램)은 필요한만큼의 메모리를 사용하여 작업을 수행 한 다음 새 사본을 어딘가에 배치하십시오.

원하는 경우 이러한 대기열을 정렬하는 방법과 관련하여 몇 가지 다른 질문이 있습니다.

+0

좋아, 내가 할 수있는 일은이 스크립트에서 할 수있는 일은 없다. 내가 말한대로 mod_php 최대 메모리 세트를 변경하거나 대기열을 변경 했어? –

+0

그런 식으로 답변 해 주셔서 감사합니다;) –

관련 문제