{"id":7359,"date":"2022-09-12T11:46:23","date_gmt":"2022-09-12T10:46:23","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=7359"},"modified":"2022-09-12T11:46:23","modified_gmt":"2022-09-12T10:46:23","slug":"supporting-heic-images-on-websites","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/supporting-heic-images-on-websites\/","title":{"rendered":"Supporting HEIC images on websites"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What are HEIC images<\/h2>\n\n\n\n<p>HEIC is Apple&#8217;s own version of the High-Efficiency Image File format (HEIF) which offers even more compression than the JPEG format without sacrificing quality meaning even smaller files. HEIC became the default photo file format on iOS 11 but at the time almost no website supported uploading these type of images. Things have improved slightly since then but there is still no support on most browsers meaning anyone wanting to support them needs to convert them to either JPEG or WEBP first.<\/p>\n\n\n\n<p>Debian 10 (&#8220;Buster&#8221;) was the first Debian Linux distribution to offer some support but as you will see some solutions work better than others.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Imagick<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>sudo apt-get install php-imagick<\/code><\/pre><\/div>\n\n\n\n<p>Using the php-imagick library to process HEIC images in our tests only worked some of the time. A simple example shown below to convert an HEIC image to a JPEG generated errors for about half the HEIC images we tried<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>&lt;?php\nif (isset($_FILES[&#39;file&#39;]))\n\t{\n\t$size\t\t= isset($_REQUEST[&quot;w&quot;]) ? $_REQUEST[&quot;w&quot;] :800 ;\n\t$source \t= $_FILES[&#39;file&#39;][&#39;tmp_name&#39;];\t\n\t$output\t\t= $_SERVER[&quot;DOCUMENT_ROOT&quot;].&quot;\/tmp\/&quot;.time().&quot;.jpg&quot;;\n\t\n\t$imagick \t= new Imagick($source);\n\t$imagick-&gt;resizeImage($size,0,Imagick::FILTER_LANCZOS,1);\n\t$imagick-&gt;setImageFormat(&#39;jpg&#39;);\n\t$imagick-&gt;setCompressionQuality(80);\n\t$imagick-&gt;writeImage($output);\n\n\t$image_data\t= file_get_contents($output);\n\t$base64 = &quot;data:image\/jpeg;base64,&quot;.base64_encode($image_data);\n\tunlink($output);\n\n\techo(&quot;&lt;p&gt;&lt;img src=\\&quot;$base64\\&quot; alt=\\&quot;\\&quot;\/&gt;&lt;\/p&gt;&quot;);\n\t}\n\t?&gt;\n    \n    &lt;form id=&quot;myinventory&quot; method=&quot;post&quot; action=&quot;\/heic2.php&quot; enctype=&quot;multipart\/form-data&quot;&gt;\n\t&lt;input id=&quot;photo&quot; type=&quot;file&quot; name=&quot;file&quot; onchange=&quot;document.forms[0].submit()&quot;\/&gt;\n    &lt;\/form&gt;    <\/code><\/pre><\/div>\n\n\n\n<p>Several sites on Google suggested compiling Imagick with HEIC support but everything appeared set up correctly on the server so I&#8217;m not sure what the issue was:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.bronco.co.uk\/our-ideas\/wp-content\/uploads\/2022\/09\/heic.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"982\" height=\"791\" src=\"https:\/\/www.bronco.co.uk\/our-ideas\/wp-content\/uploads\/2022\/09\/heic.jpg\" alt=\"\" class=\"wp-image-7361\" srcset=\"https:\/\/www.bronco.co.uk\/our-ideas\/wp-content\/uploads\/2022\/09\/heic.jpg 982w, https:\/\/www.bronco.co.uk\/our-ideas\/wp-content\/uploads\/2022\/09\/heic-300x242.jpg 300w, https:\/\/www.bronco.co.uk\/our-ideas\/wp-content\/uploads\/2022\/09\/heic-768x619.jpg 768w\" sizes=\"auto, (max-width: 982px) 100vw, 982px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using LibHeif<\/h2>\n\n\n\n<p>The most reliable solution we found in testing was to use the libheif library instead.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>apt-get install libheif1<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>&lt;?php\nif (isset($_FILES[&#39;file&#39;]))\n\t{\n\t$size\t\t= isset($_REQUEST[&quot;w&quot;]) ? $_REQUEST[&quot;w&quot;] :800 ;\n\t$source \t= $_FILES[&#39;file&#39;][&#39;tmp_name&#39;];\t\n\t$output\t\t= $_SERVER[&quot;DOCUMENT_ROOT&quot;].&quot;\/tmp\/&quot;.time().&quot;.jpg&quot;;\n\t\t\n\tshell_exec(escapeshellcmd(&quot;\/bin\/heif-convert -q 80 &quot;.$source.&quot; &quot;.$output));\n\t\n\t$image_data = file_get_contents($output);\n\t$base64     = &quot;data:image\/jpeg;base64,&quot;.base64_encode($image_data);\n\tunlink($output);\n\n\techo(&quot;&lt;p&gt;&lt;img src=\\&quot;$base64\\&quot; alt=\\&quot;\\&quot;\/&gt;&lt;\/p&gt;&quot;);\n\t}\n\t?&gt;\n    \n    &lt;form id=&quot;myinventory&quot; method=&quot;post&quot; action=&quot;\/heic2.php&quot; enctype=&quot;multipart\/form-data&quot;&gt;\n\t&lt;input id=&quot;photo&quot; type=&quot;file&quot; name=&quot;file&quot; onchange=&quot;document.forms[0].submit()&quot;\/&gt;\n    &lt;\/form&gt;    \n<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>What are HEIC images HEIC is Apple&#8217;s own version of the High-Efficiency Image File format (HEIF) which offers even more compression than the JPEG format without sacrificing quality meaning even smaller files. HEIC became the default photo file format on iOS 11 but at the time almost no website supported uploading these type of images. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7370,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-7359","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/7359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/comments?post=7359"}],"version-history":[{"count":0,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/7359\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media\/7370"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=7359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=7359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}