Php - replace base color of transparent png image

前端 未结 6 2030
栀梦
栀梦 2020-12-28 09:35

I have searched a lot and I found only few solutions (on google and stackoverflow so please don\'t mark this one as a duplicate unless there\'s really duplicate question), b

6条回答
  •  一个人的身影
    2020-12-28 09:54

    Using SteAp's accepted code as a starting point (since with it i didnt manage to achieve transparency, just a white background), i adapted said code and the result is this:

    " );
                    $flagOK = 0; 
                } else if ($flagFoundColor) {
                    imagesetpixel( $im_dst, $x, $y, $colorNew );
                    //echo "x:$x y:$y col=$colorNew alpha:$alpha
    "; } } } $flagOK2 = imagepng($im_dst, $outputFilePathIn); if ($flagOK && $flagOK2) { echo ("Congratulations, your conversion was successful
    new file $outputFilePathIn
    "); } else if ($flagOK2 && !$flagOK) { echo ("ERROR, your conversion was UNsuccessful
    Please verify if your PNG is truecolor
    input file $inputFilePathIn
    "); } else if (!$flagOK2 && $flagOK) { $dirNameOutput = dirname($outputFilePathIn)."/"; echo ("ERROR, your conversion was successful, but could not save file
    Please verify that you have PERMISSION to save to directory $dirName
    input file $inputFilePathIn
    "); } else { $dirNameOutput = dirname($outputFilePathIn)."/"; echo ("ERROR, your conversion was UNsuccessful AND could not save file
    Please verify if your PNG is truecolor
    Please verify that you have PERMISSION to save to directory $dirName
    input file $inputFilePathIn
    "); } echo ("TargetName:$outputFilePathIn wid:$width height:$height CONVERTED:|$flagOK| SAVED:|$flagOK2|
    "); imagedestroy($im_dst); imagedestroy($im_src); } $targetRed = 0; $targetGreen = 180; $targetBlue = 0; //$inputFileName = 'frameSquareBlack_88x110.png'; $inputFileName = 'testMe.png'; $dirName = "../img/profilePics/"; $nameTemp = basename($inputFileName, ".png"); $outputFileName = $nameTemp."_$targetRed"."_$targetGreen"."_$targetBlue.png"; $inputFilePath = $dirName.$inputFileName; $outputFilePath = $dirName.$outputFileName; //echo "inputFileName:$inputFilePath
    outputName:$outputFilePath
    "; colorizeKeepAplhaChannnel( $inputFilePath, $targetRed, $targetGreen, $targetBlue, $outputFilePath); ?>

    Original


    Colorized

    enter image description here

    this variation changes ALL colors to chosen color (not just black, a simple IF can solve the problem - uncomment 3 indicated lines in function and you will achieve this)

    enter image description here

    For illustrative purposes, in this case, the following image was used (because leaf.png is monochromatic, with transparency): enter image description here

提交回复
热议问题