2014-01-28 74 views
1

我有php代碼上傳圖片和添加文字水印,但我有一個輸出的小問題。上傳圖片與水印文本

我的代碼的成功結果與文字水印這樣的形象:result 1

,但我想這樣的輸出:result 2

這是我的代碼:

function UploadImage($img_name){ 
    $vdir_upload = "img/upload/"; 
    $vfile_upload = $vdir_upload . $img_name; 
    $file_name = basename($_FILES["img_1"]["name"]); 
    move_uploaded_file($_FILES["img_1"]["tmp_name"], $vfile_upload); 

    switch (strtolower(pathinfo($file_name, PATHINFO_EXTENSION))) { 
      case "jpg" : 
       $im_src = imagecreatefromjpeg($vfile_upload); 
      break; 
      case "jpeg" : 
       $im_src = imagecreatefromjpeg($vfile_upload); 
      break; 
      case "gif" : 
       $im_src = imagecreatefromgif($vfile_upload); 
      break; 
      case "png" : 
       $im_src = imagecreatefrompng($vfile_upload); 
      break; 
      default : 
       trigger_error("Error Bad Extention"); 
       exit(); 
      break; 
    } 
    $src_width = imageSX($im_src); 
    $src_height = imageSY($im_src); 
    $dst_width = 1979; 
    $dst_height = ($dst_width/$src_width)*$src_height; 
    $im = imagecreatetruecolor($dst_width,$dst_height); 
    imagecopyresampled($im, $im_src, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); 
    $font = 'Aliquam.ttf'; 
    $red = imagecolorallocate($im, 255, 0, 0); 
    imagettftext($im, 30, 0, 10, 50, $red, $font, $_POST["color"]); 
    imagejpeg($im,$vdir_upload . $_POST["number"].".jpg"); 
    imagedestroy($im_src); 
    imagedestroy($im); 
    } 

我怎麼能得到結果如上?對不起我的英語不好,先謝謝了...

+0

看一看在GD一些教程,繪製一個矩形,畫在上面的文字。 – n00dle

回答

1

首先 - 如果你想擴大圖像的寬度的餘量,我會採取圖像的原始寬度,加上你的邊緣寬度(讓我們稱它爲$ extraWidth,並假定它在某處定義)並用新寬度調整imagecreatetruecolor調用。 然後,您分配一種顏色,併爲您的顏色填充一個矩形的剩餘空間,如下所示。

$im = imagecreatetruecolor($dst_width+$extraWidth, $dst_height); 
$color = imagecolorallocate($im, 0, 0, 255); // this is blue - change to what you want 
imagefilledrectangle($im, $dst_width, 0, $dst_width+$extraWidth, $dst_height, $color); 

請注意我沒有測試過這個代碼,只是提到了文檔。

$pixel_height_of_character = 30; // change this to actual pixel height of a char 
$pixel_gap_between_chars = 3; 
$start_from_edge_of_margin = 3; 
$string_chars = str_split($_POST['color']); 
$start = ($dst_height/2) - ((count($string_chars) * ($pixel_height_of_character+$pixel_gap_between_chars))/2) 
// probably should round this 
// also need to deduct one half of pixel gap from the result for centering purposes - i think - double check my math. 
$left = $start_from_edge_of_margin + $dst_width; 
foreach($string_chars as $char){ 
    $top = $start + $pixel_height_of_character; 
    imagettftext($im, 30, 0, $left, $top, $red, $font, $char); 
    $start = $top + $pixel_gap_between_chars; 
} 

所以,這是相當多的解釋。

基本上,你計算每個字符的尺寸 - 然後使用這些尺寸計算第一個字符必須在哪裏開始 - 然後在循環中一次繪製一個字符。

該代碼完全無法表示 - 如果提供的單詞太長,則會超出圖像的邊界,因此您應該對此進行測試。此外,它可能不會完美 - 但這是一個好的開始。

修改後的代碼:

