protected function _regenerateNewImagesOfShops($dir, $type) { if (!is_dir($dir)) { return false; } $generate_hight_dpi_images = (bool) Configuration::get('PS_HIGHT_DPI'); include_once __DIR__ . '/../../../modules/aphrodinet/classes/AphStoreImage.php'; foreach (AphStoreImage::getAllImages() as $image) { $imageObj = new AphStoreImage($image['id_image']); $existing_img = $dir . $imageObj->getExistingImgPath() . '.jpg'; if (file_exists($existing_img) && filesize($existing_img)) { foreach ($type as $imageType) { unlink($dir . $imageObj->getExistingImgPath() . '-' . stripslashes($imageType['name']) . '.jpg'); //if (!file_exists($dir.$imageObj->getExistingImgPath().'-'.stripslashes($imageType['name']).'.jpg')) { if (!ImageManager::resize($existing_img, $dir . $imageObj->getExistingImgPath() . '-' . stripslashes($imageType['name']) . '.jpg', (int) $imageType['width'], (int) $imageType['height'])) { $this->errors[] = sprintf(Tools::displayError('Original image is corrupt (%s) for product ID %2$d or bad permission on folder'), $existing_img, (int) $imageObj->id_product); } if ($generate_hight_dpi_images) { if (!ImageManager::resize($existing_img, $dir . $imageObj->getExistingImgPath() . '-' . stripslashes($imageType['name']) . '2x.jpg', (int) $imageType['width'] * 2, (int) $imageType['height'] * 2)) { $this->errors[] = sprintf(Tools::displayError('Original image is corrupt (%s) for product ID %2$d or bad permission on folder'), $existing_img, (int) $imageObj->id_product); } } //} } } else { $this->errors[] = sprintf(Tools::displayError('Original image is missing or empty (%1$s) for product ID %2$d'), $existing_img, (int) $imageObj->id_product); } if (time() - $this->start_time > $this->max_execution_time - 4) { // stop 4 seconds before the tiemout, just enough time to process the end of the page on a slow server return 'timeout'; } } return (bool) count($this->errors); }
public function ajaxProcessaddStoreImage() { self::$currentIndex = 'index.php?tab=AdminAphStores'; $store = $this->loadObject(true); $legends = Tools::getValue('legend'); if (!is_array($legends)) { $legends = (array) $legends; } if (!Validate::isLoadedObject($store)) { $files = array(); $files[0]['error'] = Tools::displayError('Cannot add image because store creation failed.'); } $image_uploader = new HelperImageUploader('file'); $image_uploader->setAcceptTypes(array('jpeg', 'gif', 'png', 'jpg'))->setMaxSize($this->max_image_size); $image_uploader->setTemplateDirectory($this->tpl_dir . 'helpers/uploader/'); $files = $image_uploader->process(); foreach ($files as &$file) { $image = new AphStoreImage(); $image->id_shop = (int) $store->id_shop; $image->position = AphStoreImage::getHighestPosition($store->id_shop) + 1; foreach ($legends as $key => $legend) { if (!empty($legend)) { $image->legend[(int) $key] = $legend; } } if (!AphStoreImage::getCover($image->id_shop)) { $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('stores'); $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_shop' => $store->id_shop)); 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'] = false; //$json_shops; @unlink(_PS_TMP_IMG_DIR_ . 'shop_' . (int) $store->id_shop . '.jpg'); @unlink(_PS_TMP_IMG_DIR_ . 'shop_mini_' . (int) $store->id_shop . '_' . $this->context->shop->id . '.jpg'); } } die(Tools::jsonEncode(array($image_uploader->getName() => $files))); }
/** * Copy images from a product to another * * @param int $id_product_old Source product ID * @param bool $id_product_new Destination product ID */ public static function duplicateShopImages($id_product_old, $id_product_new, $combination_images) { $images_types = ImageType::getImagesTypes('stores'); $result = Db::getInstance()->executeS(' SELECT `id_image` FROM `' . _DB_PREFIX_ . 'store_image` WHERE `id_shop` = ' . (int) $id_product_old); foreach ($result as $row) { $image_old = new AphStoreImage($row['id_image']); $image_new = clone $image_old; unset($image_new->id); $image_new->id_shop = (int) $id_product_new; // A new id is generated for the cloned image when calling add() if ($image_new->add()) { $new_path = $image_new->getPathForCreation(); foreach ($images_types as $image_type) { if (file_exists(_PS_STORE_IMG_DIR_ . $image_old->getExistingImgPath() . '-' . $image_type['name'] . '.jpg')) { if (!Configuration::get('PS_LEGACY_IMAGES')) { $image_new->createImgFolder(); } copy(_PS_STORE_IMG_DIR_ . $image_old->getExistingImgPath() . '-' . $image_type['name'] . '.jpg', $new_path . '-' . $image_type['name'] . '.jpg'); if (Configuration::get('WATERMARK_HASH')) { copy(_PS_STORE_IMG_DIR_ . $image_old->getExistingImgPath() . '-' . $image_type['name'] . '-' . Configuration::get('WATERMARK_HASH') . '.jpg', $new_path . '-' . $image_type['name'] . '-' . Configuration::get('WATERMARK_HASH') . '.jpg'); } } } if (file_exists(_PS_STORE_IMG_DIR_ . $image_old->getExistingImgPath() . '.jpg')) { copy(_PS_STORE_IMG_DIR_ . $image_old->getExistingImgPath() . '.jpg', $new_path . '.jpg'); } // Duplicate shop associations for images //$image_new->duplicateShops($id_product_old); } else { return false; } } return AphStoreImage::duplicateAttributeImageAssociations($combination_images); }