顯示具有 php 標籤的文章。 顯示所有文章
顯示具有 php 標籤的文章。 顯示所有文章

【PHP】ColorBar


【目的】
產生色彩條來驗證與除錯。

  • ColorBar1
    1. Gray(C0C0C0)
    2. Yellow(FFFF00)
    3. Cyan(00FFFF)
    4. Green(00FF00)
    5. Magenta(FF00FF)
    6. Red(FF0000)
    7. Blue(0000FF)
  • ColorBar2
    1. 白(W)
    2. 黃(Ye)
    3. 青綠(Cy)
    4. 綠(G)
    5. 洋紅(Mg)
    6. 紅(R)
    7. 藍(B)
    8. 黑(B1)

【程式】
以第一組 ColorBar 為例,製作出一個 560x80 的 ColorBar,每條Bar顏色為 80x80。
這邊可視Panel/Lcm的大小自行調整。

<?php
$im = imagecreatetruecolor(560, 80);

$gray    = imagecolorallocate($im, 0xC0, 0xC0, 0xC0);
$yellow  = imagecolorallocate($im, 0xFF, 0xFF, 0x00);
$cyan    = imagecolorallocate($im, 0x00, 0xFF, 0XFF);
$green   = imagecolorallocate($im, 0x00, 0xFF, 0x00);
$magenta = imagecolorallocate($im, 0xFF, 0x00, 0xFF);
$red     = imagecolorallocate($im, 0xFF, 0x00, 0x00);
$blue    = imagecolorallocate($im, 0x00, 0x00, 0xFF);

imagefilledrectangle($im,   0, 0,  80, 80, $gray);
imagefilledrectangle($im,  80, 0, 160, 80, $yellow);
imagefilledrectangle($im, 160, 0, 240, 80, $cyan);
imagefilledrectangle($im, 240, 0, 320, 80, $green);
imagefilledrectangle($im, 320, 0, 400, 80, $magenta);
imagefilledrectangle($im, 400, 0, 480, 80, $red);
imagefilledrectangle($im, 480, 0, 560, 80, $blue);

imagepng($im,'colorbar.png');
imagedestroy($im);
?>

【結果】

image

【參考】

【PHP】負片效果


【目的】

  1. 呈現出類似底片的效果。

【程式】(negative.php)

  1. 法一
    <?php
    $img = imagecreatefrompng('lena_std.png');
    imageTrueColorToPalette($img,true,256);
    $numColors = imageColorsTotal($img);
    for ($x = 0; $x < $numColors; $x++)
      {
        list($r,$g,$b) = array_values(imageColorsForIndex($img,$x));
        $r = 255 - $r;
        $g = 255 - $g;
        $b = 255 - $b;
        imageColorSet($img,$x,$r,$g,$b);
      }
    imagepng($img,'lena_negative.png');
    imagedestroy($img);
    ?>
  2. 法二(還需驗證)
    <?php
    $im = imagecreatefrompng('lena_std.png');
    if($im && imagefilter($im, IMG_FILTER_NEGATE))
    {
      echo 'Reverses all colors of the imagee.';
      imagepng($im, 'lena_negative.png');
    }
     else
    {
      echo 'Reverses all colors failed.';
    }
    imagedestroy($im);
    ?>

【結果】(lena_negative.png)

image

【PHP】圖形翻轉


【程式碼】
兩種作法。

  1. 摘自http://www.oklinux.cn/html/developer/php/jc/20070919/37668.html
    <?php
    $imgstream   =   file_get_contents('lena_std.png');   
    $im   =   imagecreatefromstring($imgstream);      
    $thumbw   =   imagesx($im);
    $thumbh   =   imagesy($im);
    if(function_exists("imagecreatetruecolor"))   
      $dim   =   imagecreatetruecolor($thumbw,   $thumbh);
    else   
      $dim   =   imagecreate($thumbh,   $thumbw);
    for($x=0;$x<$thumbw;$x++)
    {
      for($y=0;$y<$thumbh;$y++)
        {
         //左右對調
          imagecopyresized($dim,$im,$thumbw-$x-1,$y,$x,$y,1,1,1,1);
         //上下翻轉
          //imagecopyresized($dim,$im,$x,$thumbh-$y-1,$x,$y,1,1,1,1);
        }
    }   
    imagepng($dim,'lena_mirror.png');
    imagedestroy($dim);
    ?>
  2. ImageSetPixel
    <?php 
    $im   =   imagecreatefrompng('lena_std.png');      
    $thumbw   =   imagesx($im);
    $thumbh   =   imagesy($im);
        
    if(function_exists("imagecreatetruecolor"))   
      $dim   =   imagecreatetruecolor($thumbw,   $thumbh);
    else   
      $dim   =   imagecreate($thumbh,   $thumbw);
    
    for($x=0;$x<$thumbw;$x++)
    {
      for($y=0;$y<$thumbh;$y++)
        {
        $rgb = ImageColorAt ($im, $x, $y);
        ImageSetPixel ($dim, $thumbw-$x, $y, $rgb);
        }
    }   
    imagepng($dim,'lena_mirr.png');
    imagedestroy($dim);
    ?>

