Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Resizing Images During Upload Using ImageMagick

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #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
  #2  
Old 07-14-2009, 04:24 AM
 
Learner Learner is offline
 

X-Adept
  
Join Date: Dec 2008
Posts: 807
 

Default Re: Resizing Images During Upload Using ImageMagick

What are the codes if the image stored in file system?
__________________
4.6.1 Platinum


Reply With Quote
  #3  
Old 07-14-2009, 04:49 AM
  pauldodman's Avatar 
pauldodman pauldodman is offline
 

X-Guru
  
Join Date: Jul 2003
Location: Spain / UK
Posts: 3,054
 

Default 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.
__________________
Paul Dodman
e-business & m-commerce consultant
w: www.luminointernet.com
e: xcart@luminointernet.com

Professional X-Cart help, advice, support and services, specialists in Mobile X-Cart.
Reply With Quote
  #4  
Old 07-20-2009, 05:05 AM
 
Learner Learner is offline
 

X-Adept
  
Join Date: Dec 2008
Posts: 807
 

Default 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?
__________________
4.6.1 Platinum


Reply With Quote
  #5  
Old 07-20-2009, 06:32 AM
  pauldodman's Avatar 
pauldodman pauldodman is offline
 

X-Guru
  
Join Date: Jul 2003
Location: Spain / UK
Posts: 3,054
 

Default 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.
__________________
Paul Dodman
e-business & m-commerce consultant
w: www.luminointernet.com
e: xcart@luminointernet.com

Professional X-Cart help, advice, support and services, specialists in Mobile X-Cart.
Reply With Quote
  #6  
Old 07-20-2009, 06:43 AM
 
GMarler GMarler is offline
 

Advanced Member
  
Join Date: Aug 2005
Location: Western NC USA
Posts: 96
 

Default 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
__________________
Greg Marler
x-Cart GoldPlus version 4.7.12
Gold 4.7.6 (under construction)
CentOS
Apache 2
Reply With Quote
  #7  
Old 07-22-2009, 12:04 PM
 
logan logan is offline
 

Member
  
Join Date: Feb 2009
Posts: 10
 

Default 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.
__________________
XCART-PRO:Version 4.1.11
Add-on: X-FeatureComparison
Add-on: X-SpecialOffers
Add-on: X-Magnifier
Add-on: X-Survey
Add-on: X-AOM (Advanced Order Management)
Add-on: X-FancyCategories
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


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

   

 
X-Cart forums © 2001-2020