【问题标题】:How to flip text vertically/horizontally using php?如何使用php垂直/水平翻转文本?
【发布时间】:2010-02-19 20:56:34
【问题描述】:

有谁知道如何做到这一点?

【问题讨论】:

    标签: php text image-processing gd flip


    【解决方案1】:

    我从您的标签中假设您的意思是翻转 GD 图像。

    你的意思是翻转就像旋转吗?这可以使用imagerotate 来完成:

    使用给定的角度旋转图像图像。

    旋转中心是图像的中心,旋转后的图像可能与原始图像有不同的尺寸。

    或者您的意思是镜像图像?没有开箱即用的方法,但 this 代码 sn-p 可能会有所帮助。 (不过,它的性能不是很好,因为它逐个像素地复制。)

    对于快速的高级图像编辑操作,ImageMagick 是最好的工具。如果您使用的是共享主机,则需要由您的提供商安装它才能工作。

    【讨论】:

    • 谢谢。我解决这个问题的三个步骤: 1. 翻转(镜像)图像; 2.写文字; 3.再次翻转图像; ImageMagick 是非常强大的工具,但找不到更简单的方法。
    【解决方案2】:

    您可以使用某种字体替换技巧,例如 here,或者您可以使用 PHP 版本的 ImageMagick

    【讨论】:

      【解决方案3】:

      未经测试...但这似乎应该可以工作,由其他 gd 函数组成(可能很慢):

      function flipImageHorizontal($im){
        $width = imagesx($im);
        $height = imagesy($im);
      
        for($y = 0; $y < $height; $y++){          // for each column
          for($x = 0; $x < ($width >> 1); $x++){  // for half the pixels in the row       
      
             // get the color on the left side
             $rgb = imagecolorat($im, $x, $y);
             $colors = imagecolorsforindex($im, $rgb);
             $current_color = imagecolorallocate($im, $colors["red"], $colors["green"], $colors["blue"]);
      
             // get the color on the right side (mirror)
             $rgb = imagecolorat($im, $width - $x, $y);
             $colors = imagecolorsforindex($im, $rgb);
             $mirror_color = imagecolorallocate($im, $colors["red"], $colors["green"], $colors["blue"]);
      
             // swap the colors
             imagesetpixel($im, $x, $y, $mirror_color);        
             imagesetpixel($im, $width - $x, $y, $color);         
          }
        }
      }
      
      function flipImageVertical($im){
        $width = imagesx($im);
        $height = imagesy($im);
      
        for($x = 0; $x < $width; $x++){           // for each row
          for($y = 0; $y < ($height >> 1); $y++){ // for half the pixels in the col
      
             // get the color on the top
             $rgb = imagecolorat($im, $x, $y);
             $colors = imagecolorsforindex($im, $rgb);
             $current_color = imagecolorallocate($im, $colors["red"], $colors["green"], $colors["blue"]);
      
             // get the color on the bottom (mirror)
             $rgb = imagecolorat($im, $x, $height - $y);
             $colors = imagecolorsforindex($im, $rgb);
             $mirror_color = imagecolorallocate($im, $colors["red"], $colors["green"], $colors["blue"]);
      
             // swap the colors
             imagesetpixel($im, $x, $y, $mirror_color);        
             imagesetpixel($im, $x, $height - $y, $color);         
          }
        }
      }
      

      所以您可以使用bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color ) 从文本字符串创建图像,然后通过我在上面编写的适当翻转函数运行它...

      【讨论】:

      • 适合整个图像翻转,但我的目标是只旋转文本:)
      • @Anatoliy,你的意思是你想识别图像中的文本并水平/垂直镜像它?还是要将镜像文本叠加到现有图像上?
      • 是的,我需要将镜像文本写入现有图像。感谢您的参与,已经找到解决方案:翻转放置文本的图像区域,而不是将文本写入该区域,然后再次翻转图像区域。
      【解决方案4】:

      要在 PHP 中为现有图像添加垂直文本,请使用函数

      imagettftext($im, 10, $angle, $x, $y, $black, $font, $text);
      

      $angle = 90,文本将是垂直的。

      例子:

      http://www.php.net/manual/en/function.imagettfbbox.php#refsect1-function.imagettfbbox-returnvalues

      提示:

      该示例使用 $angle = 45,因此图像上的文本是对角线

      【讨论】:

        【解决方案5】:

        也许就这么简单?

        function toVertical ($string)
        {
        
           foreach (str_split($string) as $letter)
           {
               $newStr.="$letter\n";
           }
           return $newStr;
        }
        
        
        function toHorizontal($string)
        {
           foreach(explode("\n",$string) as $letter)
           {
                $newStr.=$letter;
           }
           return $newStr;
        }
        
        $v = toVertical("This string should be printed vertically");
        echo $v;
        $h = toHorizontal($v);
        echo $h;
        
        
        ---------- PHP Execute ----------
        T
        h
        i
        s
        
        s
        t
        r
        i
        n
        g
        
        s
        h
        o
        u
        l
        d
        
        b
        e
        
        p
        r
        i
        n
        t
        e
        d
        
        v
        e
        r
        t
        i
        c
        a
        l
        l
        y
        This string should be printed vertically
        Output completed (0 sec consumed)
        

        【讨论】:

          猜你喜欢
          • 2013-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-27
          • 1970-01-01
          • 2015-12-28
          • 2012-02-17
          • 2016-07-29
          相关资源
          最近更新 更多