【結果】

  1. 原始圖片
    image
  2. 第一個作法的結果(imagecopyresized)
    image
  3. 第二個作法的結果
    類似上面。

【PNG】彩色轉灰階/黑白


【目的】

將彩色轉灰階/黑白。通常取黑白之前要先取灰階(RGB/3 大於7F為白,相反為黑)

【程式碼】

  1. 彩色轉灰階法一: rgb 三色取平均數
    <?php
    $img = imagecreatefrompng('lena_std.png');
    imageTrueColorToPalette($img,true,256);
    $numColors = imageColorsTotal($img);
    for ($x = 0; $x < $numColors; $x++)
      {
        list($r,$g,$b) = array_values(imageColorsForIndex($img,$x));
        $avg = intval(($r + $g + $b) / 3);
        imageColorSet($img,$x,$avg,$avg,$avg);
      }
    imagepng($img,'lena_gray.png');
    imagedestroy($img);
    ?>
  2. 彩色轉灰階法二: R*0.299 + G*0.587+B*0.114 
    <?php 
    $img = imagecreatefrompng('lena_std.png');
    imageTrueColorToPalette($img,true,256);
    $numColors = imageColorsTotal($img);
    for ($x = 0; $x < $numColors; $x++)
      {
        list($r,$g,$b) = array_values(imageColorsForIndex($img,$x));     
        $avg = intval($r*0.299 + $g*0.587 + $b*0.114);
    	imageColorSet($img,$x,$avg,$avg,$avg);
      }
    imagepng($img,'lena_gray.png');
    imagedestroy($img);
    ?>

  3. 彩色轉灰階法三: 直接使用GD內建的imagefilter(IMG_FILTER_GRAYSCALE)
    <?php 
    $im = imagecreatefrompng('lena_std.png'); 
    if($im && imagefilter($im, IMG_FILTER_GRAYSCALE)) 
    {     
      echo 'Image converted to grayscale.';     
      imagepng($im, 'lena_gray.png'); 
    } 
     else 
    {  
      echo 'Conversion to grayscale failed.'; 
    } 
    imagedestroy($im); 
    ?>
  4. 彩色轉單色法一:
    <?php
    $img = imagecreatefrompng('lena_std.png');
    imageTrueColorToPalette($img,true,256);
    $numColors = imageColorsTotal($img);
    for ($x = 0; $x < $numColors; $x++)
      {
        list($r,$g,$b) = array_values(imageColorsForIndex($img,$x));
        $avg = intval($r*0.299+$g*0.587+$b*0.114);
    	if($avg >= 127){ $avg = 255; }
    	else{$avg = 0;}
        imageColorSet($img,$x,$avg,$avg,$avg);
      }
    imagepng($img,'lena_bw.png');
    imagedestroy($img);
    ?>

【結果】

  1. 原始圖片(300x300 24bits lena_std.png)
    image
  2. 彩色轉灰階轉檔結果(300x300 8bits lena_gray.png)
    image
  3. 彩色轉單色結果(lena_bw.png)
    image

【參考】

【備註】

  • bool imagetruecolortopalette ( resource image, bool dither, int ncolors )
    第二個參數(true/false)為指明圖像是否被抖動(dithered)。
    第三個參數(2/4../256)為定調色板中被保留的顏色的最大數目。

【PHP】一些PHP程式


php for windows http://windows.php.net/download/(VC9 x86 Thread Safe)

  1. 上傳檔案 http://www.w3schools.com/PHP/php_file_upload.asp
    1. 要在與程式同一個目錄創建upload 這個目錄並設成755,就會把上傳的檔案搬到 upload 這邊。
    2. $_FILES["file"]["size"] < 120000 就是限制檔案大小大約 120k bytes。
  2. 驗證碼 http://jax-work-archive.blogspot.com/2007/11/php.html
    1. 記得要指定所要使用的字型名稱與路徑。

【Debug】

  1. Xdebug http://www.xdebug.org/download.php

【BMP】以WinHex/PHP解析bmp


【目的】

  • 利用WinHex/Php解析基本的BMP格式。

