public function ajaxProcessuploadThumbnailImages()
 {
     $category = new Category((int) Tools::getValue('id_category'));
     if (isset($_FILES['thumbnail'])) {
         //Get total of image already present in directory
         $files = scandir(_PS_CAT_IMG_DIR_);
         $assigned_keys = array();
         $allowed_keys = array(0, 1, 2);
         foreach ($files as $file) {
             $matches = array();
             if (preg_match('/^' . $category->id . '-([0-9])?_thumb.jpg/i', $file, $matches) === 1) {
                 $assigned_keys[] = (int) $matches[1];
             }
         }
         $available_keys = array_diff($allowed_keys, $assigned_keys);
         $helper = new HelperImageUploader('thumbnail');
         $files = $helper->process();
         $total_errors = array();
         if (count($available_keys) < count($files)) {
             $total_errors['name'] = sprintf(Tools::displayError('An error occurred while uploading the image :'));
             $total_errors['error'] = sprintf(Tools::displayError('You cannot upload more files'));
             die(Tools::jsonEncode(array('thumbnail' => array($total_errors))));
         }
         foreach ($files as $key => &$file) {
             $id = array_shift($available_keys);
             $errors = array();
             // Evaluate the memory required to resize the image: if it's too much, you can't resize it.
             if (isset($file['save_path']) && !ImageManager::checkImageMemoryLimit($file['save_path'])) {
                 $errors[] = Tools::displayError('Due to memory limit restrictions, this image cannot be loaded. Please increase your memory_limit value via your server\'s configuration settings. ');
             }
             // Copy new image
             if (!isset($file['save_path']) || empty($errors) && !ImageManager::resize($file['save_path'], _PS_CAT_IMG_DIR_ . (int) Tools::getValue('id_category') . '-' . $id . '_thumb.jpg')) {
                 $errors[] = Tools::displayError('An error occurred while uploading the image.');
             }
             if (count($errors)) {
                 $total_errors = array_merge($total_errors, $errors);
             }
             if (isset($file['save_path']) && is_file($file['save_path'])) {
                 unlink($file['save_path']);
             }
             //Necesary to prevent hacking
             if (isset($file['save_path'])) {
                 unset($file['save_path']);
             }
             if (isset($file['tmp_name'])) {
                 unset($file['tmp_name']);
             }
             //Add image preview and delete url
             $file['image'] = ImageManager::thumbnail(_PS_CAT_IMG_DIR_ . (int) $category->id . '-' . $id . '_thumb.jpg', $this->context->controller->table . '_' . (int) $category->id . '-' . $id . '_thumb.jpg', 100, 'jpg', true, true);
             $file['delete_url'] = Context::getContext()->link->getAdminLink('AdminBlockCategories') . '&deleteThumb=' . $id . '&id_category=' . (int) $category->id . '&updatecategory';
         }
         if (count($total_errors)) {
             $this->context->controller->errors = array_merge($this->context->controller->errors, $total_errors);
         } else {
             Tools::clearSmartyCache();
         }
         die(Tools::jsonEncode(array('thumbnail' => $files)));
     }
 }
 public function ajaxProcessaddGallaryImage()
 {
     self::$currentIndex = 'index.php?tab=AdminSmartBlog&token=' . Tools::getAdminTokenLite('AdminSmartBlog');
     $smart_blog_post = new SmartBlogPost((int) Tools::getValue('id_smart_blog_post'));
     $legends = Tools::getValue('legend');
     if (!is_array($legends)) {
         $legends = (array) $legends;
     }
     if (!Validate::isLoadedObject($smart_blog_post)) {
         $files = array();
         $files[0]['error'] = Tools::displayError('Cannot add image because product creation failed.');
     }
     $image_uploader = new HelperImageUploader('file');
     $image_uploader->setAcceptTypes(array('jpeg', 'gif', 'png', 'jpg'))->setMaxSize($this->max_image_size);
     $files = $image_uploader->process();
     foreach ($files as &$file) {
         $image = new SmartBlogGallaryImage();
         $image->id_smart_blog_post = (int) $smart_blog_post->id;
         $image->position = SmartBlogGallaryImage::getHighestPosition($smart_blog_post->id) + 1;
         foreach ($legends as $key => $legend) {
             if (!empty($legend)) {
                 $image->legend[(int) $key] = $legend;
             }
         }
         if (($validate = $image->validateFieldsLang(false, true)) !== true) {
             $file['error'] = Tools::displayError($validate);
         }
         if (isset($file['error']) && (!is_numeric($file['error']) || $file['error'] != 0)) {
             continue;
         }
         if (!$image->add()) {
             $file['error'] = Tools::displayError('Error while creating additional image');
         } else {
             if (!($new_path = $image->getPathForCreation())) {
                 $file['error'] = Tools::displayError('An error occurred during new folder creation');
                 continue;
             }
             $error = 0;
             if (!ImageManager::resize($file['save_path'], $new_path . '.' . $image->image_format, null, null, 'jpg', false, $error)) {
                 switch ($error) {
                     case ImageManager::ERROR_FILE_NOT_EXIST:
                         $file['error'] = Tools::displayError('An error occurred while copying image, the file does not exist anymore.');
                         break;
                     case ImageManager::ERROR_FILE_WIDTH:
                         $file['error'] = Tools::displayError('An error occurred while copying image, the file width is 0px.');
                         break;
                     case ImageManager::ERROR_MEMORY_LIMIT:
                         $file['error'] = Tools::displayError('An error occurred while copying image, check your memory limit.');
                         break;
                     default:
                         $file['error'] = Tools::displayError('An error occurred while copying image.');
                         break;
                 }
                 continue;
             } else {
                 $imagesTypes = BlogImageType::GetImageAllType('post');
                 foreach ($imagesTypes as $imageType) {
                     if (!ImageManager::resize($file['save_path'], $new_path . '-' . Tools::stripslashes($imageType['type_name']) . '.' . $image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) {
                         $file['error'] = Tools::displayError('An error occurred while copying image:') . ' ' . Tools::stripslashes($imageType['name']);
                         continue;
                     }
                 }
             }
             unlink($file['save_path']);
             //Necesary to prevent hacking
             unset($file['save_path']);
             Hook::exec('actionWatermark', array('id_smart_blog_post_mage' => $image->id, 'id_smart_blog_post' => $smart_blog_post->id));
             if (!$image->update()) {
                 $file['error'] = Tools::displayError('Error while updating status');
                 continue;
             }
             $file['status'] = 'ok';
             $file['id'] = $image->id;
             $file['position'] = $image->position;
             $file['cover'] = $image->cover;
             $file['legend'] = $image->legend;
             $file['path'] = $image->getExistingImgPath();
             $file['shops'] = array("{$this->context->shop->id}" => true);
             @unlink(_PS_TMP_IMG_DIR_ . 'smart_blog_post_' . (int) $smart_blog_post->id . '.jpg');
             @unlink(_PS_TMP_IMG_DIR_ . 'smart_blog_post__mini_' . (int) $smart_blog_post->id . '_' . $this->context->shop->id . '.jpg');
         }
     }
     die(Tools::jsonEncode(array($image_uploader->getName() => $files)));
 }
 public function ajaxProcessaddProductImage()
 {
     self::$currentIndex = 'index.php?tab=AdminProducts';
     $product = new Product((int) Tools::getValue('id_product'));
     $legends = Tools::getValue('legend');
     if (!is_array($legends)) {
         $legends = (array) $legends;
     }
     if (!Validate::isLoadedObject($product)) {
         $files = array();
         $files[0]['error'] = Tools::displayError('Cannot add image because product creation failed.');
     }
     $image_uploader = new HelperImageUploader('file');
     $image_uploader->setAcceptTypes(array('jpeg', 'gif', 'png', 'jpg'))->setMaxSize($this->max_image_size);
     $files = $image_uploader->process();
     foreach ($files as &$file) {
         $image = new Image();
         $image->id_product = (int) $product->id;
         $image->position = Image::getHighestPosition($product->id) + 1;
         foreach ($legends as $key => $legend) {
             if (!empty($legend)) {
                 $image->legend[(int) $key] = $legend;
             }
         }
         if (!Image::getCover($image->id_product)) {
             $image->cover = 1;
         } else {
             $image->cover = 0;
         }
         if (($validate = $image->validateFieldsLang(false, true)) !== true) {
             $file['error'] = Tools::displayError($validate);
         }
         if (isset($file['error']) && (!is_numeric($file['error']) || $file['error'] != 0)) {
             continue;
         }
         if (!$image->add()) {
             $file['error'] = Tools::displayError('Error while creating additional image');
         } else {
             if (!($new_path = $image->getPathForCreation())) {
                 $file['error'] = Tools::displayError('An error occurred during new folder creation');
                 continue;
             }
             $error = 0;
             if (!ImageManager::resize($file['save_path'], $new_path . '.' . $image->image_format, null, null, 'jpg', false, $error)) {
                 switch ($error) {
                     case ImageManager::ERROR_FILE_NOT_EXIST:
                         $file['error'] = Tools::displayError('An error occurred while copying image, the file does not exist anymore.');
                         break;
                     case ImageManager::ERROR_FILE_WIDTH:
                         $file['error'] = Tools::displayError('An error occurred while copying image, the file width is 0px.');
                         break;
                     case ImageManager::ERROR_MEMORY_LIMIT:
                         $file['error'] = Tools::displayError('An error occurred while copying image, check your memory limit.');
                         break;
                     default:
                         $file['error'] = Tools::displayError('An error occurred while copying image.');
                         break;
                 }
                 continue;
             } else {
                 $imagesTypes = ImageType::getImagesTypes('products');
                 $generate_hight_dpi_images = (bool) Configuration::get('PS_HIGHT_DPI');
                 foreach ($imagesTypes as $imageType) {
                     if (!ImageManager::resize($file['save_path'], $new_path . '-' . stripslashes($imageType['name']) . '.' . $image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) {
                         $file['error'] = Tools::displayError('An error occurred while copying image:') . ' ' . stripslashes($imageType['name']);
                         continue;
                     }
                     if ($generate_hight_dpi_images) {
                         if (!ImageManager::resize($file['save_path'], $new_path . '-' . stripslashes($imageType['name']) . '2x.' . $image->image_format, (int) $imageType['width'] * 2, (int) $imageType['height'] * 2, $image->image_format)) {
                             $file['error'] = Tools::displayError('An error occurred while copying image:') . ' ' . stripslashes($imageType['name']);
                             continue;
                         }
                     }
                 }
             }
             unlink($file['save_path']);
             //Necesary to prevent hacking
             unset($file['save_path']);
             Hook::exec('actionWatermark', array('id_image' => $image->id, 'id_product' => $product->id));
             if (!$image->update()) {
                 $file['error'] = Tools::displayError('Error while updating status');
                 continue;
             }
             // Associate image to shop from context
             $shops = Shop::getContextListShopID();
             $image->associateTo($shops);
             $json_shops = array();
             foreach ($shops as $id_shop) {
                 $json_shops[$id_shop] = true;
             }
             $file['status'] = 'ok';
             $file['id'] = $image->id;
             $file['position'] = $image->position;
             $file['cover'] = $image->cover;
             $file['legend'] = $image->legend;
             $file['path'] = $image->getExistingImgPath();
             $file['shops'] = $json_shops;
             @unlink(_PS_TMP_IMG_DIR_ . 'product_' . (int) $product->id . '.jpg');
             @unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $product->id . '_' . $this->context->shop->id . '.jpg');
         }
     }
     die(Tools::jsonEncode(array($image_uploader->getName() => $files)));
 }
 /**
 
 
     POST TYPE - GALLERY
 */
 public function ajaxProcessAddPostImages()
 {
     $image_dir = _SIMPLEBLOG_GALLERY_DIR_;
     $image_uploader = new HelperImageUploader('file');
     $image_uploader->setAcceptTypes(array('jpeg', 'gif', 'png', 'jpg'));
     $files = $image_uploader->process();
     foreach ($files as &$file) {
         $SimpleBlogPostImage = new SimpleBlogPostImage();
         $SimpleBlogPostImage->id_simpleblog_post = (int) Tools::getValue('id_simpleblog_post');
         $SimpleBlogPostImage->position = SimpleBlogPostImage::getNewLastPosition((int) Tools::getValue('id_simpleblog_post'));
         $SimpleBlogPostImage->add();
         $filenameParts = explode('.', $file['name']);
         $destFiles = array('original' => $image_dir . $SimpleBlogPostImage->id . '-' . $SimpleBlogPostImage->id_simpleblog_post . '-' . Tools::link_rewrite($filenameParts[0]) . '.jpg', 'thumbnail' => $image_dir . $SimpleBlogPostImage->id . '-' . $SimpleBlogPostImage->id_simpleblog_post . '-' . Tools::link_rewrite($filenameParts[0]) . '-thumb.jpg', 'square' => $image_dir . $SimpleBlogPostImage->id . '-' . $SimpleBlogPostImage->id_simpleblog_post . '-' . Tools::link_rewrite($filenameParts[0]) . '-square.jpg', 'wide' => $image_dir . $SimpleBlogPostImage->id . '-' . $SimpleBlogPostImage->id_simpleblog_post . '-' . Tools::link_rewrite($filenameParts[0]) . '-wide.jpg');
         if (!ImageManager::resize($file['save_path'], $destFiles['original'], null, null, 'jpg', false, $error)) {
             switch ($error) {
                 case ImageManager::ERROR_FILE_NOT_EXIST:
                     $file['error'] = Tools::displayError('An error occurred while copying image, the file does not exist anymore.');
                     $SimpleBlogPostImage->delete();
                     break;
                 case ImageManager::ERROR_FILE_WIDTH:
                     $file['error'] = Tools::displayError('An error occurred while copying image, the file width is 0px.');
                     $SimpleBlogPostImage->delete();
                     break;
                 case ImageManager::ERROR_MEMORY_LIMIT:
                     $file['error'] = Tools::displayError('An error occurred while copying image, check your memory limit.');
                     $SimpleBlogPostImage->delete();
                     break;
                 default:
                     $file['error'] = Tools::displayError('An error occurred while copying image.');
                     $SimpleBlogPostImage->delete();
                     break;
             }
             continue;
         } else {
             $SimpleBlogPostImage->image = $SimpleBlogPostImage->id . '-' . $SimpleBlogPostImage->id_simpleblog_post . '-' . Tools::link_rewrite($filenameParts[0]);
             $SimpleBlogPostImage->update();
             $thumbX = Configuration::get('PH_BLOG_THUMB_X');
             $thumbY = Configuration::get('PH_BLOG_THUMB_Y');
             $thumb_wide_X = Configuration::get('PH_BLOG_THUMB_X_WIDE');
             $thumb_wide_Y = Configuration::get('PH_BLOG_THUMB_Y_WIDE');
             $thumbMethod = Configuration::get('PH_BLOG_THUMB_METHOD');
             try {
                 $orig = PhpThumbFactory::create($destFiles['original']);
                 $thumb = PhpThumbFactory::create($destFiles['original']);
                 $square = PhpThumbFactory::create($destFiles['original']);
                 $wide = PhpThumbFactory::create($destFiles['original']);
             } catch (Exception $e) {
                 echo $e;
             }
             if ($thumbMethod == '1') {
                 $thumb->adaptiveResize($thumbX, $thumbY);
                 $square->adaptiveResize(800, 800);
                 $wide->adaptiveResize($thumb_wide_X, $thumb_wide_Y);
             } elseif ($thumbMethod == '2') {
                 $thumb->cropFromCenter($thumbX, $thumbY);
                 $square->cropFromCenter(800, 800);
                 $wide->cropFromCenter($thumb_wide_X, $thumb_wide_Y);
             }
             $orig->save($destFiles['original']);
             $thumb->save($destFiles['thumbnail']);
             $square->save($destFiles['square']);
             $wide->save($destFiles['wide']);
             unlink($file['save_path']);
             unset($file['save_path']);
             $file['status'] = 'ok';
             $file['name'] = $SimpleBlogPostImage->id . '-' . $SimpleBlogPostImage->id_simpleblog_post . '-' . Tools::link_rewrite($filenameParts[0]);
             $file['id'] = $SimpleBlogPostImage->id;
             $file['position'] = $SimpleBlogPostImage->position;
             $file['path'] = $image_dir;
         }
     }
     die(Tools::jsonEncode(array($image_uploader->getName() => $files)));
 }