PHP Conference Japan 2024

imagecreatetruecolor

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

imagecreatetruecolor建立新的真彩色影像

說明

imagecreatetruecolor(int $width, int $height): GdImage|false

imagecreatetruecolor() 會回傳一個代表指定大小黑色影像的影像物件。

參數

width

影像寬度。

height

影像高度。

回傳值

成功時回傳影像物件,錯誤時回傳 false

變更記錄

版本 說明
8.0.0 成功時,此函式現在會回傳 GDImage 實例;先前會回傳 resource

範例

範例 #1 建立新的 GD 影像串流並輸出影像。

<?php
header
('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
or die(
'無法初始化新的 GD 影像串流');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, '一個簡單的文字字串', $text_color);
imagepng($im);
imagedestroy($im);
?>

上述範例會輸出類似以下的內容

Output of example : Creating a new GD image stream and outputting an image.

參見

新增註解

使用者貢獻的註解 36 個註解

32
Richard Davey rich at corephp dot co dot uk
17 年前
如果您想要建立 *透明* 的 PNG 影像,其中背景完全透明,且所有繪圖操作都發生在其之上,請執行以下操作

<?php
$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);
imagefilledellipse($png, 400, 300, 400, 300, $red);

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

您所做的是建立一個真彩色影像,確保已開啟 alpha 儲存狀態,然後將影像填滿 alpha 層級設為完全透明 (127) 的色彩。

上述程式碼產生的 PNG 將在完全透明的背景上顯示一個紅色圓圈(將影像拖曳到 Photoshop 中即可自行查看)
20
arthur at kuhrmeier dot com
14 年前
如何在建立影像之前檢查記憶體?

我曾處理過一個需要處理大型影像的腳本。經常發生「記憶體不足」的錯誤。所以我必須找出如何在建立影像「之前」檢查記憶體。我在網路上找不到解決方案,所以我執行了自己的測試腳本。它建立了許多影像來計算所需的記憶體。我發現公式 mem = x * y * 通道是不夠的。還有一個額外的乘數會變動。結果 1.7 效果最好。所以公式是:mem = x * y * 通道 * 1.7。

我在此處張貼此腳本,希望它對將遇到相同問題的人有所幫助。

<?php
//-------------------------------------------------- 定義 MAXMEM
define ("MAXMEM", 32*1024*1024); //--- 記憶體限制 (32M) ---

//-------------------------------------------------- 記憶體足夠嗎?
function enoughmem ($x, $y, $rgb=3) {
return (
$x * $y * $rgb * 1.7 < MAXMEM - memory_get_usage() );
}

//-------------------------------------------------- 簡單範例
list ($x, $y) = @getimagesize ('your_img.jpg'); //--- 取得圖片尺寸 ---
if (enoughmem($x,$y)) {
$img = @imagecreatefromjpeg ('your_img.jpg'); //--- 開啟圖片檔案 ---
$thumb = 200; //--- 縮圖最大尺寸 ---
if ($x > $y) {
$tx = $thumb; //--- 橫向圖片 ---
$ty = round($thumb / $x * $y);
} else {
$tx = round($thumb / $y * $x); //--- 直向圖片 ---
$ty = $thumb;
}
if (
enoughmem($tx,$ty)) {
$thb = imagecreatetruecolor ($tx, $ty); //--- 建立縮圖 ---
imagecopyresampled ($thb,$img, 0,0, 0,0, $tx,$ty, $x,$y);
imagejpeg ($thb, 'your_thumbnail.jpg', 80);
imagedestroy ($thb);
}
imagedestroy ($img);
}

//--------------------------------------------------
//--- 檢查記憶體是否足夠處理 ---
//--- 黑白圖片或 GIF 請使用: ---
//--------------------------------------------------
$check = enoughmem ($x, $y, 1);
?>

藉此機會,感謝 php.net 的各位提供這麼棒的手冊和實用的使用者腳本。
6
kai at meder dot info
20 年前
使用 imagecolorallocate 指定圖片的背景顏色在真彩色圖片上無效。