【準備】

  • 使用小畫家繪製兩張16x16單色bmp格式的圖。
    一張全部為黑(black.bmp),一張全部為白(white.bmp)。
    image image
  • 先打開BMP File Format(BMP.tpl)(http://www.x-ways.net/winhex/templates/index.html),將內容先存入剪貼簿,抄錄如下
    template "BMP File Format (with Palette)"
    
    // Template by Khomenko Volodymyr, Ukraine.
    
    description "Structure of a BMP image file"
    appliesto file
    
    requires 0x00    "42 4D"     // 'BM'
    
    begin
        section    "BMP File Header"
            read-only char[2]    "BMP_ID"        // 00
            uint32    "File size"                // 02
            uint32    "Reserved"                 // 06
            uint32  "ImageDataOffset"            // 0A
        endsection
    
        section    "BMP Info Header"
            uint32    "HeaderSize"               // 0E
            uint32    "Width"                    // 12
            uint32    "Height"                   // 16
            uint16    "Planes"                   // 1A
            uint16    "BPP"                      // 1C
            uint32    "CompessionMethod"         // 1E
            uint32    "ImageSize"                // 22
            uint32    "XPixelsPerMeter"          // 26
            uint32    "YPixelsPerMeter"          // 2A
            uint32    "PaletteSize"              // 2E
            uint32    "ColorsImportant"          // 32
        endsection
    
        section    "Palette(If PaletteSize=0 then no palette)"
            numbering 0
    
            {
                byte "B[~]"
                byte "G[~]"
                byte "R[~]"
                byte "A[~]"
    
            } [PaletteSize]
        endsection
    end

【步驟】

  1. WinHex本身沒提供解析bmp的能力,要加入這種樣板。
  2. 打開WinHex(目前我是用v15.4)。
  3. View | Template Manager(Alt+F12) | New
    image
  4. 貼上剛剛存入剪貼簿的內容。Check Syntax | Save | Close。 
    image
  5. 這是white.bmp mapping結果,基本資料以32bits(資料+Padding)一組,目前是
    16bits(0xFF) 加上 16bits(0x00,Padding)。16x16的圖剛好是 0x3E~0x7D共64bytes(橙色框框)。
    image
  6. 另外根據 Bitmap data(http://en.wikipedia.org/wiki/BMP_file_format),
    有兩個地方是特別要注意的。
    image 
    6.1 資料排列方式是左下開始到右上,而不是一般常說的右上到左下
    6.2 Pixel是 BGR 排列,而不是一般常說的 RGB
    6.3 24Bits點陣圖 的話就是每個Pixel為 8(R) + 8(G) + 8(B)=24Bits
    6.4 由於會需要alignment,所以會補00 00(3C,3D)
    6.4 所以上面的圖一張就是
    (1,0) Red (00,00,FF)
    (1,1) White (FF,FF,FF)
    (0,0) Blue (FF,00,00)
    (0,1) Green (00,FF,00)

  7. 如果一直這樣看Raw Date的話,實在是有點麻煩。嘗試用php/imagecolorsforindex(ImageColorAt)來解析,但是由於php無法讀取bmp檔,所以先用小畫家將剛剛的檔案存成 test.png。
    <?php
    
    // open an image
    $im = imagecreatefrompng('test.png');
    
    // get a color
    for ($x=0;$x<2;$x++)
    {
      for ($y=0;$y<2;$y++)
        {
          $color_index = imagecolorat($im, $x, $y);
          // make it human readable
          $color_tran = imagecolorsforindex($im, $color_index);
          // what is it ?
          echo("XY[$x,$y]");
          print_r($color_tran);
        }
    }
    ?>
  8. 結果排列順序和我意料不同(x/y 和上面的表現方式不同,看來是一排一排讀),目前先留下記錄。
    $php dump.php 
    XY[0,0]Array
    (
        [red] => 0
        [green] => 0
        [blue] => 255
        [alpha] => 0
    )
    XY[0,1]Array
    (
        [red] => 255
        [green] => 0
        [blue] => 0
        [alpha] => 0
    )
    XY[1,0]Array
    (
        [red] => 0
        [green] => 255
        [blue] => 0
        [alpha] => 0
    )
    XY[1,1]Array
    (
        [red] => 255
        [green] => 255
        [blue] => 255
        [alpha] => 0
    )

【參考】

  1. WinHex 範本(模板)分享!
    http://jay-fva.blogspot.com/2010/02/winhex.html
  2. 點陣圖(Bitmap)檔案格式
    http://crazycat1130.pixnet.net/blog/post/1345538
  3. BMP file format
    http://en.wikipedia.org/wiki/BMP_file_format
  4. YUV
    http://zh.wikipedia.org/zh/YUV
  5. PNG文件結构分析
    http://www.elevatorsx.com/news.asp?Rep=NaN&id=126840
    http://www.elevatorsx.com/news.asp?Rep=NaN&id=126841
  6. The GIF File Format With C
    http://www.elevatorsx.com/news.asp?Rep=NaN&id=100089
  7. 影像處理上課資料
    http://web.meiho.edu.tw/~x00002174/95_2Course.htm
  8. WinHex
    http://zone-soft.com/index.php?newsid=1079
  9. 以php解析 bmp
    imagecreatefrom[BMP] http://www.webdeveloper.com/forum/showthread.php?t=194194

用php發送msn


參考
 

Ed32. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com