ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php – 如何在多边形外面裁剪区域?

2019-09-02 10:38:18  阅读:198  来源: 互联网

标签:php polygon crop


我想在PHP中的预定义多边形之外做任何透明的事情.让我们说,你有你头部的形状,然后图像的任何其他东西应该被分类.
非常感谢提前!
为了使自己清楚,这里有一些我一直在研究的代码:

$dat = dirname(__FILE__)."/foto_".time().".png";
$img = imagecreatefrompng("foto.png");
$points = array(93,36,147,1,255,-5,294,37,332,114,327,189,315,249,303,291,290,327,260,360,205,404,165,407,131,376,86,325,62,236,61,155,66,96,77,57,87,45);
$schwarz = ImageColorAllocate ($img, 0, 0, 0);
imagefilledpolygon($img, $points, count($points)/2, $schwarz);
imagepng($img, $dat);

可以找到原始图像here和结果here.除黑色区域外的所有内容都应该被丢弃.

解决方法:

我知道这有点晚了但是我在网上寻找解决这个问题的方法并且找不到一体化的解决方案.我发现的许多帖子都涉及按像素编辑图像,我不太喜欢这样做.在拼凑了多个站点的多个样本之后,这就是我想出的:

resizeCropPolygonImage()

function resizeCropPolygonImage($source, $dest = null, 
        $newWidth = null, $newHeight = null, $startX = 0, $startY = 0, 
        $points = array(), $numCoords = 2) {

    // Added in $numCoords in case we want to do 3D image processing in the future
    // (currently we do not process anything other than 2D coordinates)
    $points = array(100,115, 124,65, 192,65, 216,115, 192,165, 124,165);
    $numPoints = count($points) / $numCoords;

    // If there are not enough points to draw a polygon, then we can't perform any actions
    if ($numPoints < 3) {
        return;
    }

    // Get the original image's info
    list($width, $height, $file_type) = getimagesize($source);

    /******* Here I am using a custom function to resize the image  *********
     ******* keeping the aspect ratio.                              *********
     ******* You'll have to add in your own re-sizing logic         *********
    // Resize the source (using dummy vars because we don't want our
    // start x & y to be overwritten)
    scaleDimensions($width, $height, $newWidth, $newHeight, $dummyX = null, $dummyY = null);
    For simplicity sake, I'll just set the width and height to the new width and height
    *************************************************************************/
    $width = $newWidth;
    $height = $newHeight;

    switch ($file_type) {
    case 1:
        $srcImage = imagecreatefromgif($source);
        if (function_exists(ImageGIF)) {
            $imgType = "gif";
        } else {
            $imgType = "jpeg";
        }
        break;
    case 2:
        $srcImage = imagecreatefromjpeg($source);
        $imgType = "jpeg";
        break;
    case 3:
        $srcImage = imagecreatefrompng($source);
        $imgType = "png";
        break;
    default:
        return;
    }

    // Setup the merge image from the source image with scaling
    $mergeImage = ImageCreateTrueColor($width, $height);
    imagecopyresampled($mergeImage, $srcImage, 0, 0, 0, 0, $width, $height, imagesx($srcImage), imagesy($srcImage));

    /******** This is probably the part that you're most interested in *******/
    // Create the image we will use for the mask of the polygon shape and
    // fill it with an uncommon color
    $maskPolygon = imagecreatetruecolor($width, $height);
    $borderColor = imagecolorallocate($maskPolygon, 1, 254, 255);
    imagefill($maskPolygon, 0, 0, $borderColor);

    // Add the transparent polygon mask
    $transparency = imagecolortransparent($maskPolygon, imagecolorallocate($maskPolygon, 255, 1, 254));
    imagesavealpha($maskPolygon, true);
    imagefilledpolygon($maskPolygon, $points, $numPoints, $transparency);

    // Apply the mask
    imagesavealpha($mergeImage, true);
    imagecopymerge($mergeImage, $maskPolygon, 0, 0, 0, 0, $width, $height, 100);

    /******* Here I am using a custom function to get the outer     *********
     ******* perimeter of the polygon. I'll add this one in below   ********/
    // Crop down to just the polygon area
    $polygonPerimeter = getPolygonCropCorners($points, $numCoords);
    $polygonX = $polygonPerimeter[0]['min'];
    $polygonY = $polygonPerimeter[1]['min'];
    $polygonWidth = $polygonPerimeter[0]['max'] - $polygonPerimeter[0]['min'];
    $polygonHeight = $polygonPerimeter[1]['max'] - $polygonPerimeter[1]['min'];

    // Create the final image
    $destImage = ImageCreateTrueColor($polygonWidth, $polygonHeight);
    imagesavealpha($destImage, true);
    imagealphablending($destImage, true);
    imagecopy($destImage, $mergeImage, 
            0, 0, 
            $polygonX, $polygonY,
            $polygonWidth, $polygonHeight);

    // Make the the border transparent (we're assuming there's a 2px buffer on all sides)
    $borderRGB = imagecolorsforindex($destImage, $borderColor);
    $borderTransparency = imagecolorallocatealpha($destImage, $borderRGB['red'],
            $borderRGB['green'], $borderRGB['blue'], 127);
    imagesavealpha($destImage, true);
    imagealphablending($destImage, true);
    imagefill($destImage, 0, 0, $borderTransparency);

    if (!$dest) {
        // If no dest was given, then return to browser
        header('Content-Type: image/png');
        imagepng($destImage);
    } else {
        // Output image will always be png
        $dest .= '.png';

        // Save to destination
        imagepng($destImage, $dest);
    }

    // Destroy remaining images
    imagedestroy($maskPolygon);
    imagedestroy($srcImage);
    imagedestroy($destImage);

    // Only return a value if we were given a destination file
    if ($dest) {
        return $dest;
    }
}

getPolygonCropCorners()

function getPolygonCropCorners($points, $numCoords) {
    $perimeter = array();

    for ( $i = 0; $i < count($points); $i++ ) {
        $axisIndex = $i % $numCoords;

        if (count($perimeter) < $axisIndex) {
            $perimeter[] = array();
        }

        $min = isset($perimeter[$axisIndex]['min']) ? $perimeter[$axisIndex]['min'] : $points[$i];
        $max = isset($perimeter[$axisIndex]['max']) ? $perimeter[$axisIndex]['max'] : $points[$i];

        // Adding an extra pixel of buffer
        $perimeter[$axisIndex]['min'] = min($min, $points[$i] - 2);
        $perimeter[$axisIndex]['max'] = max($max, $points[$i] + 2);
    }

    return $perimeter;
}

使用此功能,我可以转动此图像

进入这个

它可能不完全完整,但我认为这绝对是一个很好的起点.

编辑

> 2014年9月19日 – 增加了边境的透明度

标签:php,polygon,crop
来源: https://codeday.me/bug/20190902/1790634.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有