X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Automatic Thumbnail Resize (https://forum.x-cart.com/showthread.php?t=4546)

eleven 09-30-2003 08:22 AM

Automatic Thumbnail Resize
 
A client of mine didn't understand how to resize his images before he added them to the cart, so I wrote a quick and dirty mod to automatically resize them when he uploads them.

WARNING:
- it only works for jpg's (although it can easily be changed)
- it only works for images stored in the file system
- it uses GD. Some servers may not have GD installed (most will)
- the width is hard coded, you need to change it to the width of your images. It is line number 39 below.
- it will not work for batch imports, only thumbnails added through the Modify Products section

In /cart/include/func.php replace the function func_get_image_content with this:

Code:

function func_get_image_content($file_upload_data, $id) {

        global $config;

        $file_aliases_count_max = 99;

        switch($file_upload_data["imtype"]) {
                case "C":
                        $config_data["location"] = $config["Images"]["icons_location"];
                        break;
                case "W":
                        $config_data["location"] = $config["Images"]["pcicons_location"];
                        break;
                case "T":
                        $config_data["location"] = $config["Images"]["thumbnails_location"];
                        break;
                case "D":
                        $config_data["location"] = $config["Images"]["det_images_location"];
                        break;
                default:
                        return false;
        }

        $file_path = $file_upload_data["file_path"];

        if ($fd = fopen($file_path, "rb")) {

                if ($config_data["location"] == "FS") {       
#
# Resize image
#
                                if (($file_upload_data["imtype"] == "T") && ($file_upload_data["image_type"] == "image/pjpeg")) {
                                        $img_src = ImageCreateFromjpeg ($imgname);

                                        $true_width = imagesx($img_src);
                                        $true_height = imagesy($img_src);
                                       
                                        # change this next line to control the width of the thumbnail
                                        $width = 360;
                                        $height = ($width/$true_width)*$true_height;
 
                                        $img_des = imagecreatetruecolor($width,$height);
                                        imagecopyresized ($img_des, $img_src, 0, 0, 0, 0, $width, $height, $true_width, $true_height);
                       
                                        imagejpeg($img_des,$file_path,80);
                                }
# end resize
               
                                       
#
# ...else image is path to file
#
                        $image = $file_path;
                }
                else
                {
#
# If image should be stored in the database, get image content from file
#
                        if ($file_upload_data["source"] == "U")
                                $file_size = 1000000;
                else
                                $file_size = filesize($file_path);

                        $image = addslashes(fread($fd, $file_size));
                }
                fclose($fd);

                if ($file_upload_data["source"] == "L" && !empty($file_upload_data["dir_upload"])) {

                        if ($config_data["location"] == "FS") {
#
# For FS storing. If image has been uploaded, move it to specified directory
#
                                $file_name = ($file_upload_data["imtype"]=="W"?"w_$id":($file_upload_data["imtype"]=="C"?"c_$id":($file_upload_data["imtype"]=="T"?"t_$id":"d_$id")));
                                $file_type = (strstr($file_path,"gif")?"gif":(strstr($file_path,"png")?"png":"jpg"));

#
# Check the existing file
#
                        $counter = 1;
                        $file_name_tmp = $file_name;

                        while (file_exists($file_upload_data["dir_upload"]."/".$file_name_tmp.".".$file_type) && $counter<$file_aliases_count_max) {
                            $file_name_tmp = $file_name."_".sprintf("%02d", $counter);
                            $counter++;
                        }
                        $file_name = $file_name_tmp;
                                $image = $file_upload_data["dir_upload"]."/".$file_name.".".$file_type;
                               
                                copy($file_path, $image);
                                @chmod($image, 0666);
                        }
#
# Delete temporary file
#
                        @unlink($file_path);
                }

        }
        return array("image"=>$image, "image_type"=>$file_upload_data["image_type"], "image_x"=>$width, "image_y"=>$height, "file_size"=>$file_upload_data["file_size"]);
}


I'm sure someone with a little more time on their hands could develop this into a nice mod that works for all images, not just thumbnails and jpg's and gets its settings from the admin area.

groovico 09-30-2003 12:57 PM

Depends on your version of GD too, the latest doesnt support GIF's at all and some will only resize images in 256 cols instead of supporting true colour.

Nifty mod tho kudos on posting it, I'll have to try it later on, maybe x-cart could put a similar on in the main program with a config for switching auto resizing on or off :)

shan 09-30-2003 01:45 PM

Yeh, nice post..

I think it would be a great addition if xcart were to add a utility that resizes the images as standard. Many users have no idea on how to use image editing packages and this would save a lot of bother for them. It could create 2 sizes, one for thumbs and one for detailed images.

ladybird 08-10-2009 04:35 AM

Re: Automatic Thumbnail Resize
 
Quote:

Originally Posted by shan
Yeh, nice post..

I think it would be a great addition if xcart were to add a utility that resizes the images as standard. Many users have no idea on how to use image editing packages and this would save a lot of bother for them. It could create 2 sizes, one for thumbs and one for detailed images.


Hi Shan,

nice one! Do you think this would work for sub-category icon thumbnails? I have read the forum and upgraded the sub-cat tpl to now show icons at the sub cat level, but unfortunately it resizes the 3 menu columns based on the width of the images - would the above mod limit the thumbnail size for sub-cat? Or do you happen to know how to do this ...

See http://www.makeuwell.com.au/home.php?cat=66 for details of what is now happening (live site)

Cheers

L

inteliboy1 11-29-2010 08:10 PM

Re: Automatic Thumbnail Resize
 
Any updates on this? (quite an old post)

Re-sizing / cropping thumbnails would be a huge help for clients!


All times are GMT -8. The time now is 03:20 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.