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

【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. 摘自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. 第二個作法的結果
    類似上面。
 

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