直接绘制文字

步骤:

- 使用imagecreatetruecolor创建真彩色图像

- 使用imagecolorallocate,imagefill填充背景色

- 使用imagecolorallocate设置文字颜色

- 使用imagettfbbox获取文字所占空间,以此为前提根据具体推算出需要文字起始点

- 使用imagettftext添加文字

- 输出图片

- 注销资源

public function images(){

$image_width = 400;

$image_height = 400;

// 创建真彩色图像

$image = imagecreatetruecolor($image_width,$image_height);

// 创建背景颜色

$background_color = imagecolorallocate($image,255,255,255);

// 填充背景色

imagefill($image,0,0,$background_color);

// 文字颜色

$text_color = imagecolorallocate($image,0,0,0);

$string = "

和晋陵陆丞早春游望

独有宦游人,偏惊物候新。

云霞出海曙,梅柳渡江春。

淑气催黄鸟,晴光转绿蘋。

忽闻歌古调,归思欲沾巾。";

// 字体地址

$fontfile = ROOT_PATH."public/assets/fonts/fzltxh.ttf";

// 取得使用 TrueType 字体的文本的范围 可参考:https://www.jc2182.com/php/php-imagettfbbox-ref.html

$ttbox = imagettfbbox(20,0,$fontfile,$string);

// 获取x,y坐标,文字居中

$x = ($image_width - $ttbox[0]-$ttbox[2])/2;

$y = ($image_height - $ttbox[1]-$ttbox[5])/2;

// 添加文字

imagettftext($image,20,0,$x,$y,$text_color,$fontfile,$string);

// 输出文件

header("Content-type:image/jpeg");

imagejpeg($image);

// 释放资源

imagedestroy($image);

exit; //tp5 需要添加断点,否则输出base64

}

给图片加文字水印

步骤

- 使用getimagesize获取图片属性

- 使用imagecreatefrom+图片类型函数,创建画布背景

- 使用imagettftext添加文字水印

- 输出图片

- 注销资源

public function images(){

// 图片地址

$fielpath = ROOT_PATH."public/uploads/touming.png";

// 获取原图像的属性

list($img_width,$img_height,$img_type) = getimagesize($fielpath);

// getimagesize()函数返回type类型定义

$img_type_list = [1=>'GIF',2=>'JPEG',3=>'PNG',4=>'SWF'];

// 使用图片创建背景

$create_function = "imagecreatefrom".$img_type_list[$img_type];

$image = $create_function($fielpath);

// 设定图像的混色模式

// 关闭 alpha 渲染并设置 alpha 标志 函数详细介绍请参考:https://www.php.net/manual/zh/function.imagesavealpha.php

imagealphablending($image,false);

imagesavealpha($image,true);

// 水印字符串

$string = "和晋陵陆丞早春游望 ";

// 水印颜色

$text_color = imagecolorallocate($image,255,23,23);

// 字体地址

$fontfile = ROOT_PATH."public/assets/fonts/fzltxh.ttf";

// 水印放置右下角

// 获取像素和字体大小之间的比例

$tt_box = imagettfbbox(16,0,$fontfile,$string);

$fontsize = (16*$img_width/3)/($tt_box[2]-$tt_box[0]);

$tt_box = imagettfbbox($fontsize,0,$fontfile,$string);

// 设置x起点

$x = $img_width*2/3;

// y起点

if ($tt_box[5] - $tt_box[1] > 0){

$y = $img_height - ($tt_box[5] - $tt_box[1]);

}else{

$y = $img_height + ($tt_box[5] - $tt_box[1]);

}

// 添加水印

imagettftext($image,$fontsize,0,$x,$y,$text_color,$fontfile,$string);

// 输出文件

header("Content-type:image/jpeg");

$put_function = "image".$img_type_list[$img_type];

$put_function($image);

// 释放资源

imagedestroy($image);

exit; //tp5 需要添加断点,否则输出base64

}

图片水印

步骤

- 创建背景图片

- 根据原图大小对水印图片进行缩放:imagecopyresampled

- imagecopy添加水印

public function images(){

// 图片地址

$fielpath = ROOT_PATH."public/uploads/touming.png";

$water_fielpath = ROOT_PATH."public/uploads/22.png";

// 获取原图像的属性

list($img_width,$img_height,$img_type) = getimagesize($fielpath);

// getimagesize()函数返回type类型定义

$img_type_list = [1=>'GIF',2=>'JPEG',3=>'PNG',4=>'SWF'];

// 使用图片创建背景

$create_function = "imagecreatefrom".$img_type_list[$img_type];

$image = $create_function($fielpath);

// 设定图像的混色模式

// 关闭 alpha 渲染并设置 alpha 标志 函数详细介绍请参考:https://www.php.net/manual/zh/function.imagesavealpha.php

imagealphablending($image,false);

imagesavealpha($image,true);

// 获取水印图片属性

list($water_img_width,$water_img_height,$water_img_type) = getimagesize($water_fielpath);

// 根据原图设置水印图片的缩放

// 缩放后的宽度,缩放后的高度

$to_zoo_width = $img_width*0.2;

$to_zoo_height = $img_height*0.2;

// 等比例缩放图片

if ($water_img_width>$water_img_height){

$to_zoo_width = $water_img_width*($to_zoo_height/$water_img_height);

}else{

$to_zoo_height = $water_img_height*($to_zoo_width/$water_img_width);

}

// 创建防止缩放后的图片地址

$to_zoo_image = imagecreatetruecolor($to_zoo_width,$to_zoo_height);

// 使用图片创建背景

$create_water_function = "imagecreatefrom".$img_type_list[$water_img_type];

$water_image = $create_water_function($water_fielpath);

// 缩放图片 ps:裁剪和缩放是方法相同

$imagecopyresampled = imagecopyresampled($to_zoo_image,$water_image,0,0,0,0,$to_zoo_width,$to_zoo_height,$water_img_width,$water_img_height);

// 缩放成功后,设置水印

if ($imagecopyresampled){

$x = $img_width - $to_zoo_width - $img_width*0.001;

$y = $img_height - $to_zoo_height - $img_height*0.001;

imagecopy($image,$to_zoo_image,$x,$y,0,0,$to_zoo_width,$to_zoo_height);

// 输出文件

header("Content-type:image/jpeg");

$put_function = "image".$img_type_list[$img_type];

$put_function($image);

// 释放资源

imagedestroy($image);

exit; //tp5 需要添加断点,否则输出base64

}else{

echo "异常错误";

}

}

