Last active
March 2, 2018 00:30
-
-
Save michimani/723e063ea2236a3626e7ee77b0c53a86 to your computer and use it in GitHub Desktop.
Fill the transparent part of transparent PNG with a solid color.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$fpath = $argv[1]; | |
$o = new Bg(); | |
$o->setBgColor($fpath); | |
class Bg | |
{ | |
public function setBgColor($file_path, $rgb = [255, 255, 255]) | |
{ | |
try | |
{ | |
if (!file_exists($file_path)) | |
{ | |
throw new Exception(sprintf('target file does not exists. >>> %s', $file_path)); | |
} | |
list($width, $height, $type, $attr) = getimagesize($file_path); | |
$image = imagecreatefrompng($file_path); | |
// 背景画像生成 | |
$bg = imagecreatetruecolor($width, $height); | |
$c = imagecolorallocate($bg, $rgb[0], $rgb[1], $rgb[2]); | |
imagefilledrectangle($bg, 0, 0, $width, $height, $c); | |
// 画像を合成 | |
imagecopy($bg, $image, 0, 0, 0, 0, $width, $height); | |
// 出力 | |
imagepng($bg, $file_path); | |
imagedestroy($bg); | |
imagedestroy($image); | |
} | |
catch (Exception $e) | |
{ | |
echo $e->getMessage(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment