X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Resizing Images During Upload Using ImageMagick (https://forum.x-cart.com/showthread.php?t=27061)

r.jones 12-02-2006 07:18 AM

Resizing Images During Upload Using ImageMagick
 
I'm posting this in the hope it will grow into a full fledged function that will simplify image handling in X-cart. The code I'm posting is all I needed, but I can envision it being extended to apply more of ImageMagick's (http://ImageMagick.org) or GraphicsMagick's (http://GraphicsMagick.org) power to manipulate images. Another variation could apply PHP image functions to get similar results.

Problem: I found it difficult and time consuming to train administrators of the shopping cart to edit and upload images properly, resulting in images that were too large or of poor quality.

This is a relatively simple modification that resizes (if needed) category icons, product thumbnails, and product detailed images when they are being uploaded.

Requirements and limitations: You need to have ImageMagick or GraphicsMagick on your server. This code only changes the size of the image if it's over a set width, which is all I was concerned with.

Notes: This has only been tested on X-cart 4.0.13 with images stored in the database.

You need add code in three files:

/admin/category_modify.php
/include/product_modify.php (add in two places)
/modules/Detailed_Product_Images/product_images_modify.php

In these files find the line of code:

Code:


    if ($image_posted) {


Add this code after it and set the $maximum_file width variable to the desired size:

Code:


//Start resize hack RJ
    //Set the maximum width.
    $maximum_file_width = 150;
    //Check to see if the file is wider than desired.
    if ($file_upload_data["image_x"] > $maximum_file_width) {
        //Save the original file name and path to the templates_c directory.
        $original_file_path = $file_upload_data["file_path"];
        //Find out what kind of image it is and create an extension string. This is a slightly modified version of code from func_get_image_content.
        $original_file_type = (strstr($original_file_path, 'gif') ? '.gif' : (strstr($original_file_path, 'png') ? '.png' : '.jpg'));
        //Append the extension string to the file so ImageMagick understands that it's an image.  It's currently saved in the templates_c directory without one.
        rename($original_file_path, $original_file_path . $original_file_type);
        //Ask ImageMagick to create a new file with the desired width.
        exec('convert ' . $original_file_path . $original_file_type . ' -resize ' . $maximum_file_width . ' ' . $original_file_path . '_resized' . $original_file_type);
        //Update the file's properties for the database.
        $new_file_size = getimagesize($original_file_path . '_resized' . $original_file_type);
        $file_upload_data["image_x"] = $new_file_size[0];
        $file_upload_data["image_y"] = $new_file_size[1];
        $file_upload_data["file_size"] = filesize($original_file_path . '_resized' . $original_file_type);
        //Rename the resized image file so it matches the original file name.
        rename($original_file_path . '_resized' . $original_file_type, $original_file_path);
        //Delete the renamed original file.
        unlink($original_file_path . $original_file_type);
    }
//End resize hack RJ


Test results:
Upload file: test.jpg 1734 x 1128 (1,761,608 bytes)
$maximum_file_width = 150 produced a reduced file of 150 x 98 (14,752 bytes)
$maximum_file_width = 400 produced a reduced file of 400 x 260 (91,589 bytes)

I don't expect to spend much more time on this, but I hope some of you will find this useful and hopefully make improvements.

Learner 07-14-2009 04:24 AM

Re: Resizing Images During Upload Using ImageMagick
 
What are the codes if the image stored in file system?

pauldodman 07-14-2009 04:49 AM

Re: Resizing Images During Upload Using ImageMagick
 
Quote:

What are the codes if the image stored in file system?
Yes, the vast majority of x-cart users will be storing images in the file system.

Learner 07-20-2009 05:05 AM

Re: Resizing Images During Upload Using ImageMagick
 
Quote:

Originally Posted by pauldodman
Yes, the vast majority of x-cart users will be storing images in the file system.


Hi, pauldodman any solution about file system stored images?

pauldodman 07-20-2009 06:32 AM

Re: Resizing Images During Upload Using ImageMagick
 
Hi Learner

This is R.Jones' mod - nothing to do with me - maybe he'll post a reply for you.

GMarler 07-20-2009 06:43 AM

Re: Resizing Images During Upload Using ImageMagick
 
Ideally, this should be accomplished with GD as most servers already have it installed.

Just my 2 cents worth.

Greg :-)

logan 07-22-2009 12:04 PM

Re: Resizing Images During Upload Using ImageMagick
 
You should just use GD, I've completely eliminated all thumbnails on our site by integrating in a script that I got for free. Just google GD image resizer script image.php

It looks like you are trying to resize image on upload, it would be better if you just allow a hi-rez image to upload and leave it in its dimensions. The idea of actually having multiple images upload with different sizes is completely out dated and bad.

Our site also has zoomify so we only need one image to be upload for each product and done, all sections of the site are populated with one image from any thubnail size, up to hi-rez image zooming, and zoomify isn't as slow as that joke of a module that qualiteam has for zooming images which again requires a separate image to be uploaded.


All times are GMT -8. The time now is 04:02 AM.

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