2009-08-15 76 views
1

下面是我爲我的php照片上傳腳本所做的水印功能。我很好奇,如果有這樣做檢查該文件類型的部件更好的辦法,發現我不得不使用的代碼部分的2倍這個水印功能可以改進嗎(PHP)

<?PHP 
function watermark($source_file,$source_width,$source_height,$image_type) { 
    //first image below will be large then small in 1line if/else 
    $watermarksize = ($source_width > 300) ? '../images/fpwatermark.gif' : '../images/fpwatermark.gif'; 
    //now add the watermark to the image. 
    $watermark = imagecreatefromgif($watermarksize); 
    switch ($image_type) { 
     case 'gif': 
      $image = imagecreatefromgif($source_file); 
      break; 
     case 'jpg': 
      $image = imagecreatefromjpeg($source_file); 
      break; 
     case 'png': 
      $image = imagecreatefrompng($source_file); 
      break; 
     default: 
      $image = imagecreatefromjpeg($source_file); 
      break; 
    } 
    //get the dimensions of the watermark 
    list($water_width, $water_height) = getimagesize($watermarksize); 
    // Water mark process 
    $x = $source_width - $water_width - 8; //horizontal position 
    $y = $source_height - $water_height - 8; //vertical positon 
    // imagesy($image) can be the source images width 
    imagecopymerge($image, $watermark, $x, $y, 0, 0, $water_width, $water_height, 65); 
    switch ($image_type) { 
     case 'gif': 
      imagegif($image, $source_file, 90); 
      break; 
     case 'jpg': 
      imagejpeg($image, $source_file, 90); 
      break; 
     case 'png': 
      imagepng($image, $source_file, 90); 
      break; 
     default: 
      imagejpeg($image, $source_file, 90); 
      break; 
    } 
    imagedestroy($image); 
    return $source_file; 
} 
?> 

回答

3

你可以使用動態函數調用,如下圖所示。此代碼與您的代碼有細微的差別,因爲如果提供了無效的圖片類型,而不是假設它是jpeg,則代碼會返回。如果你堅持這種行爲,它應該很容易改變,很難。

並非所有這些圖像類型都受PHP支持,因此您可能希望在調用它們之前使用function_exists()進行檢查。調用一個不存在的函數是PHP中的一個致命錯誤。

<?PHP 
function watermark($source_file,$source_width,$source_height,$image_type) { 
    $validTypes = array("gif" => "gif", "jpg" => "jpeg", "jpeg" => "jpeg", "png" => "png"); 
    if (!array_key_exists($image_type, $validTypes)) { 
     trigger_error("Not a valid image type", E_USER_WARNING); 
     return NULL; 
    } 

    $inFunc = "imagecreatefrom" . $validTypes[$image_type]; 
    $outFunc = "image" . $validTypes[$image_type]; 

    //first image below will be large then small in 1line if/else 
    $watermarksize = ($source_width > 300) ? '../images/fpwatermark.gif' : '../images/fpwatermark.gif'; 
    //now add the watermark to the image. 
    $watermark = imagecreatefromgif($watermarksize); 

    // open the image using the assigned function 
    $image = $inFunc($source_file); 

    //get the dimensions of the watermark 
    list($water_width, $water_height) = getimagesize($watermarksize); 
    // Water mark process 
    $x = $source_width - $water_width - 8; //horizontal position 
    $y = $source_height - $water_height - 8; //vertical positon 
    // imagesy($image) can be the source images width 
    imagecopymerge($image, $watermark, $x, $y, 0, 0, $water_width, $water_height, 65); 

    // save the image 
    $outFunc($image, $source_file, 90); 

    imagedestroy($image); 
    return $source_file; 
} 

如果您已經安裝了EXIF擴展,你可以使用exif_imagetype()自動檢測圖像的類型。

另一個選項是更優雅一點,還含有較多的代碼是使用polymorfism:

<?php 

interface Codec { 
    public function open($file); 
    public function save($img); 
} 

class JPEGCodec implements Codec { 
    public function open($file) { return imagecreatefromjpeg($file); } 
    public function save($img, $out_file) { imagejpeg($img, $out_file, 90); } 
} 

class PNGCodec implements Codec { 
    public function open($file) { return imagecreatefrompng($file); } 
    public function save($img, $out_file) { imagepng($img, $out_file, 9); } 
} 

class GIFCodec implements Codec { 
    public function open($file) { return imagecreatefromgif($file); } 
    public function save($img, $out_file) { imagegif($img, $out_file); } 
} 

class WatermarkException extends Exception {} 

class Watermark 
{ 
    private $_codecs = array(); 

    public function __construct() 
    { 
     $this->_codecs["jpg"] = $this->_codecs["jpeg"] = new JPEGCodec(); 
     $this->_codecs["png"] = new PNGCodec(); 
     $this->_codecs["gif"] = new GIFCodec(); 
    } 

    function watermark($source_file,$source_width,$source_height,$image_type) { 
     if (!array_key_exists($image_type, $this->_codecs)) { 
      throw new WatermarkException("Not a valid image type"); 
     } 

     $codec = $this->_codecs[$image_type]; 

     //first image below will be large then small in 1line if/else 
     $watermarksize = ($source_width > 300) ? '../images/fpwatermark.gif' : '../images/fpwatermark.gif'; 
     //now add the watermark to the image. 
     $watermark = imagecreatefromgif($watermarksize); 

     // load image 
     $image = $codec->open($source_file); 

     //get the dimensions of the watermark 
     list($water_width, $water_height) = getimagesize($watermarksize); 
     // Water mark process 
     $x = $source_width - $water_width - 8; //horizontal position 
     $y = $source_height - $water_height - 8; //vertical positon 
     // imagesy($image) can be the source images width 
     imagecopymerge($image, $watermark, $x, $y, 0, 0, $water_width, $water_height, 65); 

     // save image 
     $codec->save($image, $source_file); 

     imagedestroy($image); 
     return $source_file; 
    } 
} 

我知道你可能會喜歡第一個。 :)

+0

謝謝,那看起來好多了!我也會將文件類型從使用的頁面傳遞到 – JasonDavis 2009-08-15 21:37:48

+0

的頁面上。謝謝,是的,這真的讓我感到困惑,但很高興看到,並作爲未來的參考,謝謝 – JasonDavis 2009-08-15 21:58:49

+1

ImageCreateFromString(file_get_contents($ source_file)) ; – 2009-09-14 13:15:27