Join multiple PNG Images into a single one PNG using PHP

后端 未结 1 875
说谎
说谎 2021-01-16 13:20

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

  1. Output Image it´s a collection of stacked PNGs... in
相关标签:
1条回答
  • 2021-01-16 13:40

    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:

    http://phpimageworkshop.com/

    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.

    0 讨论(0)
提交回复
热议问题