【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