php adding images to another image, exact positioning

Posted by user271619 on Stack Overflow See other posts from Stack Overflow or by user271619
Published on 2010-04-08T00:51:18Z Indexed on 2010/04/08 0:53 UTC
Read the original article Hit count: 395

Filed under:
|

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?

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about image-manipulation