Jump to content

PHP Code to Regenerate thumbnails by image ID


PHPZeroCode

Recommended Posts

Hello everybody,

I'm a php developer and I'm creating my own php scripts to import products in prestashop

now i would like to use prestashop regenrate thumbnail function to create images thumbnails

 

i found this solution in an older post

 

The function that prestashop uses to rebuild the thumbnails is _regenerateThumbnails() it's in admin/tabs/AdminImages.php.

 

You can include it in your file, and call the function, or just copy the function to the file you wrote.

 

but seems it is for older prestashop version ?

 

any help about this

 

Regards

Link to comment
Share on other sites

There is a protected function in /controllers//admin/AdminImagesController.php

 

protected function _regenerateThumbnails($type = 'all', $deleteOldImages = false)

 

The one in <your admin folder>/tabs/AdminImages.php is private, and cannot be called from outside

 

private function _regenerateThumbnails($type = 'all', $deleteOldImages = false)

 

Hope this helps you in your explorations,

Pascal

Link to comment
Share on other sites

  • 4 weeks later...

Hi PascalVG,

 

I'm very interested on this topic, I'm also working in an import script for a customer of mine and I've it all solved but the thumbnails creation.

 

I've found the file you've written about but I don't know what to do with it. My script runs outside the Prestashop environment.

 

Have you got some example about how an external script can use a Prestashop controller?. I've tried to find it on the web but I haven't found anything.

 

Thanks in advance.

Juan María.

Link to comment
Share on other sites

Hi JuanMaria,

Sorry for late reply, just traveled from Thailand to Holland, so that took a while to settle in again...

 

Don't you use PS at all, and do you want to use just this part of the code? That will be difficult, as it is totally interwoven with PS, database links, objects especially created for PS etc. What you can do is look at the code how they make these thumbnails, and see if you can rewrite it for your own use. Not sure however if other samples are not easier to understand, as, as I said it's totally integrated with PS specific code... See below for alternative:

 

Maybe better to have a look at the php GD library. A php library full off useful tools, for example to create an image of specific size, copying an old image into a new one (with new size), save as jpeg file etc. etc.

http://php.net/manual/en/book.image.php

 

Here is a link to a page that describes thumbnail creation using this GD lib. Give it some attention to get the idea.

http://webcheatsheet.com/php/create_thumbnail_images.php

 

Hope this helps,

pascal

  • Like 1
Link to comment
Share on other sites

Thank you Pascal, now I see that if I cannot succeed in calling the PS functions by myself at least I can generate the thumbsnails in an easy way, those tutorials ar quite good.

 

Anyway, and not only for this problem, I would like to explore the possibility of doing it from inside PS, something similar to what the "searchcron.php" script does. In a few lines it calls an internal PS function.

 

define('_PS_ADMIN_DIR_', getcwd());
include(dirname(__FILE__).'/../config/config.inc.php');
Context::getContext()->shop->setContext(Shop::CONTEXT_ALL);

if (substr(_COOKIE_KEY_, 34, 8) != Tools::getValue('token'))
    die;

ini_set('max_execution_time', 7200);
Search::indexation(Tools::getValue('full'));
if (Tools::getValue('redirect'))
Tools::redirectAdmin($_SERVER['HTTP_REFERER'].'&conf=4');

 

Couldn't I write a similar script which called the _regenerateThumbnails function?.

 

Thank you again.

Link to comment
Share on other sites

  • 4 weeks later...

I did it! :) :)

 

I'll explain what I've done for the record:

 

I've modified the file ./override/controllers/admin/AdminImagesController.php to publish a method to regenerate the thumbnails:

 

class AdminImagesController extends AdminImagesControllerCore
{
	public function regenerateThumbnails($type = 'all', $deleteOldImages = false)
	{
			return parent::_regenerateThumbnails($type, $deleteOldImages);
	}
}

 

 

I've copied the searchcron.php file as thumbnailscron.php and modified it this way:

 

define('_PS_ADMIN_DIR_', getcwd());
include(dirname(__FILE__).'/../config/config.inc.php');
Context::getContext()->shop->setContext(Shop::CONTEXT_ALL);

if (substr(_COOKIE_KEY_, 34, 8) != Tools::getValue('token'))
	die ("bad token");


