News:

Support the VirtueMart project and become a member

Main Menu

Can We override / Extend /helpers/img2thumb.php?

Started by mowlman, March 19, 2015, 16:34:49 PM

Previous topic - Next topic

mowlman

I would like to set the thumb image quality to 87  i.e. line 395: $quality = 87;
File path: administrator/components/com_virtuemart/helpers/img2thumb.php

I do not want to lose my edit when I update. Can we override , extend,  or as suggested in a post to include it within the config file?

Thank you very much, have not moved to VM3 yet, still using 2.6.10.


Quote
Re: Dynamic creation of thumbnail > file size TOO BIG!
« Reply #3 on: August 20, 2013, 21:10:37 pm »

    Quote

Ive found where to "touch" to select quality compression of the thumbnails!
Go inside: administrator/components/com_virtuemart/helpers/img2thumb.php
In line 318 (in my case) you can find this code:
Code: [Select]

return imagejpeg($new_img, $fileout, 100);


You can chage 100 to the desired value! in my case 85, and i get a 5kB thumbnail with almost imperceptible quality loss. Thats 80% file size reduction!
Im trying to find out how to make and override of this file so i dont have to modify the core. Because i will loss this modification when i update.

Dont you think it would be nice (and very easy to implement) to have this parameter in the configuration page so the user can choose thumb compression?
Think of an 80% reduction in size of images: virtuemart/category/related products views are some pages that load pleanty of thumbnails!

Studio 42

#1
virtuemart use :
Quoteif (!class_exists('Img2Thumb')) require(VMPATH_ADMIN.DS.'helpers'.DS.'img2thumb.php');
this mean it's very easy to overide.
Add a system plugin, and declare it here for eg :
Quoterequire('mypluginpath/helpers/img2thumb.php');

YOu must know that DS is not needed seens php5 but you must declare it for Virtuemart to work. or to load the virtuemart config or your have a risk this is not working.

you cannot extend it, because virtuemart does not know tha new classname you extend

Milbo

I can extend it, he just must create his own "Img2Thumb" class earlier.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

mowlman

Hello Studio42 and Milbo,  Thank you for taking the time for this.

I have a question with my possible solution. Making it so image quality displays within VM Config beneath where the image height is set.

First a QUESTION:
To read in the Image quality value from the VM Config file, I see it is being done for img_height from within public function createThumb around line 131.
Path: \administrator\components\com_virtuemart\helpers\image.php

Can we do the same for img_quality
           After this around line 131:    if(empty($height)) $height = VmConfig::get('img_height', 90);
          INSERT:  if(empty($quality)) $quality = VmConfig::get('img_quality', 87);

****  OR ****
Can we just add if(empty($quality)) $quality = VmConfig::get('img_quality', 87);
Within \img2thumb.php  itself
Path: administrator/components/com_virtuemart/helpers/img2thumb.php

So Within private function NewImgSave Replace hard coded value with the get from VMConfig line?

Replace $quality = 89;
With: if(empty($quality)) $quality = VmConfig::get('img_quality', 87);

****************************** END of Question ************************************************

Code change for adding image Quality to VM Config:
Add image quality language fields:

COM_VIRTUEMART_ADMIN_CFG_THUMBNAIL_QUALITY_TIP
COM_VIRTUEMART_ADMIN_CFG_THUMBNAIL_QUALITY

Code:
COM_VIRTUEMART_ADMIN_CFG_THUMBNAIL_QUALITY="Thumbnail Quality"
COM_VIRTUEMART_ADMIN_CFG_THUMBNAIL_QUALITY_TIP="The <strong>quality</strong> of the resized Thumbnail Image."


Path:
/administrator/components/com_virtuemart/language/en-GB/en-GB.com_virtuemart_config.ini
Around line 304

Add Image quality fields and language code to Config template:\
Code:

<tr>
      <td class="key">
<span class="hasTip" title="<?php echo JText::_('COM_VIRTUEMART_ADMIN_CFG_THUMBNAIL_QUALITY_TIP'); ?>">
<?php echo JText::_('COM_VIRTUEMART_ADMIN_CFG_THUMBNAIL_QUALITY'); ?>
</span>
     </td>
     <td>
           <input type="text" name="img_quality" class="inputbox" value="<?php echo VmConfig::get('img_quality'?>"/>
     </td>
</tr>


Path:
/administrator/components/com_virtuemart/views/config/tmpl/default_templates.php
Around line 324

Also around line 340
AFTER Line    <input type="hidden" name="img_height" value="<?php echo  VmConfig::get('img_height', 90); ?>"/>
INSERT:        <input type="hidden" name="img_quality" value="<?php echo  VmConfig::get('img_quality', 87); ?>"/>

***************************
The following files are attached within the Zip:
\administrator\components\com_virtuemart\helpers\image.php
\administrator/components/com_virtuemart/helpers/img2thumb.php
/administrator/components/com_virtuemart/language/en-GB/en-GB.com_virtuemart_config.ini
/administrator/components/com_virtuemart/views/config/tmpl/default_templates.php

Studio 42

I'm not sure your code is complet.
YOu have PNG quality and JPG quality, i think your code is not complete for the 2 cases

Milbo

and actually if you use something like this VmConfig::get('img_quality', 87);

and there is no option in the vm config for it, then it is an hidden option. Please read here http://forum.virtuemart.net/index.php?topic=128956.0

I added now in img2thumb.php at line 392 just this $quality = VmConfig::get('img_quality', 89);
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/