Sign in to follow this  
  • entries
    25
  • comments
    26
  • views
    25622

Generating images for combined substrate

Sign in to follow this  
Liza Shulyayeva

884 views

The Laravel 5 migration is complete and I'm back to substrate mixing and brain bug fixing!

Actually, I think now that basic mixing is done I'm going to do a few weeks of just bug fixing. The substrate has no effect on snails' attributes or behaviour yet, but it will. For now you can just mix different substrate items together into new substrate items.

I'm still not sure what the best way to create images for the custom-mixed substrate might be. The possibilities are pretty much endless - you can mix any substrate-type item with any other substrate-type item.

So what happens is - the user goes to their closet. They pick two substrate items to mix together. Then this whole mess happens:

protected function mixSubstrate() { $itemID1 = Input::get('itemID1'); $itemID2 = Input::get('itemID2'); $item1 = $this->findItem($itemID1); $item2 = $this->findItem($itemID2); $item1WeightG = $item1->template->weightG; $item2WeightG = $item2->template->weightG; $resultingItemName = Input::get('itemname'); $item1Attributes = $item1->template->nutrition->getAttributes(); $item2Attributes = $item2->template->nutrition->getAttributes(); $imageTitle = preg_replace('/\s+/', '', $resultingItemName) . '.png'; $fileToSaveTo = '/assets/img/items/terrain/custom/' . $imageTitle; $resultingImage = Utility::MergeTwoImages($item1->fullImagePath, $item2->fullImagePath, $fileToSaveTo); // Create resulting item template $resultingItemTemplate = new ItemUserTemplate(); $resultingItemTemplate->description = 'Custom item created by combining ' . $item1->template->name . ' and ' . $item2->template->name; $resultingItemTemplate->weightG = $item1WeightG + $item2WeightG; $resultingItemTemplate->name = $resultingItemName; $resultingItemTemplate->typeID = $item1->template->typeID; $resultingItemTemplate->ownerID = Auth::user()->userID; $resultingItemTemplate->imageSubdir = 'terrain/custom/'; $resultingItemTemplate->imageName = $imageTitle; $resultingItemTemplate->save(); // Create resulting item nutrition template $resultingItemNutrition = new ItemUserNutrition(); $resultingItemNutrition->templateID = $resultingItemTemplate->templateID; foreach ($item1Attributes as $key => $value) { if ($key !== 'templateID' && $key !== 'updated_at' && $key !== 'created_at') { $resultingItemNutrition->$key = $item1Attributes[$key] + $item2Attributes[$key]; } } $resultingItemNutrition->save(); // Create item of new template $resultingItemProperties = [ 'templateID' => $resultingItemTemplate->templateID, 'ownerID' => Auth::user()->userID, 'isUserTemplate' => 1 ]; $resultingItem = $this->createNewItem($resultingItemProperties); // Delete original items $item1->deleteItem(); $item2->deleteItem(); return Redirect::back()->with('resultingItem', $resultingItem);}
To actually create the image for the new item I added a new utility function:

public static function MergeTwoImages($imgPath1, $imgPath2, $fileToSaveTo = null) { $x = 50; $y = 50; $root = $_SERVER['DOCUMENT_ROOT']; $finalImage = imagecreatetruecolor($x, $y); imagesavealpha($finalImage, true); $sortedImages = [$imgPath1, $imgPath2]; sort($sortedImages); $image1Layer = imagecreatefrompng($root . $sortedImages[0]); $image2Layer = imagecreatefrompng($root . $sortedImages[1]); $opacity = 50; ImageCopyMerge($finalImage, $image1Layer, 0, 0, 0, 0, 50, 50, $opacity); ImageCopyMerge($finalImage, $image2Layer, 0, 0, 0, 0, 50, 50, $opacity); header('Content-Type: image/png'); if ($fileToSaveTo) { $fileToSaveTo = $root . $fileToSaveTo; imagepng($finalImage, $fileToSaveTo); } return $finalImage;}

The results look like the image next to this post (this example is experimenting with mixing 1kg oak leaves with 1kg sand).

It'll do for now. After some bug fixing I'll go back to substrate and start having it actually have an effect on the snails.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now