ini_set('max_execution_time', 7200);
$ic = new AdminImagesController;
$ic->regenerateThumbnails();

 

Now I can use wget to open this url:

http://my_url_admin.tld/admindir/thumbnailscron.php?token=same_token_as_searchcron

 

And, Voila!, Thumbnails regenerated!

 

Greetings from Spain.

Juan María.

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

  • 1 month later...

Hi thank you for your solution...

 

but I try it on PS 1.5.4.1, but there are not /override/controllers/admin/AdminImagesController.php file

 

I can not find this file ... plese help me someone with this regenerate on PS 1.5.4.1 ?

 

Thanks.

Link to comment
Share on other sites

Hi thank you for your solution...

 

but I try it on PS 1.5.4.1, but there are not /override/controllers/admin/AdminImagesController.php file

 

I can not find this file ... plese help me someone with this regenerate on PS 1.5.4.1 ?

 

Thanks.

 

 

you have to create own override file, this is how the whole "override" system works

Link to comment
Share on other sites

you have to create own override file, this is how the whole "override" system works

 

That's right.

 

In my case the default override file was nothing but a skeleton, this was its content:

 

<?php

class AdminImagesController extends AdminImagesControllerCore
{

}

Link to comment
Share on other sites

  • 2 weeks later...
  • 3 months later...

Hi

 

My server overloads when i try to regenerate thumbnails (http://www.prestashop.com/forums/topic/292814-images-problem-regenerate-thumbnails-does-not-work/)

 

Can this solution help me to get it to be done slower?

 

I think not, I understand that the call I'm making in this function is the same that the backoffice makes when you click the regenerate option.

Link to comment
Share on other sites

I think not, I understand that the call I'm making in this function is the same that the backoffice makes when you click the regenerate option.

 

Yes

 

There is a Module that claims to not to use ajax and regenerate 1 by 1, but i am not sure if it will solve the problem

Link to comment
Share on other sites

  • 1 month later...

Hi there !!

 

This is very interesting !!

 

Could the script be executed on SSH console via putty ?

 

If so how could it be done ? Actually I have my site on Gandi and can acces SSH console ^^

 

Thanks in advance.

Regards.

Link to comment
Share on other sites

Could the script be executed on SSH console via putty ?

 

If so how could it be done ? Actually I have my site on Gandi and can acces SSH console ^^

 

 

Hi,

 

I haven't tried it but on this case I think that the safer approach would be calling the URL via wget that, in fact, is what I'm doing in my server whith a cron task.

 

Greetings fron Spain.

Juan María.

Link to comment
Share on other sites

Hi again !

 

I'm following your advice and used Gandi anacron task to execute the script.

 

But I encounter this error in cron logs :

PHP Warning:  include(//../config/config.inc.php): failed to open stream: No such file or directory in /srv/data/web/vhosts/www.mydomain.com/htdocs/manager/thumbnailscron.php on line 4
PHP Warning:  include(): Failed opening '//../config/config.inc.php' for inclusion (include_path='.:/usr/share/php:/srv/data/web/includes') in /srv/data/web/vhosts/www.mydomain.com/htdocs/manager/thumbnailscron.php on line 4
PHP Fatal error:  Class 'Context' not found in /srv/data/web/vhosts/www.mydomain.com/htdocs/manager/thumbnailscron.php on line 5

i think this is due to this line in thumbnaiscron.php :

include(_PS_ADMIN_DIR_.'/../config/config.inc.php');

i replaced it by  :

include(_PS_ADMIN_DIR_.'../config/config.inc.php');

As it seems that PHP Warning: include(//../config/config.inc.php doubles the /...

 

Hoping this would work cause this regenerating issue is very anoying ^^

 

Here's my cron task line of code working with gandi syntax, maybe it could help some of you guys in the same context :

12@hourly 0 imageregenerate php -f /srv/data/web/vhosts/www.mydomain.com/htdocs/manager/thumbnailscron.php token=mytoken

In anacron you have not to put the ? url parameter between the php file and token.

 

comming back soon to tell if it worked ^^

 

BR.

Link to comment
Share on other sites

 

Hi,

 

Actually, you aren't doing it, what I told you and IMHO is the safer approach was using wget, whis way you could simply do it with a line like this:

wget http://www.mydomain.com/manager/thumbnailscron.php?token=mytokenIn 

 

wget passes by the webservice isn't it ?

That's what I'm trying to avoid cause of the max_execution_time settled by the Varnish ^^

Link to comment
Share on other sites

wget passes by the webservice isn't it ?

That's what I'm trying to avoid cause of the max_execution_time settled by the Varnish ^^

 

True didn't remember that, but, since you has control over you server, cannot you change this configuration in your php.ini file?.

 

Ayway, for curiosity's sake, I'm going to make a little testing for direct invocacion. I'll post my findings in a few minutes.

Link to comment
Share on other sites

Hi,

 

Got it woking!

 

This code probably would need a little cleansing but is working fine, I've copied the original script as thumbnailscron_cli.php and I've made a few canges on it.

 

You can use it as: 

php thumbnailscron_cli.php my_admin_directory my_token

And this is the code:


// Addition to the original script
if ($argc != 3) die('Usage: php thumbnailscron_cli.php my_admin_directory my_token');

$myDir=$argv[1];
$myToken=$argv[2];

if (!chdir($myDir)) die($myDir . ' is not a valid directory!'); 
// end of addition

define('_PS_ADMIN_DIR_', getcwd());
include(dirname(__FILE__).'/../config/config.inc.php');
Context::getContext()->shop->setContext(Shop::CONTEXT_ALL);

// if (substr(_COOKIE_KEY_, 34, 8) != Tools::getValue('token'))	// This line is voided!!!
 if (substr(_COOKIE_KEY_, 34, 8) != $myToken)	// We've got the token from the command line
	die ("bad token");


ini_set('max_execution_time', 7200);
$ic = new AdminImagesController;
$ic->regenerateThumbnails();


Also you can notice that there is a max_execution_time setting in the original code that can be changed to suit your needs.

 

 

Hope it helps.

 

Greetings from Spain.

Juan María.

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

Hi ok I've editted the files to test it ^^

 

Thanks !

 

If I adapt your code to mine it will look like :

1@daily 0 imageregenerate php thumbnailscron.php /srv/data/web/vhosts/www.mydomain.com/htdocs/manager token=Jf7TVbk4

Is that looking well ?

 

Greetings from France ^^

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

Hi again !

 

Ok I did it differently using the Gandi SSH console and it worked !!! But...

I have another issue : I'm using the watemark module found here :http://albertosuarezcaballero.es/marca-de-agua-png-redimensionable-watermark-prestashop/

 

But the image regeneration didn't work properly as it applied the default module params :'(

Can you give a clue about this please ?

 

Thanks in advance !

Link to comment
Share on other sites

Hello!

 

Thanks for your help and advice !

I got it working simply changing the default module params to my needs directly in the module php file ^^

Just another question, do you think it's possible to add a mail confirmation when script execution is over ?

Mine worked for about an hour and had to wait this morning to know if it was finished;-)

 

Thanks again!

Link to comment
Share on other sites

Hello!

 

Thanks for your help and advice !

I got it working simply changing the default module params to my needs directly in the module php file ^^

Just another question, do you think it's possible to add a mail confirmation when script execution is over ?

Mine worked for about an hour and had to wait this morning to know if it was finished;-)

 

Thanks again!

 

 

Certainly, you can use the PHP mail() function and send whatevet you want. You simply add this to the script after the last line.

 

Look at http://php.net/manual/en/function.mail.php for more information.

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

Hi again !

 

Just one last question, how to select an specific image size with the cron ?

Like small_image or thickbox_image ?

 

Maybe in the controller override :

public function regenerateThumbnails($type = 'all', $deleteOldImages = false)

Thanks in advance !

 

Best regards !

Link to comment
Share on other sites

Hi again !!

 

The complete controller gives this :

	protected function _regenerateThumbnails($type = 'all', $deleteOldImages = false)
	{
		$this->start_time = time();
		ini_set('max_execution_time', $this->max_execution_time); // ini_set may be disabled, we need the real value
		$this->max_execution_time = (int)ini_get('max_execution_time');
		$languages = Language::getLanguages(false);

		$process =
			array(
				array('type' => 'categories', 'dir' => _PS_CAT_IMG_DIR_),
				array('type' => 'manufacturers', 'dir' => _PS_MANU_IMG_DIR_),
				array('type' => 'suppliers', 'dir' => _PS_SUPP_IMG_DIR_),
				array('type' => 'scenes', 'dir' => _PS_SCENE_IMG_DIR_),
				array('type' => 'products', 'dir' => _PS_PROD_IMG_DIR_),
				array('type' => 'stores', 'dir' => _PS_STORE_IMG_DIR_)
			);

		// Launching generation process
		foreach ($process as $proc)
		{
			if ($type != 'all' && $type != $proc['type'])
				continue;

			// Getting format generation
			$formats = ImageType::getImagesTypes($proc['type']);
			if ($type != 'all')
			{
				$format = strval(Tools::getValue('format_'.$type));
				if ($format != 'all')
					foreach ($formats as $k => $form)
						if ($form['id_image_type'] != $format)
							unset($formats[$k]);
			}

			if ($deleteOldImages)
				$this->_deleteOldImages($proc['dir'], $formats, ($proc['type'] == 'products' ? true : false));
			if (($return = $this->_regenerateNewImages($proc['dir'], $formats, ($proc['type'] == 'products' ? true : false))) === true)
			{
				if (!count($this->errors))
					$this->errors[] = sprintf(Tools::displayError('Cannot write %s images. Please check the folder\'s writing permissions %s.'), $proc['type'], $proc['dir']);
			}
			elseif ($return == 'timeout')
				$this->errors[] = Tools::displayError('Only part of the images have been regenerated. The server timed out before finishing.');
			else
			{
				if ($proc['type'] == 'products')
					if ($this->_regenerateWatermark($proc['dir']) == 'timeout')
						$this->errors[] = Tools::displayError('Server timed out. The watermark may not have been applied to all images.');
				if (!count($this->errors))
					if ($this->_regenerateNoPictureImages($proc['dir'], $formats, $languages))
						$this->errors[] = sprintf(
							Tools::displayError('Cannot write "No picture" image to (%s) images folder. Please check the folder\'s writing permissions.'),
							$proc['type']
						);
			}
		}
		return (count($this->errors) > 0 ? false : true);
	}

Can we call the image size type like this in the override :

<?php

class AdminImagesController extends AdminImagesControllerCore
{
		public function regenerateThumbnails($type = 'products', $format ='my_format', $deleteOldImages = true)
		{
				return parent::_regenerateThumbnails($type, $format, $deleteOldImages);
		}
}
?>

Thanks in advance I'm getting stucked :'(

Link to comment
Share on other sites

<?php

class AdminImagesController extends AdminImagesControllerCore
{
		public function regenerateThumbnails($type = 'products', $format ='my_format', $deleteOldImages = true)
		{
				return parent::_regenerateThumbnails($type, $format, $deleteOldImages);
		}
}
?>

Thanks in advance I'm getting stucked :'(

Can we call the image size type like this in the override :

 

Nope, the function we're calling is: protected function _regenerateThumbnails($type = 'all', $deleteOldImages = false), it has only two parameters, $type and $deleteOldImages so you cannot pass more than two parameters.

 

To do what you want you should take the original _regenerateThumbnails function, copy it with another name and change in this line the format parameter:

if (($return = $this->_regenerateNewImages($proc['dir'], $formats, ($proc['type'] == 'products' ? true : false))) === true)

 

 

Good luck!

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

  • 8 months later...

Thank you for this! It works great. I'm running PS 1.6.0.9.

 

The admin way of doing this is just a joke. It should be setup just like the search cron. I have 1500 products which I don't think is a lot considering I originally had 10K and it was taking a whole day trying to regenerate the images for just 1500 products via the admin. Totally unacceptable. The biggest issue is having to keep hitting the back button and hitting regenerate again to continue for hours. 

 

Now I can run the script via cron and it can take as long as it wants as long as I can do other things while it's running. 

 

Cheers!

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

  • 2 months later...

Hi Juanmaria,

 

I'm trying to add one function to prestashop (1.5.4) fo regenerating thumbnails, just add one more parameter - 'product_id', which will be used by AdminImagesController.php to regenrate thumbnails just for this product_id.

 

Do you have idea, how I need to do it ?

 

Copy the _regenerateThumbnails function and call it once the product_id is given ? And what to change in that function to regenerate thumbnails just for the product_id ?

 

It's acceptabble to have image_id instead product_id for me, thank you very much for any help !

 

Baso

Link to comment
Share on other sites

Hi Baso,

 

I haven't worked with PS in the latest months, gladly my customer's installations are quite stable, and I'm starting to forget the little I knew about PS working innards.

 

As much as I remember, the original function processed or all products or only the ones which were marked as pending. I think there wasn't a function which processed a single product, so this is not a trivial problem.

 

If I had to do it I would locate the function which selects the pending products and copy it to a new function with a single-product select and call this one instead of the original one, but for this you might have to write an override much more complex than the one I wrote.

 

I'm sorry for not being of more help, I wish you luck and, if you succeed, don't forget sharing your solution with us!.

 

Greetings from Spain.

Juan María.

Link to comment
Share on other sites

Hi

 

Thank you for your reply.

 

During the image conversion I've got an error:

 

Original image is corrupt (/img/p/3/1/9/319.jpg) or bad permission on folder

 

Since I have this eshop on local (Win7) computer, I tried to set everyone Full control permission. I tried to save this original .jpg with different JPEG options, nothing helps.

 

The problem was the resoultion of the image (about 5000x3000). Manually added pic to the product was converted to thumbnails correctly, but the regenration process for all images stopped here with error (above).

 

I'm using some watermark module (to use PNG as watermark).

 

I found, I can change function in classes/Image.php

 

public static function getAllImages()

 

adding a line 

 

         WHERE `id_product` = 75 
 
helps me for ma case (to renegerate thumbnails) manually just for this product.
 
It's easy to change it to image id instead of product id to regenerate just one product image.
 
Anyway, itsn't helped to my problem, error occurs again. I need to resize original jpg for about half size and everything seems to be solved.
 
Greetings from Slovakia
Baso
Link to comment
Share on other sites

 

The problem was the resoultion of the image (about 5000x3000). Manually added pic to the product was converted to thumbnails correctly, but the regenration process for all images stopped here with error (above).

 

Hi Baso,

 

Have you tried giving more memory/resources to your PHP module?.

 

It sounds like the graphic module crashes when trying to manipulate big images, you could also look into the PHP/Apache logs for some clues about those crashes.

 

Greetings from Spain.

Juan María.

Link to comment
Share on other sites

  • 2 months later...

Thanks for the useful information.

I would like to ask whether AdminImagesController.php in override/controllers/admin/ contains following lines only ?!

I still have original AdminImagesController.php in controllers/admin/ 

 

Many thanks

 

<?php

class AdminImagesController extends AdminImagesControllerCore
{
        public function regenerateThumbnails($type = 'products', $format ='my_format', $deleteOldImages = true)
        {
                return parent::_regenerateThumbnails($type, $format, $deleteOldImages);
        }
}
?>

Link to comment
Share on other sites

  • 1 year later...

Hi,

 

When I wrote: Token=same_token_as_searchcron

 

I meant to follow the instructions to run searchcron and use this same token on this call. ¿What are you using as token?.

 

Question is - how to get that token in 1.6? 

Link to comment
Share on other sites

  • 2 years later...
  • 1 month later...
  • 3 months later...
  • 6 months later...

I recently updated to PS 1.7.6.4, the code (below) was working in PS 1.7.6.1 before. After I upgraded to 1.7.6.4, I tried again by creating a file in override/controllers/admin

class AdminImagesController extends AdminImagesControllerCore
{
    public function regenerateThumbnails($type = 'all', $deleteOldImages = false)
    {
            return parent::_regenerateThumbnails($type, $deleteOldImages);
    }
}

?>

Once I put them In override, I got server error just by clicking " image setting" in BO. After I delete the file from the override directory. NO server error. I think I used PHP 7.2 after upgrade to PS 1.7.6.4.

Anyone have clue ?!

 

Thx

Link to comment
Share on other sites

  • 3 weeks later...

I made some modifications to the original code and now it's a standalone script that you can run from the root of your PrestaShop.

I have tested the code in PrestaShop v1.7.5.2

When you run the code, you need to edit two lines.

Line 4 > Comment this line. It is an easy precaution not to leave the script in the open.

Line 32 > Your backend folder name. You need to change 'administration' to the correct name.

Now regenerateThumbnails() regenerates all the thumbnails and deletes the oldies. If you want to skip some, add a string with the type to skip. ('categories', 'manufacturers', 'suppliers', 'products' or 'stores')

Also i added a log to track the progress of the script.
 

thumbs.php

Link to comment
Share on other sites

On 4/14/2020 at 2:54 AM, _SMIPro_ said:

I made some modifications to the original code and now it's a standalone script that you can run from the root of your PrestaShop.

I have tested the code in PrestaShop v1.7.5.2

When you run the code, you need to edit two lines.

Line 4 > Comment this line. It is an easy precaution not to leave the script in the open.

Line 32 > Your backend folder name. You need to change 'administration' to the correct name.

Now regenerateThumbnails() regenerates all the thumbnails and deletes the oldies. If you want to skip some, add a string with the type to skip. ('categories', 'manufacturers', 'suppliers', 'products' or 'stores')

Also i added a log to track the progress of the script.
 

thumbs.php 2 kB · 2 downloads

Wonderful. 

Tried but got errors,  and for product is pointing to "watermark" module. Is it compatible to watermark module ?!  Thanks in advance !

[15-Apr-2020 10:07:30 Asia/Hong_Kong] Start - products
[15-Apr-2020 10:07:30 Asia/Hong_Kong] PHP Notice:  Array to string conversion in /home/XXXXXX/public_html/prshopupd/modules/watermark/watermark.php on line 375
[15-Apr-2020 10:07:35 Asia/Hong_Kong] End - products
[15-Apr-2020 10:07:35 Asia/Hong_Kong] Errores
[15-Apr-2020 10:07:35 Asia/Hong_Kong] Array
(

[15-Apr-2020 10:07:35 Asia/Hong_Kong] Start - stores
[15-Apr-2020 10:07:35 Asia/Hong_Kong] End - stores
[15-Apr-2020 10:07:35 Asia/Hong_Kong] Errores
[15-Apr-2020 10:07:35 Asia/Hong_Kong] Array
(
)

[15-Apr-2020 10:12:29 Asia/Hong_Kong] Start - categories
[15-Apr-2020 10:12:29 Asia/Hong_Kong] End - categories
[15-Apr-2020 10:12:29 Asia/Hong_Kong] Errores
[15-Apr-2020 10:12:29 Asia/Hong_Kong] Array

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

I have no idea, we don't work with that module, sorry.

But the thumbnails are generated correctly, at least that's what your log says. Do you have an image compression module to contrast a report? These modules usually give you statistics on image compression and saved space. It is a way to check if you have generated images.

Link to comment
Share on other sites

17 hours ago, _SMIPro_ said:

I have no idea, we don't work with that module, sorry.

But the thumbnails are generated correctly, at least that's what your log says. Do you have an image compression module to contrast a report? These modules usually give you statistics on image compression and saved space. It is a way to check if you have generated images.

No compression module here. But, I thought "Errores" means something wrong....

Link to comment
Share on other sites

10 hours ago, dtwfung said:

No compression module here. But, I thought "Errores" means something wrong....

Doh! I didn't translate that. The log is a report, if the "Errores" array is empty means there is no errors during the process

You can look at the line 67-68 Only writes the log to report if there is an error. You can comment those lines or change them with this code.

if( count($this->errors) > 0 ){
	log_write('Errores');
	log_write($this->errors);
}

It'll prevent from writing the log when there are not errors.

I hope this solves your question.

Link to comment
Share on other sites

On 4/16/2020 at 7:23 PM, _SMIPro_ said:

Doh! I didn't translate that. The log is a report, if the "Errores" array is empty means there is no errors during the process

You can look at the line 67-68 Only writes the log to report if there is an error. You can comment those lines or change them with this code.


if( count($this->errors) > 0 ){
	log_write('Errores');
	log_write($this->errors);
}

It'll prevent from writing the log when there are not errors.

I hope this solves your question.

I see.  but the php does not regenerate.

I tried delete a image file in img/p/.../26-small_default.jpg and regenerate, This deleted file did not regenerate.

I tried to delete all *.jpg under img/p/2/6 EXCEPT 26.jpg, those files did not regenerate either.

When I regenerate with BO -> image setting -> REGENERATE THUMBNAILS, all deleted files were regenerated.

Anyway, Many thanks for your assistant !

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

  • 2 weeks later...

Hi,

Im new in prestashop, I have created my module to import data from my system to prestashop, but i have some questions about images:

- when I will upload a image I need set the tables ps_image_shop, ps_image and ps_image_lang.

- when the productid, move the image to prestashop. To do this. I will use commands of upload files by FTP with PHP, but I dont know how to name the folders and files.

- when I finished this, I want create a crontab to create the thumbnails. I see the code how extends AdminImagesControllerCore, but I do not know how use it.

If somebody can help me if my process it is correct and how use the thumbnails

Many thanks for your assitance

Link to comment
Share on other sites

On 5/1/2020 at 6:49 PM, karpoj23 said:

Hi,

Im new in prestashop, I have created my module to import data from my system to prestashop, but i have some questions about images:

- when I will upload a image I need set the tables ps_image_shop, ps_image and ps_image_lang.

- when the productid, move the image to prestashop. To do this. I will use commands of upload files by FTP with PHP, but I dont know how to name the folders and files.

- when I finished this, I want create a crontab to create the thumbnails. I see the code how extends AdminImagesControllerCore, but I do not know how use it.

If somebody can help me if my process it is correct and how use the thumbnails

Many thanks for your assitance

 

You can generate thumbnail at back office. this PHP is shortcut only, and you must have product image already exist in img/p folder ...

Link to comment
Share on other sites

14 hours ago, dtwfung said:

 

You can generate thumbnail at back office. this PHP is shortcut only, and you must have product image already exist in img/p folder ...

Thanks, I have uploaded the image in the correct folder.

How can I use a custom function from Core? I have created an override from class AdminImagesControllerCore:

 

<?php
require_once "../../controllers/admin/AdminImagesController.php";

class AdminImagesController extends AdminImagesControllerCore
{
    public function customThumbnails($type = 'all', $deleteOldImages = false, $directory ='')
    {
        $this->start_time = time();
        ini_set('max_execution_time', $this->max_execution_time); // ini_set may be disabled, we need the real value
        $this->max_execution_time = (int) ini_get('max_execution_time');
        $languages = Language::getLanguages(false);

		// Getting format generation
		$formats = ImageType::getImagesTypes($type);
		if ($type != 'all') {
			$format = (string) (Tools::getValue('format_' . $type));
			if ($format != 'all') {
				foreach ($formats as $k => $form) {
					if ($form['id_image_type'] != $format) {
						unset($formats[$k]);
					}
				}
			}
		}

		if ($deleteOldImages) {
			$this->_deleteOldImages($directory, $formats, ($type == 'products' ? true : false));
		}
		if (($return = $this->_regenerateNewImages($directory, $formats, ($type == 'products' ? true : false))) === true) {
			if (!count($this->errors)) {
				$this->errors[] = $this->trans('Cannot write images for this type: %1$s. Please check the %2$s folder\'s writing permissions.', array($type, $directory), 'Admin.Design.Notification');
			}
		} elseif ($return == 'timeout') {
			$this->errors[] = $this->trans('Only part of the images have been regenerated. The server timed out before finishing.', array(), 'Admin.Design.Notification');
		} else {
			if ($type == 'products') {
				if ($this->_regenerateWatermark($directory, $formats) == 'timeout') {
					$this->errors[] = $this->trans('Server timed out. The watermark may not have been applied to all images.', array(), 'Admin.Design.Notification');
				}
			}
			if (!count($this->errors)) {
				if ($this->_regenerateNoPictureImages($directory, $formats, $languages)) {
					$this->errors[] = $this->trans('Cannot write "No picture" image to %s images folder. Please check the folder\'s writing permissions.', array($type), 'Admin.Design.Notification');
				}
			}
		}
        

        return count($this->errors) > 0 ? false : true;
    }
}
?>

I have removed  the file of cache in /var/cache/dev and /var/cache/prod.

Later in my file PHP in folder/file.php

<?php

require_once "../override/controllers/admin/AdminImagesController.php";

?>

But it is not working, how can I use the function customThumbnails?

many thanks

 

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