将 3 个具有透明度的图像组合在一起

2024-07-04

我正在做这个项目,我想将三张图像合并为一张。

到目前为止,有效的方法是使每个图像(jpg)透明。 但将它们合并成新的 png 图像后,透明度就消失了。

这是代码:

function CreateMyCoolOutfitBaby () {

    $Outfitwidth = 250;
    $Outfitheight = 350;

    $newoutfit = imagecreatetruecolor($Outfitwidth, $Outfitheight); // create empty new image

    $dress = imagecreatefromstring(file_get_contents("http://images180.affili.net/001089/c/bb9c33888aae07d839b6724e31f462bc.jpg"));
    imagealphablending($dress, true);
    $white = imagecolorallocate($dress, 255, 255, 255);
    imagecolortransparent($dress, $white);

    $bag = imagecreatefromstring(file_get_contents("http://images180.affili.net/000698/6/40afc9ed65a94177635d1c7675fd3756.jpg"));
    imagealphablending($bag, true);
    $white = imagecolorallocate($bag, 255, 255, 255);
    imagecolortransparent($bag, $white);

    $shoe = imagecreatefromstring(file_get_contents("http://images180.affili.net/000389/4/4482beed9a949f895debe13d9dd28704.jpg"));
    imagealphablending($shoe, true);
    $white = imagecolorallocate($shoe, 255, 255, 255);
    imagecolortransparent($shoe, $white);




    imagealphablending($newoutfit,true); //on each new layer.

    // UN-COMMENT THIS PARAGRAPH TO SEE SINGLE FILES BEING TRANSPARENT
    //header('Content-Type: image/png');
    //imagepng($dress); // output to the browser
    //imagepng($bag); // output to the browser
    //imagepng($shoe); // output to the browser

    // WORKS TILL HERE :)


    // get with and height from images
    $Dresswidth = imagesx($dress);  $Bagwidth = imagesx($bag);  $Shoewidth = imagesx($shoe);    
    $Dressheight = imagesy($dress); $Bagheight = imagesy($bag); $Shoeheight = imagesy($shoe);


    // Calc Dress Position (middle)
    $DressPosWidth  = ( ( $Outfitwidth / 2 )  - ( $Dresswidth / 2 ) );
    $DressPosHeight = ( ( $Outfitheight / 2 ) - ( $Dressheight / 2 ) );
    // Calc Bag Position (right beside dress)
    $BagPosWidth    = ( ( $Outfitwidth / 2 )  - ( $Bagwidth / 2 ) + 60 ); // place bag in middle but more to the right
    $BagPosHeight = ( ( $Outfitheight / 2 ) - ( $Bagheight / 2 ) );
    // Calc Shoe Position (middle under dress)
    $ShoePosWidth   = ( ( $Outfitwidth / 2 )  - ( $Shoewidth / 2 ) );
    $ShoePosHeight = ( ( $Outfitheight / 2 ) - ( $Shoeheight / 2 ) + 100); // place further down

    // merge images together
    imagecopy($newoutfit,$dress,$DressPosWidth,$DressPosHeight,0,0,$Dresswidth,$Dressheight);
    imagealphablending($newoutfit,true);
    imagecopy($newoutfit,$bag,$BagPosWidth,$BagPosHeight,0,0,$Bagwidth,$Bagheight);
    imagealphablending($newoutfit,true);
    imagecopy($newoutfit,$shoe,$ShoePosWidth,$ShoePosHeight,0,0,$Shoewidth,$Shoeheight);
    imagealphablending($newoutfit,true);

    // make sure alpha is saved
    imagesavealpha($newoutfit, true);
    imagealphablending($newoutfit, true);

    // output to browser
    header('Content-Type: image/png');
    imagepng($newoutfit); // output to the browser
    imagedestroy($newoutfit);
}

所以单个图像透明度效果很好。就在 $newoutfit 推出时,我得到了黑色背景,而图像又回到了白色背景......

我能做些什么?


使用修复了这个问题图像复制合并() http://php.net/manual/en/function.imagecopymerge.php :

<?php

CreateMyCoolOutfitBaby ();