相反地,您必須使用 imagefill 強制使用先前配置的背景顏色填滿圖片。

$bgColor = imagecolorallocate($img, 255,255,255);
imagefill($img , 0,0 , $bgColor);

kai
7
Anonymous
19 年前
// 建立透明圖片,而非預設的黑色圖片
function imageCreateTransparent($x, $y) {
$imageOut = imagecreate($x, $y);
$colourBlack = imagecolorallocate($imageOut, 0, 0, 0);
imagecolortransparent($imageOut, $colourBlack);
return $imageOut;
}
3
aaron at aaron-wright dot com
22 年前
實際上 GD <2.0 有 imagecreatetruecolor,只是它無法運作 :P

這是一個更好的測試

function chkgd2(){
$testGD = get_extension_funcs("gd"); // 取得函式列表
if (!$testGD){ echo "GD 甚至沒有安裝。"; exit; }
if (in_array ("imagegd2",$testGD)) $gd_version = "<2"; // 檢查
if ($gd_version == "<2") return false; else return true;
}

if (chkgd2()) echo "<h1>已安裝 GD2+。</h1>"; // 測試 GD2
else echo "<h1>未安裝 GD2+。</h1>";
4
jessiedeer at hotmail dot com
11 年前
使用 imagecreatetruecolor 不需要配置顏色。所有 [256 x 256 x 256 x 128] 真彩色都已配置,您可以直接使用顏色索引。

範例
藍色 => 顏色索引 255。
白色 => 顏色索引 16777215 (= 255*256² + 255*256+255)。
完全透明 => 顏色索引 2130706432 (= 127*256^3)。
4
d dot duquenoy at kdland dot org
18 年前
我的函式用於在使用 imagecreate 或 imagecreatetruecolor 之前,先了解它們需要多少位元組的記憶體。
<?php
function getNeededMemoryForImageCreate($width, $height, $truecolor) {
return
$width*$height*(2.2+($truecolor*3));
}
?>
3
weareexit at yahoo dot co dot uk
16 年前
如果您想將圖片放置在先前使用 imagecreatetruecolor() 建立的較大畫布上,但不希望預設的黑色背景環繞它:請在 imagecopyresampled() 之後使用 imagefill()。

我不知道為什麼會這樣,但它確實有效!
2
send at mail dot 2aj dot net
19 年前
如果您想要一個簡單的圖片鏡射方式,請使用我包含在下方的函式。

<?php
function image_mirror ($input_image_resource)
{
$width = imagesx ( $input_image_resource );
$height = imagesy ( $input_image_resource );
$output_image_resource = imagecreatetruecolor ( $width, $height );
$y = 1;

while (
$y < $height )
{
for (
$i = 1; $i <= $width; $i++ )
imagesetpixel ( $output_image_resource, $i, $y, imagecolorat ( $input_image_resource, ( $i ), ( $height - $y ) ) );
$y = $y + 1;
}

return
$output_image_resource;
}
?>
範例用法
<?php

// 這對於使用者上傳 TGA 檔案時很有用,
// 在完成畫面顯示圖片,並提供一個連結
// 詢問圖片是否看起來是鏡射的,如果是,
// 該連結將會執行下面的這些指令。

// 我建議檢查 $HTTP_REFERER 以確保安全性。

// 在此範例中,$_GET['file'] 會類似於
// johndoe-img0001(需要 Basename 以防止濫用此腳本!)

if ( isset( $_GET['file'] ) )
{
$filename = "./" . basename ( $_GET['file'] ) . ".jpg";
// 建立需要鏡射的圖片資源
// 從先前匯出為 JPEG 檔案的圖片取得。
$output_resource_image = image_mirror ( imagecreatefromjpeg ( $filename ) );
if (
imagejpeg ( $output_resource_image, $filename, 75 ) )
echo
"圖片重新儲存成功";
else
echo
"圖片無法覆寫 (請檢查權限)!";
}
?>
3
Jami
21 年前
使用真彩色建立縮圖時,最好使用 imagecopyresampled 函式,它可能比 imagecopyresized 看起來更好。
1
fixxxer at php5 dot ru
20 年前
關於 Justin Greer 在 2003 年 11 月 18 日 10:40 發布的訊息的注意事項

