Jump to content

Adding image resize to Module Admin Controller


Recommended Posts

Hello,

I have a module with file input which takes image and puts it into img/modulename directory and later uses reference to that file to show it on frontend.

I want to add image resize to half size of original during upload, but I have very difficult time figuring out this upload works. I found AdminPosslideshowController and I think it's responsible for this. Another thing related could be \views\templates\admin\helpers\uploader\simple.tpl, because main module file mendtions it:

$this->admin_tpl_path = _PS_MODULE_DIR_ . $this->name . '/views/templates/admin/';

But I don't see much inside controlles except 

$this->fieldImageSettings = array(
'name' => 'image',
'dir' => 'blockslideshow'
);
$this->imageType = "jpg";

and

array(
'type' => 'file',
'label' => $this->l('Image:'),
'name' => 'image',
'desc' => $this->l('Upload  a banner from your computer.')
),

which is inside RenderForm method of this controller.

 

Is this possible that prestashop makes from this bits a whole picture uploader which knows where to put the file and how to call it?

 

I found this topic on stackoverflow, but I still don't know how to access the uploaded file to manipulate it.

Any tips in the right direction are appreciated. I couldn't find any learning sources about this matter.

regards
Mikołaj

posslideshow_controller&admin_tpl.zip

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

Looks like yes, this two bits are responsible for image upload and AdminController is able to do whole image upload from this.

The other part pointing to tpl directory could be misleading.

 

I made some progress. Here is my function:

protected function afterImageUpload()
{
$id = (int)Tools::getValue($this->identifier);
$object = new $this->className($id);
$picture = _PS_IMG_DIR_.$this->fieldImageSettings['dir'].'/'.$object->id.'.'.$this->imageType;
list($width, $height) = getimagesize($picture);


if(!ImageManager::resize($picture, _PS_IMG_DIR_.$this->fieldImageSettings['dir'].'/'.$object->id.'_resized'.'.'.$this->imageType, (int)$width/2, (int)$height/2))
return false;


return parent::afterImageUpload();
}

I wonder if this could be done via afterAdd($object).

@EDIT: Yes, it can. Like this:

protected function afterAdd($object)
{
$picture = _PS_IMG_DIR_.$this->fieldImageSettings['dir'].'/'.$object->id.'.'.$this->imageType;
list($width, $height) = getimagesize($picture);


if(!ImageManager::resize($picture, _PS_IMG_DIR_.$this->fieldImageSettings['dir'].'/'.$object->id.'_resized'.'.'.$this->imageType, (int)$width/2, (int)$height/2))
return false;
return true;
}

But I have to remember about afterUpdate($object) also:

protected function afterUpdate($object)
{ if($this->afterAdd($object))
return true;
}

I love this forum. Every time I encounter a hard problem, I write it down here and the next day solution comes to me. I hope this is not spamming and it could be useful for someone with similiar problems.

 

@EDIT2: First solution is better. I experienced some weird behaviour when using afterAdd() and afterUpdate(), for example resizing wrong picture in directory. I'm too lazy to check why and afterImageUpload() is flawless.

 

Thanks khejit68, topic is SOLVED.

Edited by khejit68 (see edit history)
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...