2015-06-20 32 views
0

我需要将一个普通的矩形图像“变形”为一个“pizze切片”。所以这个:使用GD Image的比萨饼切片形状?

enter image description here

必须拉伸成这样:

enter image description here

这样,我失去了实际的措辞,所以它不是100%正确。我需要它不仅掩盖这些部分,而且还“拉伸”切片,使其适合新的三角形形状,因此不会丢失任何文字。

我的photoshopping技巧非常有限,我甚至无法在photoshop中进行适当的预览,以显示它如何剪切。

我该如何使用GD Image做到这一点?

+0

能否请您使用jsfiddle.net –

回答

3

这应该完成工作 - 脚本被评论,所以你应该没有理解它。

<?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'); 

结果:

pizza

+0

你的问题实际上写的文字给我的代码。我应该给出一个没有文字的图像,因为问题特别是与已经有文字的图像有关。你可以调整你的答案吗?那么我一定会将它标记为正确的。 – coderama

+0

?这将适用于任何'$ img',只要你先画出你想要的。 –

+0

我标记了正确。这太棒了!很棒! – coderama