php 图片旋转和png透明

因需要先处理生成的二维码图片旋转,再和另外一张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 获取图片的宽度和高度。

参考
  1. imagecreatefrompng() Makes a black background instead of transparent? -- 学习用到 imagecolortransparent() 方法
  2. Rotate a PNG then resave with Image Transparency -- 主要参考这个回答旋轉 png 并透明低

转载于:https://www.cnblogs.com/fsong/p/11260115.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值