【问题标题】:Create ellipse using php使用 php 创建椭圆
【发布时间】:2015-03-20 08:51:38
【问题描述】:

我需要使用自定义颜色创建如下所示的椭圆。

我正在使用Intervention image 库来实现这一点。

我所做的是:
我为每个部分创建了 6 个不同的透明图像。
并尝试创建一个画布,然后在其上遮盖其他图层,但结果不如预期。 通过这个过程,我只能为图像的第一部分着色。

    Image::configure(array('driver' => 'gd'));
    $img = Image::canvas(150,104,'#000')->insert(WWW_ROOT.DS.IMAGES_URL.'test/masks/1.png');
    $img->mask(WWW_ROOT.DS.IMAGES_URL.'test/masks/2.png', true);
    $img->mask(WWW_ROOT.DS.IMAGES_URL.'test/masks/3.png', true);
    $img->mask(WWW_ROOT.DS.IMAGES_URL.'test/masks/4.png', true);
    $img->mask(WWW_ROOT.DS.IMAGES_URL.'test/masks/5.png', true);
    $img->mask(WWW_ROOT.DS.IMAGES_URL.'test/masks/6.png', true);
    $img->save(WWW_ROOT.DS.IMAGES_URL.'test/test.png');
    echo $img->response();

我需要帮助来创建上述自定义颜色图像或任何其他选项来实现此目的。

【问题讨论】:

    标签: php image gd ellipse


    【解决方案1】:

    不完美但更好:

    <?php
         $image = imagecreatetruecolor(300, 300);
    
    
        $white    = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
        $gray     = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
        $darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
        $navy     = imagecolorallocate($image, 0x00, 0x00, 0x80);
        $darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
        $red      = imagecolorallocate($image, 0xFF, 0x00, 0x00);
        $darkred  = imagecolorallocate($image, 0x90, 0x00, 0x00);
        $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
    
    
        for ($i = 60; $i > 50; $i--) {
           imagefilledarc($image, 150, $i, 300, 50, 0, 60, $darknavy, IMG_ARC_PIE);
           imagefilledarc($image, 150, $i, 300, 50, 60, 120 , $darkgray, IMG_ARC_PIE);
           imagefilledarc($image, 150, $i, 300, 50, 120, 180 , $darkred, IMG_ARC_PIE);
    
           imagefilledarc($image, 150, $i, 300, 50, 180, 240 , $navy, IMG_ARC_PIE);
           imagefilledarc($image, 150, $i, 300, 50, 240, 270 , $gray, IMG_ARC_PIE);
           imagefilledarc($image, 150, $i, 300, 50, 270, 360 , $red, IMG_ARC_PIE);
    
    
    
        }
    
        imagefilledarc($image, 150, 50, 300, 50, 0, 60, $navy, IMG_ARC_PIE);
        imagefilledarc($image, 150, 50, 300, 50, 60, 120 , $gray, IMG_ARC_PIE);
        imagefilledarc($image, 150, 50, 300, 50, 120, 180 , $red, IMG_ARC_PIE);
    
        imagefilledarc($image, 150, 50, 300, 50, 180, 240 , $navy, IMG_ARC_PIE);
        imagefilledarc($image, 150, 50, 300, 50, 240, 270 , $gray, IMG_ARC_PIE);
        imagefilledarc($image, 150, 50, 300, 50, 270, 360 , $red, IMG_ARC_PIE);
    
        imagefilledarc($image, 150, 50, 280, 40, 0, 360, $white, IMG_ARC_PIE);
    
    
    
        header('Content-type: image/png');
        imagepng($image);
        imagedestroy($image);
    ?>
    

    【讨论】:

    • 感谢您的努力,但它与我在上面提供的图像相去甚远。
    • 图像仍然失真,不知道如何使其清晰,但我感谢您的努力。
    【解决方案2】:

    为什么不使用imageellipse(),imagefilledellipse()imagefilledarc()

    <?php
    
        // Création de l'image
        $image = imagecreatetruecolor(100, 100);
    
        // Allocation de quelques couleurs
        $white    = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
        $gray     = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
        $darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
        $navy     = imagecolorallocate($image, 0x00, 0x00, 0x80);
        $darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
        $red      = imagecolorallocate($image, 0xFF, 0x00, 0x00);
        $darkred  = imagecolorallocate($image, 0x90, 0x00, 0x00);
    
        // Création de l'effet 3D
        for ($i = 60; $i > 50; $i--) {
           imagefilledarc($image, 50, $i, 100, 50, 0, 45, $darknavy, IMG_ARC_PIE);
           imagefilledarc($image, 50, $i, 100, 50, 45, 75 , $darkgray, IMG_ARC_PIE);
           imagefilledarc($image, 50, $i, 100, 50, 75, 360 , $darkred, IMG_ARC_PIE);
        }
    
        imagefilledarc($image, 50, 50, 100, 50, 0, 45, $navy, IMG_ARC_PIE);
        imagefilledarc($image, 50, 50, 100, 50, 45, 75 , $gray, IMG_ARC_PIE);
        imagefilledarc($image, 50, 50, 100, 50, 75, 360 , $red, IMG_ARC_PIE);
    
    
        // Affichage de l'image
        header('Content-type: image/png');
        imagepng($image);
        imagedestroy($image);
        ?> 
    

    【讨论】:

    • 我已经在 php.net 上看到过这个例子,但是创建的图像也不清晰,我怎样才能让它感觉像有问题的图像?
    • 我没有最好的解决方案,但也许这会有所帮助:
    【解决方案3】:

    终于可以达到预期的效果了。

    我所做的是:

    使用这个image通过这个jQuery library获取每个区域的多边形坐标。

    获取每个区域的坐标后,我使用Intervention Image Library 提供的polygon 函数创建了所需的图像。

    感谢大家的帮助,也许这可以帮助其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多