Looking t speed up this Laravel Seeder












0












$begingroup$


I don't proclaim to be the best developer, but I am always looking for new ways to learn how to improve my code. I have never posted on this stack section before so please be gentle.



consider the following code:



<?php

use IlluminateDatabaseSeeder;

use AppModulesLocationsServicesCreateMapService;
use ChristianEsslLandmapGenerationStructColor;
use AppModulesLocationsModelsLocation;

class SurfaceLocations extends Seeder
{

private $imageResource;

/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$water = new Color(66, 129, 178);
$land = new Color(23, 132, 72);

$createImage = new CreateMapService($land, $water, 350, 350, 'random_map');
$createImage->generateMap('surface');

$contents = Storage::disk('maps')->get('surface.png');
$this->imageResource = imagecreatefromstring($contents);

$waterRgb = 112219255;

for ($x = 0; $x <= 349; $x++) {
for($y = 0; $y <= 349; $y++) {

$rgb = imagecolorat($this->imageResource, $x, $y);

$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

$color = $r.$g.$b;

if ((int)$color === $waterRgb) {
$locationExists = Location::where('x', $x)->where('y', $y)->first();

if (is_null($locationExists)) {
Location::create([
'x' => $x,
'y' => $y,
'is_water' => true
]);

$this->setPort($waterRgb, $x, $y);
}
} else {
$locationExists = Location::where('x', $x)->where('y', $y)->first();

if (is_null($locationExists)) {
Location::create([
'x' => $x,
'y' => $y,
'is_water' => false
]);
}
}
}
}
}

protected function setPort(int $waterColor, int $x, int $y) {
if ($this->isLandLeft($waterColor, $x, $y)) {
if (rand(1, 50) >= 50) {
Location::create([
'x' => $x - 1,
'y' => $y,
'is_port' => true,
]);
}
}

if ($this->isLandRight($waterColor, $x, $y)) {
if (rand(1, 50) >= 50) {
Location::create([
'x' => $x + 1,
'y' => $y,
'is_port' => true,
]);
}
}

if ($this->isLandUp($waterColor, $x, $y)) {
if (rand(1, 50) >= 50) {
Location::create([
'x' => $x,
'y' => $y - 1,
'is_port' => true,
]);
}
}

if ($this->isLandDown($waterColor, $x, $y)) {
Location::create([
'x' => $x,
'y' => $y + 1,
'is_port' => true,
]);
}

if ($this->isLandDiagonalRightUp($waterColor, $x, $y)) {
Location::create([
'x' => $x + 1,
'y' => $y - 1,
'is_port' => true,
]);
}

if ($this->isLandDiagonalRightDown($waterColor, $x, $y)) {
Location::create([
'x' => $x + 1,
'y' => $y + 1,
'is_port' => true,
]);
}

if ($this->isLandDiagonalLeftUp($waterColor, $x, $y)) {
Location::create([
'x' => $x - 1,
'y' => $y - 1,
'is_port' => true,
]);
}

if ($this->isLandDiagonalLeftDown($waterColor, $x, $y)) {
Location::create([
'x' => $x - 1,
'y' => $y + 1,
'is_port' => true,
]);
}
}

private function isLandLeft(int $waterColor, int $x, int $y) {
$x = $x - 1;

if ($x <= 0) {
return false;
}

$rgb = imagecolorat($this->imageResource, $x, $y);

$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

$color = $r.$g.$b;

if ($color !== $waterColor) {
return true;
}

return false;
}

private function isLandRight(int $waterColor, int $x, int $y) {
$x = $x + 1;

if ($x > 349) {
$x = 349;
}

$rgb = imagecolorat($this->imageResource, $x, $y);

$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

$color = $r.$g.$b;

if ($color !== $waterColor) {
return true;
}

return false;
}

private function isLandUp(int $waterColor, int $x, int $y) {
$y = $y - 1;

if ($y <= 0) {
return false;
}

$rgb = imagecolorat($this->imageResource, $x, $y);

$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

$color = $r.$g.$b;

if ($color !== $waterColor) {
return true;
}

return false;
}

private function isLandDown(int $waterColor, int $x, int $y) {
$y = $y + 1;

if ($y > 349) {
$y = 349;
}

$rgb = imagecolorat($this->imageResource, $x, $y);

$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

$color = $r.$g.$b;

if ($color !== $waterColor) {
return true;
}

return false;
}