雖然這個資訊已經由 php at REMOVEreallynicejerk dot com 在 2002 年 9 月 16 日 12:01 發布,但我認為有必要注意,因為 Justin 的訊息在頂部,可能會誤導人們:Justin 偵測要使用哪個 imagecreate 函式的方法太複雜,其實有一個簡單的標準方法

<?php
if (function_exists('imagecreatetruecolor') {
// 使用 imagecreatetruecolor
} else {
// 使用 imagecreate
}
1
marc at thewebguys dot com dot au
21 年前
我發現在安裝 GD 2+ 時,ImageCreate() 在處理 JPEG 圖片時效果不佳,當與 ImageCreateFromJPEG() 結合使用時,會將真彩色 JPEG 圖片變成 16 色的混亂影像。如果您有 GD 2+,您必須使用 ImageCreateTrueColor() 來處理縮圖等。
2
Hamza Ahmad
3 年前
從 PHP 8 開始,圖片建立函式的回傳類型是 GdImage 而不是 resource。因此,人們可以使用類型提示。
<?php
class MyImage {
private
GdImage $img;
};
?>
同樣地,<?php
function save_image(GdImage $img, string $name, string $directory = null) : bool
{
/*...*/
};
?>
1
eric at spiderws dot com
23 年前
這個小函式可以幫您做到

<?
$image_id = imageCreateFromJPEG($image);
for($a=0;$a<imagecolorstotal ($image_id);$a++)
{
$color = ImageColorsForIndex($image_id,$i);
$R=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']);
$G=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']);
$B=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']);
ImageColorSet($image_id, $a, $R, $G, $B);
}
imageJPEG($image_id,"$image");
?>

數值 .299、.578、.114 是估計值,並為色彩添加了伽瑪校正。若要讓結果有更多或更少的亮度,您可以變更這些數值。

祝您好運,
Eric Mulders,荷蘭
0
Florin [ florin.at.flachi.dot.net ]
18 年前
一個從十六進位 (HTML) 標記建立 RGB 色彩的非常簡單有效率的方法

<?php

function color_hex2dec ($color) {
return array (
hexdec (substr ($color, 0, 2)), hexdec (substr ($color, 2, 2)), hexdec (substr ($color, 4, 2)));
}

list (
$r, $g, $b) = color_hex2dec ('FFEECC');

?>
0
j.fenin _At_ seekport [d0t] biz
18 年前
我在 Debian Linux 上使用 PHP5 時發生了一個奇怪的錯誤。

在檔案 B(我在其中建立影像)中,我包含了一個檔案(檔案 A)。產生的影像始終是損毀的。當我不包含檔案 A 時,一切都很好。

經過 2 小時的搜尋後,我發現檔案 A 中的 PHP 關閉標籤 ?> 後面有一些額外的空格和換行符號。移除這些字元後,一切又恢復正常。

顯然檔案 A 的這些額外位元被加到影像中,導致其損毀。
0
Justin Greer
20 年前
我知道這不是討論區,但是當發布不正確的資訊時,應該加以修正。

function_exists() 檢查無法正確運作,因為如果 PHP 是使用較舊的 GD 程式庫編譯的,imagecreatetruecolor() 函式仍然存在 -- 只是在呼叫時會產生嚴重錯誤,指出需要 GD2。因此,在任何只有 GD 1.x 的較新 PHP 版本上,function_exists() 方法都會失敗。(這包括我看到的大部分 4.1.x 和 4.2.x 安裝。)
0
Justin Greer
21 年前
很不幸地,@imagecreatetruecolor() 方法甚至無法運作,因為 php 會因指出需要 GD 2 而導致嚴重錯誤。您甚至無法使用自訂錯誤處理常式來擷取此錯誤。

我設計了一個函式來取得 GD 版本號碼,它在每個我測試過的 PHP 和 GD 版本(甚至 CLI 版本)上似乎都能運作良好。它顯然不是世界上最有效率的東西,但它會將結果快取到靜態變數中,因此多次呼叫它不會進一步降低速度。

function gd_version() {
static $gd_version_number = null;
if ($gd_version_number === null) {
// 使用輸出緩衝來從 phpinfo() 取得結果
// 而不會干擾我們所在的頁面。輸出
// 緩衝是「可堆疊的」,因此我們甚至不必
// 擔心先前或包含的緩衝。
ob_start();
phpinfo(8);
$module_info = ob_get_contents();
ob_end_clean();
if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i",
$module_info,$matches)) {
$gd_version_number = $matches[1];
} else {
$gd_version_number = 0;
}
}
return $gd_version_number;
}

