如何在图像处理中设置x和y坐标。

时间:2015-11-02 13:30:41

标签: php image-processing image-manipulation

我只是在php中进行图像水印,但是没有像我想要的那样设置图像,这里是我的php文件代码。

<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
// Give the Complete Path of the folder where you want to save the image    
$folder="uploads/";
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "$folder".$_FILES["fileToUpload"]["name"]);
$file='uploads/'.$_FILES["fileToUpload"]["name"];

$uploadimage=$folder.$_FILES["fileToUpload"]["name"];
$newname=$_FILES["fileToUpload"]["name"];

// Set the thumbnail name
$thumbnail = $folder.$newname."_thumbnail.jpg"; 
$actual = $folder.$newname.".jpg";
$imgname=$newname."_thumbnail.jpg";

// Load the mian image
$source = imagecreatefromjpeg($uploadimage);

// load the image you want to you want to be watermarked
$watermark = imagecreatefrompng('uploads/logo1.png');

// get the width and height of the watermark image
$water_width = imagesx($watermark);
$water_height = imagesy($watermark);

// get the width and height of the main image image
$main_width = imagesx($source);
$main_height = imagesy($source);

// Set the dimension of the area you want to place your watermark we use 0
// from x-axis and 0 from y-axis 
$dime_x = 0;
$dime_y = 0;

// copy both the images
imagecopy($source, $watermark, $dime_x, $dime_y, 0, 0, $water_width, $water_height);

// Final processing Creating The Image
imagejpeg($source, $thumbnail, 100);
}
?>
<img src='uploads/<?php echo $imgname;?>'>
</body>
</html>

和我的HTML代码也工作得很好。但是生成的图像问题就是这样 enter image description here

带有&#39; JACLIN ADMIN&#39;的文字是我的png图像,我想从中间向上和向左应用它。我只是为这两个都设置了0但问题是如何在高度和宽度不同的图像尺寸时将它放在动态中间?请帮助我。

2 个答案:

答案 0 :(得分:2)

首先,您需要找到图像的中间点:

$im_middle_w = $main_width/2;
$im_middle_h = $main_height/2;

然后你只需要在那里添加水印,但你需要将水印向左移动一半(所以它实际上居中):

$dime_x = $im_middle_w - $water_width/2;
$dime_y = $im_middle_h - $water_height/2;

Haven没有对它进行测试,但它应该可行。如果它不起作用,请随时链接图像,我自己会看到代码。

答案 1 :(得分:0)

首先设定值:

$ watermark_pos_x =(imagesx($ image)/ 2) - (imagesx($ watermark)/ 2) - 15; $ watermark_pos_y =(imagesy($ image)/ 2) - (imagesy($ watermark)/ 2) - 10;

然后在Function:

上应用值之后

//合并源图像和水印

imagecopy($ image,$ watermark,$ watermark_pos_x,$ watermark_pos_y,0,0,imagesx($ watermark),imagesy($ watermark));