日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

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

我正在使用一個(gè)腳本,讓用戶上傳圖像。腳本調(diào)整大小并將圖像轉(zhuǎn)換為JPEG。

我遇到的問題是上傳透明度的PNG時(shí),生成的JPEG圖像是透明度為黑色的圖像。

如何編輯下面的腳本來替換黑色的白色?它已經(jīng)為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是完全白色的。

更新:

我自己修好了

謝謝大家,貢獻(xiàn)!

我換了

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;

總結(jié)

以上是生活随笔為你收集整理的php改变图片大小png背景变黑,php – 当将透明背景的PNG图像调整大小/转换为JPEG时,如何用白色替换黑色背景...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。