zoukankan      html  css  js  c++  java
  • 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 并透明低
  • 相关阅读:
    Eclipse安装aptana
    mysql获取下一篇和上一篇文章的ID
    Java回顾之Spring基础
    纯CSS实现各类气球泡泡对话框效果
    百度编辑器ueditor的简单使用
    实施接口
    Java快速教程
    Java GUI程序设计
    JAVA之关于This的用法
    Java 数组基础
  • 原文地址:https://www.cnblogs.com/fsong/p/11260115.html
Copyright © 2011-2022 走看看