然後您可以簡單地像這樣使用它

if (gd_version() >= 2) {
$image = ImageCreateTrueColor($width, $height);
} else {
$image = ImageCreate($width, $height);
}
0
Hallvord RM Steen <phpnet at hallvord dot com>
21 年前
關於自動選擇 TrueColor 或普通版本的 imagecreate,我發現以下方法對我有效

<?php

$newImg
=@ImageCreateTrueColor($width, $height)
or
$newImg=ImageCreate($width, $height);

?>
0
bjorntje at hotmail dot com
21 年前
我剛剛在 Phpix 上測試,它在使用 GD 程式庫時遇到一些問題
(色彩褪色)。
我變更了程式碼,這解決了問題。
以防你們有些人仍然遇到問題且
您無法變更主機端的 PHP 安裝
在主機端

$im = ImageCreateFromJPEG($source);
$new_im = ImageCreate($new_width,$new_height);

ImageCopyResized($new_im,$im,0,0,0,0,
$new_width,$new_height,ImageSX($im),ImageSY($im));

變更為

$im = ImageCreateFromJPEG($source);
$new_im = ImageCreateTrueColor($new_width,$new_height);

ImageCopyResized($new_im,$im,0,0,0,0,$new_width,
$new_height,ImageSX($im),ImageSY($im));
0
Andreas from www.ems-p.de
21 年前
brad at brwebdesign dot com 的 gdlib 要求無法在 PHP < 4.1 (version_compare) 下運作。

某些 phpinfo 版本提供的版本號碼沒有空格,因此您最好尋找點號

ob_start();
phpinfo(8);
$phpinfo=ob_get_contents();
ob_end_clean();
$phpinfo=strip_tags($phpinfo);
$phpinfo=stristr($phpinfo,"gd version");
$phpinfo=stristr($phpinfo,"version");
$end=strpos($phpinfo,".");
$phpinfo=substr($phpinfo,0,$end);
$length = strlen($phpinfo)-1;
$phpinfo=substr($phpinfo,$length);
if($phpinfo<2){
$dst_img=ImageCreate($new_w,$new_h);}
else {
$dst_img=ImageCreateTrueColor($new_w,$new_h);
}
0
billet_jerome at yahoo dot fr
21 年前
因為 imagecreatetruecolor 在 gd 和 gd2 中都存在,
您無法使用 function_exists 或 get_extension_funcs 來檢查 gd2!
但是 ... 在 gd 和 gd2 之間,imagettfbbox 有差異
gd 使用像素,而 gd2 使用點。

然後您可以使用此函式檢查 gd2

