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

how to set x and y coordinates in 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 代码也能正常工作 fine.but 生成图像的问题就是这样

带有 'JACLIN ADMIN' 的文本是我的 png 图像,我想将它从上到左应用在中间。我只是为两者都设置了 0,但问题是当图像大小具有不同的高度和宽度时,我如何将它放在动态中间?请帮助我。

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

$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;

尚未测试,但应该可以。如果它不起作用,请随时 link 图片,我会自己查看代码。

第一个设置值:

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

然后在函数上应用值后:

//合并源图和水印

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