2017-03-26 1 views
0

좋아요.이 워터 마크를 전체 너비와 높이로 설정하려고합니다. 그래야 이미지가 채워지지만 어떤 이유로 든 원래 크기로만 계속 나타납니다. 그래서 기본적으로 대신 이미지에 맞게 워터 마크를 스트레칭의GD 라이브러리를 사용하여 워터 마크 전체 너비와 높이

은하지 그냥 같은 크기에 워터 마크 이미지를 sclae하는 ... 이미지의 중간에

function watermark($sourcefile, $watermarkfile) { 

    # 
    # $sourcefile = Filename of the picture to be watermarked. 
    # $watermarkfile = Filename of the 24-bit PNG watermark file. 
    # 

    //Get the resource ids of the pictures 
    $watermarkfile_id = imagecreatefrompng($watermarkfile); 

    imageAlphaBlending($watermarkfile_id, false); 
    imageSaveAlpha($watermarkfile_id, true); 

    $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3)); 

    switch($fileType) { 
     case('gif'): 
      $sourcefile_id = imagecreatefromgif($sourcefile); 
      break; 

     case('png'): 
      $sourcefile_id = imagecreatefrompng($sourcefile); 
      break; 

     default: 
      $sourcefile_id = imagecreatefromjpeg($sourcefile); 
    } 

    //Get the sizes of both pix 
    $sourcefile_width=imageSX($sourcefile_id); 
    $sourcefile_height=imageSY($sourcefile_id); 
    $watermarkfile_width=imageSX($watermarkfile_id); 
    $watermarkfile_height=imageSY($watermarkfile_id); 

    $dest_x = ($sourcefile_width/2) - ($watermarkfile_width/2); 
    $dest_y = ($sourcefile_height/2) - ($watermarkfile_height/2); 

    // if a gif, we have to upsample it to a truecolor image 
    if($fileType == 'gif') { 
     // create an empty truecolor container 
     $tempimage = imagecreatetruecolor($sourcefile_width, 
                      $sourcefile_height); 

     // copy the 8-bit gif into the truecolor image 
     imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0, 
          $sourcefile_width, $sourcefile_height); 

     // copy the source_id int 
     $sourcefile_id = $tempimage; 
    } 

    imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0, 
         $watermarkfile_width, $watermarkfile_height); 

    //Create a jpeg out of the modified picture 
    switch($fileType) { 

     // remember we don't need gif any more, so we use only png or jpeg. 
     // See the upsaple code immediately above to see how we handle gifs 
     case('png'): 
      header("Content-type: image/png"); 
      imagepng ($sourcefile_id); 
      break; 

     default: 
      header("Content-type: image/jpg"); 
      imagejpeg ($sourcefile_id); 
    }   

    imagedestroy($sourcefile_id); 
    imagedestroy($watermarkfile_id); 

} 
+0

그러나 코드는 새 이미지의 가운데에 넣습니다. 너 뭔가 다른 걸하려고도하지 않았어? 당신은'$ dest_x'와'$ dest_y'로 중간을 계산합니다 ... 나는 당신의 질문을 얻지 못합니다, 아마 첫 번째 시도는 이미지를 늘리시겠습니까? '0,0'과'$ sourcefile_width','$ sourcefile_height'을 너비/높이로 사용하십시오.) –

+0

전체 소스 이미지에 워터 마크를 "스트레치"하려고합니다. –

답변

0

사용 imagescale을 워터 마크를 배치 원본 이미지를 사용한 다음 사용하십시오 imagecopy :