function UploadImage($img_name){ 
    $vdir_upload = "img/upload/"; 
    $vfile_upload = $vdir_upload . $img_name; 
    $file_name = basename($_FILES["img_1"]["name"]); 
    move_uploaded_file($_FILES["img_1"]["tmp_name"], $vfile_upload); 

    switch (strtolower(pathinfo($file_name, PATHINFO_EXTENSION))) { 
      case "jpg" : 
       $im_src = imagecreatefromjpeg($vfile_upload); 
      break; 
      case "jpeg" : 
       $im_src = imagecreatefromjpeg($vfile_upload); 
      break; 
      case "gif" : 
       $im_src = imagecreatefromgif($vfile_upload); 
      break; 
      case "png" : 
       $im_src = imagecreatefrompng($vfile_upload); 
      break; 
      default : 
       trigger_error("Error Bad Extention"); 
       exit(); 
      break; 
    } 
    $src_width = imageSX($im_src); 
    $src_height = imageSY($im_src); 
    $dst_width = 1979; 
    $dst_height = ($dst_width/$src_width)*$src_height; 
     $im = imagecreatetruecolor($dst_width+$extraWidth, $dst_height); 
     $color = imagecolorallocate($im, 0, 0, 255); // this is blue - change to what you want 
     imagefilledrectangle($im, $dst_width, 0, $dst_width+$extraWidth, $dst_height, $color); 
    imagecopyresampled($im, $im_src, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); 
    $font = 'Aliquam.ttf'; 
    $red = imagecolorallocate($im, 255, 0, 0); 
    $pixel_height_of_character = 30; // change this to actual pixel height of a char 
    $pixel_gap_between_chars = 3; 
    $start_from_edge_of_margin = 3; 
    $string_chars = str_split($_POST['color']); 
    $start = ($dst_height/2) - ((count($string_chars) * ($pixel_height_of_character+$pixel_gap_between_chars))/2) 
    // probably should round this 
    // also need to deduct one half of pixel gap from the result for centering purposes - i think - double check my math. 
    $left = $start_from_edge_of_margin + $dst_width; 
    foreach($string_chars as $char){ 
     $top = $start + $pixel_height_of_character; 
     imagettftext($im, 30, 0, $left, $top, $red, $font, $char); 
     $start = $top + $pixel_gap_between_chars; 
    } 
    imagejpeg($im,$vdir_upload . $_POST["number"].".jpg"); 
    imagedestroy($im_src); 
    imagedestroy($im); 
    } 
+0

thx爲您的答案,這是我的工作.. –

0
class Watermark { 
/** 
* 
* @var image resource 
*/ 
private $image = null; 

/** 
* 
* @var image resource 
*/ 
private $watermark = null; 

/** 
* 
* @var string 
*/ 
private $output_file = null; 

/** 
* 
* @var int 
*/ 
private $type = ''; 


const BOTTOM_RIGHT = 1; 
const CENTER = 2; 
const BOTTOM_RIGHT_SMALL = 3; 

/** 
* 
* @param string $path_to_image 
*/ 
public function __construct($path_to_image = ''){ 
    if (file_exists($path_to_image)){ 
     $this->image = $path_to_image; 
    } 
    $this->type = Watermark::BOTTOM_RIGHT; 
} 

/** 
* 
* @param string $path_to_watermark 
* @return boolean 
*/ 
public function setWatermarkImage($path_to_watermark){ 
    if (file_exists($path_to_watermark) && preg_match('/\.png$/i',$path_to_watermark)){ 
     $this->watermark = $path_to_watermark; 
     return true; 
    } 
    return false; 
} 

/** 
* 
* @return boolean 
*/ 
public function save(){ 
    $this->output_file = $this->image; 
    return $this->process(); 
} 

/** 
* 
* @param string $path_to_image 
* @return boolean 
*/ 
public function saveAs($path_to_image){ 
    $this->output_file = $path_to_image; 
    return $this->process(); 
} 

/** 
* 
* @param int $type 
*/ 
public function setType($type){ 
    $this->type = $type; 
} 

/** 
* 
* @return boolean 
*/ 
private function process(){ 
    $watermark = imagecreatefrompng($this->watermark); 
    if ($watermark){ 
     $image = imagecreatefromjpeg($this->image); 
     if ($image){ 
      switch ($this->type){ 
       case Watermark::BOTTOM_RIGHT: 
        return $this->watermark_bottom_right($image, $watermark); 
        break; 
       case Watermark::CENTER: 
        return $this->watermark_center($image, $watermark); 
        break; 
       case Watermark::BOTTOM_RIGHT_SMALL: 
        return $this->watermark_bottom_right_small($image, $watermark); 
        break; 
      } 
      return true; 
     }else{ 
      return false; 
     } 
    }else { 
     return false; 
    } 
} 

/** 
* 
* @param image resource $image 
* @param image resource $watermark 
* @return boolean 
*/ 
private function watermark_bottom_right(&$image, &$watermark){ 
    $watermark_width = imagesx($watermark); 
    $watermark_height = imagesy($watermark); 
    $size = getimagesize($this->image); 
    $dest_x = $size[0] - $watermark_width - 5; 
    $dest_y = $size[1] - $watermark_height - 5; 
    imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height); 

