{"id":27,"date":"2012-02-13T18:51:09","date_gmt":"2012-02-13T18:51:09","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2012\/02\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/"},"modified":"2012-02-13T18:51:09","modified_gmt":"2012-02-13T18:51:09","slug":"drawing_a_mandelbrot_set_with_zend_pdf-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/","title":{"rendered":"Drawing A Mandelbrot Set With Zend PDF"},"content":{"rendered":"<p>\nAs you may know from some of my previous posts, I&#8217;ve been looking at Zend Framework&#8217;s PDF support.\n<\/p>\n<p>\nAs a bit of fun on the train home from work, I wondered if it would be possible to draw out a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mandelbrot_set\">Mandelbrot Set<\/a> using the PDF drawing functions in Zend_Pdf.\n<\/p>\n<p>\nFor those who don&#8217;t know what the Mandelbrot Set is, it is a fractal shape, and one of those projects every developer plays with at some point. It looks like my time has come here.\n<\/p>\n<p>\nThere is a reasonably simple formula for generating the image, we just need to iterate between a maximum and minimum x and y co-ordinate set and draw a pixel at each point when the number of iterations of the formula has reached a pre-defined number. In my example, I&#8217;m using 20 iterations, though you can go higher for greater accuracy. I won&#8217;t go over the formula, as it is explained in far greater depth than I have time for elsewhere on the internet.\n<\/p>\n<p>\nIn this code example, I&#8217;m using variables similar to those used in Mandelbrots formula, so they do trigger warnings with Zend Framework&#8217;s coding standards, but I think they make sense.\n<\/p>\n<pre class=\"lang:php decode:true \" >\n\/\/ load the Zend Framework Autoloader.\nini_set('include_path', '.\/library');\nrequire_once 'Zend\/Loader\/Autoloader.php';\n$loader = Zend_Loader_Autoloader::getInstance();\n$maxIterations = 20;\n$minX = -2;\n$maxX = 1;\n$minY = -1;\n$maxY = 1;\ntry {\n  \/\/ create a new PDF document.\n  $pdf = new Zend_Pdf();\n  \/\/ create a new A4 sized page.\n  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);\n  \/\/ calculate the width and height of the image to generate.\n  $width = $page->getWidth();\n  $height = $page->getHeight();\n  if ($width > $height) {\n    $width = $height;\n  } else {\n    $height = $width;\n  }\n  \/\/ prepare a black colour we can reuse when we draw the mandelbrot set.\n  $black = new Zend_Pdf_Color_Html('#000000');\n  \/\/ iterate over the mandelbrot set and draw it out.\n  for ($x=0; $x<=$width; $x++) {\n    for ($y=0; $y<=$height; $y++) {\n      $c1 = $minX + ($maxX - $minX) \/ $width * $x;\n      $c2 = $minY + ($maxY - $minY) \/ $height * $y;\n      $z1 = 0;\n      $z2 = 0;\n      for ($i=0; $i<$maxIterations; $i++) {\n        $newZ1 = $z1 * $z1 - $z2 * $z2 + $c1;\n        $newZ2 = 2 * $z1 * $z2 + $c2;\n        $z1 = $newZ1;\n        $z2 = $newZ2;\n        if ($z1 * $z1 + $z2 * $z2 >= 4) {\n          break;\n        }\n      }\n      \/\/ if we reach max iterations, draw a black pixel\n      if ($i >= $maxIterations) {\n        $page->setFillColor($black)\n             ->setLineColor($black)\n             ->drawRectangle(\n               $x,\n               $y,\n               $x+1,\n               $y+1,\n               Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE\n             );\n      }\n    }\n  }\n  \/\/ add the page to the pdf document.\n  $pdf->pages[] = $page;\n  \/\/ save the finished PDF to a file\n  $pdf->save('fractal.pdf');\n  \/\/ catch any errors\n} catch (Zend_Pdf_Exception $e) {\n  die('PDF error: ' . $e->getMessage());\n} catch (Exception $e) {\n  die ('Error: ' . $e->getMessage());\n}\n<\/pre>\n<p>\nYou will need to run this code on the command line, and it will take a few seconds to execute. When complete, you will have a PDF document with the following image.\n<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/robblog\/images\/mandelbrot_pdf1.png\" alt=\"Mandelbrot set rendered from a PDF\" width=\"381\" height=\"500\" class=\"blogimage\" \/>\n<\/p>\n<p>\nWe can refine this slightly to get a prettier picture if we show the number of iterations used in a different colour, instead of just showing black if we go over our limit of maximum iterations.\n<\/p>\n<p>\nTo do this, we&#8217;d need to prepare an associative array with a colour for each iteration. The easiest option is to use grayscale and <code>Zend_Pdf_Color_GrayScale<\/code>. After we work out the max width and height, insert the following code.\n<\/p>\n<pre class=\"lang:php decode:true \" >\/\/ prepare colours\nfor ($i=0; $i >= $maxIterations; $i++) {\n  $grayLevel = $i \/ $maxIterations;\n  $colours[$i] = new Zend_Pdf_Color_GrayScale($grayLevel);\n}\n<\/pre>\n<p>\nNow we have an associative array to look up a different shade of gray from 0 to $maxIterations.\n<\/p>\n<p>\nInstead of the code that checks if $maxIterations has been reached and then draw a black pixel, we can replace it with the following\n<\/p>\n<pre class=\"lang:php decode:true \" >\n$page->setLineColor($colours[$i])\n     ->setFillColor($colours[$i])\n     ->drawRectangle($x, $y, $x+1, $y+1, Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE);\n<\/pre>\n<p>\nRun this modified script on the command line, and you&#8217;ll generate a PDF with the following image.\n<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/robblog\/images\/mandelbrot_pdf2.png\" alt=\"Mandelbrot set rendered from a PDF in grayscale\" width=\"494\" height=\"500\" class=\"blogimage\" \/>\n<\/p>\n<p>\nThat image looks great, but not very colourful. I&#8217;ll cover how to get some colour in there another time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you may know from some of my previous posts, I&#8217;ve been looking at Zend Framework&#8217;s PDF support. As a bit of fun on the train home from work, I wondered if it would be possible to draw out a Mandelbrot Set using the PDF drawing functions in Zend_Pdf. For those who don&#8217;t know what &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Drawing A Mandelbrot Set With Zend PDF&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[50,81],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-dev","tag-php","tag-zend-framework"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Drawing A Mandelbrot Set With Zend PDF - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Drawing A Mandelbrot Set With Zend PDF - Robert Price\" \/>\n<meta property=\"og:description\" content=\"As you may know from some of my previous posts, I&#8217;ve been looking at Zend Framework&#8217;s PDF support. As a bit of fun on the train home from work, I wondered if it would be possible to draw out a Mandelbrot Set using the PDF drawing functions in Zend_Pdf. For those who don&#8217;t know what &hellip; Continue reading &quot;Drawing A Mandelbrot Set With Zend PDF&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2012-02-13T18:51:09+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Drawing A Mandelbrot Set With Zend PDF\",\"datePublished\":\"2012-02-13T18:51:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\\\/\"},\"wordCount\":396,\"keywords\":[\"PHP\",\"Zend Framework\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\\\/\",\"name\":\"Drawing A Mandelbrot Set With Zend PDF - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2012-02-13T18:51:09+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Drawing A Mandelbrot Set With Zend PDF\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Drawing A Mandelbrot Set With Zend PDF - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Drawing A Mandelbrot Set With Zend PDF - Robert Price","og_description":"As you may know from some of my previous posts, I&#8217;ve been looking at Zend Framework&#8217;s PDF support. As a bit of fun on the train home from work, I wondered if it would be possible to draw out a Mandelbrot Set using the PDF drawing functions in Zend_Pdf. For those who don&#8217;t know what &hellip; Continue reading \"Drawing A Mandelbrot Set With Zend PDF\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/","og_site_name":"Robert Price","article_published_time":"2012-02-13T18:51:09+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Drawing A Mandelbrot Set With Zend PDF","datePublished":"2012-02-13T18:51:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/"},"wordCount":396,"keywords":["PHP","Zend Framework"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/","name":"Drawing A Mandelbrot Set With Zend PDF - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2012-02-13T18:51:09+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/drawing_a_mandelbrot_set_with_zend_pdf-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Drawing A Mandelbrot Set With Zend PDF"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}