我有上传图片的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 :(得分: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);
}
答案 1 :(得分: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');