php adding images to another image, exact positioning
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 开发者_StackOverflow社区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 )
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);
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.
精彩评论