Skip to content

Instantly share code, notes, and snippets.

@michimani
Last active March 2, 2018 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michimani/723e063ea2236a3626e7ee77b0c53a86 to your computer and use it in GitHub Desktop.
Save michimani/723e063ea2236a3626e7ee77b0c53a86 to your computer and use it in GitHub Desktop.
Fill the transparent part of transparent PNG with a solid color.
<?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