ホームページ  >  記事  >  バックエンド開発  >  PHP は PNG 画像を拡大縮小し、透明な背景をサポートします_PHP チュートリアル

PHP は PNG 画像を拡大縮小し、透明な背景をサポートします_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:22:17881ブラウズ

PHP对png图像行缩放、透明背景サポート

PHP对png图像行缩放、透明背景サポート

function Smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false )

{

if ( $height

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

$final_height = ( $height

}

スイッチ ($info[2] ) {

ケースIMAGETYPE_GIF:

$image = imagecreatefromgif($file);

休憩;

ケース IMAGETYPE_JPEG:

$image = imagecreatefromjpeg($file);

休憩;

ケースIMAGETYPE_PNG:

$image = imagecreatefrompng($file);

休憩;

デフォルト:

falseを返す;

}

$image_resize = imagecreatetruecolor( $final_width, $final_height );

if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {

$trnprt_indx = imagecolortransparent($image);

// 特定の透明色がある場合

if ($trnprt_indx >= 0) {

// 元画像の透明色のRGB値を取得します

$trnprt_color = imagecolorsforindex($image, $trnprt_indx);

// 新しい画像リソースに同じ色を割り当てます

$trnprt_indx = imagecolorallocate($image_resize, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);

// 新しい画像の背景を割り当てられた色で完全に塗りつぶします。

imagefill($image_resize, 0, 0, $trnprt_indx);

// 新しい画像の背景色を透明に設定します

imagecolortransparent($image_resize, $trnprt_indx);

}

// まだ割り当てられていない PNG には常に透明な背景色を作成します

elseif ($info[2] == IMAGETYPE_PNG) {

// 透明ブレンドを(一時的に)オフにする

imagealphablending($image_resize, false);

// 画像の新しい透明色を作成します

$color = imagecolorallocatealpha($image_resize, 0, 0, 0, 127);

// 新しい画像の背景を割り当てられた色で完全に塗りつぶします。

imagefill($image_resize, 0, 0, $color);

// 透明ブレンドを復元します

imagesavealpha($image_resize, true);

}

}

imagecopyresampled($image_resize, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);

if ( $delete_original ) {

if ( $use_linux_commands )

exec('rm '.$file);

その他

@unlink($file);

}

switch ( strto lower($output) ) {

ケース「ブラウザ」:

$mime = image_type_to_mime_type($info[2]);

header("Content-type: $mime");

$output = NULL;

休憩;

ケース「ファイル」:

$output = $file;

休憩;

ケース「返品」:

return $image_resize;

休憩;

デフォルト:

休憩;

}

スイッチ ($info[2] ) {

ケースIMAGETYPE_GIF:

imagegif($image_resize, $output);

休憩;

ケース IMAGETYPE_JPEG:

imagejpeg($image_resize, $output);

休憩;

ケースIMAGETYPE_PNG:

imagepng($image_resize, $output);

休憩;

デフォルト:

falseを返す;

}

trueを返します;

}

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/848344.html技術記事 PHP は PNG 画像を拡大縮小し、透明な背景をサポートします PHP は PNG 画像を拡大縮小し、透明な背景をサポートします 関数 Smart_resize_image( $file, $width = 0, $height = 0, $proportional =...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。