    imagejpeg($image,$this->output_file,100); 
    imagedestroy($image); 
    imagedestroy($watermark); 
    return true; 
} 

/** 
* 
* @param image resource $image 
* @param image resource $watermark 
* @return booelan 
*/ 
private function watermark_center(&$image, &$watermark){ 
    $size = getimagesize($this->image); 
    $watermark_x = imagesx($watermark); 
    $watermark_y = imagesy($watermark); 
    $im_x = $size[0]; 
    $im_y = $size[1]; 
    $cof = $im_x/($watermark_x*1.3); // 5/1 = im_x/(wx*cof) ; wx*cof = im_x/5 ; cof = im_x/wx*5 
    $w = intval($watermark_x*$cof); 
    $h = intval($watermark_y*$cof); 

    $watermark_mini = ImageCreateTrueColor($w, $h); 
    imagealphablending($watermark_mini, false); 
    imagesavealpha($watermark_mini,true); 
    ImageCopyResampled ($watermark_mini, $watermark, 0, 0, 0, 0, $w, $h, $watermark_x, $watermark_y); 


    $dest_x = $im_x - $w - (($im_x-$w)/2); 
    $dest_y = $im_y - $h - (($im_y-$h)/2); 

    imagecopy($image, $watermark_mini, $dest_x, $dest_y, 0, 0, $w, $h); 

    imagejpeg($image,$this->output_file,100); 
    imagedestroy($image); 
    imagedestroy($watermark); 
    return true; 
} 

/** 
* 
* @param image resource $image 
* @param image resource $watermark 
* @return boolean 
*/ 
private function watermark_bottom_right_small(&$image, &$watermark){ 
    $size = getimagesize($this->image); 
    $orig_watermark_x = imagesx($watermark); 
    $orig_watermark_y = imagesy($watermark); 
    $im_x = $size[0]; 
    $im_y = $size[1]; 
    $cof = $im_x/($orig_watermark_x*5); // 5/1 = im_x/(wx*cof) ; wx*cof = im_x/5 ; cof = im_x/wx*5 
    $w = intval($orig_watermark_x*$cof); 
    $h = intval($orig_watermark_y*$cof); 

    $watermark_mini = ImageCreateTrueColor($w, $h); 
    imagealphablending($watermark_mini, false); 
    imagesavealpha($watermark_mini,true); 
    ImageCopyResampled ($watermark_mini, $watermark, 0, 0, 0, 0, $w, $h, $orig_watermark_x, $orig_watermark_y); 
    // 
    $dest_x = $size[0] - $w - 5; 
    $dest_y = $size[1] - $h -5; 

    imagecopy($image, $watermark_mini, $dest_x,$dest_y , 0, 0, $w, $h); 
    imagejpeg($image,$this->output_file,100); 
    imagedestroy($image); 
    imagedestroy($watermark); 
    imagedestroy($watermark_mini); 
    return true; 
} 
} 

使用

$watermark = new Watermark('file_path.jpg'); 
$watermark->setWatermarkImage('watermark_path.png'); 
$watermark->setType(Watermark::CENTER); 
$watermark->saveAs('file_path.jpg');