PHP Conference Japan 2024

GD 與圖像函數

目錄

新增註釋

使用者貢獻的註釋 8 則註釋

chuckstudios at gmail dot com
16 年前
我寫了一個簡單的函式,將影像資源轉換為 PGM(可攜式灰階圖),以便將其饋送到 OCR 程式。它的運作方式與其他影像輸出函式相同,並且會為您轉換為灰階。

<?php
function imagepgm($image, $filename = null)
{
$pgm = "P5 ".imagesx($image)." ".imagesy($image)." 255\n";
for(
$y = 0; $y < imagesy($image); $y++)
{
for(
$x = 0; $x < imagesx($image); $x++)
{
$colors = imagecolorsforindex($image, imagecolorat($image, $x, $y));
$pgm .= chr(0.3 * $colors["red"] + 0.59 * $colors["green"] + 0.11 * $colors["blue"]);
}
}
if(
$filename != null)
{
$fp = fopen($filename, "w");
fwrite($fp, $pgm);
fclose($fp);
}
else
{
return
$pgm;
}
}
?>
michal-ok at o2 dot pl
19 年前
下方提供的影像銳化函式(作者:Alex R. Austin)似乎非常耗費資源,我在兩台不同的伺服器上都無法讓它正常運作 - 嘗試銳化一張 413 x 413 的圖片時,最後出現了「致命錯誤:允許的記憶體大小 8388608 位元組已耗盡」或「內部伺服器錯誤」,或者腳本無預警終止。由於我沒有在這些伺服器上更改預設記憶體限制的權限,所以我開始尋找其他銳化函式。我找到了可在處理過的兩台伺服器上完美運作的 PHP Unsharp Mask 函式。它可以在 http://vikjavev.no/hovudsida/umtestside.php 找到。
felipensp at gmail dot com
18 年前
用於 GD 函式庫函式的十六進位色彩的十進位表示法。

<?php

// 十六進位表示法
$var = '#FFFFFF';

function
getRgbFromGd($color_hex) {

return
array_map('hexdec', explode('|', wordwrap(substr($color_hex, 1), 2, '|', 1)));

}

print_r(getRgbFromGd($var));

// 輸出:Array ( [0] => 255 [1] => 255 [2] => 255 )

?>
shd at earthling dot net
18 年前
如果您需要輸出 Windows BMP 檔案的方法(例如,使用 PEAR ExcelWriter 時),可以隨意使用以下程式碼

<?php
function imagebmp ($im, $fn = false)
{
if (!
$im) return false;

if (
$fn === false) $fn = 'php://output';
$f = fopen ($fn, "w");
if (!
$f) return false;

//Image dimensions
$biWidth = imagesx ($im);
$biHeight = imagesy ($im);
$biBPLine = $biWidth * 3;
$biStride = ($biBPLine + 3) & ~3;
$biSizeImage = $biStride * $biHeight;
$bfOffBits = 54;
$bfSize = $bfOffBits + $biSizeImage;

//BITMAPFILEHEADER
fwrite ($f, 'BM', 2);
fwrite ($f, pack ('VvvV', $bfSize, 0, 0, $bfOffBits));

//BITMAPINFO (BITMAPINFOHEADER)
fwrite ($f, pack ('VVVvvVVVVVV', 40, $biWidth, $biHeight, 1, 24, 0, $biSizeImage, 0, 0, 0, 0));

$numpad = $biStride - $biBPLine;
for (
$y = $biHeight - 1; $y >= 0; --$y)
{
for (
$x = 0; $x < $biWidth; ++$x)
{
$col = imagecolorat ($im, $x, $y);
fwrite ($f, pack ('V', $col), 3);
}
for (
$i = 0; $i < $numpad; ++$i)
fwrite ($f, pack ('C', 0));
}
fclose ($f);
return
true;
}
?>

它的運作方式與一般的 imagejpeg/imagepng 相同,並且只支援 GD2.0 真彩色點陣圖(這是 ExcelWriter 所要求的)。
ingo at jache dot de
13 年前
我知道這對其他人來說可能看起來有點多餘,但我曾經遇到一種情況,我需要對圖像進行*強*模糊處理,但沒有安裝 ImageMagick。在同一張圖像上多次執行捲積濾波器速度非常慢,而且仍然無法獲得良好的模糊效果。

以下函式接受一個真彩色圖像和一個介於 0.0 到 1.0 之間的模糊因子。注意:它仍然相當慢。

<?php

