首页 > Web开发 > 详细

php 图片旋转和png透明

时间:2019-07-28 20:24:04      阅读:213      评论:0      收藏:0      [点我收藏+]

因需要先处理生成的二维码图片旋转,再和另外一张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 并透明低

php 图片旋转和png透明

原文:https://www.cnblogs.com/fsong/p/11260115.html

(0)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!