function sysB_chkgd2()
{

$rep=false;
if(isset($GLOBALS["gBGDVersion"]))
{
$rep=$GLOBALS["gBGDVersion"];
}
else
{
if(function_exists("gd_info"))
{
$gdver=gd_info();
if(strstr($gdver["GD Version"],"1.")!=false)
{
$rep=false;
}
else
{
$rep=true;
}
}
else
{
$size=40;
$font= 您在這裡的字型檔案路徑;
$b=imagettfbbox ($size,0,$font,"abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ");

if(($size+1)<abs($b[5]-$b[3]))
{
$rep=true;
}

}
$GLOBALS["gBGDVersion"]=$rep;
}

return $rep;
}
0
php at REMOVEreallynicejerk dot com
22 年前
為什麼不直接使用 function_exists?

https://php.dev.org.tw/function_exists

僅僅因為他們有 2.0 版的 GD,並不表示他們沒有停用該函式。

此外,您可以編寫可在任何設定上使用的通用程式碼,而不是判斷您機器上的版本,然後編寫特定設定的程式碼。

::虛擬碼:

if (function_exists(imagecreatetruecolor)){
使用 imagecreatetruecolor()
}else{
使用 imagecreate()
}
0
pmas7354 at artax dot karlin dot mff dot cuni dot cz
22 年前
這是第一個問題「為什麼我需要真彩色影像」的答案。

當您想處理像是照片(快照)、影片幀等影像時,您需要真彩色影像。

當您需要為使用者組合這些影像,或只向他傳送部分影像時,如果您嘗試使用 256 種色彩,影像會很難看(如果您想試試看)。有些色彩會發生劇烈變化。

您可能看到的「反鋸齒」線條可能是使用 JPEG 壓縮的結果。JPEG 並非無損壓縮,因此「可能會變更細微的影像細節」,以大幅縮減影像大小。在真正的真彩色影像(例如您拍攝的風景快照等等)上,並使用較小的壓縮等級,您很難看到差異。但是在精確的物件(例如您使用單一色彩在實色背景上繪製的線條、圓圈)上,如果您儲存並載入此影像,您會看到某些細節發生變更。
-1
send at mail dot 2aj dot net
19 年前
感謝 OverFlow636 at gmail dot com 和 kuya1284 at techie dot com 的原始程式碼,此函式是基於他們的程式碼。

PHP 缺少 imagecreatefromtga 函式,因此我準備了一個簡單的函式,如果您處理 TGA 影像,可以將其包含在您的程式庫中。除了從 TGA 建立影像外,它也會選擇性地輸出包含影像尺寸的陣列,因為 getimagesize() 無法處理 TGA 檔案。

如果 GD 能夠更新以支援 TGA 格式,希望這個函式最終將不再需要。

請記住,TGA 有許多不同的壓縮、色彩設定,因此此函式並非 100% 皆能正常運作。產生的影像將符合 kuya1284 at techie dot com 提到的症狀,因此您可能需要建立第二個函式。請記住,使用她的程式碼會在使用以下函式正確載入的影像上產生「鏡像」效果。

<?php
function imagecreatefromtga ( $filename, $return_array = 0 )
{
$handle = fopen ( $filename, 'rb' );
$data = fread ( $handle, filesize( $filename ) );
fclose ( $handle );

$pointer = 18;
$x = 0;
$y = 0;
$w = base_convert ( bin2hex ( strrev ( substr ( $data, 12, 2 ) ) ), 16, 10 );
$h = base_convert ( bin2hex ( strrev ( substr ( $data, 14, 2 ) ) ), 16, 10 );
$img = imagecreatetruecolor( $w, $h );

while (
$pointer < strlen ( $data ) )
{
imagesetpixel ( $img, $x, $y, base_convert ( bin2hex ( strrev ( substr ( $data, $pointer, 3 ) ) ), 16, 10 ) );
$x++;

if (
$x == $w)
{
$y++;
$x=0;
}

$pointer += 3;
}

if (
$return_array )
return array (
$img, $w, $h );
else
return
$img;
}
?>

使用範例

<?php
// 取得影像及其尺寸
$array = imagecreatefromtga("source_image.tga",1);

// 影像 ( 資源影像 )
$image = $array[0];

// 影像尺寸 ( 整數 )
$dimensions['width'] = $array[1];
$dimensions['height'] = $array[2];