function CreateMyCoolOutfitBaby () {

    $Outfitwidth = 250;
    $Outfitheight = 350;

    $newoutfit = imagecreatetruecolor($Outfitwidth, $Outfitheight); // create empty new image
    imagealphablending($newoutfit, true);
    $black = imagecolorallocate($newoutfit, 0, 0, 0);imagecolortransparent($newoutfit, $black); // Making it transparent

    $dress = imagecreatefromstring(file_get_contents('http://example.com/dress.jpg'));
    imagealphablending($dress, true);
    $white = imagecolorallocate($dress, 255, 255, 255);
    imagecolortransparent($dress, $white);

    $bag = imagecreatefromstring(file_get_contents('http://example.com/bag.jpg'));
    imagealphablending($bag, true);
    $white = imagecolorallocate($bag, 255, 255, 255);
    imagecolortransparent($bag, $white);

    $shoe = imagecreatefromstring(file_get_contents('http://example.com/shoe.jpg'));
    imagealphablending($shoe, true);
    $white = imagecolorallocate($shoe, 255, 255, 255);
    imagecolortransparent($shoe, $white);

    // get with and height from images
    $Dresswidth = imagesx($dress);  $Bagwidth = imagesx($bag);  $Shoewidth = imagesx($shoe);    
    $Dressheight = imagesy($dress); $Bagheight = imagesy($bag); $Shoeheight = imagesy($shoe);


    // Calc Dress Position (middle)
    $DressPosWidth  = ( ( $Outfitwidth / 2 )  - ( $Dresswidth / 2 ) );
    $DressPosHeight = ( ( $Outfitheight / 2 ) - ( $Dressheight / 2 ) );
    // Calc Bag Position (right beside dress)
    $BagPosWidth    = ( ( $Outfitwidth / 2 )  - ( $Bagwidth / 2 ) + 60 ); // place bag in middle but more to the right
    $BagPosHeight = ( ( $Outfitheight / 2 ) - ( $Bagheight / 2 ) );
    // Calc Shoe Position (middle under dress)
    $ShoePosWidth   = ( ( $Outfitwidth / 2 )  - ( $Shoewidth / 2 ) );
    $ShoePosHeight = ( ( $Outfitheight / 2 ) - ( $Shoeheight / 2 ) + 100); // place further down

    // merge images together
    imagecopymerge($newoutfit,$dress,$DressPosWidth,$DressPosHeight,0,0,$Dresswidth,$Dressheight,100);
    imagealphablending($newoutfit,true);

    imagecopymerge($newoutfit,$bag,$BagPosWidth,$BagPosHeight,0,0,$Bagwidth,$Bagheight,100);
    imagealphablending($newoutfit,true);

    imagecopymerge($newoutfit,$shoe,$ShoePosWidth,$ShoePosHeight,0,0,$Shoewidth,$Shoeheight,100);
    imagealphablending($newoutfit,true);

    header('Content-Type: image/png');
    imagepng($newoutfit); // output to the browser
    imagedestroy($newoutfit);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 3 个具有透明度的图像组合在一起 的相关文章

  • PHP项目的文件夹结构[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我决定从头开始完全重写我的旧 PHP 项目 之前 我每一页都有一个文件 现在我想使用具有一个入口点的 MVC 模式方法 该项目本身相当大 我正在
  • 如何将路径附加到 .htaccess 中的 PHP include_path

    目前在我的网站上我使用的语句如下 include head php include head php include head php 取决于我有多少嵌套文件夹 我确信有更好的方法来做到这一点 我相信 htaccess是解决方案 但我不确定
  • 在 php 中编写回调函数时传递参数(Laravel 5)

    我正在使用 Laravel 5 进行 php 编程 我有这段代码 newUser this gt create request gt all newUser gt save newAccount new Account user id gt
  • PHP SQLSRV:sqlsrv_query() 是否可以正确地准备 select 语句?

    TL DR Does sqlsrv query 做同样的工作select陈述比sqlsrv prepare and sqlsrv execute 关于准备好的陈述 做什么 我怎样才能做一个安全的select陈述 一点历史 我是 PHP 开发
  • 未找到命令 - exec() 命令中出现错误

    我从 php 文件运行这个 exec epm package 我在 error log 中收到以下错误 sh epm command not found 我在终端手动测试它工作正常 尝试输入完整路径名 exec path to epm pa
  • Cron 调度程序 CakePHP 2.0

    我正在使用 CakePHP 2 0 并一直在尝试设置一个 cronjob 来执行控制器中的方法 我一直在疯狂地浏览各种教程和随机网站 看看是否能找到解决方案 我收到的错误是这样的 Undefined variable argc APP we
  • i识别人的扫描图像中的眼睛

    我想开发一个 iPhone 应用程序 它应该识别 QR 阅读器扫描的图像中人的眼睛 脸部和肤色 如何在图像中检测眼睛 虽然这可能是可能的 但我只是警告您 无论编程如何 它都会有一定程度的不准确性 任何面部 视网膜检测软件都可能被欺骗 并且考
  • PHP 插入数组值、表名

    我正在努力处理 PHP 插入语句 我希望它通过使用将数据插入数据库array keys values and array values values 我试图弄清楚如何做到这一点 到目前为止 我的插入中已包含此代码 并且还包含了我的索引页 我
  • Cakephp:如何将值传递到 JavaScript 文件中?

    我有一些 javascript 包含在视图中 并且我在此线程中使用了 inkedmn 的方法 将页面特定的 javascript 添加到 cakephp 中的每个视图 https stackoverflow com questions 14
  • PHP回显随机数组并插入数据库而不重复

    我有一个代码可以滚动一个随机数字 并根据获胜者的机会显示获胜者 data array foreach getAllUserTicketHistoryJson as value data value user id number format
  • 检测图像的边缘在 Matlab 中不起作用

    我正在编写一个检测图像边缘的脚本 这是脚本 clear all close all clc c rgb2gray imread image S004 I0004 jpg c double c k imnoise c salt pepper
  • PDO中使用持久连接有什么缺点

    在 PDO 中 可以使用以下方法使连接持久化PDO ATTR PERSISTENT属性 根据 php 手册 持久连接不会在脚本结束时关闭 而是 当另一个脚本请求连接时 会被缓存并重新使用 相同的凭据 持久连接缓存允许您 避免每次建立新连接的
  • 从控制器访问 Liip Imagine 包 - 将服务分配给变量 (Symfony 4)

    On Symfony 4 如何从 PHP 代码中访问 Liip Imagine 包 https stackoverflow com questions 54543563 symfony 4 how do i access the liip
  • matlab的imregionalmax()和scipy.ndimage.filters.maximum_filter有什么区别

    我需要找到图像的区域最大值以获得用于分水岭分割的前景标记 我在 matlab 中看到使用该函数imregionalmax http www mathworks com help images ref imregionalmax html 由
  • php curl 获取 html 和 js 渲染

    php curl 只获取html页面的源代码 不执行js脚本 我需要我的网站获取已执行所有 JavaScript 的源代码 我使用ajax 但无法在页面中添加更多js 因为当我加载另一个页面时脚本会保留 我找到了 SpiderMonkey
  • 获取pygame中图像各个像素的颜色

    如何获取传输到 pygame 表面的图像像素的颜色值 使用 Surface get at 仅返回表面层的颜色 而不返回其上位图传输的图像 方法surface get at很好 下面的示例显示了在没有 Alpha 通道的情况下位图传输图像时的
  • preg_replace '

    ' 为 '
    '?

    我的代码删除了 p 起始标签 但现在我想替换结尾 p 带换行符的标签 我怎样才能做到这一点 这就是我所拥有的 content This is the content newcontent preg replace
  • mySQL 和 XAMPP 端口冲突

    我已经使用 XAMPP Apache 一段时间了 最 近决定安装 MySQL 然而 它们似乎是矛盾的 MySQL 似乎优先 并且它运行正常 但是 XAMPP Apache 在尝试启动 Apache 时给我此错误消息 03 07 32 AM
  • 如何使用call_user_func作为静态类方法?

    下面的代码工作正常 LibraryTests TestGetServer 获取 LibraryTests 中的函数数组并运行它们 methods get class methods LibraryTests foreach methods
  • 解析错误:语法错误,意外的 T_SL PHP heredoc

    我不断收到以下错误 解析错误 语法错误 home a4999406 public html willingLog html 第 70 行出现意外的 T SL 以下代码 第一行是第 70 行 echo lt lt

随机推荐

  • jquery中的ontouchstart和ontouchend?

    我目前正在对要更改触摸类的每个元素使用以下内容 ontouchstart this addClass select ontouchend this removeClass select 我想知道是否有这样的事情 element touchs
  • 未找到特征“Venturecraft\Revisionable\RevisionableTrait”

    我正在使用 laravel 4 我想跟踪对表进行的所有交易的历史记录 我按照这些步骤操作 added venturecraft revisionable 1 在作曲家 json php composer phar update 在我的项目的
  • HBase 无法在 Windows 上以独立模式启动

    我下载了HBase 1 0 1在我的 Windows 机器上 无法启动它 我收到以下错误消息 C Users admin Downloads hbase 1 0 1 gt bin start hbase cmd Error Could no
  • 单击 Facebook 登录按钮后重定向,即使已经登录 Facebook

    我的主页上有标准的 Facebook 登录按钮 我不希望人们仅在用户单击登录按钮时才使用其 Facebook 帐户自动登录我的网站 如果用户未登录 Facebook 则会出现一个弹出窗口 询问他的凭据 然后他将被重定向到loggedin h
  • 如何通过在单个查询中添加 MySQL 数据库来更新 MySQL 数据库表中的字段

    我有一个表 用于存储随着时间的推移而添加的值 当我想添加值时 我想在单个查询中执行此操作 而不是 从数据库获取oldValue 新值 旧值 X 使用 newValue 更新行 query1 从表中选择值 其中 id thisID 结果1 m
  • 为什么 Stream 没有 toList() 方法?

    使用 Java 8 流时 获取一个列表 从中创建一个流 执行业务并将其转换回来是很常见的 就像是 Stream of 2 1 2 5 filter n gt n gt 0 map n gt n n collect Collectors to
  • OpenGL Vulkan 互操作性

    我需要一些有关 OpenGL Vulkan 内存交换的帮助 我已经找到这个主题了如何从 Vulkan 渲染到 OpenGL https stackoverflow com questions 38907764 how to render t
  • AWS SQS 触发器 Step Functions

    快速问题 发送 SQS 消息后是否可以触发 Step Function 的执行 如果可以 您如何将其指定到 cloudformation yaml 文件中 提前致谢 首先要考虑的是 你really需要使用 SQS 启动 Step Funct
  • Qt组件的边框颜色

    我想更改 QFrame 组件的边框颜色 还尝试使用样式表 但在运行时没有效果 在我的项目中 有各种对话框 UI 它们基本上都是 QFrame 我想更改所选对话框的边框颜色 即 QFrame 边框颜色 QT 中有没有解决这个问题的方法 如下所
  • SUM 的 LINQ 组

    给出以下查询 Dim Query From c In DB Crt Where c Member Locked False And c Member Verified True And c Ct gt 0 Order By c Ct Asc
  • 为什么有些网站不允许更改字体大小?

    我几乎总是使用比默认字体大得多的字体浏览网页 使它更容易阅读 但有时我注意到有些网站不允许放大字体 至少在 Chrome 上是这样 比如这个网站 http en support wordpress com domains map exist
  • 如何使用 Android Volley 显示/请求 JSON 对象?

    我在 Android Studio 中遇到如何请求 JSON 对象的问题 我的 Logcat 只能打印 String onResponse 而不能打印 JSONObject 值 我在 AccessActivity java 内的 try 行
  • 将全局变量传递给函数有问题吗?

    考虑以下函数声明 int abmeld char strsend 这是这样称呼的 abmeld str where str是在程序文件开头 包含之后 声明和初始化的全局变量 如下所示 char str 300 现在我已经知道这是不必要的代码
  • 将 PHP 转换为数组并循环

    我正在为我们的空手道学校开发一个应用程序 希望从数据库中获取技术名称 将它们以随机顺序存储在一个数组中 并且能够单击一个按钮一次在整个数组中移动 我以几种不同的方式考虑过这个问题 包括从数据库中随机进行 这非常简单 但它多次提取相同的技术
  • Scala 防止混合的方法

    我想创造以下特征 trait IntSet A extends Traversable A self Product gt def foreach U f A gt U Unit case class AProduct a List Int
  • 使用 Matplotlib 创建箱线图

    我正在使用 python 3 和 jupyter 笔记本 我有一个 pandas 数据框 其结构如下 location price Apr 25 ASHEVILLE 15 0 Apr 25 ASHEVILLE 45 0 Apr 25 ASH
  • 时钟()精度

    我看过很多关于使用clock 函数来确定程序中经过的时间量的帖子 代码如下 start time clock code to be timed end time clock elapsed time end time start time
  • 如何在 jasmine 中模拟 $scope.variables

    我有以下测试用例 CompanyCtrlSpec js describe ViewCompanyCtrl function var rootScope scope controller q beforeEach angular mock m
  • jquery 解决方案从静态 html 页面发布到另一个站点

    需要将数据从静态 html 页面发布到托管在另一个域上的另一个页面 通常 我会使用 post 方法创建一个包含表单的 iframe 其操作定向到该网页 最后提交该表单 复杂之处在于我要从静态 html 页面收集数据并在 iframe 内创建
  • 将 3 个具有透明度的图像组合在一起

    我正在做这个项目 我想将三张图像合并为一张 到目前为止 有效的方法是使每个图像 jpg 透明 但将它们合并成新的 png 图像后 透明度就消失了 这是代码 function CreateMyCoolOutfitBaby Outfitwidt