使用 PHP 将多个 PNG 图像合并为一个 PNG

编程入门 行业动态 更新时间:2024-10-23 17:38:38
本文介绍了使用 PHP 将多个 PNG 图像合并为一个 PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 PHP 基于我自己的 PNG 制作自定义精灵,但我遇到了两个问题:

  • 输出图像是堆叠的 PNG 的集合......换句话说:源 PNG 是相互的.
  • 我需要输出图像的透明背景!
  • 这是我使用的代码:

    $width = 210;$高度 = 190;$layers = array();$layers[] = imagecreatefrompng("copy.png");$layers[] = imagecreatefrompng("cut.png");$image = imagecreatetruecolor($width, $height);//使背景透明?imagealphablending($image, false);$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);imagefill($image, 0, 0, $transparency);imagesavealpha($image, true);imagealphablending($image, true);for ($i = 0; $i < count($layers); $i++) {imagecopy($image, $layers[$i], 0, 0, 0, 0, $width, $height);}imagealphablending($image, false);imagesavealpha($image, true);imagepng($image, 'final_img.png');

    解决方案

    尝试仅使用 PHP GD 完成工作一小时后,我决定给这个名为ImageWorkshop"的库提供机会,可从此处访问:

    >

    phpimageworkshop/

    结果很棒,我用不到 10 行代码就解决了这个问题.方法如下:

    (显然,首先你必须下载ImageWorkshop)

    注意:我将使用一些描述性代码以确保每个人都能理解:)

    require_once('libs/PHPImageWorkshop/ImageWorkshop.php');/*空层有100x100...而且是透明的!!*/$emptyLayer = ImageWorkshop::initVirginLayer(100, 100);$cut = ImageWorkshop::initFromPath(__DIR__ .'/icons/copy.png');$copy = ImageWorkshop::initFromPath(__DIR__ .'/icons/cut.png');/*设置剪切"和复制"图标在emptyLayer内的位置*/$emptyLayer->addLayerOnTop($cut, 20, 10, 'LT');$emptyLayer->addLayerOnTop($copy, 20, 30, 'LT');//保存结果$dirPath = __DIR__ ."/图标/";$filename = "输出.png";$createFolders = 真;//如果文件夹不存在则创建$backgroundColor = null;//透明,仅适用于 PNG(否则设置为 null 时为白色)$imageQuality = 100;//对 GIF 无用,对 PNG 和 JPEG 有用(0 到 100%)$emptyLayer->save($dirPath, $filename, $createFolders, $backgroundColor, $imageQuality);

    仅此而已!

    顺便说一下,这个小库使用了 PHP GD 库.

    I´m trying to make custom sprites based on my own PNGs using PHP, but I got two problems:

  • Output Image it´s a collection of stacked PNGs... in other words: source PNGs are one over other.
  • I need a transparent background for the output image!
  • This is the code that I used:

    $width = 210; $height = 190; $layers = array(); $layers[] = imagecreatefrompng("copy.png"); $layers[] = imagecreatefrompng("cut.png"); $image = imagecreatetruecolor($width, $height); // to make background transparent? imagealphablending($image, false); $transparency = imagecolorallocatealpha($image, 0, 0, 0, 127); imagefill($image, 0, 0, $transparency); imagesavealpha($image, true); imagealphablending($image, true); for ($i = 0; $i < count($layers); $i++) { imagecopy($image, $layers[$i], 0, 0, 0, 0, $width, $height); } imagealphablending($image, false); imagesavealpha($image, true); imagepng($image, 'final_img.png');

    解决方案

    After one hour trying to do the Job using only PHP GD I decided to give a chance to this Library called "ImageWorkshop" which is accessible from here:

    phpimageworkshop/

    The result is AWESOME, with less of 10 lines of code I solve the situation. Here is How:

    (Obviously, first you have to download ImageWorkshop)

    NOTE: I will use a little bit descriptive code to ensure everybody understanding :)

    require_once('libs/PHPImageWorkshop/ImageWorkshop.php'); /*The Empty Layer have 100x100... And is TRANSPARENT!!*/ $emptyLayer = ImageWorkshop::initVirginLayer(100, 100); $cut = ImageWorkshop::initFromPath(__DIR__ . '/icons/copy.png'); $copy = ImageWorkshop::initFromPath(__DIR__ . '/icons/cut.png'); /*Set the position of "cut" and "copy" icons inside the emptyLayer*/ $emptyLayer->addLayerOnTop($cut, 20, 10, 'LT'); $emptyLayer->addLayerOnTop($copy, 20, 30, 'LT'); // Saving the result $dirPath = __DIR__ . "/icons/"; $filename = "output.png"; $createFolders = true; //will create the folder if not exist $backgroundColor = null; // transparent, only for PNG (otherwise it will be white if set null) $imageQuality = 100; // useless for GIF, usefull for PNG and JPEG (0 to 100%) $emptyLayer->save($dirPath, $filename, $createFolders, $backgroundColor, $imageQuality);

    Thats all!

    By the way this small Library uses the PHP GD library.

    更多推荐

    使用 PHP 将多个 PNG 图像合并为一个 PNG

    本文发布于:2023-07-27 00:16:51,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1219186.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:多个   并为   图像   PHP   PNG

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!