我有一個很酷的代碼片段,除了一件事情以外,效果很好。php將圖像添加到另一個圖像,精確定位
該代碼將採取我想要添加到現有圖片的圖標。我可以把它放在我想要的地方!這正是我需要做的。
但是,我被困在一件事上,關於安置。
代碼「起始位置」(在主圖像上:navIcons.png)來自底部右側。
我有2個變量:$ move_left = 10; & $ move_up = 8 ;. 所以,我的意思是可以將icon.png左上角10px和右下角8px。
我真的很想從圖像的左上角開始定位,所以我確實從主圖像的左上角位置向下移動了圖標10px右邊的12px。
有人可以看看我的代碼,看看我是否錯過了一些反轉該起始位置的東西?
<?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圖標添加到單個圖像,而使用css來顯示單個圖標。這涉及到我下載圖像(sprite)並打開photoshop,添加新圖標(將其定位),然後將其重新上傳到服務器。不是一個巨大的考驗,但只是與PHP的樂趣。