【问题标题】:Create an image in PHP to show guitar chords在 PHP 中创建一个图像以显示吉他和弦
【发布时间】:2011-12-23 03:43:48
【问题描述】:

我有一个以文本格式显示吉他和弦/标签的网站。这是我当前显示的内容:

他们:

| | | | | |
| | | | | |
| 2 2 | | |
| | | | | |
| | | | | |

我了解到 GD 可以为此创建动态图像。但我是 PHP 新手,我不知道该怎么做。

在PHP中创建这样的东西来显示图像是否简单?

谢谢

【问题讨论】:

  • 如果您在文本输出中添加一些语义,您也可以使用 CSS 对其进行样式设置。但是我不想阻止你使用GD,这是一件好事。

标签: php image guitar music-notation


【解决方案1】:

首先下载arial字体,得到这个图片(另存为guitar.jpg):

并将它们放在与此脚本相同的文件夹中(例如 chords.php):

<?php

  function showChord($chord) {
    $imgfile = "./guitar.jpg";
    $text = ".";
    $font = './arial.ttf';

    $im = imagecreatefromjpeg($imgfile);
    $x = imagesx($im);
    $y = imagesy($im);
    $fontsize = 100;
    $white = imagecolorallocate($im, 0, 0, 0);

    $chords = explode('-', $chord);
    // chords[0] = e1 and chords[5] = e6

    $minimum = min($chords);
    imagettftext($im, 15, 0, 1, 32, $white, $font, $minimum);
    $add = 0;
    if($minimum > 0) {
      $add = 30;  
    }
    // chords positions
    $interval1 = ($chords[0] != 0 ? (25 + $add + (intval($chords[0]) - $minimum) * 30) : 0);
    $interval2 = ($chords[1] != 0 ? (25 + $add + (intval($chords[1]) - $minimum) * 30) : 0);
    $interval3 = ($chords[2] != 0 ? (25 + $add + (intval($chords[2]) - $minimum) * 30) : 0);
    $interval4 = ($chords[3] != 0 ? (25 + $add + (intval($chords[3]) - $minimum) * 30) : 0);
    $interval5 = ($chords[4] != 0 ? (25 + $add + (intval($chords[4]) - $minimum) * 30) : 0);
    $interval6 = ($chords[5] != 0 ? (25 + $add + (intval($chords[5]) - $minimum) * 30) : 0);
    // write to the image
    imagettftext($im, $fontsize, 0, 01, $interval1, $white, $font, $text);
    imagettftext($im, $fontsize, 0, 18, $interval2, $white, $font, $text);
    imagettftext($im, $fontsize, 0, 36, $interval3, $white, $font, $text);
    imagettftext($im, $fontsize, 0, 53, $interval4, $white, $font, $text);
    imagettftext($im, $fontsize, 0, 70, $interval5, $white, $font, $text);
    imagettftext($im, $fontsize, 0, 86, $interval6, $white, $font, $text);
    header("Content-type: image/jpeg");
    imagejpeg($im);
    ImageDestroy($im);
  }

#  $chord = '0-2-2-0-0-0'; //Em
  $chord = '2-4-4-3-2-2'; //F#
  showChord($chord);

这将输出类似于 F# 的内容。左上角的2表示二品:

**注意:我还将图像保存到磁盘,这样您就不必一遍又一遍地重新生成相同的选项卡。*

【讨论】:

    【解决方案2】:

    Tony Pottier 编写了一个漂亮的类来做到这一点。 http://www.tonypottier.info/

    编辑:解决如下所述的问题:

    $c = new chord(array('x',13,12,11,12,'x'));
    $c->setMarginRight(20);
    $c->setStartingFret(10);
    $c->draw();
    

    如果没有右边距,双数号将无法正确显示。

    【讨论】:

    • 不错,但是当你输入:x-13-12-11-12-x 时,它会显示一个空白标签
    猜你喜欢
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2011-08-13
    • 2011-07-24
    • 2011-06-22
    • 2010-11-05
    • 2019-03-01
    相关资源
    最近更新 更多