2009-11-13 88 views
4

我正在使用此代碼來創建水印。PHP水印

$image = '1.jpg'; 
    $overlay = 'stamp.png'; 
    $opacity = "20"; 
    if (!file_exists($image)) { 
     die("Image does not exist."); 
    } 
    // Set offset from bottom-right corner 
    $w_offset = 0; 
    $h_offset = 100; 
    $extension = strtolower(substr($image, strrpos($image, ".") + 1)); 
    // Load image from file 
    switch ($extension) 
    { 
     case 'jpg': 
     $background = imagecreatefromjpeg($image); 
     break; 
     case 'jpeg': 
     $background = imagecreatefromjpeg($image); 
     break; 
     case 'png': 
     $background = imagecreatefrompng($image); 
     break; 
     case 'gif': 
     $background = imagecreatefromgif($image); 
     break; 
     default: 
     die("Image is of unsupported type."); 
    } 
    // Find base image size 
    $swidth = imagesx($background); 
    $sheight = imagesy($background); 
    // Turn on alpha blending 
    imagealphablending($background, true); 
    // Create overlay image 
    $overlay = imagecreatefrompng($overlay); 
    // Get the size of overlay 
    $owidth = imagesx($overlay); 
    $oheight = imagesy($overlay); 
    // Overlay watermark 
    imagecopymerge($background, $overlay, $swidth - $owidth - $w_offset, $sheight - $oheight - $h_offset, 0, 0, $owidth, $oheight, $opacity); 
    imagejpeg($background,$image); 
    // Destroy the images 
    imagedestroy($background); 
    imagedestroy($overlay); 

png圖像包含一個文本,其他所有區域都是透明的。 但是當我執行這段代碼時,它將png應用於jpg,但透明度並不維持png。它顯示在一個盒子裏。

我該如何實現這一目標。即如果一個PNG包含transaparent部分,它應該在該部分顯示下面的圖像....?

+0

從文件擴展名獲取圖像類型是一種天真的方法:使用getimagesize()來代替! – ntd 2009-11-13 15:43:00

回答

4

用imagecopy替換imagecopymerge解決了這個問題。這裏是新代碼

function watermark($image){ 
    $overlay = '../../../photos/photosets/stamp.png'; 
    $opacity = "20"; 
    if (!file_exists($image)) { 
     die("Image does not exist."); 
    } 
    // Set offset from bottom-right corner 
    $w_offset = 0; 
    $h_offset = 100; 
    $extension = strtolower(substr($image, strrpos($image, ".") + 1)); 
    // Load image from file 
    switch ($extension) 
    { 
     case 'jpg': 
     $background = imagecreatefromjpeg($image); 
     break; 
     case 'jpeg': 
     $background = imagecreatefromjpeg($image); 
     break; 
     case 'png': 
     $background = imagecreatefrompng($image); 
     break; 
     case 'gif': 
     $background = imagecreatefromgif($image); 
     break; 
     default: 
     die("Image is of unsupported type."); 
    } 
    // Find base image size 
    $swidth = imagesx($background); 
    $sheight = imagesy($background); 
    // Turn on alpha blending 
    imagealphablending($background, true); 
    // Create overlay image 
    //$overlay = imagecreatefrompng($overlay); 
    // Get the size of overlay 
    $owidth = imagesx($overlay); 
    $oheight = imagesy($overlay); 

    $photo = imagecreatefromjpeg($image); 
    $watermark = imagecreatefrompng($overlay); 
      // This is the key. Without ImageAlphaBlending on, the PNG won't render correctly. 
    imagealphablending($photo, true); 
      // Copy the watermark onto the master, $offset px from the bottom right corner. 
    $offset = 10; 
    imagecopy($photo, $watermark, imagesx($photo) - imagesx($watermark) - $offset, imagesy($photo) - imagesy($watermark) - $offset, 0, 0, imagesx($watermark), imagesy($watermark)); 
      // Output to the browser 
    header("Content-Type: image/jpeg"); 
    imagejpeg($photo,$image); 
    // Overlay watermark 
    // Destroy the images 
    imagedestroy($background); 
    imagedestroy($overlay); 
} 
3

jpg格式不支持透明度,所以在概念上你必須:

  • 抓住從較大的圖像(以JPEG)的顯示像素,並把它們放入一個緩衝
  • 搶非透明像素從較小的圖像(水印),並將它們移動到該緩衝區中,沿途應用alpha值

您可能想讓庫執行此操作。我喜歡ImageMagick,特別是因爲它的內置到PHP ...這裏有一個如何使用它用於此目的從PHP的例子:

// Let's read the images. 
$glasses = new Imagick(); 
if (FALSE === $glasses->readImage($dir . '/glasses.png')) 
{ 
    throw new Exception(); 
} 

$face = new Imagick(); 
if (FALSE === $face->readImage($dir . '/face.jpg')) 
{ 
    throw new Exception(); 
} 

// Let's put the glasses on (10 pixels from left, 20 pixels from top of face). 
$face->compositeImage($glasses, Imagick::COMPOSITE_DEFAULT, 10, 20); 

而且here's the link爲ImageMagick的:: compositeImage PHP手冊頁(從上面的例子來了)。