Jump to content

Multiple images in a single adminController


Snamor

Recommended Posts

Hi!

 

I'm currently developing a custom adminController and I'm facing a problem. I need to up to 3 images per instance, but i'm unable to do that.

 

This is what i've got, but i can't seem to find a way to do it for multiple images:

 

$this->fieldImageSettings = array(
		'name' => 'image',
		'dir' => 'bs'
	);

 

$image = ImageManager::thumbnail(_PS_IMG_DIR_.'bs/'.$obj->id.'.png', $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true);


$this->fields_value = array(
  'image' => $image ? $image : false,
  'size' => $image ? filesize(_PS_IMG_DIR_.'bs/'.$obj->id.'.png') / 1000 : false,
 );

 

Any clues?

 

Thanks in advance!

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

  • 3 months later...

Hi!

 

Alright, this was a bit tricky. It was our first time with prestashop, so probably this is not the best solution, but it seems to work. For this to work you must create the 'bs' folder inside the images folder. You can change 'bs' to whatever other name you want. Inside bs you will have a folder for each instance and inside this folder all the images of it.

 

 

// /controllers/admin/CustomAdminController.php
protected function postImage($id)
{
	$id_instance = (int)Tools::getValue('id_instance');
	$this->fieldImageSettings['dir'] = 'bs/'.$id_instance;

	$this->createImgFolder($id_instance);

	$img_id = CustomObjectModel::nextImage($id_instance);

	$ret = parent::postImage($img_id);
	if ($id_instance && isset($_FILES) && count($_FILES) && file_exists(_PS_IMG_DIR_.'bs/'.$id.'/'.$img_id.'.jpg'))
	{
		$images_types = ImageType::getImagesTypes('stores');
		foreach ($images_types as $k => $image_type)
		{
			ImageManager::resize(_PS_IMG_DIR_.'bs/'.$id_instance.'/'.$img_id.'.jpg',
				_PS_IMG_DIR_.'bs/'.$id_instance.'/'.$img_id.'-'.stripslashes($image_type['name']).'.jpg',
				(int)$image_type['width'], (int)$image_type['height']
			);
		}
	}
	return $ret;
}

public function createImgFolder($id)
{
	if (!$id)
		return false;

	$source_index = _PS_IMG_DIR_.'bs/index.php';

	if (!is_dir(_PS_IMG_DIR_.'bs/'.$id))
	{
		// Apparently sometimes mkdir cannot set the rights, and sometimes chmod can't. Trying both.
		$success = @mkdir(_PS_IMG_DIR_.'bs/'.$id,0775, true);
		$chmod = @chmod(_PS_IMG_DIR_.'bs/'.$id,0775);

		// Create an index.php file in the new folder
		if (($success || $chmod)
			&& !file_exists(_PS_IMG_DIR_.'bs/'.$id.'/index.php')
			&& file_exists($source_index))
			return @copy($source_index, _PS_IMG_DIR_.'bs/'.$id.'/index.php');
	}
	return true;
}
public function renderForm(){
	//... Your render form code. Add this at the end of the function

	if (!($obj = $this->loadObject(true)))
		return;

	if(!empty($obj->id))
		$this->fields_form['rightCols']['input']['images'] = $obj->getImages($obj->id,true,$this->imageType);

	return parent::renderForm();
}

 

Also, you will need these functions that we put in our ObjectModel

// classes/CustomObjectModel.php
public function getImages($id_instance,$thumbs = false,$imageType="jpg"){
	$aImatges = array();
	if ($handle = opendir(_PS_IMG_DIR_.'bs/'.$id_instance)) {
		while (false !== ($entry = readdir($handle))) {
			if(preg_match("/([0-9]+)\.(jpg|jpeg|png|gif)/i",$entry,$matches)){
				$aImatges[(int)$matches[1]]['img_name']=$entry;
				if($thumbs){
					$aImatges[(int)$matches[1]]['thumb'] = ImageManager::thumbnail(_PS_IMG_DIR_.'bs/'.$id_instance.'/'.(int)$matches[1].'.jpg', $this->table.'_'.$id_instance.'_'.(int)$matches[1].'.'.$imageType, 100, $imageType, true);
				}
			}
		}
		closedir($handle);
	}
	return $aImatges;
}

public function nextImage($id_instance){
	$aImatges = CustomObjectModel::getImages($id_instance);
	$i=1;
	foreach($aImatges as $k=>$v){
		if($k>$i) break;
		$i++;
	}
	return $i;
}

 

I think that this is all the code you need, but probably I missed something. With it every time you save the object you can save the image that you added to the image field.

 

Hope this helps!

Link to comment
Share on other sites

  • 5 months later...

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...