Android png透明图片转jpg时背景变黑的解决方法


-
主要介绍了Android png透明图片转jpg时背景变黑的解决方法,需要的朋友可以参考下
-
2020-08-28
PNG的透明背景保存为黑色_course
2013-06-21<div class="post-text" itemprop="text"> <p>I recently followed <a href="http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/" rel="nofollow">this</a> tutorial, but the thing is when I save a PNG it has a solid black background?.. I am rather new to php.. how do I save it with a transparent background?</p> <p>Here's the php class:</p> <pre><code># include("classes/resize_class.php"); # $resizeObj = new resize('images/cars/large/input.jpg'); # $resizeObj -> resizeImage(150, 100, 0); # $resizeObj -> saveImage('images/cars/large/output.jpg', 100); Class resize { // *** Class variables private $image; private $width; private $height; private $imageResized; function __construct($fileName) { // *** Open up the file $this->image = $this->openImage($fileName); // *** Get width and height $this->width = imagesx($this->image); $this->height = imagesy($this->image); } ## -------------------------------------------------------- private function openImage($file) { // *** Get extension $extension = strtolower(strrchr($file, '.')); switch($extension) { case '.jpg': case '.jpeg': $img = @imagecreatefromjpeg($file); break; case '.gif': $img = @imagecreatefromgif($file); break; case '.png': $img = @imagecreatefrompng($file); break; default: $img = false; break; } return $img; } ## -------------------------------------------------------- public function resizeImage($newWidth, $newHeight, $option="auto") { // *** Get optimal width and height - based on $option $optionArray = $this->getDimensions($newWidth, $newHeight, $option); $optimalWidth = $optionArray['optimalWidth']; $optimalHeight = $optionArray['optimalHeight']; // *** Resample - create image canvas of x, y size $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight); imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height); // *** if option is 'crop', then crop too if ($option == 'crop') { $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight); } } ## -------------------------------------------------------- private function getDimensions($newWidth, $newHeight, $option){ switch ($option) { case 'exact': $optimalWidth = $newWidth; $optimalHeight= $newHeight; break; case 'portrait': $optimalWidth = $this->getSizeByFixedHeight($newHeight); $optimalHeight= $newHeight; break; case 'landscape': $optimalWidth = $newWidth; $optimalHeight= $this->getSizeByFixedWidth($newWidth); break; case 'auto': $optionArray = $this->getSizeByAuto($newWidth, $newHeight); $optimalWidth = $optionArray['optimalWidth']; $optimalHeight = $optionArray['optimalHeight']; break; case 'crop': $optionArray = $this->getOptimalCrop($newWidth, $newHeight); $optimalWidth = $optionArray['optimalWidth']; $optimalHeight = $optionArray['optimalHeight']; break; } return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); } ## -------------------------------------------------------- private function getSizeByFixedHeight($newHeight) { $ratio = $this->width / $this->height; $newWidth = $newHeight * $ratio; return $newWidth; } private function getSizeByFixedWidth($newWidth) { $ratio = $this->height / $this->width; $newHeight = $newWidth * $ratio; return $newHeight; } private function getSizeByAuto($newWidth, $newHeight) { if ($this->height < $this->width) // *** Image to be resized is wider (landscape) { $optimalWidth = $newWidth; $optimalHeight= $this->getSizeByFixedWidth($newWidth); } elseif ($this->height > $this->width) // *** Image to be resized is taller (portrait) { $optimalWidth = $this->getSizeByFixedHeight($newHeight); $optimalHeight= $newHeight; } else // *** Image to be resized is a square { if ($newHeight < $newWidth) { $optimalWidth = $newWidth; $optimalHeight= $this->getSizeByFixedWidth($newWidth); } else if ($newHeight > $newWidth) { $optimalWidth = $this->getSizeByFixedHeight($newHeight); $optimalHeight= $newHeight; } else { // *** Sqaure being resized to a square $optimalWidth = $newWidth; $optimalHeight= $newHeight; } } return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); } ## -------------------------------------------------------- private function getOptimalCrop($newWidth, $newHeight) { $heightRatio = $this->height / $newHeight; $widthRatio = $this->width / $newWidth; if ($heightRatio < $widthRatio) { $optimalRatio = $heightRatio; } else { $optimalRatio = $widthRatio; } $optimalHeight = $this->height / $optimalRatio; $optimalWidth = $this->width / $optimalRatio; return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight); } ## -------------------------------------------------------- private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight) { // *** Find center - this will be used for the crop $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 ); $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 ); $crop = $this->imageResized; //imagedestroy($this->imageResized); // *** Now crop from center to exact requested size $this->imageResized = imagecreatetruecolor($newWidth , $newHeight); imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight); } ## -------------------------------------------------------- public function saveImage($savePath, $imageQuality="100") { // *** Get extension $extension = strrchr($savePath, '.'); $extension = strtolower($extension); switch($extension) { case '.jpg': case '.jpeg': if (imagetypes() & IMG_JPG) { imagejpeg($this->imageResized, $savePath, $imageQuality); } break; case '.gif': if (imagetypes() & IMG_GIF) { imagegif($this->imageResized, $savePath); } break; case '.png': // *** Scale quality from 0-100 to 0-9 $scaleQuality = round(($imageQuality/100) * 9); // *** Invert quality setting as 0 is best, not 9 $invertScaleQuality = 9 - $scaleQuality; if (imagetypes() & IMG_PNG) { imagepng($this->imageResized, $savePath, $invertScaleQuality); } break; // ... etc default: // *** No extension - No save. break; } imagedestroy($this->imageResized); } } ?> </code></pre> <p>a lot of code i know.. but i wouldn't know which parts are vital..apologies...</p> <p>Any Help Greatly Appreciated.</p> </div>
- android显示PNG图片背景黑色问题 ImageView PNG 背景黑色 6512020-06-08APP需要动态从网络下载PNG图片,然后在BottomNavigationView菜单中显示出来 下载PNG图片后,显示发送背景一直是黑色的。。。 使用Glide显示PNG图片也是背景黑色 网上查了好久,没发现解决方法。。。。 实在走投无路了,看到一篇文章说是不是PNG本身背景就是黑色的呢? 果不其然,马上使用浏览器下载PNG图片查看,发现背景确实是黑色的。。。 怎么会这样,想到是不是当初我把PNG图片通过QQ发送给别人上传到服务器时,格式被转换掉了? 抓包看了下 返回的HTTP头也正常:
Android实现bmp图片转换成jpg_course
2013-10-15由于BMP图片太大,imageview控件无法正常显示,所以我想把BMP图片先转换成jpg再进行显示,但由于刚刚接触不甚了解,请大神们帮帮我。。谢谢了
9.11MB
图片转换Png源代码
2012-11-12可以把jpg等格式的图片转成Png,在Delphi xe2 下通过测试。
16.59MB
Android将bitmap保存到本地png/jpg格式等
2018-12-21详情请见本人的博客文章https://blog.csdn.net/qwe25878/article/details/85159702
82.2MB
微信小程序源码-合集6.rar
2020-09-04微信小程序源码,包含:图片展示、外卖点餐、小工具类、小游戏类、演绎博览、新闻资讯、医疗保健、艺术生活等源码。
133KB
python 京东预约抢购茅台脚本插件 一键运行
2021-02-26python 京东预约抢购茅台脚本插件 一键运行,按照readme介绍的步骤即可。 已经测试可以抢购得到。 注意:本资源仅用于用来学习,严禁用于任何商业目的,下载之后应当在24小时之内删除。
28KB
各显卡算力对照表!
2018-01-11挖矿必备算力对照!看看你的机器是否达到标准!看完自己想想办法刷机!
Python自动化爬虫实战与高级架构技巧
2020-07-22讲解基于Python Selenium 的自动化数据采集,自动化框架设计,SEO搜索收录引擎与接口对接等实战项目
103KB
七夕情人节表白HTML源码(两款)
2016-08-23七夕节、情人节表白用的HTML源码(两款)
-
学院
《文件过滤及内容编辑处理命令》<Linux核心命令系列Series>
《文件过滤及内容编辑处理命令》<Linux核心命令系列Series>
-
下载
第8章 起重机的电气控制.ppsx
第8章 起重机的电气控制.ppsx
-
下载
160wifi(电脑Wifi共享软件)官方中文版V1111_4.3.10.20 | 160wifi电脑版怎么用 | 电脑wifi共享软件哪个好用
160wifi(电脑Wifi共享软件)官方中文版V1111_4.3.10.20 | 160wifi电脑版怎么用 | 电脑wifi共享软件哪个好用
-
学院
MySQL 主从复制 Replication 详解(Linux 和 W
MySQL 主从复制 Replication 详解(Linux 和 W
-
下载
中文词汇分类搜狗语料训练结果
中文词汇分类搜狗语料训练结果
-
博客
利用内网穿透搭建Jupyter lab远程服务器
利用内网穿透搭建Jupyter lab远程服务器
-
学院
计算机网络 静态路由和动态路由协议
计算机网络 静态路由和动态路由协议
-
学院
MySQL 备份与恢复详解(高低版本 迁移;不同字符集 相互转换;表
MySQL 备份与恢复详解(高低版本 迁移;不同字符集 相互转换;表
-
学院
MySQL 数据库的基本操作(数据完整性约束)
MySQL 数据库的基本操作(数据完整性约束)
-
博客
六.模型基础 2021-03-03
六.模型基础 2021-03-03
-
下载
武汉大学2018年秋季《大学物理B(下)》期中考试试卷A、B卷(含答案).pdf
武汉大学2018年秋季《大学物理B(下)》期中考试试卷A、B卷(含答案).pdf
-
博客
Python.牛客.HJ50.四则运算
Python.牛客.HJ50.四则运算
-
学院
CCNA_CCNP 思科网络认证 PAT NAT 端口或地址转换 与端
CCNA_CCNP 思科网络认证 PAT NAT 端口或地址转换 与端
-
下载
第二章 Profibus现场总线通信系统的组建.pptx
第二章 Profibus现场总线通信系统的组建.pptx
-
博客
x265并行代码分析(二)
x265并行代码分析(二)
-
学院
朱老师鸿蒙系列课程第1期-2鸿蒙系统Harmonyos源码架构分析
朱老师鸿蒙系列课程第1期-2鸿蒙系统Harmonyos源码架构分析
-
学院
xxljob源码分析
xxljob源码分析
-
学院
P1Python100练从入门到入土系列
P1Python100练从入门到入土系列
-
学院
计算机网络 应用层 诸多协议 实验环境搭建
计算机网络 应用层 诸多协议 实验环境搭建
-
学院
2021年软考系统规划与管理师-下午历年真题解析视频课程
2021年软考系统规划与管理师-下午历年真题解析视频课程
-
下载
浙江工商大学《微积分(下)》10-12历年期末考试试卷(含答案).pdf
浙江工商大学《微积分(下)》10-12历年期末考试试卷(含答案).pdf
-
博客
Python笔记
Python笔记
-
学院
《文件和目录操作命令》<Linux核心命令系列Series> <2.>
《文件和目录操作命令》<Linux核心命令系列Series> <2.>
-
博客
2021-03-06
2021-03-06
-
学院
MySQL 多平台多模式(安装、配置和连接 详解)
MySQL 多平台多模式(安装、配置和连接 详解)
-
下载
武汉大学2019年《大学物理AB(下)》期中考试试卷(含答案).pdf
武汉大学2019年《大学物理AB(下)》期中考试试卷(含答案).pdf
-
学院
QT编程思想【C++,基于QT 6】
QT编程思想【C++,基于QT 6】
-
博客
Java010: Java开发工具包-Jdk的下载
Java010: Java开发工具包-Jdk的下载
-
博客
centOS安装PHP后,php-fpm启动失败的解决
centOS安装PHP后,php-fpm启动失败的解决
-
博客
service指令
service指令