Jump to content

[RESOLU] import image produits depuis un module sur un produit


Recommended Posts

Bonjour a tous,

je cherche depuis un petit moment mais la je commence à bloquer.

Je suis en train de faire un module qui "detoure" des images et je souhaiterais qu'il créer un produit à la volée (ça c'est bon) le seul problème c'est que je trouve pas comment importer l'image du produit.

Je me suis inspiré du 

protected static function copyImg($id_entity, $id_image = null, $url, $entity = 'products', $regenerate = true)
	{
		$tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
		$watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));

		switch ($entity)
		{
			default:
			case 'products':
				$image_obj = new Image($id_image);
				$path = $image_obj->getPathForCreation();
			break;
			case 'categories':
				$path = _PS_CAT_IMG_DIR_.(int)$id_entity;
			break;
			case 'manufacturers':
				$path = _PS_MANU_IMG_DIR_.(int)$id_entity;
			break;
			case 'suppliers':
				$path = _PS_SUPP_IMG_DIR_.(int)$id_entity;
			break;
		}
		$url = str_replace(' ', '%20', trim($url));

		// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
		if (!ImageManager::checkImageMemoryLimit($url))
			return false;

		// 'file_exists' doesn't work on distant file, and getimagesize makes the import slower.
		// Just hide the warning, the processing will be the same.
		if (Tools::copy($url, $tmpfile))
		{
			ImageManager::resize($tmpfile, $path.'.jpg');
			$images_types = ImageType::getImagesTypes($entity);

			if ($regenerate)
				foreach ($images_types as $image_type)
				{
					ImageManager::resize($tmpfile, $path.'-'.stripslashes($image_type['name']).'.jpg', $image_type['width'], $image_type['height']);
					if (in_array($image_type['id_image_type'], $watermark_types))
						Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
				}
		}
		else
		{
			unlink($tmpfile);
			return false;
		}
		unlink($tmpfile);
		return true;
	} 

mais ça ne marche pas.
si quelqu'un à deja eu ça ça m'aiderai pas mal.

merci d'avance!

Edited by guigui23 (see edit history)
Link to comment
Share on other sites

Bonjour oui j'essayes mais je ne trouve pas ou se passe le moment ou je défini une image et je le transforme et défini pour le produit qui a pour ID x;
j'ai regardé au niveau du ImageManager mais rien trouvé de particulier non plus.

le probleme est que si on instencie l'objet Image sans id dans le __contruct() 

$image_obj = new Image();
$path = $image_obj->getPathForCreation(); // $path == ''

la $path est vide est donc je peux rien faire avec.

Si quelqu'un à un petit exemple de code, ça ne ferait gagner du temps.

Merci encore

Edited by guigui23 (see edit history)
Link to comment
Share on other sites

J'ai trouvé !

voila mon code si ça peut aider quelqu'un !


public static function copyImg($id_entity, $id_image = null ,$url, $entity = 'products'){
$tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
$watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));
$image_obj = new Image($id_image);
$image_obj->cover = true;
$image_obj->id_product = $id_entity;
$languages = Language::getLanguages();
        foreach ($languages as $language)
            $image_obj->legend[$language['id_lang']] = 'Click to view';
 
 
$image_obj->add();
$tmpName = tempnam(_PS_IMG_DIR_, 'PS');
        Tools::copy($url,$tmpName);
 
$new_path = $image_obj->getPathForCreation();
ImageManager::resize($tmpName, $new_path.'.'.$image_obj->image_format);
$imagesTypes = ImageType::getImagesTypes($entity);
foreach ($imagesTypes as $imageType)
   ImageManager::resize($tmpName, $new_path.'-'.stripslashes($imageType['name']).'.'.$image_obj->image_format, $imageType['width'], $imageType['height'], $image_obj->image_format);
 
 
}

Merci encore @DevNet

Edited by guigui23 (see edit history)
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...