程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php imagecreatetruecolor 創建高清和透明圖片代碼小結

php imagecreatetruecolor 創建高清和透明圖片代碼小結

編輯:關於PHP編程

(PHP 4 >= 4.0.6, PHP 5)
imagecreatetruecolor — 新建一個真彩色圖像

說明
resource imagecreatetruecolor ( int $x_size , int $y_size )
imagecreatetruecolor() 返回一個圖像標識符,代表了一幅大小為 x_size 和 y_size 的黑色圖像。

是否定義了本函數取決於 PHP 和 GD 的版本。從 PHP 4.0.6 到 4.1.x 只要加載了 GD 模塊本函數一直存在,但是在沒有安裝 GD2 的時候調用,PHP 將發出致命錯誤並退出。在 PHP 4.2.x 中此行為改為發出警告而不是錯誤。其它版本只在安裝了正確的 GD 版本時定義了本函數。

新建一個新的 GD 圖像流並輸出圖像
復制代碼 代碼如下:
<?php
header("Content-type: image/png");
$im = @imagecreatetruecolor(50, 100)
or die("Cannot Initialize new GD image stream");
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>

Note: 本函數需要 GD 2.0.1 或更高版本(推薦 2.0.28 及更高版本)。

php imagecolorallocatealpha 創建透明圖片實例
imagecolorallocatealpha(resource $image , int $red , int $green , int $blue, int $alpha )
imagecolorallocatealpha()的行為相同imagecolorallocate()同阿爾法增加透明度參數。


$image
圖像資源,通過創造的圖像功能,如,一返回imagecreatetruecolor()。

$red
紅色分量的價值。

$green
價值的綠色成分。

$blue
藍色成分的價值。

$alpha
一個介於0和127的價值。 0表示完全不透明,而127表示完全透明。
來看個imagecolorallocatealpha實例教程
復制代碼 代碼如下:
<?php
$size = 300;
$image=imagecreatetruecolor($size, $size);

// something to get a white background with black border
$back = imagecolorallocate($image, 255, 255, 255);
$border = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $size - 1, $size - 1, $back);
imagerectangle($image, 0, 0, $size - 1, $size - 1, $border);

$yellow_x = 100;
$yellow_y = 75;
$red_x = 120;
$red_y = 165;
$blue_x = 187;
$blue_y = 125;
$radius = 150;

// allocate colors with alpha values
$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75);
$red = imagecolorallocatealpha($image, 255, 0, 0, 75);
$blue = imagecolorallocatealpha($image, 0, 0, 255, 75);

// drawing 3 overlapped circle
imagefilledellipse($image, $yellow_x, $yellow_y, $radius, $radius, $yellow);
imagefilledellipse($image, $red_x, $red_y, $radius, $radius, $red);
imagefilledellipse($image, $blue_x, $blue_y, $radius, $radius, $blue);

// don't forget to output a correct header!
header('Content-type: image/png');

// and finally, output the result
imagepng($image);
imagedestroy($image);
?>


php imagecreatetruecolor創建高清圖片函數
imagecreatetruecolor()返回一個圖像標識符代表指定大小的黑色形象。

根據你的PHP和GD版本中函數定義與否。對於PHP 4.0.6通過4.1.x這個函數總是存在的

,如果廣東模塊加載,但它要求GD2的情況下被安裝了PHP將發出一個致命錯誤並退出。

用PHP 4.2.x版這種行為是不同的人發出警告,而不是一個錯誤。其他版本只定義此功

能,

看看實例
復制代碼 代碼如下:
<?php
header ('Content-type: image/png');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
?>


我提出這方面合作 - 結合一些例子,然後動態生成的文本。但是,與此設置,我能得

到透明背景的工作也。
復制代碼 代碼如下:
<?php
// Set the content-type

header('Content-type: image/png');

// Create the image
$im = imagecreatetruecolor(175, 15);
imagesavealpha($im, true);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 25, $black);
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);

// The text to draw
$text = $_GET['text'];
// Replace path by your own font path
$font = 'catriel regular.ttf';

// Add some shadow to the text
imagettftext($im, 9, 0, 13, 16, $black, $font, $text);

// Add the text
imagettftext($im, 9, 0, 12, 15, $white, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

ph利用imagecreatetruecolor動態生成高清圖片代碼
復制代碼 代碼如下:
//實例用我們用imagecreatetruecolor
header ('Content-type: image/png');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);

//我把這個一起 - 結合較好的例子,然後動態生成的文本。但是,與此成立,我能得到透明背景以及工作。
//實例二imagecreatetruecolor
header('Content-type: image/png');

// Create the image
$im = imagecreatetruecolor(175, 15);
imagesavealpha($im, true);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 25, $black);
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);

// The text to draw
$text = $_GET['text'];
// Replace path by your own font path
$font = 'catriel regular.ttf';

// Add some shadow to the text
imagettftext($im, 9, 0, 13, 16, $black, $font, $text);

// Add the text
imagettftext($im, 9, 0, 12, 15, $white, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

/*
實例三創建透明圖片

如果你想創建一個PNG圖像*透明*,其中的背景是完全透明的,所有行動發生在借鑒,除此之外,然後執行下列操作:
*/
$png = imagecreatetruecolor(800, 600);
imagesavealpha($png, true);

$trans_colour = imagecolorallocatealpha($png, 0, 0, 0, 127);
imagefill($png, 0, 0, $trans_colour);

$red = imagecolorallocate($png, 255, 0, 0);
imagefilledellips教程e($png, 400, 300, 400, 300, $red);

header("Content-type: image/png");
imagepng($png);

你要做的就是創建一個真正的彩色圖像,確保阿爾法保存狀態是,然後填寫一個顏色,也經歷了阿爾法級別設置為完全透明(127)的圖像。

從上面的代碼產生的巴新將有一個完全透明的背景(一紅色圓圈拖到Photoshop中的圖像,以了解自己)
The resulting PNG from the code above will have a red circle on a fully transparent background (drag the image into Photoshop to see for yourself)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved