title: Magento2: error when trying to save data with image in admin
body: <p>I wrote a custom slider module which includes slide items form. In this form, there is an image field to upload images.</p>
  
  <p>When I create for the first time a slide item with an image everything is working fine and I can retrieve all my data but when I edit it and save it again I got this error: </p>
  
  <blockquote>
    <p>1 exception(s): Exception #0 (Exception): Notice: Array to string
    conversion in
    /Users/louisserre/Sites/messika/lib/internal/Magento/Framework/Data/Form/Element/Image.php
    on line 51</p>
  </blockquote>
  
  <p>I got this error even if the image field is not changed.</p>
  
  <p>At ligne 51 of this file the code is:</p>
  
  <pre><code>public function getElementHtml(){
      $html = '';
  
      if ((string)$this-&gt;getValue()) {
  </code></pre>
  
  <p>I logged $this->getValue() and i got: imagepath/image.jpg when I create the slide item for the first time and Array([value] => <a href="http://imagepath/image.jpg" rel="nofollow noreferrer">http://imagepath/image.jpg</a>) when I edit the slide item.</p>
  
  <p>Here is my form field code:</p>
  
  <pre><code>    $fieldset-&gt;addField('image_url', 'image', [
          'label' =&gt; __('Image'),
          'title' =&gt; __('Image'),
          'name' =&gt; 'image_url',
          'required' =&gt; true
      ]);
  </code></pre>
  
  <p>Here is my controller Save.php file:</p>
  
  <pre><code>&lt;?php
  
  namespace Ultranoir\Slider\Controller\Adminhtml\SliderItem;
  
  
  use Ultranoir\Slider\Model\Slider;
  use Magento\Backend\App\Action;
  use Magento\TestFramework\ErrorLog\Logger;
  use Magento\Framework\App\Filesystem\DirectoryList;
  
  class Save extends \Magento\Backend\App\Action
  {
  
  /**
   * Store manager
   *
   * @var \Magento\Store\Model\StoreManagerInterface
   */
  protected $storeManager;
  
  /**
   * @var \Magento\Framework\Image\Factory
   */
  protected $imageFactory;
  
  /**
   * @var \Magento\Framework\Image\Factory
   */
  protected $mediaDirectory;
  
  /**
   * @var \Magento\Framework\View\Asset\Repository
   */
  protected $assetRepo;
  
  /**
   * @param \Magento\Store\Model\StoreManagerInterface $storeManager
   * @param \Magento\Framework\Image\Factory $imageFactory
   * @param \Magento\Framework\Filesystem $fileSystem
   * @param \Magento\Framework\View\Asset\Repository $assetRepo
   * @param Action\Context $context
   */
  public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager,
                              \Magento\Framework\Image\Factory $imageFactory,
                              \Magento\Framework\Filesystem $fileSystem,
                              \Magento\Framework\View\Asset\Repository $assetRepo,
                              Action\Context $context)
  {
      $this-&gt;storeManager = $storeManager;
      $this-&gt;imageFactory = $imageFactory;
      /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
      $this-&gt;mediaDirectory = $fileSystem-&gt;getDirectoryRead(DirectoryList::MEDIA);
      parent::__construct($context);
  }
  
  /**
   * {@inheritdoc}
   */
  protected function _isAllowed()
  {
      return $this-&gt;_authorization-&gt;isAllowed('Ultranoir_Slider::save_slideritem');
  }
  
  /**
   * Save action
   *
   * @return \Magento\Framework\Controller\ResultInterface
   */
  public function execute()
  {
      $data = $this-&gt;getRequest()-&gt;getPostValue();
      /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
      $resultRedirect = $this-&gt;resultRedirectFactory-&gt;create();
      if ($data) {
          /** @var \Ultranoir\Slider\Model\SliderItem $model */
          $model = $this-&gt;_objectManager-&gt;create('\Ultranoir\Slider\Model\SliderItem');
  
          $id = $this-&gt;getRequest()-&gt;getParam('slider_item_id');
          if ($id) {
              $model-&gt;load($id);
          }
          /** @var \Ultranoir\Slider\Model\Slider $slider */
          $slider = $this-&gt;_objectManager-&gt;create('\Ultranoir\Slider\Model\Slider');
          $slider-&gt;load($model-&gt;getSliderId());
  
          try{
              $data['image_url'] = $this-&gt;saveImage();
  
          } catch (\Exception $e) {
              $result = ['error' =&gt; $e-&gt;getMessage(), 'errorcode' =&gt; $e-&gt;getCode()];
              $this-&gt;messageManager-&gt;addException($e, __('Something went wrong while uploading the file.'));
              $this-&gt;_getSession()-&gt;setFormData($data);
              return $resultRedirect-&gt;setPath('*/*/edit', ['slider_item_id' =&gt; $this-&gt;getRequest()-&gt;getParam
              ('slider_item_id')]);
          }
  
          $model-&gt;setData($data);
  
          $this-&gt;_eventManager-&gt;dispatch(
              'ultraslider_slideritem_prepare_save',
              ['slideritem' =&gt; $model, 'request' =&gt; $this-&gt;getRequest()]
          );
  
          try {
              $model-&gt;save();
              $this-&gt;messageManager-&gt;addSuccess(__('You saved this item.'));
              $this-&gt;_objectManager-&gt;get('Magento\Backend\Model\Session')-&gt;setFormData(false);
              if ($this-&gt;getRequest()-&gt;getParam('back')) {
                  return $resultRedirect-&gt;setPath('*/*/edit', ['slider_item_id' =&gt; $model-&gt;getId(), '_current' =&gt;
                      true]);
              }
              return $resultRedirect-&gt;setPath('*/*/');
          } catch (\Magento\Framework\Exception\LocalizedException $e) {
              $this-&gt;messageManager-&gt;addError($e-&gt;getMessage());
          } catch (\RuntimeException $e) {
              $this-&gt;messageManager-&gt;addError($e-&gt;getMessage());
          } catch (\Exception $e) {
              $this-&gt;messageManager-&gt;addException($e, __('Something went wrong while saving the record.'));
          }
  
          $this-&gt;_getSession()-&gt;setFormData($data);
          return $resultRedirect-&gt;setPath('*/*/edit', ['slider_item_id' =&gt; $this-&gt;getRequest()-&gt;getParam
          ('slider_item_id')]);
      }
      return $resultRedirect-&gt;setPath('*/*/');
  }
  
  protected function saveImage(){
      $uploader = $this-&gt;_objectManager-&gt;create(
          'Magento\MediaStorage\Model\File\Uploader',
          ['fileId' =&gt; 'image_url']
      );
      $uploader-&gt;setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
      /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
      $imageAdapter = $this-&gt;_objectManager-&gt;get('Magento\Framework\Image\AdapterFactory')-&gt;create();
      $uploader-&gt;addValidateCallback('ultraslider_slideritem_image', $imageAdapter, 'validateUploadFile');
      $uploader-&gt;setAllowRenameFiles(true);
      $uploader-&gt;setFilesDispersion(true);
  
  
      $result = $uploader-&gt;save($this-&gt;getBaseMediaPath());
      $this-&gt;generateThumbnails($result['file'], $this-&gt;getBaseMediaPath());
      $this-&gt;_eventManager-&gt;dispatch(
          'ultraslider_slideritem_gallery_upload_image_after',
          ['result' =&gt; $result, 'action' =&gt; $this]
      );
  
      unset($result['tmp_name']);
      unset($result['path']);
  
      $result['url'] = $this-&gt;getBaseMediaUrl(). $result['file'];
      return $result['url'];
  
  }
  
  protected function getBaseMediaPath(){
      return $this-&gt;mediaDirectory-&gt;getAbsolutePath('slider/slideritem');
  }
  
  protected function getBaseMediaUrl(){
      return $this-&gt;storeManager-&gt;getStore()-&gt;getBaseUrl(
          \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
      ).'slider/slideritem';
  }
  
  /**
   * @return string
   */
  public function getImageUrl($filePath)
  {
      if (!$this-&gt;mediaDirectory-&gt;isFile($filePath)) {
          $url = $this-&gt;assetRepo-&gt;getUrl(
              "Ultranoir_Slider::images/slideritem/placeholder/thumbnail.jpg"
          );
      } else {
          $url = $this-&gt;storeManager-&gt;getStore()-&gt;getBaseUrl(
                  \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
              ) . $filePath;
      }
  
      return $url;
  }
  
  public function generateThumbnails($fileName, $destinationBaseDir){
  
      $configs = [
          [
              'width' =&gt; 100,
              'height' =&gt; 100
          ],
          [
              'width' =&gt; 200,
              'height' =&gt; 200
          ],
      ];
      // build new filename (most important params)
  
  
      foreach($configs as $config){
          $processor = $this-&gt;imageFactory-&gt;create($destinationBaseDir.$fileName);
          $processor-&gt;keepTransparency(true);
          $processor-&gt;keepAspectRatio(true);
          $processor-&gt;resize($config['width'], $config['height']);
          $path = [
              $destinationBaseDir,
              'cache',
              $this-&gt;storeManager-&gt;getStore()-&gt;getId(),
              'thumbnail'
          ];
          $path[] = "{$config['width']}x{$config['height']}";
          $newFile = implode('/', $path) . $fileName;
          $processor-&gt;save($newFile);
      }
  }
  
  }
  </code></pre>
  
  <p>Thanks in advance for your answers.</p>