2017-09-27 3 views
0

경계 상자를 사용하여 초점을 중심으로 이미지를 자르고 크기를 조정하는 이론에 대한 지침이 필요합니다.자르기 및 크기 조정

내 경우에는 다양한 정의 (1x, 2x, 3x 등)에서 다양한 이미지 크기 요구 사항 (예 : 100x100, 500x200, 1200x50)이 있습니다.

이 정의는 50x50 자른 이미지를 100x100 자른 이미지로 효과적으로 전환하여 더 높은 화면 정의 장치에 2x 정의를 제공합니다.

x, y 초점 및 두 x, y 좌표가있는 경계 상자 (topLeft [x, y], bottomRight [x, y])가있는 사용자 업로드 이미지가 제공됩니다.

내 사용자가 제공 한 이미지를 다양한 크기와 해상도로 다양한 이미지로 변환하는 이론은 무엇입니까? 연구를 통해 저의 요구 사항 중 일부 또는 전부를 찾지 못했습니다.

내 특정 환경에서이 질문의 본질 때문에 다소 관련이 없지만 PHP, Laravel 및 이미지 중재 라이브러리를 사용하고 있습니다.

답변

1

다음은 GD lib을 사용하는 일부 이미지 클래스입니다.

https://github.com/delboy1978uk/image/blob/master/src/Image.php

나는 크기를 조정하고 초점에 따라 자르기에 대한 코드를 작성하지 않은,하지만 난 초점이 중심에 있음을 전제로 작동하는 resizeAndCrop() 방법이 수행

를 여기
public function resizeAndCrop($width,$height) 
    { 
     $target_ratio = $width/$height; 
     $actual_ratio = $this->getWidth()/$this->getHeight(); 

     if($target_ratio == $actual_ratio){ 
      // Scale to size 
      $this->resize($width,$height); 

     } elseif($target_ratio > $actual_ratio) { 
      // Resize to width, crop extra height 
      $this->resizeToWidth($width); 
      $this->crop($width,$height,true); 

     } else { 
      // Resize to height, crop additional width 
      $this->resizeToHeight($height); 
      $this->crop($width,$height,true); 
     } 
    } 

는 당신이 초점을 설정할 수 crop() 방법의 왼쪽, 가운데 또는 오른쪽 :

/** 
* @param $width 
* @param $height 
* @param string $trim 
*/ 
public function crop($width,$height, $trim = 'center') 
{ 
    $offset_x = 0; 
    $offset_y = 0; 
    $current_width = $this->getWidth(); 
    $current_height = $this->getHeight(); 
    if($trim != 'left') 
    { 
     if($current_width > $width) { 
      $diff = $current_width - $width; 
      $offset_x = ($trim == 'center') ? $diff/2 : $diff; //full diff for trim right 
     } 
     if($current_height > $height) { 
      $diff = $current_height - $height; 
      $offset_y = ($trim = 'center') ? $diff/2 : $diff; 
     } 
    } 
    $new_image = imagecreatetruecolor($width,$height); 
    imagecopyresampled($new_image,$this->_image,0,0,$offset_x,$offset_y,$width,$height,$width,$height); 
    $this->_image = $new_image; 
} 
,

나는 그냥 자르기 뒤에 이론을 찾고 된 이후 imagecopyresampled()을 설명 귀찮게하지 않습니다,하지만 문서는 PHP의 GD 라이브러리를 사용하여 이미지 크기를 조정하면의 크기에 따라 메모리를 많이 염두에 여기 http://php.net/manual/en/function.imagecopyresampled.php

곰이다 영상. 저는 을 사용하고 있습니다. PHP는 Imagick이라는 래퍼 클래스를 가지고 있는데, 문제가 생길 때 찾아 볼만한 가치가 있습니다.

나는 이것이 당신에게 도움이되기를 바랍니다, 행운을 비네!

관련 문제