2017-11-09 23 views
0

Laravel에서 이미지 변환을 수행하고 있습니다. 내가 원하는 것은 화면에 표시되는 이미지를로드하는 페이지가 축소판으로 변환되어 화면에 표시 될 때입니다. 지금은 미리보기 이미지로 변환되지 않은 이미지를 화면에 표시 할 수 있으므로 페이지를로드하면 이미지를 화면에 표시하는 데 너무 많은 시간이 걸립니다. 누군가가 나를 도울 수 있기를 바랍니다.Laravel에서 화면에 표시되는 동안 런타임에 이미지 축소판을 만듭니다.

답변

0

Laravel 5의 Imagine 라이브러리를 사용했습니다. 비행 중에 이미지 미리보기 이미지를 만들려면이 작업을 수행하십시오.

  1. 은 이미지 컨트롤러 ImageController.php를 만들고 코드를 아래에 붙여이 Route::get('/images1/{file}', '[email protected]');
  2. 에 web.php에 경로 추가 https://github.com/orchestral/imagine

  3. 을 통해이 라이브러리를 통합 할 수 있습니다.

    <?php 
    

    네임 스페이스 App \ Http \ Controllers; Illuminate \ Foundation \ Bus \ DispatchesJobs를 사용하십시오. Illuminate \ Routing \ Controller를 BaseController로 사용하십시오. Illuminate \ Foundation \ Validation \ ValidatesRequests를 사용하십시오. Illuminate \ Foundation \ Auth \ Access \ AuthorizesRequests를 사용하십시오. Illuminate \ Support \ Facades \ File을 사용하십시오. Illuminate \ Support \ Facades \ Config를 사용하십시오. 세션 사용; 보기 사용; 경로를 사용하십시오. 사용 응답;

    class ImageController는 BaseController를 확장합니다. {

    protected $ imagine;

    공개 된 GetImage 함수 ($ 파일명) {

    //if (!$this->imagine) { 
    // if (!$this->library and class_exists('Imagick')) { 
    //  $this->imagine = new \Imagine\Imagick\Imagine(); 
    // } else { 
    //  $this->imagine = new \Imagine\Gd\Imagine(); 
    // } 
    //} 
    $this->imagine = new \Imagine\Gd\Imagine(); 
    
    
    // Append the filename to the path where our images are located 
    $path = "C:\\xampp\\htdocs\\Eidolon_laravel-Git\\public\\Images\\".$filename; //$filename; 
    $filename1 = $filename; 
    // Initialize an instance of Symfony's File class. 
    // This is a dependency of Laravel so it is readily available. 
    $filename = new \Symfony\Component\HttpFoundation\File\File($path); 
    
    // Make a new response out of the contents of the file 
    // Set the response status code to 200 OK 
    $response = Response::make(
        //\File::get($path), 
        $this->resize($filename, 150,150), 
        200 
    ); 
    
    // Modify our output's header. 
    // Set the content type to the mime of the file. 
    // In the case of a .jpeg this would be image/jpeg 
    $response->header(
        'Content-type', 
        'image/jpg' //$file->getMimeType() 
    ); 
    
    // We return our image here. 
    //return view('value.pushMessages', $response); 
    return $response; 
    

    }

    /*

    • 리사이즈 기능.
    • @param 문자열 sizeString *
    • @return 블롭 영상 콘텐츠

    • @param 문자열 이름. */ public function resize ($ filename, $ width, $ height) {

      // 구성 파일에서 출력 경로를 읽을 수 있습니다. // $ outputDir = \ Config :: get ('assets.images.paths.output'); $ outputDir = "C : \ xampp \ htdocs \ Eidolon_laravel-Git \ public \ Images \ Output";

      // 크기 및 파일 이름에서 출력 파일 경로를 만듭니다. $ outputFile = $ outputDir. $ 파일 이름;

      // 크기가 조정 된 파일이 이미있는 경우 반환합니다. if (\ File :: isFile ($ outputFile)) { return \ File :: get ($ outputFile); }

      // 파일이 아직 존재하지 않으므로 원본 크기를 조정합니다. // $ inputDir = \ Config :: get ('assets.images.paths.input'); // $ inputFile = $ inputDir. '/'. $ 파일 이름;

      $ inputFile = $ filename; // Config 파일에서 선택한 크기의 너비와 높이를 가져옵니다. // $ sizeArr = Config :: get ('assets.images.sizes.'. $ sizeString); // $ width = $ sizeArr [ 'width']; // $ height = $ sizeArr [ 'height'];

      // 크기 조정 모드와 크기를 설정하기 위해 이미지 자르기를 원합니다. $ size = new \ Imagine \ Image \ Box ($ width, $ height); $ mode = \ Imagine \ Image \ ImageInterface :: THUMBNAIL_OUTBOUND;

      // 아직 출력 디렉토리가없는 경우 출력 디렉토리를 만듭니다. if (! \ File :: isDirectory ($ outputDir)) { \ File :: makeDirectory ($ outputDir); }

      // 파일을 열고 크기를 조정하여 저장하십시오. $ this-> imagine-> open ($ inputFile) -> 축소판 ($ 크기, $ 모드) -> save ($ outputFile, array ('quality'=> 90));

      // 크기가 조정 된 파일을 반환하십시오. return \ File :: get ($ outputFile);

    }

    /**

    • 파라미터 : 문자열 $ 파일 이름
    • @return 문자열 마임 */ 공공 기능 getMimeType ($ 파일 이름) {

      //// 입력 파일 경로를 만듭니다. // $ inputDir = \ Config :: get ('assets.images.paths.input'); // $ inputFile = $ inputDir. '/'. $ 파일 이름; $ inputFile = $ filename; // Symfony File 클래스를 사용하여 파일 mimetype을 가져옵니다. $ file = new \ Symfony \ Component \ HttpFoundation \ File \ File ($ inputFile); return $ file-> getMimeType();

      } }?>

지금 그대에게 코드를 실행합니다.