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

Em:

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

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

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

谢谢

最佳答案

首先下载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 表示第二个品格:

**注意:我还将图像保存到磁盘,因此您不必一遍又一遍地重新生成同一标签。*

07-25 23:55
查看更多