GD库图片裁剪指定形状解决办法(PHP GD库 海报)

需求描述:需要把图片裁剪成一个指定的平行四边形,目的是使用GD库,把裁剪后的图片放在底图上面,使最终合成的图片看起来是一个底图+平行四边形的样子

提示:可以结合本作者的其他文章,来生成一个定制化的海报(到此为止,定制化海报生成的所有知识点就写完了,大家可以自己看看那部分是自己需要,可以自己获取相应部分)

解决方案:第一步便是生成矩形的图片,但是经过测试后发现,无论是使用GD库还是使用PS,最终的图片还是一个矩形,并不能真正的实现把图片裁剪成为自己想要的形状,那么一种方案行不通那就换成另外一种方案,使用遮蔽物遮挡,把图片变成我们想要的形状,比如左边的三角形和右边的三角形,这两个形状就是使用imagefilledpolygon函数绘制填充的多边形来填充,最终获取到想要的图案

示例图:

//图片像素点

public function pixels(){

$imgpath = '地址';

$image = $this->createImageFromFile($imgpath);

//定位(左边渲染块)

$values = array(

0, 0, // Point 1 (x, y)

0, 420, // Point 2 (x, y)

100, 0 // Point 3 (x, y)

);

//获取颜色

$gr = imagecolorallocate($image, 228, 251, 255);

imagefilledpolygon($image, $values, 3, $gr);

//定位(右边渲染块)

$values = array(

420, 420,

420, 0,

320, 420

);

$gr = imagecolorallocate($image, 228, 251, 255);

imagefilledpolygon($image, $values, 3, $gr);

header('Content-type:image/png');

imagepng($image);

}

//获取图片类型,从字符串中的图像流新建一图像:非本地图片获取图像流生成本地图片

function createImageFromFile($file)

{

if (preg_match('/http(s)?:\/\//', $file)) {

$fileSuffix = $this->getNetworkImgType($file);

} else {

$fileSuffix = pathinfo($file, PATHINFO_EXTENSION);

}

if (!$fileSuffix) return false;

switch ($fileSuffix) {

case 'jpeg':

if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {

// imagecreatefromstring:从字符串中的图像流新建一图像, 返回一个图像标识符,其表达了从给定字符串得来的图像

$theImage = @imagecreatefromstring($this->http_get_data($file));

} else {

$theImage = @imagecreatefromjpeg($file);

if (!$theImage) {

$theImage = @imagecreatefromstring($this->http_get_data($file));

}

}

break;

case 'jpg':

if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {

$theImage = @imagecreatefromstring($this->http_get_data($file));

} else {

$theImage = @imagecreatefromjpeg($file);

if (!$theImage) {

$theImage = @imagecreatefromstring($this->http_get_data($file));

}

}

break;

case 'png':

if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {

$theImage = @imagecreatefromstring($this->http_get_data($file));

} else {

$theImage = @imagecreatefrompng($file);

if (!$theImage) {

$theImage = @imagecreatefromstring($this->http_get_data($file));

}

}

break;

case 'gif':

if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {

$theImage = @imagecreatefromstring($this->http_get_data($file));

} else {

$theImage = @imagecreatefromgif($file);

if (!$theImage) {

$theImage = @imagecreatefromstring($this->http_get_data($file));

}

}

break;

default:

$theImage = @imagecreatefromstring($this->http_get_data($file));

break;

}

return $theImage;

}

//获取图片类型

private function getNetworkImgType($url)

{

$ch = curl_init(); //初始化curl

curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL

curl_setopt($ch, CURLOPT_NOBODY, 1);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时

curl_setopt($ch, CURLOPT_TIMEOUT, 3);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https

curl_exec($ch);//执行curl会话

$http_code = curl_getinfo($ch);//获取curl连接资源句柄信息

curl_close($ch);//关闭资源连接

if ($http_code['http_code'] == 200) {

$theImgType = explode('/', $http_code['content_type']);

if ($theImgType[0] == 'image') {

return $theImgType[1];

} else {

return false;

}

} else {

return false;

}

}

//获取远程图片

function http_get_data($url)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_URL, $url);

ob_start();

curl_exec($ch);

$return_content = ob_get_contents();

ob_end_clean();

curl_getinfo($ch, CURLINFO_HTTP_CODE);

return $return_content;

}


http://www.niftyadmin.cn/n/69278.html

相关文章

第四章 - 基础查询语句(select,distinct,limit)

基础查询语句,select,distinct,limit查询单列数据查询多列数据查询所有数据去重查询(distinct)限制查询结果(limit)查询单列数据 在列名和函数没有冲突的时候可以不用加前后的比如有时候时间的…

关于JSON

<!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title></title> </head> <body> <script> /* 1、JSON的英文全称&#xff1a;Java…

企业降本增效的催化剂:敏捷迭代

伴随着开源技术的大爆发&#xff0c;新一代的软件技术如雨后春笋般层出不穷。每家企业在硬件及软件开发上都有许多开源技术可选&#xff0c;目的还是在于提高效率&#xff0c;降低开发成本。 本篇文章&#xff0c;带大家了解下促进企业降本增效的重要理念&#xff1a;敏捷迭代…

ChatGPT is not all you need,一文看尽SOTA生成式AI模型:6大公司9大类别21个模型全回顾(三)

文章目录ChatGPT is not all you need&#xff0c;一文看尽SOTA生成式AI模型&#xff1a;6大公司9大类别21个模型全回顾&#xff08;三&#xff09;Text-to-Text 模型ChatGPTLaMDAPEERMeta AI Speech from BrainText-to-Code 模型CodexAlphacodeText-to-Science 模型GalacticaM…

第06章_MySQL多表查询

第06章_多表查询 讲师&#xff1a;尚硅谷-宋红康&#xff08;江湖人称&#xff1a;康师傅&#xff09; 官网&#xff1a;http://www.atguigu.com 多表查询&#xff0c;也称为关联查询&#xff0c;指两个或更多个表一起完成查询操作。 前提条件&#xff1a;这些一起查询的表之…

类和对象(下)(一)

类和对象&#xff08;下&#xff09;&#xff08;一&#xff09;1.再谈构造函数1.1构造函数体赋值1.2初始化列表1.3explicit关键字2.static成员2.1概念2.2特性3.匿名对象&#x1f31f;&#x1f31f;hello&#xff0c;各位读者大大们你们好呀&#x1f31f;&#x1f31f; &#x…

ChatGPT 未来会造成大量职业的裁员和失业吗?

大家好&#xff0c;小编来为大家解答以下问题&#xff0c;一个有趣的事情&#xff0c;一个有趣的事情&#xff0c;今天让我们一起来看看吧&#xff01; 1、chatGpt来自那个国家&#xff1f; ChatGPT是一种聊天机器人技术&#xff0c;它来自美国&#xff0c;由OpenAI公司开发。…

操作系统的概念,功能和目标

一、概念&#xff1a; 操作系统是指①控制和管理整个计算机系统的硬件和软件资源&#xff0c;并合理地组织调度计算机的工作和资源的分配&#xff0c;以②提供给用户和其他软件方便的接口和环境&#xff0c;它③是计算机系统中最基本的系统软件。 二、功能和目标&#xff1a;…