php将图像添加到另一个图像,精确定位

标签 php image-manipulation

我有一个很酷的代码片段,效果很好,除了一件事。

该代码将采用我想添加到现有图片的图标。我也可以把它放在我想要的地方!这正是我需要做的。

但是,我坚持一件事,关于位置。

代码“起始位置”(在主图像上:navIcons.png)来自右下角。

我有 2 个变量: $move_left = 10; & $move_up = 8;。
因此,这意味着我可以将 icon.png 从右下角向左放置 10 像素,向上放置 8 像素。

我真的很想从图像的左上角开始定位,所以我真的将图标从主图像的左上角位置向右移动 10 像素和向下移动 8 像素。

有人可以看看我的代码,看看我是否只是遗漏了一些可以反转起始位置的东西?

<?php

function attachIcon($imgname)
{
    $mark = imagecreatefrompng($imgname);
 imagesavealpha($mark, true);

    list($icon_width, $icon_height) = getimagesize($imgname);

    $img = imagecreatefrompng('images/sprites/navIcons.png');
 imagesavealpha($img, true);

    $move_left = 10;
    $move_up = 9;

    list($mainpic_width, $mainpic_height) = getimagesize('images/sprites/navIcons.png');
    imagecopy($img, $mark, $mainpic_width-$icon_width-$move_left, $mainpic_height-$icon_height-$move_up, 0, 0, $icon_width, $icon_height);
    imagepng($img);  // display the image + positioned icon in the browser
  //imagepng($img,'newnavIcon.png'); // rewrite the image with icon attached.
}

header('Content-Type: image/png');
 attachIcon('icon.png');
?>

对于那些想知道我为什么要这样做的人。简而言之,我喜欢将 16x16 图标添加到 1 个单个图像,同时使用 css 显示该单个图标。这确实涉及我下载图像( Sprite )并打开 photoshop,添加新图标(定位它),然后将其重新上传到服务器。不是一个巨大的考验,但只是享受 php 的乐趣。

最佳答案

bool imagecopy  (  resource $dst_im  ,  resource $src_im  ,  int $dst_x  ,  int $dst_y  ,  int $src_x  ,  int $src_y  ,  int $src_w  ,  int $src_h  )  

将 src_im 的一部分复制到 dst_im 上,从 x,y 坐标 src_x、src_y 开始,宽度为 src_w,高度为 src_h。定义的部分将被复制到 x,y 坐标、dst_x 和 dst_y。 ( PHP.net
$move_right = 10;
$move_down = 8;    
imagecopy($img, $mark, $move_right, $move_down, 0, 0, $icon_width, $icon_height);

关于php将图像添加到另一个图像,精确定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2596879/

相关文章:

php - 在 PHP 中将 IP 地址数组中的数字加一

php - 如何从 url 中查找 slug 名称并进行动态查询语句

php - 使用 Smarty 修复响应式 Bootstrap 列

javascript - Ajax返回值显示代码

php - 如何使用 laravel 中的干预图像处理库以自定义比例调整图像大小

php - 使用 MySQL 表值作为标题

python - 图像处理,在 Python 中?

algorithm - 识别图片中的特定区域

image - 以编程方式 "smudge"图像的最佳方法是什么?

silverlight - Silverlight 可以执行以下操作吗?