2011-08-17 15 views
1
から

ガイドライントライアングルをカット。 簡単にするには、画像を4つの部分に分割すると仮定します。各部分は、 の中心から始まります。あなたはそれを持っていますか?PHP GDピクチャー

Illustration rectangle to 4 triangles

GDと

絵画三角形はこのように書き:

<?php 
//create a white canvas 
$im = @imagecreate(500, 500) or die("Cannot Initialize new GD image stream"); 
imagecolorallocate($im, 255, 255, 255); 
//triangle 
$t1 = rand(0,400); 
$t2 = rand(0,400); 
$t3 = rand(10,100); 
$t4 = rand(10,100); 
$points = array(
$t1, $t2, 
($t1+$t3), $t2, 
$t1, ($t2+$t4) 
); 
$trcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255)); 
imagefilledpolygon($im, $points, 3, $trcol); 
//make png and clean up 
header("Content-type: image/png"); 
imagepng($im); 
imagedestroy($im); 
?> 

今、私たちは、実際には、既存の画像から三角形をカットします。 は、私はこれだけのように、既存の画像から長方形をカットする方法を知っている:

<?php 
// Create image instances 
$src = imagecreatefromgif('php.gif'); 
$dest = imagecreatetruecolor(80, 40); 

// Copy 
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40); 

// Output and free from memory 
header('Content-Type: image/gif'); 
imagegif($dest); 

imagedestroy($dest); 
imagedestroy($src); 
?> 

PHP GD imagecopy ブールIMAGECOPY(リソースの$ dst_im、リソース$ src_im、int型$ dst_x、int型$ dst_y、int型$ src_x、 int $ src_y、int $ src_w、int $ src_h)

この2つのアプローチをどのように組み合わせてインテントにするのですか?

答えて

1

x/y計算はこれでより最適化できると確信していますが、与えられた画像から上/右/下/左三角形を含むpngを生成し、透明な背景を持つpngとして出力します -

// Set which triangle to generate 
// top, right, bottom or left 

$triangle = 'top'; 

// Load source image 

$src  = imagecreatefromjpeg ('Desert.jpg'); 

// Get image width/height 

$srcWidth = imagesx ($src); 
$srcHeight = imagesy ($src); 

// Get centre position 

$centreX = floor ($srcWidth/2); 
$centreY = floor ($srcHeight/2); 

// Set new image size and start x/y 

switch ($triangle) 
{ 

    case 'top': 

     $destWidth = $srcWidth; 
     $destHeight = $centreY; 

     $destSX  = 0; 
     $destSY  = 0; 

     break; 

    case 'right': 

     $destWidth = $centreX; 
     $destHeight = $srcHeight; 

     $destSX  = $centreX; 
     $destSY  = 0; 

     break; 

    case 'bottom': 

     $destWidth = $srcWidth; 
     $destHeight = $centreY; 

     $destSX  = 0; 
     $destSY  = $centreY; 

     break; 

    case 'left': 

     $destWidth = $centreX; 
     $destHeight = $srcHeight; 

     $destSX  = 0; 
     $destSY  = 0; 

     break; 

} 

// Create the image 

$dest   = imagecreatetruecolor ($destWidth, $destHeight); 

// Copy from source 

imagecopy ($dest, $src, 0, 0, $destSX, $destSY, $destWidth, $destHeight); 

// OK... we now have the correctly sized rectangle copied over from the source image 
// Lets cut it down and turn it into the triangle we want by removing the other triangles 
// We remove the area by defining another colour as transparent and creating shapes with that colour 

$colRed   = imagecolorallocatealpha ($dest, 255, 0, 0, 0); 
imagecolortransparent ($dest, $colRed); 

switch ($triangle) 
{ 

    case 'top': 

     imagefilledpolygon ($dest, array ($centreX, $destHeight, 0, 0, 0, $destHeight), 3, $colRed); 
     imagefilledpolygon ($dest, array ($centreX, $destHeight, $destWidth, 0, $destWidth, $destHeight), 3, $colRed); 
     break; 

    case 'right': 

     imagefilledpolygon ($dest, array (0, $centreY, 0, 0, $destWidth, 0), 3, $colRed); 
     imagefilledpolygon ($dest, array (0, $centreY, 0, $destHeight, $destWidth, $destHeight), 3, $colRed); 
     break; 

    case 'bottom': 

     imagefilledpolygon ($dest, array ($centreX, 0, 0, 0, 0, $destHeight), 3, $colRed); 
     imagefilledpolygon ($dest, array ($centreX, 0, $destWidth, 0, $destWidth, $destHeight), 3, $colRed); 
     break; 

    case 'left': 

     imagefilledpolygon ($dest, array ($destWidth, $centreY, 0, 0, $destWidth, 0), 3, $colRed); 
     imagefilledpolygon ($dest, array ($destWidth, $centreY, 0, $destHeight, $destWidth, $destHeight), 3, $colRed); 
     break; 

} 

// Output new image 

header ('Content-Type: image/png'); 
imagepng ($dest); 

// Clean up 

imagedestroy ($src); 
imagedestroy ($dest); 
+0

こんにちは。非常に良いとスマートですあなたの貢献をありがとう。三角形を切り取っていなくても私はあなたのアプローチが好きです。しかし、おそらく、それは画像からrectangulares以外の形状をカットすることはできませんgdのライブラリを使用してPHPで。それは...ですか?私はそのような目的のために、デフォルトのphpがインストールされていないimagemagickに相談しなければならないと恐れています。私はスマートな作業場があることを望みますが。 – Email

+0

このソリューションは、どのような画像に関わらず正三角形を作成するためにどのように適応されますか? (スケーリングと品質の損失は問題ではない)??ありがとう – Owen