php 图片处理慢,php图片处理相关推荐

  1. Python使用PIL将png图片转化为jpg图片

    Python使用PIL将png图片转化为jpg图片 pip install Pillow #PIL将png图片转化为jpg图片语法 from PIL import Imageim1 = Image.o ...

  2. jquery实现上传图片及图片大小验证、图片预览效果代码

    jquery实现上传图片及图片大小验证.图片预览效果代码 jquery实现上传图片及图片大小验证.图片预览效果代码 上传图片验证 */ function submit_upload_picture() ...

  3. CSDN博文中完美地去掉图片水印、调整图片位置和大小

    写在这里的初衷,一是备忘,二是希望得到高人指点,三是希望能遇到志同道合的朋友. 写博客的时候,大多数人会有这种感觉:图片上传会有水印,位置默认靠左,有时候嫌直接上传比较大,不太美观,特此查找了解决办法 ...

  4. 微信小程序 --- 图片自适应、本地图片的使用

    1.关于图片自适应 image标签中添加mode属性: 默认值:scaleToFill  ----  不保持纵横比例缩放图片,使图片的宽高完全拉伸至填满image标签 aspectFit  ---- ...

  5. Android之给图片去色,返回灰度图片以及ColorMatrix中setSaturation方法的用法

    原图: 效果图: 实现以上效果其实很简单,直接上代码: public class MainActivity extends Activity {private Button btn_start;pri ...

  6. [转]图片自动缩放 js图片缩放

    转自:http://hi.baidu.com/crystalhx/blog/item/deba9b2320274340ac34de09.html 图片自动缩放 js图片缩放 2008-03-27 10 ...

  7. 显示服务器图片url,服务器上图片的url地址

    服务器上图片的url地址 内容精选 换一换 分析并识别用户上传的图像内容是否有敏感内容(如涉及政治人物.暴恐元素.涉黄内容等),并将识别结果返回给用户.在使用图像内容审核之前需要您完成服务申请和认证鉴 ...

  8. 一个图片 在另一个图片定位_一个好的listing,图片有哪些基本要求

    众所周知做跨境电商,产品的listing非常重要,一个好的listing的五要素都要有优化,那么listing产品图片有哪些最基本的要求呢?我们一起来看看. 图片定义 在亚马逊上传的每个产品都必须要求 ...

  9. python下载图片-Python下载URL图片

    所谓下载URL图片就是指通过网络图片的URL去用脚本自动获取和下载图片到本地. 这里介绍两种方法,一种需要用到第三方库requests,一种直接使用Python自带的库urllib. 首先找到你要下载 ...

  10. C#跑马灯,图片滚动,后台获取图片地址。动态绑定图片,imag显示文字

    下面附下载地址. http://download.csdn.net/download/njxiaogui/10002058 1.跑马灯效果,图片连续循环滚动,图片下面并可附文字描述,图片是从数据库中获 ...

最新文章

  1. SaaS到底是什么,如何做?这份笔记讲明白了
  2. html主动发起重新布局,重启连不上网
  3. “365算法每日学计划”:05打卡-图解冒泡排序(多解法)
  4. Java进阶之深入理解JVM类加载机制
  5. [MATLAB学习笔记]view相机视角
  6. 【Python】 1055 集体照 (25 分)
  7. android之SQLite数据库insert操作
  8. 第13章 Django框架
  9. opencv简单的矩阵操作
  10. Daily Scrum M2 11-19
  11. 员工的12个需求及实现
  12. Presentation Prompter for Mac(屏幕提词器)
  13. (20)python_matplotlib解决中文乱码问题
  14. Logstash_snmp与Logstash_snmptrap日志采集配置说明
  15. 使用dd命令克隆整个系统
  16. Graphite详解
  17. 输出青蛙跳台所有路径
  18. Matlab — 常见矩阵生成及矩阵运算
  19. OpenGL之glut、glfw、glew、glad等库之间的关系
  20. [转]IT人的学习方法

热门文章

  1. 微信银行突破单一模式功能日渐强大
  2. html 锁屏模板,如何自定义锁屏样式
  3. 远程办公何时了,网络打洞帮你搞
  4. 挪威访学2:SOLA UIS 学生宿舍
  5. 场景建模都用到哪些三维软件,入门学习选择3DsMax还是Maya
  6. POWER BI:SSAS表格建模(转自博客园,ps:部分已修改)
  7. 每日英语:Japan Leader Warns China on Islands Dispute
  8. 初出茅庐的小李第73篇博客之offsetof(type, member-designator)使用
  9. Java数据库插入记录的语句-单引号-双引号values('username+'-'+password
  10. ARCGIS中坐标转换及地理坐标、投影坐标的定义(转载)