Skip to content

Instantly share code, notes, and snippets.

@mgng
Created May 28, 2010 01:10
Show Gist options
  • Save mgng/416606 to your computer and use it in GitHub Desktop.
Save mgng/416606 to your computer and use it in GitHub Desktop.
透過pngのリサイズ(ダメな例)
<?php
/**
* GD だけで png のリサイズしようとしたけどダメぽ
*
* @param string $in
* @param string $out
* @param int $per
*/
function resizePng($in, $out, $per) {
$size = getimagesize($in);
if ($size === false || $size['mime'] !== 'image/png') {
return false;
}
$w = $size[0];
$h = $size[1];
$nw = floor($w*$per/100);
$nh = floor($h*$per/100);
$gd = imagecreatefrompng($in);
$gd_p = imagecreatetruecolor($nw, $nh);
imagecopyresampled($gd_p, $gd, 0, 0, 0, 0, $nw, $nh, $w, $h);
$idx = imagecolortransparent($gd);
if ($idx >= 0) {
$col = imagecolorsforindex($gd, $idx);
$idx = imagecolorallocate($gd_p, $col['red'], $col['green'], $col['blue']);
imagefill($gd_p, 0, 0, $idx);
imagecolortransparent($gd_p, $idx);
} else {
imagealphablending($gd_p, false);
$col = imagecolorallocatealpha($gd_p, 255, 255, 255, 127);
imagefill($gd_p, 0, 0, $col);
imagesavealpha($gd_p, true);
}
imagepng($gd_p, $out);
imagedestroy($gd);
imagedestroy($gd_p);
return true;
}
resizePng('test1.png', 'test1_resize.png', 50);
resizePng('test2.png', 'test2_resize.png', 50);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment