Php 将图像添加到另一个图像中
我试图添加一个图像到模板,这也是另一个图像;我正在使用PHP codeigniter;我有一段很好的代码,当两个文件都是.PNG文件时,可以很好地实现这一目的;我的代码如下:Php 将图像添加到另一个图像中,php,codeigniter,Php,Codeigniter,我试图添加一个图像到模板,这也是另一个图像;我正在使用PHP codeigniter;我有一段很好的代码,当两个文件都是.PNG文件时,可以很好地实现这一目的;我的代码如下: <?php function attachIcon($imgname) { $mark = imagecreatefrompng($imgname); imagesavealpha($mark, true); list($icon_width, $icon_height) = getimages
<?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');
?>
但还是不行 很明显,您的问题在于您试图将
imagecreatefrompng()
用于JPEG
请看下面的评论。尤其是gmail.com上的juozaspo所写的。它有一个打开任何图像类型的函数,因为PHP的内部图像函数不支持自动选择文件类型
function open_image ($file) {
//detect type and process accordinally
global $type;
$size=getimagesize($file);
switch($size["mime"]){
case "image/jpeg":
$im = imagecreatefromjpeg($file); //jpeg file
break;
case "image/gif":
$im = imagecreatefromgif($file); //gif file
break;
case "image/png":
$im = imagecreatefrompng($file); //png file
break;
default:
$im=false;
break;
}
return $im;
}
我想@sinni800想让你做这样的事
<?php
function open_image ($file) {
$size=getimagesize($file);
switch($size["mime"]){
case "image/jpeg":
$im = imagecreatefromjpeg($file); //jpeg file
break;
case "image/gif":
$im = imagecreatefromgif($file); //gif file
break;
case "image/png":
$im = imagecreatefrompng($file); //png file
break;
default:
$im=false;
break;
}
return $im;
}
function attachIcon($imgname)
{
$mark = open_image($imgname);
if($mark !== false){
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
}
}
header('Content-Type: image/png');
attachIcon('icon.png');
?>