/**
  * @param $element
  * @param $field
  * @param $_data
  *
  * @return JRegistry|null
  */
 protected function createImages($element, $field, $_data)
 {
     jimport('joomla.filesystem.file');
     $app = JFactory::getApplication();
     if (($image = $_data->get('image')) && file_exists(JPath::clean(JPATH_ROOT . '/' . $image))) {
         $_data = new JRegistry($_data->getProperties(true));
         $scaleImage = (int) $field->params->def('type.scale', $this->params->get('scale', 0));
         $createThumb = (bool) $field->params->get('type.create_thumb');
         $scaleThumb = (int) $field->params->def('type.scale_thumb', $this->params->get('scale_thumb', 0));
         if ($scaleImage || $createThumb && $scaleThumb) {
             $isCreated = true;
             $isCreatedThumb = true;
             $folder = FieldsandfiltersImage::getCacheFolder() . '/' . $field->id . '/';
             $imageInfo = $this->prepareImageInfo($field, $element, $image, false, $scaleImage);
             if ($scaleImage) {
                 $src = JPath::clean($folder . $imageInfo->name);
                 $srcOld = $_data->get('src', false);
                 if ($src != $srcOld || !file_exists(JPath::clean(JPATH_ROOT . '/' . $src))) {
                     try {
                         if ($srcOld && file_exists($srcOld = JPath::clean(JPATH_ROOT . '/' . $srcOld))) {
                             JFile::delete($srcOld);
                         }
                         if ($src = FieldsandfiltersImage::createImage($field->name, $imageInfo)) {
                             $_data->set('src', $src);
                             $app->enqueueMessage(JText::sprintf('COM_FIELDSANDFILTERS_SUCCESS_CREATE_IMAGE', $field->name));
                         } else {
                             throw new RuntimeException(JText::sprintf('COM_FIELDSANDFILTERS_ERROR_NOT_CREATE_IMAGE', $field->name));
                         }
                     } catch (Exception $e) {
                         $isCreated = false;
                         $_data->set('src', '');
                         $app->enqueueMessage($e->getMessage(), 'error');
                     }
                 }
             }
             if ($createThumb && $scaleThumb) {
                 $src = JPath::clean($folder . 'thumbs/' . $imageInfo->name);
                 $srcOld = $_data->get('src_thumb', false);
                 if ($scaleThumb && ($src != $srcOld || !file_exists(JPath::clean(JPATH_ROOT . '/' . $src)))) {
                     $imageInfo = $this->prepareImageInfo($field, $element, $image, $imageInfo->name, $scaleThumb, 'thumb');
                     try {
                         if ($srcOld && file_exists($srcOld = JPath::clean(JPATH_ROOT . '/' . $srcOld))) {
                             JFile::delete($srcOld);
                         }
                         if ($src = FieldsandfiltersImage::createImage($field->name . ' Thumbs', $imageInfo)) {
                             $_data->set('src_thumb', $src);
                             $app->enqueueMessage(JText::sprintf('COM_FIELDSANDFILTERS_SUCCESS_CREATE_IMAGE', $field->name . ' Thumb'));
                         } else {
                             throw new RuntimeException(JText::sprintf('COM_FIELDSANDFILTERS_ERROR_NOT_CREATE_IMAGE', $field->name . ' Thumb'));
                         }
                     } catch (Exception $e) {
                         $isCreatedThumb = false;
                         $_data->set('src_thumb', '');
                         $app->enqueueMessage($e->getMessage(), 'error');
                     }
                 }
             }
             if (!$isCreated && !($createThumb && $isCreatedThumb)) {
                 $_data = null;
             }
         }
         unset($imageInfo);
     } else {
         if (($src = $_data->get('src')) && file_exists(JPath::clean(JPATH_ROOT . '/' . $src))) {
             // delete image
             if (!JFile::delete(JPATH_ROOT . '/' . $src)) {
                 $app->enqueueMessage(JText::sprintf('PLG_FAF_TS_IE_SUCCESS_DELETE_IMAGE', $field->name, $src), 'error');
             }
         }
         if (($src = $_data->get('src_thumb')) && file_exists(JPath::clean(JPATH_ROOT . '/' . $src))) {
             // delete thumb
             if (!JFile::delete(JPATH_ROOT . '/' . $src)) {
                 $app->enqueueMessage(JText::sprintf('PLG_FAF_TS_IE_SUCCESS_DELETE_IMAGE', $field->name . ' Thumb', $src), 'error');
             }
         }
         $_data = null;
     }
     return $_data;
 }