private function isLandDiagonalRightUp(int $waterColor, int $x, int $y) {
$y = $y - 1;
$x = $x + 1;

if ($y <= 0) {
return false;
}

if ($x > 349) {
$x = 349;
}

$rgb = imagecolorat($this->imageResource, $x, $y);

$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

$color = $r.$g.$b;

if ($color !== $waterColor) {
return true;
}

return false;
}

private function isLandDiagonalRightDown(int $waterColor, int $x, int $y) {
$y = $y + 1;
$x = $x + 1;

if ($x > 349) {
$x = 349;
}

if ($y > 349) {
$y = 349;
}

$rgb = imagecolorat($this->imageResource, $x, $y);

$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

$color = $r.$g.$b;

if ($color !== $waterColor) {
return true;
}

return false;
}

private function isLandDiagonalLeftUp(int $waterColor, int $x, int $y) {
$y = $y - 1;
$x = $x - 1;

if ($y <= 0 || $x <= 0) {
return false;
}

$rgb = imagecolorat($this->imageResource, $x, $y);

$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

$color = $r.$g.$b;

if ($color !== $waterColor) {
return true;
}

return false;
}

private function isLandDiagonalLeftDown(int $waterColor, int $x, int $y) {
$y = $y - 1;
$x = $x + 1;

if ($y <= 0) {
return false;
}

if ($x > 349) {
$x = 349;
}

$rgb = imagecolorat($this->imageResource, $x, $y);

$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

$color = $r.$g.$b;

if ($color !== $waterColor) {
return true;
}

return false;
}
}


Basically whats going on here is were create an image using the land map generation library which I have wrapped in a service called CreateMapService. This on its own, because we are creating an image, takes a smidge.



But on to the real meat.



The rest of this walks over 350 pixels (349 because of imagecolorat) then I check if the x, y are the waterRgb.



If so, I then attempt to create (as long as it doesn't exist, see why below) a port. To do this, if we are on water, we check in all 8 directions around that pixel to se if there is land, if there is we do a random check to see if we should create a port or not. If so we create the location with is_port set to true.



This is why, as above, I stated we check if the location exists, so we don't re-create it.



The issue is that this seed takes more then 10 minutes to create. I expect with the image generation for it to take at least a few minutes, it has to create the image.



I also expect a few minutes for the looping and the checking, but I feel it shouldn't take up to and including 30+ minutes.



So my question for you is how do I make this code cleaner, more robust and faster (if possible) (not counting the image generation part)









share









$endgroup$

















    0












    $begingroup$


    I don't proclaim to be the best developer, but I am always looking for new ways to learn how to improve my code. I have never posted on this stack section before so please be gentle.



    consider the following code:



    <?php

    use IlluminateDatabaseSeeder;

    use AppModulesLocationsServicesCreateMapService;
    use ChristianEsslLandmapGenerationStructColor;
    use AppModulesLocationsModelsLocation;

    class SurfaceLocations extends Seeder
    {

    private $imageResource;

    /**
    * Run the database seeds.
    *
    * @return void
    */
    public function run()
    {
    $water = new Color(66, 129, 178);
    $land = new Color(23, 132, 72);

    $createImage = new CreateMapService($land, $water, 350, 350, 'random_map');
    $createImage->generateMap('surface');

    $contents = Storage::disk('maps')->get('surface.png');
    $this->imageResource = imagecreatefromstring($contents);

    $waterRgb = 112219255;

    for ($x = 0; $x <= 349; $x++) {
    for($y = 0; $y <= 349; $y++) {

    $rgb = imagecolorat($this->imageResource, $x, $y);

    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;

    $color = $r.$g.$b;

    if ((int)$color === $waterRgb) {
    $locationExists = Location::where('x', $x)->where('y', $y)->first();

    if (is_null($locationExists)) {
    Location::create([
    'x' => $x,
    'y' => $y,
    'is_water' => true
    ]);

    $this->setPort($waterRgb, $x, $y);
    }
    } else {
    $locationExists = Location::where('x', $x)->where('y', $y)->first();

    if (is_null($locationExists)) {
    Location::create([
    'x' => $x,
    'y' => $y,
    'is_water' => false
    ]);
    }
    }
    }
    }
    }

    protected function setPort(int $waterColor, int $x, int $y) {
    if ($this->isLandLeft($waterColor, $x, $y)) {
    if (rand(1, 50) >= 50) {
    Location::create([
    'x' => $x - 1,
    'y' => $y,
    'is_port' => true,
    ]);
    }
    }

    if ($this->isLandRight($waterColor, $x, $y)) {
    if (rand(1, 50) >= 50) {
    Location::create([
    'x' => $x + 1,
    'y' => $y,
    'is_port' => true,
    ]);
    }
    }

    if ($this->isLandUp($waterColor, $x, $y)) {
    if (rand(1, 50) >= 50) {
    Location::create([
    'x' => $x,
    'y' => $y - 1,
    'is_port' => true,
    ]);
    }
    }

    if ($this->isLandDown($waterColor, $x, $y)) {
    Location::create([
    'x' => $x,
    'y' => $y + 1,
    'is_port' => true,
    ]);
    }

    if ($this->isLandDiagonalRightUp($waterColor, $x, $y)) {
    Location::create([
    'x' => $x + 1,
    'y' => $y - 1,
    'is_port' => true,
    ]);
    }

    if ($this->isLandDiagonalRightDown($waterColor, $x, $y)) {
    Location::create([
    'x' => $x + 1,
    'y' => $y + 1,
    'is_port' => true,
    ]);
    }

    if ($this->isLandDiagonalLeftUp($waterColor, $x, $y)) {
    Location::create([
    'x' => $x - 1,
    'y' => $y - 1,
    'is_port' => true,
    ]);
    }

    if ($this->isLandDiagonalLeftDown($waterColor, $x, $y)) {
    Location::create([
    'x' => $x - 1,
    'y' => $y + 1,
    'is_port' => true,
    ]);
    }
    }

    private function isLandLeft(int $waterColor, int $x, int $y) {
    $x = $x - 1;

    if ($x <= 0) {
    return false;
    }

    $rgb = imagecolorat($this->imageResource, $x, $y);

    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;

    $color = $r.$g.$b;

    if ($color !== $waterColor) {
    return true;
    }

    return false;
    }

    private function isLandRight(int $waterColor, int $x, int $y) {
    $x = $x + 1;

    if ($x > 349) {
    $x = 349;
    }

    $rgb = imagecolorat($this->imageResource, $x, $y);

    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;

    $color = $r.$g.$b;

    if ($color !== $waterColor) {
    return true;
    }

    return false;
    }

    private function isLandUp(int $waterColor, int $x, int $y) {
    $y = $y - 1;

    if ($y <= 0) {
    return false;
    }

    $rgb = imagecolorat($this->imageResource, $x, $y);

    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;

    $color = $r.$g.$b;

    if ($color !== $waterColor) {
    return true;
    }

    return false;
    }

    private function isLandDown(int $waterColor, int $x, int $y) {
    $y = $y + 1;

    if ($y > 349) {
    $y = 349;
    }

    $rgb = imagecolorat($this->imageResource, $x, $y);

    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;

    $color = $r.$g.$b;

    if ($color !== $waterColor) {
    return true;
    }

    return false;
    }

    private function isLandDiagonalRightUp(int $waterColor, int $x, int $y) {
    $y = $y - 1;
    $x = $x + 1;

    if ($y <= 0) {
    return false;
    }

    if ($x > 349) {
    $x = 349;
    }

    $rgb = imagecolorat($this->imageResource, $x, $y);

    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;

    $color = $r.$g.$b;

    if ($color !== $waterColor) {
    return true;
    }

    return false;
    }

    private function isLandDiagonalRightDown(int $waterColor, int $x, int $y) {
    $y = $y + 1;
    $x = $x + 1;

    if ($x > 349) {
    $x = 349;
    }

    if ($y > 349) {
    $y = 349;
    }

    $rgb = imagecolorat($this->imageResource, $x, $y);

    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;

    $color = $r.$g.$b;

    if ($color !== $waterColor) {
    return true;
    }

    return false;
    }

    private function isLandDiagonalLeftUp(int $waterColor, int $x, int $y) {
    $y = $y - 1;
    $x = $x - 1;

    if ($y <= 0 || $x <= 0) {
    return false;
    }

    $rgb = imagecolorat($this->imageResource, $x, $y);

    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;

    $color = $r.$g.$b;

    if ($color !== $waterColor) {
    return true;
    }

    return false;
    }

    private function isLandDiagonalLeftDown(int $waterColor, int $x, int $y) {
    $y = $y - 1;
    $x = $x + 1;

    if ($y <= 0) {
    return false;
    }

    if ($x > 349) {
    $x = 349;
    }

    $rgb = imagecolorat($this->imageResource, $x, $y);

    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;

    $color = $r.$g.$b;

    if ($color !== $waterColor) {
    return true;
    }

    return false;
    }
    }


    Basically whats going on here is were create an image using the land map generation library which I have wrapped in a service called CreateMapService. This on its own, because we are creating an image, takes a smidge.



    But on to the real meat.



    The rest of this walks over 350 pixels (349 because of imagecolorat) then I check if the x, y are the waterRgb.



    If so, I then attempt to create (as long as it doesn't exist, see why below) a port. To do this, if we are on water, we check in all 8 directions around that pixel to se if there is land, if there is we do a random check to see if we should create a port or not. If so we create the location with is_port set to true.



    This is why, as above, I stated we check if the location exists, so we don't re-create it.



    The issue is that this seed takes more then 10 minutes to create. I expect with the image generation for it to take at least a few minutes, it has to create the image.



    I also expect a few minutes for the looping and the checking, but I feel it shouldn't take up to and including 30+ minutes.



    So my question for you is how do I make this code cleaner, more robust and faster (if possible) (not counting the image generation part)









    share









    $endgroup$















      0












      0








      0





      $begingroup$


      I don't proclaim to be the best developer, but I am always looking for new ways to learn how to improve my code. I have never posted on this stack section before so please be gentle.



      consider the following code:



      <?php

      use IlluminateDatabaseSeeder;

      use AppModulesLocationsServicesCreateMapService;
      use ChristianEsslLandmapGenerationStructColor;
      use AppModulesLocationsModelsLocation;

      class SurfaceLocations extends Seeder
      {

      private $imageResource;

      /**
      * Run the database seeds.
      *
      * @return void
      */
      public function run()
      {
      $water = new Color(66, 129, 178);
      $land = new Color(23, 132, 72);

      $createImage = new CreateMapService($land, $water, 350, 350, 'random_map');
      $createImage->generateMap('surface');

      $contents = Storage::disk('maps')->get('surface.png');
      $this->imageResource = imagecreatefromstring($contents);

      $waterRgb = 112219255;

      for ($x = 0; $x <= 349; $x++) {
      for($y = 0; $y <= 349; $y++) {

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ((int)$color === $waterRgb) {
      $locationExists = Location::where('x', $x)->where('y', $y)->first();

      if (is_null($locationExists)) {
      Location::create([
      'x' => $x,
      'y' => $y,
      'is_water' => true
      ]);

      $this->setPort($waterRgb, $x, $y);
      }
      } else {
      $locationExists = Location::where('x', $x)->where('y', $y)->first();

      if (is_null($locationExists)) {
      Location::create([
      'x' => $x,
      'y' => $y,
      'is_water' => false
      ]);
      }
      }
      }
      }
      }

      protected function setPort(int $waterColor, int $x, int $y) {
      if ($this->isLandLeft($waterColor, $x, $y)) {
      if (rand(1, 50) >= 50) {
      Location::create([
      'x' => $x - 1,
      'y' => $y,
      'is_port' => true,
      ]);
      }
      }

      if ($this->isLandRight($waterColor, $x, $y)) {
      if (rand(1, 50) >= 50) {
      Location::create([
      'x' => $x + 1,
      'y' => $y,
      'is_port' => true,
      ]);
      }
      }

      if ($this->isLandUp($waterColor, $x, $y)) {
      if (rand(1, 50) >= 50) {
      Location::create([
      'x' => $x,
      'y' => $y - 1,
      'is_port' => true,
      ]);
      }
      }

      if ($this->isLandDown($waterColor, $x, $y)) {
      Location::create([
      'x' => $x,
      'y' => $y + 1,
      'is_port' => true,
      ]);
      }

      if ($this->isLandDiagonalRightUp($waterColor, $x, $y)) {
      Location::create([
      'x' => $x + 1,
      'y' => $y - 1,
      'is_port' => true,
      ]);
      }

      if ($this->isLandDiagonalRightDown($waterColor, $x, $y)) {
      Location::create([
      'x' => $x + 1,
      'y' => $y + 1,
      'is_port' => true,
      ]);
      }

      if ($this->isLandDiagonalLeftUp($waterColor, $x, $y)) {
      Location::create([
      'x' => $x - 1,
      'y' => $y - 1,
      'is_port' => true,
      ]);
      }

      if ($this->isLandDiagonalLeftDown($waterColor, $x, $y)) {
      Location::create([
      'x' => $x - 1,
      'y' => $y + 1,
      'is_port' => true,
      ]);
      }
      }

      private function isLandLeft(int $waterColor, int $x, int $y) {
      $x = $x - 1;

      if ($x <= 0) {
      return false;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandRight(int $waterColor, int $x, int $y) {
      $x = $x + 1;

      if ($x > 349) {
      $x = 349;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandUp(int $waterColor, int $x, int $y) {
      $y = $y - 1;

      if ($y <= 0) {
      return false;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandDown(int $waterColor, int $x, int $y) {
      $y = $y + 1;

      if ($y > 349) {
      $y = 349;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandDiagonalRightUp(int $waterColor, int $x, int $y) {
      $y = $y - 1;
      $x = $x + 1;

      if ($y <= 0) {
      return false;
      }

      if ($x > 349) {
      $x = 349;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandDiagonalRightDown(int $waterColor, int $x, int $y) {
      $y = $y + 1;
      $x = $x + 1;

      if ($x > 349) {
      $x = 349;
      }

      if ($y > 349) {
      $y = 349;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandDiagonalLeftUp(int $waterColor, int $x, int $y) {
      $y = $y - 1;
      $x = $x - 1;

      if ($y <= 0 || $x <= 0) {
      return false;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandDiagonalLeftDown(int $waterColor, int $x, int $y) {
      $y = $y - 1;
      $x = $x + 1;

      if ($y <= 0) {
      return false;
      }

      if ($x > 349) {
      $x = 349;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }
      }


      Basically whats going on here is were create an image using the land map generation library which I have wrapped in a service called CreateMapService. This on its own, because we are creating an image, takes a smidge.



      But on to the real meat.



      The rest of this walks over 350 pixels (349 because of imagecolorat) then I check if the x, y are the waterRgb.



      If so, I then attempt to create (as long as it doesn't exist, see why below) a port. To do this, if we are on water, we check in all 8 directions around that pixel to se if there is land, if there is we do a random check to see if we should create a port or not. If so we create the location with is_port set to true.



      This is why, as above, I stated we check if the location exists, so we don't re-create it.



      The issue is that this seed takes more then 10 minutes to create. I expect with the image generation for it to take at least a few minutes, it has to create the image.



      I also expect a few minutes for the looping and the checking, but I feel it shouldn't take up to and including 30+ minutes.



      So my question for you is how do I make this code cleaner, more robust and faster (if possible) (not counting the image generation part)









      share









      $endgroup$




      I don't proclaim to be the best developer, but I am always looking for new ways to learn how to improve my code. I have never posted on this stack section before so please be gentle.



      consider the following code:



      <?php

      use IlluminateDatabaseSeeder;

      use AppModulesLocationsServicesCreateMapService;
      use ChristianEsslLandmapGenerationStructColor;
      use AppModulesLocationsModelsLocation;

      class SurfaceLocations extends Seeder
      {

      private $imageResource;

      /**
      * Run the database seeds.
      *
      * @return void
      */
      public function run()
      {
      $water = new Color(66, 129, 178);
      $land = new Color(23, 132, 72);

      $createImage = new CreateMapService($land, $water, 350, 350, 'random_map');
      $createImage->generateMap('surface');

      $contents = Storage::disk('maps')->get('surface.png');
      $this->imageResource = imagecreatefromstring($contents);

      $waterRgb = 112219255;

      for ($x = 0; $x <= 349; $x++) {
      for($y = 0; $y <= 349; $y++) {

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ((int)$color === $waterRgb) {
      $locationExists = Location::where('x', $x)->where('y', $y)->first();

      if (is_null($locationExists)) {
      Location::create([
      'x' => $x,
      'y' => $y,
      'is_water' => true
      ]);

      $this->setPort($waterRgb, $x, $y);
      }
      } else {
      $locationExists = Location::where('x', $x)->where('y', $y)->first();

      if (is_null($locationExists)) {
      Location::create([
      'x' => $x,
      'y' => $y,
      'is_water' => false
      ]);
      }
      }
      }
      }
      }

      protected function setPort(int $waterColor, int $x, int $y) {
      if ($this->isLandLeft($waterColor, $x, $y)) {
      if (rand(1, 50) >= 50) {
      Location::create([
      'x' => $x - 1,
      'y' => $y,
      'is_port' => true,
      ]);
      }
      }

      if ($this->isLandRight($waterColor, $x, $y)) {
      if (rand(1, 50) >= 50) {
      Location::create([
      'x' => $x + 1,
      'y' => $y,
      'is_port' => true,
      ]);
      }
      }

      if ($this->isLandUp($waterColor, $x, $y)) {
      if (rand(1, 50) >= 50) {
      Location::create([
      'x' => $x,
      'y' => $y - 1,
      'is_port' => true,
      ]);
      }
      }

      if ($this->isLandDown($waterColor, $x, $y)) {
      Location::create([
      'x' => $x,
      'y' => $y + 1,
      'is_port' => true,
      ]);
      }

      if ($this->isLandDiagonalRightUp($waterColor, $x, $y)) {
      Location::create([
      'x' => $x + 1,
      'y' => $y - 1,
      'is_port' => true,
      ]);
      }

      if ($this->isLandDiagonalRightDown($waterColor, $x, $y)) {
      Location::create([
      'x' => $x + 1,
      'y' => $y + 1,
      'is_port' => true,
      ]);
      }

      if ($this->isLandDiagonalLeftUp($waterColor, $x, $y)) {
      Location::create([
      'x' => $x - 1,
      'y' => $y - 1,
      'is_port' => true,
      ]);
      }

      if ($this->isLandDiagonalLeftDown($waterColor, $x, $y)) {
      Location::create([
      'x' => $x - 1,
      'y' => $y + 1,
      'is_port' => true,
      ]);
      }
      }

      private function isLandLeft(int $waterColor, int $x, int $y) {
      $x = $x - 1;

      if ($x <= 0) {
      return false;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandRight(int $waterColor, int $x, int $y) {
      $x = $x + 1;

      if ($x > 349) {
      $x = 349;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandUp(int $waterColor, int $x, int $y) {
      $y = $y - 1;

      if ($y <= 0) {
      return false;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandDown(int $waterColor, int $x, int $y) {
      $y = $y + 1;

      if ($y > 349) {
      $y = 349;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandDiagonalRightUp(int $waterColor, int $x, int $y) {
      $y = $y - 1;
      $x = $x + 1;

      if ($y <= 0) {
      return false;
      }

      if ($x > 349) {
      $x = 349;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandDiagonalRightDown(int $waterColor, int $x, int $y) {
      $y = $y + 1;
      $x = $x + 1;

      if ($x > 349) {
      $x = 349;
      }

      if ($y > 349) {
      $y = 349;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandDiagonalLeftUp(int $waterColor, int $x, int $y) {
      $y = $y - 1;
      $x = $x - 1;

      if ($y <= 0 || $x <= 0) {
      return false;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }

      private function isLandDiagonalLeftDown(int $waterColor, int $x, int $y) {
      $y = $y - 1;
      $x = $x + 1;

      if ($y <= 0) {
      return false;
      }

      if ($x > 349) {
      $x = 349;
      }

      $rgb = imagecolorat($this->imageResource, $x, $y);

      $r = ($rgb >> 16) & 0xFF;
      $g = ($rgb >> 8) & 0xFF;
      $b = $rgb & 0xFF;

      $color = $r.$g.$b;

      if ($color !== $waterColor) {
      return true;
      }

      return false;
      }
      }


      Basically whats going on here is were create an image using the land map generation library which I have wrapped in a service called CreateMapService. This on its own, because we are creating an image, takes a smidge.



      But on to the real meat.



      The rest of this walks over 350 pixels (349 because of imagecolorat) then I check if the x, y are the waterRgb.



      If so, I then attempt to create (as long as it doesn't exist, see why below) a port. To do this, if we are on water, we check in all 8 directions around that pixel to se if there is land, if there is we do a random check to see if we should create a port or not. If so we create the location with is_port set to true.



      This is why, as above, I stated we check if the location exists, so we don't re-create it.



      The issue is that this seed takes more then 10 minutes to create. I expect with the image generation for it to take at least a few minutes, it has to create the image.



      I also expect a few minutes for the looping and the checking, but I feel it shouldn't take up to and including 30+ minutes.



      So my question for you is how do I make this code cleaner, more robust and faster (if possible) (not counting the image generation part)







      php laravel





      share












      share










      share



      share










      asked 5 mins ago









      Kyle AdamsKyle Adams

      1112




      1112






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214507%2flooking-t-speed-up-this-laravel-seeder%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214507%2flooking-t-speed-up-this-laravel-seeder%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Сан-Квентин

          8-я гвардейская общевойсковая армия

          Алькесар