PHP實(shí)現(xiàn)對(duì)png圖像進(jìn)行縮放的方法(支持透明背景)
本文實(shí)例講述了PHP實(shí)現(xiàn)對(duì)png圖像進(jìn)行縮放的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false )
{
if ( $height <= 0 && $width <= 0 ) {
return false;
}
$info = getimagesize($file);
$image = '';
$final_width = 0;
$final_height = 0;
list($width_old, $height_old) = $info;
if ($proportional) {
if ($width == 0) $factor = $height/$height_old;
elseif ($height == 0) $factor = $width/$width_old;
else $factor = min ( $width / $width_old, $height / $height_old);
$final_width = round ($width_old * $factor);
$final_height = round ($height_old * $factor);
}
else {
$final_width = ( $width <= 0 ) ? $width_old : $width;
$final_height = ( $height <= 0 ) ? $height_old : $height;
}
switch ($info[2] ) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($file);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($file);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($file);
break;
default:
return false;
}
$image_resized = imagecreatetruecolor( $final_width, $final_height );
if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
$trnprt_indx = imagecolortransparent($image);
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($image, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($image_resized, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($image_resized, $trnprt_indx);
}
// Always make a transparent background color for PNGs that don't have one allocated already
elseif ($info[2] == IMAGETYPE_PNG) {
// Turn off transparency blending (temporarily)
imagealphablending($image_resized, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($image_resized, 0, 0, $color);
// Restore transparency blending
imagesavealpha($image_resized, true);
}
}
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
if ( $delete_original ) {
if ( $use_linux_commands )
exec('rm '.$file);
else
@unlink($file);
}
switch ( strtolower($output) ) {
case 'browser':
$mime = image_type_to_mime_type($info[2]);
header("Content-type: $mime");
$output = NULL;
break;
case 'file':
$output = $file;
break;
case 'return':
return $image_resized;
break;
default:
break;
}
switch ($info[2] ) {
case IMAGETYPE_GIF:
imagegif($image_resized, $output);
break;
case IMAGETYPE_JPEG:
imagejpeg($image_resized, $output);
break;
case IMAGETYPE_PNG:
imagepng($image_resized, $output);
break;
default:
return false;
}
return true;
}
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- PHP簡(jiǎn)單實(shí)現(xiàn)圖片格式轉(zhuǎn)換(jpg轉(zhuǎn)png,gif轉(zhuǎn)png等)
- PHP中使用Imagick讀取pdf并生成png縮略圖實(shí)例
- PHP使用imagick讀取PDF生成png縮略圖的兩種方法
- PHP輸出圖像imagegif、imagejpeg與imagepng函數(shù)用法分析
- php縮放gif和png圖透明背景變成黑色的解決方法
- PHP實(shí)現(xiàn)生成透明背景的PNG縮略圖函數(shù)分享
- PHP基于GD庫(kù)的縮略圖生成代碼(支持jpg,gif,png格式)
- php 處理png圖片白色背景色改為透明色的實(shí)例代碼
- 支持png透明圖片的php生成縮略圖類分享
- PHP添加PNG圖片背景透明水印操作類定義與用法示例
- php 實(shí)現(xiàn)svg轉(zhuǎn)化png格式的方法分析
相關(guān)文章
利用PHP+JS實(shí)現(xiàn)搜索自動(dòng)提示(實(shí)例)
本篇文章對(duì)利用PHP+JS實(shí)現(xiàn)搜索自動(dòng)提示的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
解決wincache不支持64位PHP5.5/5.6的問(wèn)題(提供64位wincache下載)
這篇文章主要解決wincache不支持64位PHP5.5/5.6的問(wèn)題,并提供64位wincache的下載,需要的朋友可以參考下。2016-06-06
php調(diào)用mysql存儲(chǔ)過(guò)程實(shí)例分析
這篇文章主要介紹了php調(diào)用mysql存儲(chǔ)過(guò)程,綜合各種常見(jiàn)實(shí)例分析了php調(diào)用mysql存儲(chǔ)過(guò)程的各種常見(jiàn)操作與使用技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
9個(gè)PHP開(kāi)發(fā)常用功能函數(shù)小結(jié)
9個(gè)PHP開(kāi)發(fā)常用功能函數(shù)小結(jié),學(xué)習(xí)php的朋友可以參考下。2011-07-07
PHP實(shí)現(xiàn)過(guò)濾各種HTML標(biāo)簽
在做項(xiàng)目的過(guò)程中,我們經(jīng)常需要用到過(guò)濾一些html標(biāo)簽來(lái)實(shí)現(xiàn)提高數(shù)據(jù)的安全性,其實(shí)就是刪除那些對(duì)應(yīng)用程序有潛在危害的數(shù)據(jù)。它用于去除標(biāo)簽以及刪除或編碼不需要的字符。2015-05-05
PhpStorm+xdebug+postman調(diào)試技巧分享
寫(xiě)PHP時(shí),一直用postman做測(cè)試,最近發(fā)現(xiàn)在測(cè)試過(guò)程中可以用xdebug來(lái)斷點(diǎn)調(diào)試,比原來(lái)手動(dòng)打exit或者die來(lái)斷點(diǎn)效率高多了2020-09-09
學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)單例模式(singleton)
這篇文章主要介紹了php設(shè)計(jì)模式中的單例模式,使用php實(shí)現(xiàn)單例模式,感興趣的小伙伴們可以參考一下2015-12-12
CURL的學(xué)習(xí)和應(yīng)用(附多線程實(shí)現(xiàn))
這篇文章主要介紹了CURL的安裝與多線程實(shí)現(xiàn)方法,需要的朋友可以參考下2013-06-06