function blurImage($srcimg,$blur)
{
$blur = $blur*$blur;
$blur = max(0,min(1,$blur));

$srcw = imagesx($srcimg);
$srch = imagesy($srcimg);

$dstimg = imagecreatetruecolor($srcw,$srch);

$f1a = $blur;
$f1b = 1.0 - $blur;


$cr = 0; $cg = 0; $cb = 0;
$nr = 0; $ng = 0; $nb = 0;

$rgb = imagecolorat($srcimg,0,0);
$or = ($rgb >> 16) & 0xFF;
$og = ($rgb >> 8) & 0xFF;
$ob = ($rgb) & 0xFF;

//-------------------------------------------------
// first line is a special case
//-------------------------------------------------
$x = $srcw;
$y = $srch-1;
while (
$x--)
{
//horizontal blurren
$rgb = imagecolorat($srcimg,$x,$y);
$cr = ($rgb >> 16) & 0xFF;
$cg = ($rgb >> 8) & 0xFF;
$cb = ($rgb) & 0xFF;

$nr = ($cr * $f1a) + ($or * $f1b);
$ng = ($cg * $f1a) + ($og * $f1b);
$nb = ($cb * $f1a) + ($ob * $f1b);

$or = $nr;
$og = $ng;
$ob = $nb;

imagesetpixel($dstimg,$x,$y,($nr << 16) | ($ng << 8) | ($nb));
}
//-------------------------------------------------

//-------------------------------------------------
// now process the entire picture
//-------------------------------------------------
$y = $srch-1;
while (
$y--)
{

$rgb = imagecolorat($srcimg,0,$y);
$or = ($rgb >> 16) & 0xFF;
$og = ($rgb >> 8) & 0xFF;
$ob = ($rgb) & 0xFF;

$x = $srcw;
while (
$x--)
{
//horizontal
$rgb = imagecolorat($srcimg,$x,$y);
$cr = ($rgb >> 16) & 0xFF;
$cg = ($rgb >> 8) & 0xFF;
$cb = ($rgb) & 0xFF;

$nr = ($cr * $f1a) + ($or * $f1b);
$ng = ($cg * $f1a) + ($og * $f1b);
$nb = ($cb * $f1a) + ($ob * $f1b);

$or = $nr;
$og = $ng;
$ob = $nb;


//vertical
$rgb = imagecolorat($dstimg,$x,$y+1);
$vr = ($rgb >> 16) & 0xFF;
$vg = ($rgb >> 8) & 0xFF;
$vb = ($rgb) & 0xFF;

$nr = ($nr * $f1a) + ($vr * $f1b);
$ng = ($ng * $f1a) + ($vg * $f1b);
$nb = ($nb * $f1a) + ($vb * $f1b);

$vr = $nr;
$vg = $ng;
$vb = $nb;

imagesetpixel($dstimg,$x,$y,($nr << 16) | ($ng << 8) | ($nb));
}

}
//-------------------------------------------------
return $dstimg;

}


$srcimg = imagecreatefromjpeg("test.jpg");
$dstimg = blurImage($srcimg,0.2);

header('Content-type: image/jpeg');
echo(
imagejpeg($dstimg) );
exit();


?>
jeff at lushmedia dot com
21 年前
我寫了一個線上圖像函式概覽,人們可能會覺得有用。除了各種函式類別和程式碼範例的一般概覽之外,我還包含了許多函式的互動式範例,允許瀏覽者試驗參數,並即時查看結果。簡報位於紐約 PHP
http://www.nyphp.org/content/presentations/GDintro/
delabahan at gmail dot com
8 年前
這是獲取高解析度圖像的範例。

<?php
/**
* Class name : resizeImage
* Created by : wang
* Description : This class is to resize the image from original size to new size
*/
class resizeImage
{
/**
* Function name : resize_img
* Description : This function is to resize image
* @param : $origimg variable is the original image
* @param : $newimg variable is the new image
* @param : $w variable is the width of image
* @param : $f variable is the height of image
*/
public function resize_img($origimg,$newimg,$w,$h){
$info = getimagesize($origimg);
$mime = $info['mime'];

// Make sure that the requested file is actually an image
if(substr($mime, 0, 6) != 'image/')
{
header('HTTP/1.1 400 Bad Request');
return
'Error: requested file is not an accepted type: ' .$origimg;
exit();
}

// Check they extention of image
$extension = image_type_to_extension($info[2]);
if(
strtolower($extension) == '.png'){
$img = $this->resize_imagepng($origimg,$w, $h);
imagepng($img,$newimg);
imagedestroy($img);
}elseif(
strtolower($extension) == '.jpeg'){
$img = $this->resize_imagejpeg($origimg, $w, $h);
imagejpeg($img, $newimg);
imagedestroy($img);
}elseif(
strtolower($extension == '.gif')){
$img = $this->resize_imagegif($origimg, $w, $h);
imagegif($img,$newimg);
imagedestroy($img);
}

}
/**
* End function name : resize_img
*/

/**
* Function name : resize_imagepng
* Description : This function is to resize png image
* @param : $file variable is the original image
* @param : $w variable is the width of image
* @param : $f variable is the height of image
*/
private function resize_imagepng($file, $w, $h) {
list(
$width, $height) = getimagesize($file);
$src = imagecreatefrompng($file);
$dst = imagecreatetruecolor($w, $h);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
return
$dst;
}
/**
* End function name : resize_imagepng
*/

/**
* Function name : resize_imagejpeg
* Description : This function is to resize jpeg image
* @param : $file variable is the original image
* @param : $w variable is the width of image
* @param : $f variable is the height of image
*/
private function resize_imagejpeg($file, $w, $h) {
list(
$width, $height) = getimagesize($file);
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($w, $h);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
return
$dst;
}
/**
* End function name : resize_imagejpeg
*/

/**
* Function name : resize_imagegif
* Description : This function is to resize gif image
* @param : $file variable is the original image
* @param : $w variable is the width of image
* @param : $f variable is the height of image
*/
private function resize_imagegif($file, $w, $h) {
list(
$width, $height) = getimagesize($file);
$src = imagecreatefromgif($file);
$dst = imagecreatetruecolor($w, $h);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
return
$dst;
}
/**
* End function name : resize_imagegif
*/
}
/**
* End class name : resizeImage
*/
?>
mpyw
8 年前
這是黑白 imagebmp() 實作的範例。

