3

如何创建这样的模式:

从带有GD的PHP中的单个图像?

我已经尝试了很多,但我认为它可以更容易地完成。

我的想法:用 imagefilledrectangle 填充一行。对于第二行,用左右边距填充它(对于我的第一个示例)。接下来将图像切成两块并用它填充边距并为每一行重复此操作。

我希望有人可以帮助我:)

谢谢!

4

2 回答 2

1

对于任何类型的图案效果,我推荐以下之一:

  1. 手动执行模式:

    <?php
    $srcimg = imagecreatefromgif('data:image/gif;base64,R0lGODlhFAAUAPcAAAICAgICAxkWBxoYBysoC0VCD1JODVtXDF5ZDl1YD15aEGNeDmFcEG1oDm9qDGZhEGhjEWxoE3FsDXVvFnZwB3VwDXlyB3t0BXt0B3x0BX53BndxEXdyEHhzE3t2EX95EH95KIN7BIV9BIZ+BYd/BoN+DIF6EYJ7E4N+NIR+N4eABoiABYqDBYmCBoyFBo2GBY2FDo2HDY2IDo+JD4eBEYuDEImDEoqEEZSNB5WPB5GJDpKLE5OMEJuTCZ2VCJ2WCJ+YCZiRFJ2WFoiCLo6JLoaBNpCML5OOP6CZCaCaCaGaCqCZDaObDaOdCaOcC6OcDaSbCaSdCaWdC6aeCKaeCaWeCqafC6aeDKGZEaOcEaafJ6mhDayjCq2kCq2kC6+mC6+lDK+oCaqjE6uiF7CnC7CoCrGoC7OrC7OrDbSrDLWtC7SsDLauDLauDbmwC7qxDLiwDbuyDLqyDbu0DbyzDbyzDr20DLy0DbuxErmwF7qyFL2zEr61EL21FaiiKY6KTJKOUJWQRZuWQJ2YRcG3DcC2DsK4DsG4D8S6DsS7D8e9Dsa8D8i+D8K5EcW8E8i/FcrAD83DD8vCEMzBEM3CEM7EEM/FEdHHD9DFENHGENHHEdDHFNLIENPJE9TKEdXLE9bMEtbME9rPE9rQE93SE97UE97UFOHVE+LWE+TYFOTYFebbFOfcFujcFeneFOreFevfFuvgFezhFe/jFvHlFvHmFfLnFvXrF/PoGvTrGPnsGfvxG/vyGfzzGvz2HP36IP37I/37Nfz7Nv39PtzXct7aef38TP38Tujhb+zleaypgLGuhrKwhLSxh726mLy6m8LAmsLBn8vImdrWktrXk8XDpMTCpsbDp8XEqfXwjvbxkf39kvz7v9vazeblxefmxuHg2Ono2uno3vz7wf791v7+2PHx5vLy5/Hw6/Py6PLy6fLy7f7/9P7/9f38+fz8+vz8+/39/P7+/f7+/v///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAP8ALAAAAAAUABQAAAj/AOkJFDivHbdsyYpN8+Zu3sCH9Oax2xbM161XpTK50SINnkOI5YwB22XrVSpSnigdgjIo3ceI5Ib94oVLCCtUojrRaBQnChF18gSyO/arl64xAfKQAiUmwI07aXoEehdxGzBeump1AGBjlCcTARbYWcOFhLOCwnbdoiUrQoAPoTJxCHCADpsuP0DAG+fL1ixYq04E2MGpUo0ADeSs+VIFQzdtuWK1SnWqzwBHlibhEZAFzhovVkY8Q+ZK1SlSo0BtskSJESI+ctig8VKFhTJipoIUeOTpUiVJrgnRgaNmCYESLZZR0/RpgiZMkSApQlToDpw2Z8AocBLC2rc3lDzooIHEaJGhQnNirwkDo4ISCuDc+TG050Gi83fkwGGzpowXA0/gkII880gzRRw8bFCHfm2scQYZXiAgAxIXYCOQO4JUscYMCWDhoBlf6GBADEqo8IdHAp1jhA9dbOEAAxA8kIAETCSxAgroPDSPOUe0AAQVVFwhRRNK5JABIOtARI8870AzhAYrvOCCCBYUcU08ShK0ZDjRMNNMNeI49BI9AQEAOw==');
    $sw = imagesx($srcimg);
    $sh = imagesy($srcimg);
    
    $dw = 500;
    $dh = 500;
    $destimg = imagecreatetruecolor($dw, $dh);
    
    // Switch statement for the patterns you listed...
    switch (0) {
      case 0 :
        $px = 0;
        for ($y = 0; $y < $dh; $y += $sh) {
          $px = ($px - ($sw / 2)) % $sw;
          for ($x = $px; $x < $dw, $x += $sw) {
            imagecopy($destimg, $srcimg, $x, $y, 0, 0, $sw, $sh);
          }
        }
        break;
      case 1 :
        $py = 0;
        for ($x = 0; $x < $dw; $x += $sw) {
          $py = ($py - ($sh / 2)) % $sh;
          for ($y = $py; $y < $dh, $y += $sh) {
            imagecopy($destimg, $srcimg, $x, $y, 0, 0, $sw, $sh);
          }
        }
        break;
      case 2 :
        for ($y = 0; $y < $dh; $y += 2 * $sh) {
          for ($x = $px; $x < $dw, $x += 2 * $sw) {
            imagecopyresized($destimg, $srcimg, $x, $y, $sw, $sh, $sw,$sh,-$sw,-$sh);
            imagecopyresized($destimg, $srcimg, $x+$sw,$y, 0,$sh, $sw,$sh,   0,-$sh);
            imagecopyresized($destimg, $srcimg, $x,$y+$sh, $sw,0, $sw,$sh,-$sw,   0);
            imagecopyresized($destimg, $srcimg, $x+$sw,$y+$sh,0,0,$sw,$sh,   0,   0);
          }
        }
        break;
    }
    
    header('Content-Type: image/png');
    imagepng($destimg);
    ?>
    
  2. 像上面一样手动做图案,但只创建一个小图案 [0:(sw,sh*2), 1:(sw*2, sh), 2:(sw*2, sh*2)] 并用于imagesettile填充具有该图案的图像。

    <?php
    // $src and $dest as above, except:
    $dw = $sw * 2;
    $dh = $sh * 2;
    
    // Now copy the entire switch statement from above; the loops can be reduced
    // to 4-5 `imagecopy` statements each instead of loops if you want.
    
    // Create the actual output image:
    $outputimg = imagecreatetruecolor(500, 500);
    // Set tile and use the fill functions...
    imagesettile($outputimg, $destimg);
    imagefilledellipse($outputimg, 250, 250, 500, 500, IMG_COLOR_TILED);
    
    header('image/png');
    imagepng($outputimg);
    ?>
    
