これは、画像の上にテキストがオーバーレイされ、グラデーションがテキストの周りに奇妙なピクセレーションを持っているという問題ですが、その理由はわかりません。
-->
コードは次のとおりです。
<?php
function hex2rgb($hex) {
$rgb[0] = hexdec(substr($hex, 0, 2));
$rgb[1] = hexdec(substr($hex, 2, 2));
$rgb[2] = hexdec(substr($hex, 4, 2));
return $rgb;
}
$sourceImage = imagecreatefromstring(file_get_contents("source.jpg"));
$imagePadding = 10;
$height = imagesy($sourceImage);
$width = imagesx($sourceImage);
$baseImage = imagecreatetruecolor($width, $height);
$backgroundColor = imagecolorallocate($baseImage, 0, 0, 0);
imagefill($baseImage, 0, 0, $backgroundColor);
imagecopyresampled($baseImage, $sourceImage, 0, 0, 0, 0, $width, $height, $width, $height);
//==== GRADIENT ====//
// Modified from: http://stackoverflow.com/questions/14684622/blend-transparent-gradient-with-an-image-using-php-gd-library
$gradientCanvas = imagecreatetruecolor($width, $height);
$transColor = imagecolorallocatealpha($gradientCanvas, 0, 0, 0, 127);
imagefill($gradientCanvas, 0, 0, $transColor);
$color = hex2rgb("000000");
$start = -40;
$stop = 120;
$range = $stop - $start;
for ($y = 0; $y < $height; ++$y) {
$alpha = $y <= $start ? 0 : round(min(($y - $start) / $range, 1) * 127);
$newColor = imagecolorallocatealpha($gradientCanvas, $color[0], $color[1], $color[2], $alpha);
imageline($gradientCanvas, 0, $y, $width, $y, $newColor);
}
imagecopyresampled($baseImage, $gradientCanvas, 0, 0, 0, 0, $width, $height, $width, $height);
//==== TEXT ====//
putenv('GDFONTPATH=.');
$font = "HelveticaNeueBold.ttf";
$white = imagecolorallocate($baseImage, 255, 255, 255);
imagettftext($baseImage, 14, 0, $imagePadding, $imagePadding + 16, $white, $font, "Foobar");
header('Content-Type: image/jpeg');
imagejpeg($baseImage);
imagedestroy($baseImage);
imagedestroy($sourceImage);
?>