View Single Post
  #1  
Old 12-02-2006, 07:18 AM
 
r.jones r.jones is offline
 

Newbie
  
Join Date: May 2005
Posts: 5
 

Post 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.
__________________
r.jones
X-Cart Version 4.0.13
Reply With Quote