您现在的位置是:网站首页 > 脚本编程>

TP5结合Grafika实现生成九宫格图片

2019-10-173612人围观
简介 这些是使Grafika与其他库不同的功能:1.智能作物 -Grafika可以根据保留最重要区域的图像内容猜测作物的位置。2.动画GIF支持 -可以在GD和Imagick上调整动画GIF的大小。在GD上,Grafika使用自己的GIF解析器来执行此操作。3.5种调整大小模式 -“调整大小”是Grafika中的头等公民。使用resizeFit,resizeFill,resizeExact,resizeExactWidth和resizeExactHeight直接调用它们,或使用通用的resize API。4.图像比较 -查找两个图像的相似程度,或检查它们是否完全相等。5.高级过滤器 -Sobel边缘检测,扩散和有序抖动。将来的版本中将添加更多内容。6.图像混合 -使用以下模式混合2张图像:正常,倍增,叠加和屏幕。7.标准化的API-无需担心GD和Imagick API之间的差异,Grafika会为您标准化这些操作。

一.什么是Grafika

     Grafika是PHP的高级图像处理和图形库

     官方地址:https://kosinix.github.io/grafika/index.html

二.Grafika独特的功能

    这些是使Grafika与其他库不同的功能:

    1.智能作物 -Grafika可以根据保留最重要区域的图像内容猜测作物的位置。

    2.动画GIF支持 -可以在GD和Imagick上调整动画GIF的大小。在GD上,Grafika使用自己的GIF解析器来执行此操作。

    3.5种调整大小模式 -“调整大小”是Grafika中的头等公民。使用resizeFit,resizeFill,resizeExact,resizeExactWidth和resizeExactHeight直接调用它们,或使用通用的resize API。

    4.图像比较 -查找两个图像的相似程度,或检查它们是否完全相等。

    5.高级过滤器 -Sobel边缘检测,扩散和有序抖动。将来的版本中将添加更多内容。

    6.图像混合 -使用以下模式混合2张图像:正常,倍增,叠加和屏幕。

    7.标准化的API-无需担心GD和Imagick API之间的差异,Grafika会为您标准化这些操作。

三.安装 

    1.使用composer安装

composer require kosinix/grafika:dev-master --prefer-dist

    2.从Github存储库下载放到您目录的位置在文件中引用

require_once '/path/to/src/autoloader.php';

四.实现九宫格的方法

<?php
use Grafika\Grafika;
$pic=array(
    "/uploads/avatar/15674176122.jpg",
    "/uploads/avatar/15674176122.jpg",
    "/uploads/avatar/15674176122.jpg",
    "/uploads/avatar/15674176122.jpg",
    "/uploads/avatar/15674176122.jpg",
    "/uploads/avatar/15674176122.jpg"
);
$this->jiugongge($pic);
/*
    九宫格图片
    $pic_list  一维数组图片的路径
    $filename 图片的名称 为空时候 自动生成图片的名称
    $bg_w 生成图片画布的宽度
    $bg_h 生成图片画布的高度
    画布上的图片根据画布的高度自行计算
*/
public function jiugongge($pic_list = array(),$filename,$bg_w = 100,$bg_h=100)
{
    $fname= empty($filename)?date('YmdHis').rand(100,999):$filename;
    $imagePath = './uploads/groupthumb/'.$fname.'.jpg';
    $imagePaths = '/uploads/groupthumb/'.$fname.'.jpg';
    /*if(file_exists($imagePath))
    {
        return SITE_URL.$imagePaths;
    } */
    $pic_list= array_slice($pic_list, 0, 9);
    $pic_count = count($pic_list);
    $imageBack = Grafika::createBlankImage($bg_w,$bg_h);
    switch($pic_count)
     {
         case 1:
         $pic_w = $bg_w;
         $pic_h = $bg_h;
         $weizhi=array("center");
         break;
         case 2: 
         $pic_w = intval($bg_w/2);
         $pic_h = intval($bg_h/2);
         $weizhi=array("center-right","center-left");
         break;
         case 3:
         $pic_w = intval($bg_w/2);
         $pic_h = intval($bg_h/2);
         $weizhi=array("top-left","top-right","bottom-left");
         break;
         case 4:
         $pic_w = intval($bg_w/2);
         $pic_h = intval($bg_h/2);
         $weizhi=array("top-left","top-right","bottom-left","bottom-right");
         break;
         case 5:
         $pic_w = intval($bg_w/3)+1;
         $pic_h = intval($bg_h/3)+1;
         $weizhi=array("top-left","top-right","center","bottom-left","bottom-right");
         break;
         case 6:
         $pic_w = intval($bg_w/3)+1;
         $pic_h = intval($bg_h/3)+1;
         $weizhi=array("top-left","top-center","top-right","center-left","center","center-right");
         break;
         case 7:
         $pic_w = intval($bg_w/3)+1; 
         $pic_h = intval($bg_h/3)+1; 
         $weizhi=array("top-left","top-center","top-right","center-left","center","center-right","bottom-left");
         break;
         case 8:
         $pic_w = intval($bg_w/3)+1; 
         $pic_h = intval($bg_h/3)+1;
         $weizhi=array("top-left","top-center","top-right","center-left","center","center-right","bottom-left","bottom-center");
         break;
         case 9:
         $pic_w = intval($bg_w/3)+1; 
         $pic_h = intval($bg_h/3)+1; 
         $weizhi=array("top-left","top-center","top-right","center-left","center","center-right","bottom-left","bottom-center","bottom-right");
         break;
        }
        foreach($pic_list as $k=>$v)
        {
            Grafika::createEditor()
            ->open( $image, ".".$v )
            ->resizeFill( $image, $pic_w, $pic_h )
            ->blend($imageBack, $image, 'normal', 1, $weizhi[$k])
            ->save( $imageBack, $imagePath, null, 90 );
        }
        return SITE_URL.$imagePaths;
    }
?>

效果图:

111.jpg

打赏本站,你说多少就多少

本文地址:https://www.qi522.com/view/108.html

来     源:千奇博客

精彩评论

微信关注

Copyright © 2013-2019 千奇博客 保留所有权利 辽ICP备13008238号