【问题标题】:how to set x and y coordinates in image manipulation.如何在图像处理中设置 x 和 y 坐标。
【发布时间】:2016-02-02 09:42:05
【问题描述】:

我只是在 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 代码也可以正常工作。但是生成图像的问题就是这样

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

【问题讨论】:

    标签: php image-processing image-manipulation


    【解决方案1】:

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

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

    尚未对其进行测试,但它应该可以工作。如果它不起作用,请随意链接图像,我会自己查看代码。

    【讨论】:

      【解决方案2】:

      第一次设定值:

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

      【讨论】:

        猜你喜欢
        • 2014-12-10
        • 2021-05-14
        • 2011-11-13
        • 2020-06-06
        • 2014-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多