php 图片旋转和png透明
PHP 图片 旋转 透明 PNG
2023-09-11 14:20:09 时间
因需要先处理生成的二维码图片旋转,再和另外一张png图片合并,图片都是png
<?php
// this file writes the image into the http response,
// so we cant have any output other than headers and the file data
ob_start();
$filename = 'qrcode.png'; //my qrcode background color is white
$degrees = 7;
// open the image file
$im = imagecreatefrompng( $filename );
// create a transparent "color" for the areas which will be new after rotation
// r=255,b=255,g=255 ( white ), 127 = 100% transparency - we choose "invisible black"
$transparency = imagecolorallocatealpha( $im,255,255,255,0 );
// rotate, last parameter preserves alpha when true
$rotated = imagerotate( $im, $degrees, $transparency, 1);
//maybe there have make white color is transparent
$background = imagecolorallocate($rotated , 255, 255, 255);
imagecolortransparent($rotated,$background);
// disable blendmode, we want real transparency
imagealphablending( $rotated, false );
// set the flag to save full alpha channel information
imagesavealpha( $rotated, true );
// now we want to start our output
ob_end_clean();
// we send image/png
header( 'Content-Type: image/png' );
imagepng( $rotated );
// clean up the garbage
imagedestroy( $im );
imagedestroy( $rotated );
因为图片旋转之后,图片的大小是会变化的,所以可以 使用 imagesx
,imagesy
可以获取图片资源的宽度和高度,就不需要 保存图片,之后读取图片,再使用 getimagesize 获取图片的宽度和高度。
参考
- imagecreatefrompng() Makes a black background instead of transparent? -- 学习用到
imagecolortransparent()
方法 - Rotate a PNG then resave with Image Transparency -- 主要参考这个回答旋轉 png 并透明低
相关文章
- php使用gd库输出中文内容的图片
- PHP利用GD库处理图片方法实现
- PHP上传图片,路径保存在数据库中,根据图片路径显示图片
- PHP下载/采集远程图片到本地
- PHP常量PHP_SAPI与函数php_sapi_name()简介,PHP运行环境检测
- php数组打乱顺序
- 2020 PHP 初级 / 基础面试题,祝你金三银四跳槽加薪 (适合基础不牢固的 PHPer)
- PHP高并发和大流量怎么解决?
- php文件上传
- [获取行数]php读取大文件提供性能的方法,PHP的stream_get_line函数读取大文件获取文件的行数的方...
- PHP的cURL快速入门 (小偷采集程序)
- PHP Primary script unknown , File not found
- php读取和保存base64编码的图片内容
- PHP写的异步高并发服务器,基于libevent
- file not found. nginx php nginx 如何开启解析 PHP 的功能
- PHP执行insert语句报错“Data too long for column”解决办法
- thinkphp6:访问redis6(thinkphp 6.0.9/php 8.0.14)
- paip.数组以及集合的操作uapi java php python总结..
- 使用PHP实现查找你想要的附近人
- PHP面试题:写一个二维数组排序算法函数,能够具有通用性,可以调用php内置函数(array_multisort())
- php导入sql文件
- php实现base64图片上传方式实例代码
- PHP 使用 Redis