于 2012-11-07T18:31:29.230 回答
0

抱歉我迟到了,同时我为此使用了 Imagick,具有以下效果:

<?php
$output_h   = 500;
$output_w   = 500;
$image_size = 100;
$src    = 'input.jpg';
$output = 'output.jpg';

//Centering
exec('convert '.$src.' -resize '.$image_size.' -background white -gravity center -extent '.$output_w.'x'.$output_h.' '.$outfile);

//Mirror
exec('convert '.$src.' -resize '.$image_size.' \( -clone 0 -flip \) -append -write mpr:sometile +delete -size '.$output_w.'x'.$output_h.' tile:mpr:sometile '.$outfile);

//Half drop vertical
exec('convert '.$src.' -resize '.$image_size.' -format "%[fx:round(h/2)]" info:',$result);
exec('convert '.$src.' -resize '.$image_size.' \( -clone 0 -roll +0+'.$result[0].' \) +append -write mpr:sometile +delete -size '.$output_w.'x'.$output_h.' tile:mpr:sometile '.$outfile);

//Half drop horizontal
exec('convert '.$src.' -resize '.$image_size.' -format "%[fx:round(w/2)]" info:',$result);
exec('convert '.$src.' -resize '.$image_size.' \( -clone 0 -roll -'.$result[0].'-0 \) -append -write mpr:sometile +delete -size '.$output_w.'x'.$output_h.' tile:mpr:sometile '.$outfile);

//Fill
exec('convert '.$src.' -resize '.$image_size.' +append -write mpr:sometile +delete -size '.$output_w.'x'.$output_h.' tile:mpr:sometile '.$outfile);
?>
于 2012-11-08T10:49:53.467 回答