我正在使用一个脚本,让用户上传图像。脚本调整大小并将图像转换为JPEG。

我遇到的问题是上传透明度的PNG时,生成的JPEG图像是透明度为黑色的图像。

如何编辑下面的脚本来替换黑色的白色?它已经为GIF而做,但不是PNG的。

// RESIZE IMAGE AND PUT IN USER DIRECTORY

switch($this->file_ext)

{

case "gif":

$file = imagecreatetruecolor($width, $height);

$new = imagecreatefromgif($this->file_tempname);

$kek=imagecolorallocate($file, 255, 255, 255);

imagefill($file,0,0,$kek);

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

case "bmp":

$file = imagecreatetruecolor($width, $height);

$new = $this->imagecreatefrombmp($this->file_tempname);

for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

case "jpeg":

case "jpg":

$file = imagecreatetruecolor($width, $height);

$new = imagecreatefromjpeg($this->file_tempname);

for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

case "png":

$file = imagecreatetruecolor($width, $height);

$new = imagecreatefrompng($this->file_tempname);

for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

}

chmod($photo_dest, 0777);

return true;

}

我尝试编辑案例“png”:部分匹配案例“gif”:代码,但生成的JPEG是完全白色的。

更新:

我自己修好了

谢谢大家,贡献!

我换了

case "png":

$file = imagecreatetruecolor($width, $height);

$new = imagecreatefrompng($this->file_tempname);

for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

有:

case "png":

$file = imagecreatetruecolor($width, $height);

$new = imagecreatefrompng($this->file_tempname);

$kek=imagecolorallocate($file, 255, 255, 255);

imagefill($file,0,0,$kek);

imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);

imagejpeg($file, $photo_dest, 100);

ImageDestroy($new);

ImageDestroy($file);

break;

php改变图片大小png背景变黑,php – 当将透明背景的PNG图像调整大小/转换为JPEG时,如何用白色替换黑色背景...相关推荐

  1. Linux改变图片大小的命令,如何在Ubuntu命令行上调整图像大小

    在过去的几年中,共享图形和照片非常流行,我相信您一定也已经发现自己正在共享甚至创建一些内容.在处理图形文件时,有时我们还必须通过更改其尺寸来调整它们的大小.这样,我们可以确保图像适合最终要显示它的视图 ...

  2. VC2010“添加资源-引入JPG图片”会改变图片大小

    结论: 通过VC2010添加JPG图片(添加资源->引入jpg图片)会改变图片大小. 本身只是为了想给对话框添加一个背景图片,试了下bmp的,都没有问题. 为了测试下是否可以加载(用LoadIm ...

  3. css让image不改变大小_如何改变图片大小

    我们进行网页设计的时候图片有时候可以会不符合大小,那我们怎么样才能在代码中实现图片大小的改变呢?本篇文章就来给大家介绍关于如何改变图片的大小? 我们改变图片的大小有两种方法:一种是在HTML标签内更改 ...

  4. html怎么改变图片整体大小,css怎么改变图片大小?

    css怎么改变图片大小?下面本篇文章就来给大家介绍一下使用css改变图片大小的方法,希望对大家有所帮助. 在HTML页面中,图片的显示方法有两种,分别为:img图片,background(背景)图片. ...

  5. JavaScript改变图片大小

    JavaScript改变图片大小 <!DOCTYPE html><html lang="en"><head><meta charset=& ...

  6. 通过拖拽改变图片大小

    <!doctype html> <html lang="en"><head><meta charset="utf-8" ...

  7. 微信小程序tabBar改变图片大小方法

    1.最少用,但是最简单,用原生tabBar用ps改变图片大小 2.用原生提供的custom-tab-bar(可能会存在一些小问题,比如switchTab的跳转之类的) 3.自定义组件,把所有页面变成子 ...

  8. C#使用OpenCv图像批处理——改变图片大小并且重命名

    添加NuGet包 选择最新版就好了 安装完成后右键项目重新生成方案 使用OpenCvSharp.CPlusPlus命名空间 using OpenCvSharp.CPlusPlus; 写一个函数用来改变 ...

  9. python改变图片大小不失针

    import os from math import floor import PIL.Image as ImageIMAGES_PATH = "C:\\Users\\Administrat ...

最新文章

  1. 如何看待团队合作这件事?
  2. python编程入门书-读书笔记之《编程小白的第1本Python入门书》
  3. linux忘记mysql的密码忘记了_linux忘记mysql密码找回方法
  4. MySQL查询中LIMIT的大offset导致性能低下浅析
  5. tensorflow神经网络结构可视化
  6. 质量故事(6)---质量就是客户的满意度
  7. swift-switch使用方法
  8. day07【后台】SpringSecurity
  9. 24.go clean
  10. DOM方式操作元素属性
  11. 程序设计导引及在线实践之大整数除法
  12. 北京思科CCNP和思科 CCIE考试常见问题GRE虚拟专用网络详解
  13. 计算机生物科学论文,生物信息学论文范文
  14. SylixOS操作系统自学经历(一)
  15. WebWork深入探索之初见端倪
  16. Android,IOS QQ在线客服代码
  17. python自定义html_Python 第十二篇:HTML基础
  18. 深入剖析Flannel-启动流程
  19. java系统高并发解决方案之图片服务器分离
  20. 个人笔记-本地WIN10搭建Minecraft服务器

热门文章

  1. 【转】javax.xml.transform.TransformerFactoryConfigurationError
  2. 迭代与递归实现无限级分类
  3. Java同步工具类——信号量
  4. Android EditText如何去除边框添加下划线
  5. Oracle 11g R2安装过程中遇到的报错及解决办法
  6. 查看Windows端口及端口关闭方法
  7. treeview托拽和动态添加节点以及treeview和xml的交互的实现
  8. PHP CURL 哈哈哈哈哈记录一下
  9. JS关于提交的RSA加密算法
  10. 光纤看起来长得都一样?其实它们之间大有不同—Vecloud微云