gpt4 book ai didi

php - 将一个图像添加到另一个图像中

转载 作者:行者123 更新时间:2023-12-01 14:30:06 26 4
gpt4 key购买 nike

我正在尝试将图像添加到模板,该模板也是另一幅图像;我正在使用 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');
?>

-Am I missing anything? - Is it supposed to work with any file extensions? - can the template be .PNG file and the other image another type (lets say .JPG)!May be I need to change something;

感谢任何帮助!

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');
?>

但还是不行!

最佳答案

我想 @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');
?>

关于php - 将一个图像添加到另一个图像中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19262225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com