php将图像添加到另一个图像,精确定位
我有一段很酷的代码片段,运行良好,除了一件事之外。
该代码将采用我想要添加到现有图片中的图标。我也可以把它放在我想要的地方!这正是我需要做的。
然而,我坚持一件事,关于安置。
代码“起始位置”(在主图像上:navIcons.png)位于右下角。
我有 2 个变量: $move_left = 10; & $move_up = 8;. 因此,这意味着我可以将 icon.png 放置在距离右下角向左 10 像素、向上 8 像素的位置。
我真的很想从图像的左上角开始定位,所以我真的将图标向右移动 10px距主图像左上角位置向下 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 来显示该单个图标。这确实涉及我下载图像(精灵)并打开 Photoshop,添加新图标(定位它),然后将其重新上传到服务器。这不是一个巨大的考验,只是享受 php 的乐趣。
I have a cool snippet of code that works well, except one thing.
The code will take an icon I want to add to an existing picture. I can position it where I want too! Which is exactly what I need to do.
However, I'm stuck on one thing, concerning the placement.
The code "starting position" (on the main image: navIcons.png) is from the Bottom Right.
I have 2 variables: $move_left = 10; & $move_up = 8;.
So, the means I can position the icon.png 10px left, and 8px up, from the bottom right corner.
I really really want to start the positioning from the Top Left of the image, so I'm really moving the icon 10px right & 8px down, from the top left position of the main image.
Can someone look at my code and see if I'm just missing something that inverts that starting position?
<?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');
?>
For those who are wondering why I'd even bother doing this. In a nutshell, I like to add 16x16 icons to 1 single image, while using css to display that individual icon. This does involve me downloading the image (sprite) and open photoshop, add the new icon (positioning it), and reuploading it to the server. Not a massive ordeal, but just having fun with php.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 x,y 坐标 src_x、src_y 开始,将 src_im 的一部分复制到 dst_im 上,宽度为 src_w,高度为 src_h。定义的部分将被复制到 x,y 坐标 dst_x 和 dst_y 上。 (PHP.net
Copy a part of src_im onto dst_im starting at the x,y coordinates src_x, src_y with a width of src_w and a height of src_h. The portion defined will be copied onto the x,y coordinates, dst_x and dst_y. (PHP.net
事实证明,我并没有比我应该做的更简单。
它已经达到了我需要的定位,我只需要不要使 int $dst_x/$dst_y 复杂化。
我在那里放了简单的整数,它起作用了。
turns out, I'm not being more simple than I should be.
It's already to the positioning I need, I just need to NOT complicate the int $dst_x/$dst_y.
I put simple integers there, and it works.