// 刪除影像資源陣列項目以釋放記憶體
imagedestroy($array[0]);
?>

第二個變數 $return_array 是可選的。如果您只想載入 TGA,請將此變數從函式呼叫中省略,如下所示
<?
// 單獨回傳資源影像
$resource_image = imagecreatefromtga ( "source_image.tga" );

// 將 Content-type 宣告為 JPEG 影像。
header ( 'Content-type: image/jpeg' );

// 將影像轉換為 JPEG 以縮小檔案大小並提高相容性
imagejpeg ( $resource_image, NULL, 100 );
imagedestroy ( $resource_image );
?>
-1
behun at webconsult dot sk
18 年前
imagecreatetruecolor() 不僅回傳代表大小為 x_size 乘 y_size 的黑色影像的影像識別碼。

呼叫 imagecreatetruecolor() 來建立真彩色影像,其顏色數量基本上不受限制 (而不僅限於 256 色)。
-1
termian
20 年前
kai 寫道
// 使用 imagecolorallocate 來指定影像的背景
// 顏色不適用於真彩色影像。
//
// 相反地,您必須使用 imagefill 來強制使用
// 先前配置的背景顏色填滿影像
//
//$bgColor = imagecolorallocate($img, 255,255,255);
//imagefill($img , 0,0 , $bgColor);

即使這樣對我的設定也無效 - fedora core2、php 4.3.8 + 捆綁的 gd (與 2.0.23 相容),我必須這樣做
$img = imagecreatetruecolor($x, $y);
$bgColor = imagecolorallocate($img, 255,255,255);
imagefilledrectangle($img, 0, 0, $x-1, $y-1, $bgColor);
-2
hemjesti at yahoo dot com
15 年前
我將這組合起來 - 結合了兩個範例,然後動態產生文字。但是,透過此設定,我也能夠讓透明背景正常運作。

<?php
// 設定內容類型

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

// 建立影像
$im = imagecreatetruecolor(175, 15);
imagesavealpha($im, true);

// 建立一些顏色
$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);

// 要繪製的文字
$text = $_GET['text'];
// 將路徑替換為您自己的字型路徑
$font = 'catriel regular.ttf';

// 為文字新增一些陰影
imagettftext($im, 9, 0, 13, 16, $black, $font, $text);

// 新增文字
imagettftext($im, 9, 0, 12, 15, $white, $font, $text);

// 使用 imagepng() 會產生比 imagejpeg() 更清晰的文字
imagepng($im);
imagedestroy($im);
?>
-1
phpnet at sjeiti dot com
17 年前
關於透明度,只需對 '
thomas dot urban at toxa dot de' (2006 年 5 月 23 日 03:28) 的訊息做小修正。
範例中的第 3 行會回傳「錯誤的參數警告」:「imagecolorallocate」應該替換為「imagecolorallocatealpha」。
(您最好只是變更該註解並刪除這一個。)
-1
OverFlow636 at gmail dot com
19 年前
我不知道是否有更簡單的方法可以做到這一點,但這是我的程式碼,可將 .tga 影像轉換為您要輸出的任何格式
它僅適用於基本的 tga 檔案

<?
$handle = fopen("xxx.tga","rb");
$data = fread($handle,filesize("xxx.tga"));
fclose($handle);
$pointer = 18;
$x = 0;
$y = 0;
$w = fileint(substr($data,12,2));
$h = fileint(substr($data,14,2));
$img = imagecreatetruecolor($w,$h);

while ($pointer < strlen($data))
{
imagesetpixel($img, $x,$y,fileint(substr($data,$pointer,3)));
$x++;

if ($x==$w)
{
$y++;
$x=0;
}

$pointer += 3;
}

header("Content-type: image/jpeg");
imagepng($img);
imagedestroy($img);

function fileint($str)
{
return base_convert(bin2hex(strrev($str)),16,10);
}
?>
-1
brad at brwebdesign dot com
22 年前
我今天想出了這個。您需要 GD2.0 或更高版本才能使用 imagecreatetruecolor,因此您可以剖析 phpinfo 以取得所需的資訊。這必須放在頁面最頂端,且其上方沒有空白字元

