日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

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

發布時間:2025/5/22 php 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php改变图片大小png背景变黑,php – 当将透明背景的PNG图像调整大小/转换为JPEG时,如何用白色替换黑色背景... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在使用一個腳本,讓用戶上傳圖像。腳本調整大小并將圖像轉換為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时,如何用白色替换黑色背景...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。