dro62273 2011-08-17 08:36
浏览 184
已采纳

PHP GD从图片切三角形

Guidelines: Using PHP and GD library

I have ordinary pictures (png|gif|jpeg) and want to cut out triangles. To facilitate lets assume we want to cut images in 4 parts, each triangle starting from the center. You got it?

Illustration rectangle to 4 triangles

Painting triangles with GD goes like this:

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

now we Actually want to CUT a triangle from an already existing picture. I only know how to cut out a rectangle from an existing picture like this:

<?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 bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )

So how do we combine these two approaches to do what is intented?

  • 写回答

1条回答 默认 最新

  • douzouchang7448 2011-08-17 10:59
    关注

    I'm sure the x/y calculation could be more optimised with this, but it works for generating a png with top/right/bottom/left triangles from a given image, and outputting them as a png with transparent background -

    // 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用