<?php

/**
* Output a black-and-white BMP image to either the browser or a file.
*
* @param resource $image
* An image resource, returned by one of the image creation functions,
* such as imagecreatetruecolor().
*
* @param string|null $to
* The path or an open stream resource
* (which is automatically being closed after this function returns)
* to save the file to.
* If not set or NULL, the raw image stream will be outputted directly.
*
* @param float $threshold
* The number in the range of 0.0 to 1.0.
* Brighter for larger, or darker for smaller.
*
* @return bool Returns TRUE on success or FALSE on failure.
*
*/
function imagebwbmp($image, $to = null, $threshold = 0.5)
{
if (
func_num_args() < 1) {
$fmt = "imagebwbmp() expects a least 1 parameters, %d given";
trigger_error(sprintf($fmt, func_num_args()), E_USER_WARNING);
return;
}
if (!
is_resource($image)) {
$fmt = "imagebwbmp() expects parameter 1 to be resource, %s given";
trigger_error(sprintf($fmt, gettype($image)), E_USER_WARNING);
return;
}
if (!
is_numeric($threshold)) {
$fmt = "imagebwbmp() expects parameter 3 to be float, %s given";
trigger_error(sprintf($fmt, gettype($threshold)), E_USER_WARNING);
return;
}

if (
get_resource_type($image) !== 'gd') {
$msg = "imagebwbmp(): supplied resource is not a valid gd resource";
trigger_error($msg, E_USER_WARNING);
return
false;
}
switch (
true) {
case
$to === null:
break;
case
is_resource($to) && get_resource_type($to) === 'stream':
case
is_string($to) && $to = fopen($to, 'wb'):
if (
preg_match('/[waxc+]/', stream_get_meta_data($to)['mode'])) {
break;
}
default:
$msg = "imagebwbmp(): Invalid 2nd parameter, it must a writable filename or a writable stream";
trigger_error($msg, E_USER_WARNING);
return
false;
}

if (
$to === null) {
$to = fopen('php://output', 'wb');
}

$biWidth = imagesx($image);
$biHeight = imagesy($image);
$biSizeImage = ((int)ceil($biWidth / 32) * 32 / 8 * $biHeight);
$bfOffBits = 54 + 4 * 2; // Use two colors (black and white)
$bfSize = $bfOffBits + $biSizeImage;

fwrite($to, 'BM');
fwrite($to, pack('VvvV', $bfSize, 0, 0, $bfOffBits));
fwrite($to, pack('VVVvvVVVVVV', 40, $biWidth, $biHeight, 1, 1, 0, $biSizeImage, 0, 0, 0, 0));
fwrite($to, "\xff\xff\xff\x00"); // white
fwrite($to, "\x00\x00\x00\x00"); // black

for ($y = $biHeight - 1; $y >= 0; --$y) {
$byte = 0;
for (
$x = 0; $x < $biWidth; ++$x) {
$rgb = imagecolorsforindex($image, imagecolorat($image, $x, $y));
$value = (0.299 * $rgb['red'] + 0.587 * $rgb['green'] + 0.114 * $rgb['blue']) / 0xff;
$color = (int)($value > $threshold);
$byte = ($byte << 1) | $color;
if (
$x % 8 === 7) {
fwrite($to, pack('C', $byte));
$byte = 0;
}
}
if (
$x % 8) {
fwrite($to, pack('C', $byte << (8 - $x % 8)));
}
if (
$x % 32) {
fwrite($to, str_repeat("\x00", (int)((32 - $x % 32) / 8)));
}
}

return
true;
}
?>
To Top