PHP的将图像添加到另一个图像,确切的位置 [英] php adding images to another image, exact positioning

查看:55
本文介绍了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.

代码起始位置"(在主图像上:navIcons.png)是从右下角开始的.

The code "starting position" (on the main image: navIcons.png) is from the Bottom Right.

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

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.

我真的很想从图片的左上角开始定位,所以我真的将图标右移了10px&从主图像的左上角向下向下8像素.

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');
?>

对于那些想知道为什么我什至不愿意这样做的人.简而言之,我想将16x16的图标添加到1张单个图像中,同时使用css显示该单个图标.这确实涉及我下载图像(精灵)并打开photoshop,添加新图标(定位),然后将其重新上传到服务器.不是一个大的考验,只是玩php.

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.

推荐答案

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  )  

从x,y坐标src_x,src_y(宽度为src_w,高度为src_h)开始,将src_im的一部分复制到dst_im上.定义的部分将被复制到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

$move_right = 10;
$move_down = 8;    
imagecopy($img, $mark, $move_right, $move_down, 0, 0, $icon_width, $icon_height);

这篇关于PHP的将图像添加到另一个图像,确切的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