2016-06-09 5 views
0

프로젝트에서이 라이브러리를 Intervention Image 이미지 조작 라이브러리를 사용하고 있는데 원본 이미지 전체에 워터 마크 이미지를 추가해야합니다.반복 워터 마크 PHP

다음 예제와 같이 원본 이미지 전체에 워터 마크 이미지를 반복 할 수 있습니까?

stock-photo-79576145-old-room

나는 다음 코드를 시도,하지만 나를 위해 작동하지 않습니다.

$thumbnail = $manager->make($name); 
$watermark = $manager->make($watermarkSource); 
$x = 0; 

while ($x < $thumbnail->width()) { 
    $y = 0; 

    while($y < $thumbnail->height()) { 
     $thumbnail->insert($watermarkSource, 'top-left', $x, $y); 
     $y += $watermark->height(); 
    } 

    $x += $watermark->width(); 
} 

$thumbnail->save($name, 80); 

답변

1

나는이 문제를 Laravel Framework에서 사용하는 중재 이미지 라이브러리를 통해 방금 해결했습니다. 여기 코드 스 니펫이 있습니다.

public function watermarkPhoto(String $originalFilePath,String $filePath2Save){ 

    $watermark_path='photos/watermark.png'; 
    if(\File::exists($watermark_path)){ 

     $watermarkImg=Image::make($watermark_path); 
     $img=Image::make($originalFilePath); 
     $wmarkWidth=$watermarkImg->width(); 
     $wmarkHeight=$watermarkImg->height(); 

     $imgWidth=$img->width(); 
     $imgHeight=$img->height(); 

     $x=0; 
     $y=0; 
     while($y<=$imgHeight){ 
      $img->insert($watermark_path,'top-left',$x,$y); 
      $x+=$wmarkWidth; 
      if($x>=$imgWidth){ 
       $x=0; 
       $y+=$wmarkHeight; 
      } 
     } 
     $img->save($filePath2Save); 

     $watermarkImg->destroy(); 
     $img->destroy(); // to free memory in case you have a lot of images to be processed 
    } 
    return $filePath2Save; 
} 

PHP 버전 7을 사용하는 경우 함수 인수에서 문자열 유형 선언을 제거하십시오. 당신이 Laravel 프레임 워크를 사용하지 않는 경우 또한 그것은

public function watermarkPhoto($originalFilePath, $filePath2Save){....} 

을하고 당신은 함수에서 redundand 검사를 제거 포함 File 클래스가 없습니다.

 if(\File::exists($watermark_path)) 

그래서 간단한 프레임 워크에 얽매 기능은 다음과 같습니다
function watermarkPhoto($originalFilePath, $filePath2Save){ 

     $watermark_path='photos/watermark.png'; 
     $watermarkImg=Image::make($watermark_path); 
     $img=Image::make($originalFilePath); 
     $wmarkWidth=$watermarkImg->width(); 
     $wmarkHeight=$watermarkImg->height(); 

     $imgWidth=$img->width(); 
     $imgHeight=$img->height(); 

     $x=0; 
     $y=0; 
     while($y<=$imgHeight){ 
      $img->insert($watermark_path,'top-left',$x,$y); 
      $x+=$wmarkWidth; 
      if($x>=$imgWidth){ 
       $x=0; 
       $y+=$wmarkHeight; 
      } 
     } 
     $img->save($filePath2Save); 

     $watermarkImg->destroy(); 
     $img->destroy(); 

    return $filePath2Save; 
} 

는 또한 투명 배경 형식을 PNG로에 워터 마크 이미지가 필요합니다.

+0

답장을 보내 주셔서 대단히 감사합니다. 제 코드는 "imagick"드라이버에 문제가 있었지만 나중에 정상적으로 작동한다는 것을 알았습니다. 아직도, 나는 그것을 나의 프로젝트에서 사용하고있다. – Shifrin