# Creating Image Blocks programmatically

**URL:** https://forum.getkirby.com/t/creating-image-blocks-programmatically/24204
**Category:** Questions
**Tags:** v3
**Created:** [December 9, 2021, 7:48pm UTC](https://forum.getkirby.com/t/creating-image-blocks-programmatically/24204 "2021-12-09T19:48:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![gerricom](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gerricom/32/53_2.png) [@gerricom](https://forum.getkirby.com/u/gerricom)
#### Post date: [December 9, 2021, 7:48pm UTC](https://forum.getkirby.com/t/creating-image-blocks-programmatically/24204/1 "2021-12-09T19:48:50Z")

</div>

Moin everybody!

I’m trying to convert a rather old kirby page to a new version with blocks instead of kirbytext fields. As the most pages are not that complex, I’m using `Blocks::parse()` for that. This works good except with images.

`parse` is creating image blocks but obviously referecing them with a web location an with the mediaUrl.

So I thought of just rewriting that block to a kirby location but I’m not getting behind this. My last approach was rather “harsh” because I thought there might be some problems when using `update()` on a blocks content but… it’s not working that way:

```auto
		$block = $oldBlock->toArray();
		if ($block['type'] == 'image') {
			$url = explode('/', $block['content']['src']);
			$block['content']['location'] = 'kirby';
			$block['content']['image'] = [A::last($url)];
			$block['content']['src'] = '';
		}
		$modifiedBlocks->add(Block::factory([
			'content' => $block['content'],
			'type' => $block['type'],
		]));

```

Are there any caveats when creating a image block programmatically? How should I reference an image when doing so?

Thanks!  
Markus

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [December 9, 2021, 8:59pm UTC](https://forum.getkirby.com/t/creating-image-blocks-programmatically/24204/2 "2021-12-09T20:59:02Z")

</div>

An image block looks basically like this:

```auto
$image = new Kirby\Cms\Block([
  'content' => [
    'location' => 'kirby',
    'image' => [
      "dark-forest.jpg"
    ],
  ],
  'type' => 'image',
]);

```

```auto
$blocks = $page->text()->toBlocks();
$blocks = $blocks->add(new Kirby\Cms\Blocks([$image]));
$kirby->impersonate('kirby');

$page->update([
  'text' => json_encode($blocks->toArray()),
]);

```

---

<div class="post-metadata">

### Author: ![gerricom](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gerricom/32/53_2.png) [@gerricom](https://forum.getkirby.com/u/gerricom)
#### Post date: [December 10, 2021, 1:17pm UTC](https://forum.getkirby.com/t/creating-image-blocks-programmatically/24204/3 "2021-12-10T13:17:05Z")

</div>

Thanks, Sonja!

I fed the `$page->update()` with the blocks collection because I thought Kirby would handle this directly. I now converted the collection to an array, did an json\_encode and now every works like it should.

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [December 10, 2021, 1:23pm UTC](https://forum.getkirby.com/t/creating-image-blocks-programmatically/24204/4 "2021-12-10T13:23:12Z")

</div>

Yes, the strange thing is that I wrote a recipe that works with just the blocks in the past, as if something changed in the meantime. Have to check this and adapt the recipe if necessary.

---

<div class="post-metadata">

### Author: ![jbidoret](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jbidoret/32/9445_2.png) [@jbidoret](https://forum.getkirby.com/u/jbidoret)
#### Post date: [January 3, 2022, 9:20pm UTC](https://forum.getkirby.com/t/creating-image-blocks-programmatically/24204/5 "2022-01-03T21:20:36Z")

</div>

Hi.

Indeed, the [recipe](https://getkirby.com/docs/cookbook/templating/update-blocks-programmatically) did’nt mentionned the `json_encode($blocks->toArray())`, which seem necessary. Thanks !

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [January 3, 2022, 9:56pm UTC](https://forum.getkirby.com/t/creating-image-blocks-programmatically/24204/6 "2022-01-03T21:56:45Z")

</div>

Ok, I finally got round to dumping the recipe code into a Starterkit and it worked like advertised without me having to `json_encode()` the blocks.

Hm, the problem seems to be that it stops working when trying to add image to the mix.

I fixed the recipe.
