使用GD Image的披萨片形状?
Pizza slice shape using GD Image?
我需要 "transform" 一个普通的矩形图像变成 "pizze slice"。所以这个:
必须拉长成这样:
这样我就失去了实际的措辞,所以它不是 100% 正确的。我需要它不仅要遮盖那些部分,还要遮盖 "stretch" 切片,以便它适合新的三角形形状,所以 none 的文本会丢失。
我的 Photoshop 技术非常有限,我什至无法在 Photoshop 中进行适当的预览以显示必须如何剪切。
我如何使用 GD Image 执行此操作?
这应该可以完成工作 - 脚本已被注释,因此您应该可以毫不费力地理解它。
<?php
$img = imagecreatetruecolor(600, 130);
$text = "Text that can be\nwrapped to next line";
//draw red background
imagefilledrectangle($img, 0, 0, imagesx($img), imagesy($img), 0xa00000);
//draw green text
putenv('GDFONTPATH=/usr/share/fonts/TTF');
imagettftext($img, 40, 0, 20, 50, 0x00a000, 'arial.ttf', $text);
//stretch whole thing vertically
$img = imagescale($img, imagesx($img), imagesy($img) * 2);
//compute pizza transformation
$y_arr = [];
for ($x = 0; $x < imagesx($img); $x++)
{
//compute simple "triangle" shape
$linear_y = $x * imagesy($img) / 2 / imagesx($img);
//get some variations with cos()
$cos_y = cos($x * 2 * M_PI / imagesx($img));
$cos_y *= cos($x * 2 * M_PI / imagesx($img) / 2);
$cos_y = (1 - $cos_y) * imagesy($img) / 4;
//finally combine these two
$y = ($cos_y + $linear_y * 2) / 3;
//and push the coordinate onto stack
$y_arr []= $y;
}
//create target image
$dstimg = imagecreatetruecolor(imagesx($img), imagesy($img));
//scale each column according to pizza transformation
foreach ($y_arr as $x => $y)
imagecopyresized($dstimg, $img, $x, $y, $x, 0, 1, max(1, imagesy($img) - 2 * $y), 1, imagesy($img) - 1);
//write the image to PNG file
imagepng($dstimg, 'test.png');
结果:
我需要 "transform" 一个普通的矩形图像变成 "pizze slice"。所以这个:
必须拉长成这样:
这样我就失去了实际的措辞,所以它不是 100% 正确的。我需要它不仅要遮盖那些部分,还要遮盖 "stretch" 切片,以便它适合新的三角形形状,所以 none 的文本会丢失。
我的 Photoshop 技术非常有限,我什至无法在 Photoshop 中进行适当的预览以显示必须如何剪切。
我如何使用 GD Image 执行此操作?
这应该可以完成工作 - 脚本已被注释,因此您应该可以毫不费力地理解它。
<?php
$img = imagecreatetruecolor(600, 130);
$text = "Text that can be\nwrapped to next line";
//draw red background
imagefilledrectangle($img, 0, 0, imagesx($img), imagesy($img), 0xa00000);
//draw green text
putenv('GDFONTPATH=/usr/share/fonts/TTF');
imagettftext($img, 40, 0, 20, 50, 0x00a000, 'arial.ttf', $text);
//stretch whole thing vertically
$img = imagescale($img, imagesx($img), imagesy($img) * 2);
//compute pizza transformation
$y_arr = [];
for ($x = 0; $x < imagesx($img); $x++)
{
//compute simple "triangle" shape
$linear_y = $x * imagesy($img) / 2 / imagesx($img);
//get some variations with cos()
$cos_y = cos($x * 2 * M_PI / imagesx($img));
$cos_y *= cos($x * 2 * M_PI / imagesx($img) / 2);
$cos_y = (1 - $cos_y) * imagesy($img) / 4;
//finally combine these two
$y = ($cos_y + $linear_y * 2) / 3;
//and push the coordinate onto stack
$y_arr []= $y;
}
//create target image
$dstimg = imagecreatetruecolor(imagesx($img), imagesy($img));
//scale each column according to pizza transformation
foreach ($y_arr as $x => $y)
imagecopyresized($dstimg, $img, $x, $y, $x, 0, 1, max(1, imagesy($img) - 2 * $y), 1, imagesy($img) - 1);
//write the image to PNG file
imagepng($dstimg, 'test.png');
结果: