
Today my client is looking for a possibility to resize the full product image dynamically and give it maximal dimensions 600×600 during uploading. Not resize the image afterwards in the shop_product_details.php or the flypage.php. So we have a little search on this and found a solution to this, and would love to share it to the community with it.
The solution for this case is to hack the main product image resize to max height/width 600px, open the the file in administrator/components/com_virtuemart/classes/imageTools.class.php file. Locate the following code.
original code
// Resize the Full Image
if( !empty ( $_FILES[$tmp_field_name]["tmp_name"] )) {
$full_file = $_FILES[$tmp_field_name]["tmp_name"];
$image_info = getimagesize($full_file);
}
Replace with the following code
// Beginning of Modify by DesignerSandbox
// resize the full product image dynamically and give it maximal dimensions 600x600 during uploading
// Resize the Full Image
if( !empty ( $_FILES[$tmp_field_name]["tmp_name"] )) {
$full_file = $_FILES[$tmp_field_name]["tmp_name"];
$image_info = getimagesize($full_file);
$original_height = $image_info[1];
$original_width = $image_info[0];
if ($original_height > $original_width) {
$largewidth = (600 / $original_height) * $original_width;
$largeheight = "600px";
} else {
$largeheight = (600 / $original_width) * $original_height;
$largewidth = "600px";
}
//Get Image size info
list($original_width, $original_height, $image_type) = getimagesize($full_file);
switch ($image_type)
{
case 1: $im = imagecreatefromgif($full_file); break;
case 2: $im = imagecreatefromjpeg($full_file); break;
case 3: $im = imagecreatefrompng($full_file); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
$big = imagecreatetruecolor($largewidth, $largeheight);
/* Check if this image is PNG or GIF, then set if Transparent*/
if(($image_type == 1) OR ($image_type==3))
{
imagealphablending($big, false);
imagesavealpha($big,true);
$transparent = imagecolorallocatealpha($big, 255, 255, 255, 127);
imagefilledrectangle($big, 0, 0, $largewidth, $largeheight, $transparent);
}
imagecopyresampled($big, $im, 0, 0, 0, 0, $largewidth, $largeheight, $original_width, $original_height);
//Generate the file, and rename it to $newfilename
switch ($image_type)
{
case 1: imagegif($big,$full_file); break;
case 2: imagejpeg($big,$full_file); break;
case 3: imagepng($big,$full_file); break;
default: trigger_error('Er ging iets fout!', E_USER_WARNING); break;
}}
// end of edit by DesignerSandbox
Be a link sponsor for this page now

I hope you have enjoyed this post, be sure to subscribe to my rss feed by click the subscribe button at the top.
Good Luck!




That is awesome! thankyou so much. You dont know how many clients i get uploading like 12megapixel photos to their shop and then asking me why theres problems with the lightbox etc…
And all the photo resizing lessons i give them go over their head!
Can you please tell me if i can apply that script to the category area as well?
I would love to do the same for the Category image upload.
If you could tell me where that file is for ( image resizing / image categories ) i can have a go at inserting the code you have written.
Thankyou again for such a great modification to something thats really needed in VM
John
Thanks Jonnypixel,
I believe my next hack will be on this topics.
But I am not currently available to do this hack yet, maybe next week I am going to have more free time to do this hack. Because we are now in the time of wrapping up our first free exciting premium business template.
For my wild guess, and a bit of knowledge of VM, I believe you can look into this files.
1. administrator/components/com_virtuemart/classes/ps_product_category.php (this is where it control the admin side of it – I guess this is the place to hack. :-O wild guess only. havn't look deep in it yet.)
2. components/com_virtuemart/themes/default/templates/common/categoryChildlist.tpl.php (this is where the template call the category_thumb_image.
If you have found a solution to this, you can leave us a comment to share with the community.
Hi Jonnypixel,
Here's the new off the shelf hack for your category images.
http://tny.hk/2c
With this hack we had go beyond the product images hack.
This hack add in several feature from the control panel
thanks again for the support
good blog keep it up.
This is very nice hack, but i have have one problem. If i have smaler images(for examlpe 300px width), script forced resize image to 600px and my image loses quality. Is there some way to not resize if image is smaller than 600px?
Thanks
Hi, I have modified the code to use ratios instead so that if you want to set a different max height to width you can ie. width = 650 and height = 550.
replace:
$image_info = getimagesize($full_file); $original_height = $image_info[1]; $original_width = $image_info[0]; if ($original_height > $original_width) { $largewidth = (600 / $original_height) * $original_width; $largeheight = "600px"; } else { $largeheight = (600 / $original_width) * $original_height; $largewidth = "600px"; }with:
$max_width = 650; $max_height = 550; $max_ratio = $max_width / $max_height; $image_info = getimagesize($full_file); $original_height = $image_info[1]; $original_width = $image_info[0]; $original_ratio = $original_width / $original_height; // set some defaults $largewidth = $original_width; $largeheight = $original_height; if ($original_ratio > $max_ratio) { if ($original_width > $max_width) { $largewidth = $max_width; $largeheight = round($max_width / $original_ratio); } } else if ($original_ratio < $max_ratio) { if ($original_height > $max_height) { $largewidth = round($max_height * $original_ratio);; $largeheight = $max_height; } }Impressive blog.