<?php
ob_start
();
phpinfo(8);
$phpinfo=ob_get_contents();
ob_end_clean();
$phpinfo=strip_tags($phpinfo);
$phpinfo=stristr($phpinfo,"gd version");
$phpinfo=stristr($phpinfo,"version");
$end=strpos($phpinfo," ");
$phpinfo=substr($phpinfo,0,$end);
$phpinfo=substr($phpinfo,7);
if(
version_compare("2.0", "$phpinfo")==1)
echo
"you have a version less then 2";
?>

這個 if 語句適用於 PHP 4.1 或更新的版本,但如果您的伺服器沒有該版本的 PHP,您可以使用其他方法來比較版本號。
-1
rossa at studioware dot net
22 年前
function ConvertGreyscale($image){
# 這個檔案會輸出指定圖片的灰階版本

$total = ImageColorsTotal($image);
for( $i=0; $i<$total; $i++){
$old = ImageColorsForIndex($image, $i);

# 嘗試在轉換時保持適當的飽和度
$commongrey = (int)(($old[red] + $old[green] + $old[blue]) / 3);

ImageColorSet($image, $i, $commongrey, $commongrey, $commongrey);
}
}
-1
smcjones at gmail dot com
10 年前
請注意,如果您在 PHP 中收到關於尺寸不正確的警告,可能是因為您的值被儲存為字串。您可以使用 intval() 將字串值更改為整數值,以便將正確的資訊傳遞給此函式。
-1
Micke (micke dot prag at newstonight dot net)
21 年前
這是我的解決方案,用於為 GD < 2 和 GD > 2 建立具有相同程式碼的圖片

$dst_img = @imageCreateTrueColor($width, $height);
if (!$dst_img) { $dst_img = imageCreate($width, $height); }
-2
kuya1284 at techie dot com
19 年前
OverFlow636,

看起來您的程式碼導致 TGA 圖片水平翻轉且上下顛倒。我已相應修改您的程式碼。下面的範例是我將 tga 轉換為 jpg 的方式。for 迴圈取自下方 (Eric Mulders) 的評論,以正確呈現影像。

哥們,程式碼寫得不錯。

function tga2jpg ($image)
{
$handle = fopen($image, "rb");
$data = fread($handle, filesize($image));
fclose($handle);

$pointer = 18;
$w = fileint (substr ($data, 12, 2));
$h = fileint (substr ($data, 14, 2));
$x = 0;
$y = $h;

$img = imagecreatetruecolor($w, $h);

while ($pointer < strlen($data))
{
imagesetpixel ($img, $x, $y, fileint (substr ($data, $pointer, 3)));

$x++;

if ($x == $w)
{
$y--;
$x = 0;
}

$pointer += 3;
}

for($a = 0; $a < imagecolorstotal ($img); $a++)
{
$color = imagecolorsforindex ($img, $a);

$R=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']);
$G=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']);
$B=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 * ($color['blue']);

imagecolorset ($img, $a, $R, $G, $B);
}

imagejpeg ($img, 'test.jpg', 100);
imagedestroy ($img);
}

function fileint($str)
{
return base_convert (bin2hex (strrev ($str)), 16, 10);
}

tga2jpg ('test.tga');
-3
unigram at byair dot net
20 年前
如果您的主機系統由於 PHP<4.2 和 GD<2.0 而沒有 imagecreatetruecolor(),則可以這樣處理:

<?
$thumb = imagecreate ($width, $height);
imageJPEG($thumb,"images/temp.jpg");
$thumb = @imagecreatefromjpeg("images/temp.jpg");
?>

這會建立正確尺寸的縮圖,以 jpeg 格式儲存,然後以真彩色讀取。

這修正了使用調色盤影像作為 imagecopyresized () 目的地的降級問題
To Top