我正在尝试将一张图片添加到另一张图片作为模板中;我使用的是PHP CodeIgniter框架;当两个文件都是PNG格式时,我有一个很好的代码片段可以很好地实现这个目的;我的代码如下:
<?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');
?>
然而,如果模板“images/sprites/navIcons.png”是一个png文件,而另一个图像是另一种类型(比如说.JPG),那么我的下面的代码就无法正常工作:
<?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/jpg');
attachIcon('icon.jpg');
?>
-我有什么遗漏吗? -它是否应该与任何文件扩展名一起工作? -模板可以是.PNG文件,而其他图像可以是另一种类型(比如.JPG)!也许我需要更改一些内容;
感谢您的帮助!
PS:我从这个论坛的一个非常旧的帖子中得到了代码,但我认为它非常古老,没有人会查看它,所以我在一个新的线程中提出了我的问题!谢谢
谢谢sinni800,
我本来应该在评论中回复你,但由于我想添加我的代码,所以我在这里添加了一条新消息来回复。
我还尝试了以下代码,其中我使用了“imagecreatefromjpeg”;我的代码:
<?php
function attachIcon($imgname) {
$mark = imagecreatefromjpeg($imgname);
imagesavealpha($mark, true);
list($icon_width, $icon_height) = getimagesize($imgname);
$img = imagecreatefrompng(base_url() . '/uploaded_images/output/viewer.png');
imagesavealpha($img, true);
$move_left = 280;
$move_up = 450;
list($mainpic_width, $mainpic_height) = getimagesize(base_url() . '/uploaded_images/output/viewer.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/jpeg');
attachIcon( base_url().'/uploaded_images/output/koala.jpg');
?>
但仍未生效!