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

quick hack to eliminate thumbnail image jaggies (libgd req)

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #1  
Old 10-01-2005, 08:31 AM
 
diab303 diab303 is offline
 

Newbie
  
Join Date: Sep 2005
Posts: 7
 

Default quick hack to eliminate thumbnail image jaggies (libgd req)

Here is a small mod I've just finished that allows the image.php image wrapper to be called with an optional width=XX parameter that smoothly resizes (aspect ratio kept) images on the fly and caches them to disk.

It is a quick hack that only works for JPEG images when the files are stored on the FS but for me it was all I needed.

The generated thumbnails are stored in the same directory as the pictures themselves called "small-XXw-$origname" so for example t_1234.jpg,width=70 becomes small-70w-t_1234.jpg. It's not touching it if the resulting thumbnail would be bigger than the original image

for the mod to be working /image.php should be changed (begins around line 80):

Code:
if ($config["Images"]["thumbnails_location"] == "FS" || !empty($no_image_db)) { if (!empty($image_path)) { header("Content-type: $image_type"); /* mod begin */ if (substr($image_type,-4) == "jpeg" && is_numeric($width)) { $image_path2 = preg_replace("/(.*)\/([^\/]*)/","$1/small-".$width."w-$2",$image_path); if (!file_exists($image_path2)) { //not in cache $size = GetImageSize($image_path); if ($size[0]>$width) { //resize needed $im = imagecreatefromjpeg($image_path); $sizey = $width/$size[0]*$size[1]; $small = imagecreatetruecolor($width, $sizey); ImageCopyResampled($small, $im, 0, 0, 0, 0, $width, $sizey, $size[0], $size[1]); ImageJPEG($small,$image_path2,85); chmod($image_path2,0666); } else { $image_path2 = $image_path; //use the original file } } $image_path = $image_path2; } /* mod end */ func_readfile($image_path, true); exit;

also /product_thumbnail.tpl should be changed (i've left the original code there commented out using {*** / ***}

Code:
{* $Id: product_thumbnail.tpl,v 1.14 2004/06/24 09:53:29 max Exp $ *} {if $config.Appearance.show_thumbnails eq "Y"}[img]{*** if $tmbn_url}{$tmbn_url}{else ***}{if $full_url}{$http_location}{else}{$xcart_web_dir}{/if}/image.php?productid={$productid}&width={$image_x}{if $file_upload_data.file_path}&tmp=y{/if}{/if}[/img]{*** /if ***}

basically this disables direct linking of the images and forces xcart to use the image wrapper so the the above image.php script always runs.

please feel free to use it and post your comments!
__________________
X-Cart Pro Version 4.0.14
Reply With Quote
  #2  
Old 10-01-2005, 02:03 PM
 
mffowler mffowler is offline
 

X-Adept
  
Join Date: Mar 2003
Location: Melbourne, Australia
Posts: 811
 

Default

That's great diab! I just implemented this and it works perfectly. Auto-generated true thumbs for products from the product thumb. image. You might change yor post to instruct the template change as per product_thumbnail.tpl vs. proudt.tpl.

So, I take it that although images are still shown as a php call, they are not drawn from the DB, but the product thumbnail location? A cool little mod, but one that will save me a lot of time while having the flexability of still keeping the product thumb in the product.tpl to take advantage or the tab menu and true detailed images easily....

Thanks!

- Mike
__________________
4.1.9
Reply With Quote
  #3  
Old 10-02-2005, 01:51 PM
 
diab303 diab303 is offline
 

Newbie
  
Join Date: Sep 2005
Posts: 7
 

Default

Hi Mike, it was worth posting then
I've also fixed the mistake you mentioned, thanks!

I also changed the image uplolder script in the admin area so it can resize pictures on the fly (selected from a dropdown menu next to the [browse] button) because our providers are not too computer savy and we wouldn't want them to upload 3000x2000 pictures of their products, I might post that one too, if anyone's interested..

--
diab
__________________
X-Cart Pro Version 4.0.14
Reply With Quote
  #4  
Old 10-03-2005, 03:22 AM
  shan's Avatar 
shan shan is offline
 

X-Guru
  
Join Date: Sep 2002
Location: Birmingham, UK
Posts: 6,163
 

Default

Quote:
Originally Posted by diab303
Hi Mike, it was worth posting then
I've also fixed the mistake you mentioned, thanks!

I also changed the image uplolder script in the admin area so it can resize pictures on the fly (selected from a dropdown menu next to the [browse] button) because our providers are not too computer savy and we wouldn't want them to upload 3000x2000 pictures of their products, I might post that one too, if anyone's interested..

--
diab

yeh, you should post that one. very useful for people who dont know about setting image sizes
__________________
Looking for a reliable X-cart host ?
You wont go wrong with either of these.

EWD Hosting
Hands On Hosting
Reply With Quote
  #5  
Old 10-03-2005, 01:43 PM
 
mffowler mffowler is offline
 

X-Adept
  
Join Date: Mar 2003
Location: Melbourne, Australia
Posts: 811
 

Default

diab,

Yes, please put that up! It can be used for a couple of things...

Your first little mod is a lifesaver. I have a site with 20,000 products and I was able to move them to the XC images directory and auto- generate true thumbs through product import and CSV.

It took longer to mget the images in ftp...

- Mike
__________________
4.1.9
Reply With Quote
  #6  
Old 10-04-2005, 06:13 AM
  B00MER's Avatar 
B00MER B00MER is offline
 

X-Guru
  
Join Date: Sep 2002
Location: Keller, TX (Cart-Lab.com)
Posts: 3,165
 

Default

I've had this done for quite some time, but what i'd like to see is a full interface, to scale, crop, resize and rotate. Along with watermark abilities in X-Cart.

The current code I have makes mini-thumbs just the same, but not on the fly, and the data saved to the file system along with an new entry in xcart_images table so you don't get HTML resized images.

Its quite lenghty code, I suppose I could post it if anyones interested, I doubt I'll get around to making a fully featured GD extension like I mentioned above anytime soon. Perhaps someone can integrate the two GD mods?
__________________
Cart-Lab - 100+ Social Bookmarks for X-Cart.
Reply With Quote
  #7  
Old 10-04-2005, 06:24 AM
  B00MER's Avatar 
B00MER B00MER is offline
 

X-Guru
  
Join Date: Sep 2002
Location: Keller, TX (Cart-Lab.com)
Posts: 3,165
 

Default

Just wanted to note how much of a speed difference it makes having "mini-thumbs" instead of resizing with HTML.

A simple case study:

http://forum.x-cart.com/viewtopic.php?t=17667&start=30#83499

And thanks to diab303 for posting his code!
__________________
Cart-Lab - 100+ Social Bookmarks for X-Cart.
Reply With Quote
  #8  
Old 10-04-2005, 06:28 AM
 
mffowler mffowler is offline
 

X-Adept
  
Join Date: Mar 2003
Location: Melbourne, Australia
Posts: 811
 

Default

Post away Boomer. I'm always up for taking a crack at testing image scripts and code. Waiting for a better advanced image mod...

Thanks,

- Mike
__________________
4.1.9
Reply With Quote
  #9  
Old 10-04-2005, 06:33 AM
 
mffowler mffowler is offline
 

X-Adept
  
Join Date: Mar 2003
Location: Melbourne, Australia
Posts: 811
 

Default

Boomer,

To solidify your efficiency post: I loaded a 300x300px image into a product (116kb) and resized it used diab's mod. The 100x100px resized thumb is only 3kb!

Makes you really wonder what X-Cart is thinking by not using true thumbnails.

- Mike
__________________
4.1.9
Reply With Quote
  #10  
Old 10-04-2005, 06:46 AM
  B00MER's Avatar 
B00MER B00MER is offline
 

X-Guru
  
Join Date: Sep 2002
Location: Keller, TX (Cart-Lab.com)
Posts: 3,165
 

Default

SQL for table storage:
Code:
ALTER TABLE `xcart_thumbnails` ADD `thumb_path` VARCHAR( 255 ) NOT NULL;

Edit include/func.php find:
Code:
# # Get thumbnail's URL (uses only if images stored in FS) # $result[$key]["tmbn_url"] = func_get_thumbnail_url($result[$key]["productid"]);

Add after:

Code:
$result[$key]["thumb"] = func_get_thumbnail_url_($result[$key]["productid"]);

There may be some other locations like featured products or upselling, cross linking php code you may need to do a search for any occurance of "func_get_thumbnail" and make a duplicate of it like above calling the new function with the trailing _ (underscore).

With func.php still open: add this function to include/func.php:
Code:
# # Get thumbnail URL (if images are stored on the FS only) for GD creation of images # function func_get_thumbnail_url_($productid) { global $config, $sql_tbl, $xcart_dir, $current_location; if ($config["Images"]["thumbnails_location"] == "FS") { # # Thumbnail data # $thumbnail_info = func_query_first("SELECT thumb_path FROM $sql_tbl[thumbnails] WHERE productid='$productid'"); if (eregi("^(http|ftp)://", $thumbnail_info["image_path"])) # image_path is an URL return $thumbnail_info["thumb_path"]; elseif (!strncmp($xcart_dir, $thumbnail_info["thumb_path"], strlen($xcart_dir))) { # image_path is an locally placed image $url = $current_location.substr($thumbnail_info["thumb_path"], strlen($xcart_dir)); return $url; } } return false; }

Add thumb=$products[product].thumb to the product_thumbnail.tpl calls in any products.tpl or products_t.tpl file, etc.

Code:
{include file="product_thumbnail.tpl" productid=$products[product].productid image_x=$config.Appearance.thumbnail_width product=$products[product].product tmbn_url=$products[product].tmbn_url thumb=$products[product].thumb}

Update product_thumbnail.tpl with:

Code:
{if $thumb ne ""}[img]{$thumb}[/img] {else} {if $config.Appearance.show_thumbnails eq "Y"}[img]{if $tmbn_url}{$tmbn_url}{else}{if $full_url}{$http_location}{else}{$xcart_web_dir}{/if}/image.php?productid={$productid}{if $file_upload_data.file_path}&tmp=y{/if}{/if}[/img]{/if} {/if}

For version 4.0.x edit include/search.php and add the new function call to the array:

Code:
$products[$k]["thumb"] = func_get_thumbnail_url_($v["productid"]);

Now comes the fun tricky part, edit include/product_modify.php.

I'm gonna paste the whole product_modify.php file so you can see where and what has changed. Alot of variables are hardcoded so you'll need to edit them accordingly. Like the full path's, etc. The watermarking code is commented out as not many people need this feature but its available. Also make note of the new db_query Inserts and updates with the new thumb_path field:

Code:
<?php /*****************************************************************************\ +-----------------------------------------------------------------------------+ | X-Cart | | Copyright (c) 2001-2005 Ruslan R. Fazliev <rrf@rrf.ru> | | All rights reserved. | +-----------------------------------------------------------------------------+ | PLEASE READ THE FULL TEXT OF SOFTWARE LICENSE AGREEMENT IN THE "COPYRIGHT" | | FILE PROVIDED WITH THIS DISTRIBUTION. THE AGREEMENT TEXT IS ALSO AVAILABLE | | AT THE FOLLOWING URL: http://www.x-cart.com/license.php | | | | THIS AGREEMENT EXPRESSES THE TERMS AND CONDITIONS ON WHICH YOU MAY USE | | THIS SOFTWARE PROGRAM AND ASSOCIATED DOCUMENTATION THAT RUSLAN R. | | FAZLIEV (hereinafter referred to as "THE AUTHOR") IS FURNISHING OR MAKING | | AVAILABLE TO YOU WITH THIS AGREEMENT (COLLECTIVELY, THE "SOFTWARE"). | | PLEASE REVIEW THE TERMS AND CONDITIONS OF THIS LICENSE AGREEMENT | | CAREFULLY BEFORE INSTALLING OR USING THE SOFTWARE. BY INSTALLING, | | COPYING OR OTHERWISE USING THE SOFTWARE, YOU AND YOUR COMPANY | | (COLLECTIVELY, "YOU") ARE ACCEPTING AND AGREEING TO THE TERMS OF THIS | | LICENSE AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY THIS | | AGREEMENT, DO NOT INSTALL OR USE THE SOFTWARE. VARIOUS COPYRIGHTS AND | | OTHER INTELLECTUAL PROPERTY RIGHTS PROTECT THE SOFTWARE. THIS | | AGREEMENT IS A LICENSE AGREEMENT THAT GIVES YOU LIMITED RIGHTS TO USE | | THE SOFTWARE AND NOT AN AGREEMENT FOR SALE OR FOR TRANSFER OF TITLE.| | THE AUTHOR RETAINS ALL RIGHTS NOT EXPRESSLY GRANTED BY THIS AGREEMENT. | | | | The Initial Developer of the Original Code is Ruslan R. Fazliev | | Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2005 | | Ruslan R. Fazliev. All Rights Reserved. | +-----------------------------------------------------------------------------+ \*****************************************************************************/ # # $Id: product_modify.php,v 1.120.2.20 2005/06/16 10:00:03 max Exp $ # if ( !defined('XCART_SESSION_START') ) { header("Location: ../"); die("Access denied"); } x_session_register("product_modified_data"); if (x_session_is_registered("search_data")) $smarty->assign("flag_search_result", 1); # # Define the location line # $location[] = array(func_get_langvar_by_name("lbl_adm_product_management"), "search.php"); # # Define the current section # if (!in_array($section, array("lng", "subscr", "options", "variants", "pclass", "wholesale", "upselling", "images", "reviews", 'manufacturer','feature_class'))) $section = "main"; # # Add, modify product # Get product information # x_session_register("store_productids"); if(!$mode) $store_productids = ''; if($mode == 'list' && $productids) { $store_productids = $productids; func_header_location("product_modify.php?mode=return&productid=".key($store_productids)); } elseif($store_productids) $productids = $store_productids; if($productids && !$productid) { list($productid,) = each($productids); reset($productids); } if($productids && $productid) { unset($productids[$productid]); $products_condition = " AND productid IN (".implode(",", array_keys($productids)).") "; } if ($mode == "delete_thumbnail" && !empty($productid)) { # # Delete product thumbnail # $image_paths = func_query("SELECT image_path FROM $sql_tbl[thumbnails] WHERE productid='$productid'"); if(!empty($image_paths)) { foreach($image_paths as $v) { @unlink($v['image_path']); } } db_query("DELETE FROM $sql_tbl[thumbnails] WHERE productid='$productid'"); db_query("UPDATE $sql_tbl[products] SET image_x='0', image_y='0' WHERE productid='$productid'"); func_header_location("product_modify.php?mode=return&productid=$productid"); } x_session_register("file_upload_data"); if ($productid != "") { if(empty($product_lng_code)) $product_lng_code = "US"; # Get the product info or display 'Access denied' message if not exists $product_info = func_select_product($productid, $user_account['membership']); # Correct the location line $location[] = array($product_info["product"], "product_modify.php?productid=$productid"); # Get the product international descriptions $product_languages = func_query_first ("SELECT $sql_tbl[products_lng].* FROM $sql_tbl[products_lng] WHERE $sql_tbl[products_lng].productid='$productid' AND $sql_tbl[products_lng].code = '$product_lng_code'"); $smarty->assign("page_title", func_get_langvar_by_name("lbl_adm_product_management")); } else { $smarty->assign("page_title", func_get_langvar_by_name("lbl_adm_add_product")); $location[] = array(func_get_langvar_by_name("lbl_add_product"), ""); } if (empty($product_info)) { if ($login_type == "A") { $providers = func_query("SELECT login, title, firstname, lastname FROM $sql_tbl[customers] WHERE usertype='P' ORDER BY login, lastname, firstname"); if (!empty($providers)) $smarty->assign("providers", $providers); else { $top_message["content"] = func_get_langvar_by_name("msg_adm_warn_no_providers"); $top_message["type"] = "W"; $smarty->assign("top_message", $top_message); $top_message = ""; $section = "error"; } } else $product_owner = $login; } else $product_owner = addslashes($product_info["provider"]); if (!empty($product_owner)) { $provider_info = func_query_first("SELECT login, title, firstname, lastname FROM $sql_tbl[customers] WHERE login='$product_owner' AND usertype IN ('P','A')"); $smarty->assign("provider_info", $provider_info); } if ($REQUEST_METHOD == "POST") { if ($mode == "update_lng") { # # Update international descriptions # if($product_lng) { db_query("DELETE FROM $sql_tbl[products_lng] WHERE code='$product_lng_code' AND productid='$productid'"); $product_lng['code'] = $product_lng_code; $product_lng['productid'] = $productid; func_array2insert("products_lng", $product_lng); if(!empty($fields['languages']) && $productids) { $product_lng_ge = array(); foreach($fields['languages'] as $k => $v) { if(isset($product_lng[$k])) { $product_lng_ge[$k] = $product_lng[$k]; } } if(!empty($product_lng_ge)) { $product_lng_ge['code'] = $product_lng_code; foreach($productids as $k=>$v) { db_query("DELETE FROM $sql_tbl[products_lng] WHERE code='$product_lng_code' AND productid='$k'"); $product_lng_ge['productid'] = $k; func_array2insert("products_lng", $product_lng_ge); } } } } if ($product_lng) { $top_message["content"] = func_get_langvar_by_name("msg_adm_product_int_upd"); $top_message["type"] = "I"; } func_header_location("product_modify.php?mode=return&productid=$productid&section=lng&product_lng_code=".$product_lng_code); } } if ($REQUEST_METHOD == "GET") { if ($mode == "del_lang") { # # Delete selected international description # db_query ("DELETE FROM $sql_tbl[products_lng] WHERE productid='$productid' AND code='$product_lng_code'"); $top_message["content"] = func_get_langvar_by_name("msg_adm_product_int_del"); $top_message["type"] = "I"; func_header_location("product_modify.php?mode=return&productid=$productid&section=lng&product_lng_code=".$product_lng_code); } $smarty->assign("product_languages", $product_languages); } $smarty->assign("main", "product_modify"); # # Define data for the navigation within section # $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid", "title" => func_get_langvar_by_name("lbl_product_details")); if (!empty($product_info)) { if (!empty($avail_languages)) $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=lng", "title" => func_get_langvar_by_name("txt_international_descriptions")); if (!empty($active_modules["Product_Options"])) { $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=options", "title" => func_get_langvar_by_name("lbl_product_options")); $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=variants", "title" => func_get_langvar_by_name("lbl_product_variants")); } if (!empty($active_modules["Product_Configurator"])) $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=pclass", "title" => func_get_langvar_by_name("lbl_pconf_product_classification")); if (!empty($active_modules["Subscriptions"])) $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=subscr", "title" => func_get_langvar_by_name("lbl_subscriptions")); if (!empty($active_modules["Wholesale_Trading"])) $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=wholesale", "title" => func_get_langvar_by_name("lbl_wholesale_prices")); if (!empty($active_modules["Upselling_Products"])) $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=upselling", "title" => func_get_langvar_by_name("lbl_upselling_links")); if (!empty($active_modules["Detailed_Product_Images"])) $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=images", "title" => func_get_langvar_by_name("lbl_detailed_images")); if (!empty($active_modules["Customer_Reviews"])) $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=reviews", "title" => func_get_langvar_by_name("lbl_customer_reviews")); if (!empty($active_modules["Manufacturers"])) $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=manufacturer", "title" => func_get_langvar_by_name("lbl_manufacturer")); if (!empty($active_modules["Feature_Comparison"])) $dialog_tools_data["left"][] = array("link" => "product_modify.php?mode=select&productid=$productid&section=feature_class", "title" => func_get_langvar_by_name("lbl_feature_class")); } $dialog_tools_data["right"][] = array("link" => "search.php", "title" => func_get_langvar_by_name("lbl_search_products")); $dialog_tools_data["right"][] = array("link" => "product_modify.php", "title" => func_get_langvar_by_name("lbl_add_product")); if (!empty($active_modules["Product_Configurator"]) and ($current_area == "P" || !empty($active_modules["Simple_Mode"]))) $dialog_tools_data["right"][] = array("link" => "pconf.php", "title" => func_get_langvar_by_name("lbl_product_configurator")); if ($current_area == "A" or !empty($active_modules["Simple_Mode"])) $dialog_tools_data["right"][] = array("link" => $xcart_catalogs["admin"]."/categories.php", "title" => func_get_langvar_by_name("lbl_categories")); if (!empty($active_modules["Manufacturers"])) $dialog_tools_data["right"][] = array("link" => "manufacturers.php", "title" => func_get_langvar_by_name("lbl_manufacturers")); $dialog_tools_data["right"][] = array("link" => "orders.php", "title" => func_get_langvar_by_name("lbl_orders")); # # This flag means that this product is configurator # $is_pconf = false; # # Product Configurator module # if (!empty($active_modules["Product_Configurator"])) include $xcart_dir."/modules/Product_Configurator/product_modify.php"; # # Update product details or create product # if (($REQUEST_METHOD == "POST") && ($mode == "product_modify")) { $sku_is_exist = (func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products] WHERE productcode='$productcode' AND productid!='$productid'") ? true : false); # Check if form filled with errors $fillerror = (($categoryid == "") || empty($product) || empty($descr) || ($price == "") || ($avail == "") || empty($low_avail_limit) || ($productcode == '') || $sku_is_exist); # Prepare for thumbnail updating $image_posted = func_check_image_posted($file_upload_data, "T"); $store_in = ($config["Images"]["thumbnails_location"] == "FS"?"FS":"DB"); if (!$fillerror) { # # If no errors # if (empty($productid)) { # # Create a new product # $provider = ($login_type == "A" ? $provider : $login); # # Insert new product into the database and get its productid # db_query("INSERT INTO $sql_tbl[products] (productcode, provider, add_date) VALUES ('$productcode', '$provider', '".time()."')"); $productid = db_insert_id(); # Insert price and image db_query("INSERT INTO $sql_tbl[pricing] (productid, quantity, price) VALUES ('$productid', '1', '".abs($price)."')"); # If image was posted if ($image_posted) { # Get image content $image_data = func_get_image_content($file_upload_data, $productid); # Store image (DB) or full path to image (FS) in the database if ($store_in == "FS") { # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # Add our watermark and generate thumbnail with GD by B00MER # ALTER TABLE `xcart_thumbnails` ADD `thumb_path` VARCHAR( 255 ) NOT NULL # get image $photoImage = ImageCreateFromJPEG($image_data["image"]); ImageAlphaBlending($photoImage, true); #ImageAntiAlias($photoImage, true); # get watermark file and width/height #$logoImage = ImageCreateFromPNG('../watermark.png'); #$logoW = ImageSX($logoImage); #$logoH = ImageSY($logoImage); # get size of original image $size = getimagesize($image_data["image"]); # start create a thumbnail # resize our original image $new_w = 75; $new_h = 75; $old_x=imageSX($photoImage); $old_y=imageSY($photoImage); if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } # create image resource and resize $thumb_img = imagecreatetruecolor($thumb_w,$thumb_h); #imagecopyresized($thumb_img, $photoImage, 0,0,0,0, imagesx($thumb_img), imagesy($thumb_img), imagesx($photoImage), imagesy($photoImage)); imagecopyresampled($thumb_img, $photoImage, 0,0,0,0, imagesx($thumb_img), imagesy($thumb_img), imagesx($photoImage), imagesy($photoImage)); # begin output of new thumbnail ob_start(); ImageJPEG($thumb_img,"",90); $thumb_imagedata_ = ob_get_contents(); ob_end_clean(); # write thumbnail to a file $handle = fopen("/usr/shared/web/pedig4/xcart/files/images/thumb/".$productid.".jpg", "wb"); fwrite($handle, $thumb_imagedata_); fclose($handle); # reference thumbnail image to store in db $image_data[thumb_image]="/usr/shared/web/pedig4/xcart/files/images/thumb/".$productid.".jpg"; # end of creating thumbnail /* # calcs to determine where to place watermark (center) $dest_x = $size[0] / 2 - $logoW / 2; $dest_y = $size[1] / 2; # merge the two images #if($size[0] < $logoW) { # imagecopyresized($photoImage, $logoImage, $dest_x, $dest_y, 0, 0, $size[0]-50, $size[1]-25, $logoW, $logoH); #} else { ImageCopy($photoImage, $logoImage, $dest_x, $dest_y, 0, 0, $logoW, $logoH); #} # begin output of new watermarked image ob_start(); ImageJPEG($photoImage,"",85); $imagedata_ = ob_get_contents(); ob_end_clean(); # write file $handle = fopen("/usr/shared/web/pedig4/xcart/files/images/".$productid.".jpg", "wb"); fwrite($handle, $imagedata_); fclose($handle); # reference the file created $image_data[image]="/usr/shared/web/pedig4/xcart/files/images/".$productid.".jpg"; */ # clean up ImageDestroy($photoImage); ImageDestroy($logoImage); # end of mod # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #db_query("INSERT INTO $sql_tbl[thumbnails] (productid, image_path, image_type) VALUES ('$productid', '".addslashes($image_data["image"])."', '$image_data[image_type]')"); db_query("insert into $sql_tbl[thumbnails] (productid, image_path, thumb_path, image_type) values ('$productid', '".addslashes($image_data[image])."', '".addslashes($image_data[thumb_image])."', '$image_data[image_type]')"); } else { db_query("INSERT INTO $sql_tbl[thumbnails] (productid, image, image_type) VALUES ('$productid', '$image_data[image]', '$image_data[image_type]')"); } } $status = "created"; } else { # # Update the existing product # # Update the default price db_query("UPDATE $sql_tbl[pricing] SET price='$price' WHERE productid='$productid' AND quantity='1' AND membership='' AND $sql_tbl[pricing].variantid = 0"); if($fields['price'] == 'Y' && $productids) db_query("UPDATE $sql_tbl[pricing] SET price='$price' WHERE quantity='1' AND $sql_tbl[pricing].variantid = 0 AND membership=''".$products_condition); # If image was posted if ($image_posted) { # Get image content $image_data = func_get_image_content($file_upload_data, $productid); # Replace image (DB) or full path to image (FS) in the database if ($store_in == "FS") { # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # Add our watermark and generate thumbnail with GD by B00MER # get image $photoImage = ImageCreateFromJPEG($image_data["image"]); ImageAlphaBlending($photoImage, true); #die("test");exit; #ImageAntiAlias($photoImage, true); # get watermark file and width/height #$logoImage = ImageCreateFromPNG('../watermark.png'); #$logoW = ImageSX($logoImage); #$logoH = ImageSY($logoImage); # get size of original image $size = getimagesize($image_data["image"]); # start create a thumbnail # resize our original image $new_w = 75; $new_h = 75; $old_x=imageSX($photoImage); $old_y=imageSY($photoImage); if ($old_x > $old_y) { $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y) { $thumb_w=$new_w; $thumb_h=$new_h; } # create image resource and resize $thumb_img = imagecreatetruecolor($thumb_w,$thumb_h); #imagecopyresized($thumb_img, $photoImage, 0,0,0,0, imagesx($thumb_img), imagesy($thumb_img), imagesx($photoImage), imagesy($photoImage)); imagecopyresampled($thumb_img, $photoImage, 0,0,0,0, imagesx($thumb_img), imagesy($thumb_img), imagesx($photoImage), imagesy($photoImage)); # begin output of new thumbnail ob_start(); ImageJPEG($thumb_img,"",90); $thumb_imagedata_ = ob_get_contents(); ob_end_clean(); # write thumbnail to a file $handle = fopen("/usr/shared/web/pedig4/xcart/files/images/thumb/".$productid.".jpg", "wb"); fwrite($handle, $thumb_imagedata_); fclose($handle); # reference thumbnail image to store in db $image_data[thumb_image]="/usr/shared/web/pedig4/xcart/files/images/thumb/".$productid.".jpg"; # end of creating thumbnail /* # calcs to determine where to place watermark (center) $dest_x = $size[0] / 2 - $logoW / 2; $dest_y = $size[1] / 2; # merge the two images #if($size[0] < $logoW) { # imagecopyresized($photoImage, $logoImage, $dest_x, $dest_y, 0, 0, $size[0]-50, $size[1]-25, $logoW, $logoH); #} else { ImageCopy($photoImage, $logoImage, $dest_x, $dest_y, 0, 0, $logoW, $logoH); #} # begin output of new watermarked image ob_start(); ImageJPEG($photoImage,"",85); $imagedata_ = ob_get_contents(); ob_end_clean(); # write file $handle = fopen("/home/modc/public_html/files/images/".$productid.".jpg", "wb"); fwrite($handle, $imagedata_); fclose($handle); # reference the file created $image_data[image]="/home/modc/public_html/files/images/".$productid.".jpg"; */ # clean up ImageDestroy($photoImage); ImageDestroy($logoImage); # end of mod # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- $image_data["image"] = addslashes($image_data["image"]); #db_query("REPLACE INTO $sql_tbl[thumbnails] (productid, ".($store_in == "FS"?"image_path":"image").", image_type) VALUES ('$productid', '$image_data[image]', '$image_data[image_type]')"); db_query("REPLACE into $sql_tbl[thumbnails] (productid, ".($store_in == "FS"?"image_path":"image").", thumb_path, image_type) values ('$productid', '$image_data[image]', '$image_data[thumb_image]', '$image_data[image_type]')"); } } if($fields['thumbnail'] == 'Y' && $productids) { $img = func_query_first("SELECT * FROM $sql_tbl[thumbnails] WHERE productid = '$productid'"); $img = func_array_map("addslashes", $img); foreach($productids as $k => $v) { $img['productid'] = $k; func_array2insert("thumbnails", $img, true); } $image_size = func_query_first("SELECT image_x, image_y FROM $sql_tbl[products] WHERE productid = '$productid'"); db_query("UPDATE $sql_tbl[products] SET image_x='$image_size[image_x]', image_y='$image_size[image_y]' WHERE 1".$products_condition); } $status = "modified"; } # For existing product: get the categories list before updating if ($product_info) { $_old_product_categories = func_query("SELECT categoryid FROM $sql_tbl[products_categories] WHERE productid='$productid'"); if (is_array($_old_product_categories)) { foreach ($_old_product_categories as $k=>$v) $old_product_categories[] = $v["categoryid"]; } } # Prepare and update categories associated with product... if(!func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products_categories] WHERE categoryid = '$categoryid' AND productid = '$productid' AND main = 'Y'")) { db_query("DELETE FROM $sql_tbl[products_categories] WHERE productid = '$productid' AND (main = 'Y' OR categoryid = '$categoryid')"); db_query("INSERT INTO $sql_tbl[products_categories] (categoryid, productid, main) VALUES ('$categoryid', '$productid', 'Y')"); } if($productids && $fields['categoryid']) { foreach($productids as $pid => $v2) { if(!func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products_categories] WHERE categoryid = '$categoryid' AND productid = '$pid' AND main = 'Y'")) { db_query("DELETE FROM $sql_tbl[products_categories] WHERE productid = '$pid' AND (main = 'Y' OR categoryid = '$categoryid')"); db_query("INSERT INTO $sql_tbl[products_categories] (categoryid, productid, main) VALUES ('$categoryid', '$pid', 'Y')"); } } } if($categoryids) { foreach ($categoryids as $k=>$v) { if(!func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products_categories] WHERE categoryid = '$v' AND productid = '$productid'")) { db_query("INSERT INTO $sql_tbl[products_categories] (categoryid, productid, main) VALUES ('$v', '$productid', 'N')"); } if($productids && $fields['categoryids']) { foreach($productids as $pid => $v2) { if(!func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products_categories] WHERE categoryid = '$v' AND productid = '$pid'")) { db_query("INSERT INTO $sql_tbl[products_categories] (categoryid, productid, main) VALUES ('$v', '$pid', 'N')"); } } } } db_query("DELETE FROM $sql_tbl[products_categories] WHERE productid = '$productid' AND main != 'Y' AND categoryid NOT IN ('".implode("','", $categoryids)."')"); if($productids && $fields['categoryids']) { db_query("DELETE FROM $sql_tbl[products_categories] WHERE productid IN ('".implode("','",array_keys($productids))."') AND main != 'Y' AND categoryid NOT IN ('".implode("','", $categoryids)."')"); } } else { db_query("DELETE FROM $sql_tbl[products_categories] WHERE productid = '$productid' AND main != 'Y'"); if($productids && $fields['categoryids']) { db_query("DELETE FROM $sql_tbl[products_categories] WHERE productid IN ('".implode("','",array_keys($productids))."') AND main != 'Y'"); } } # Correct the min_amount if (empty($min_amount) or intval($min_amount) == 0) $min_amount = 1; # # Update product data # db_query("UPDATE $sql_tbl[products] SET product='$product', descr='$descr', fulldescr='$fulldescr', avail='$avail', list_price='$list_price', weight='$weight', productcode='$productcode', forsale='$forsale', distribution='$distribution', free_shipping='$free_shipping', shipping_freight='$shipping_freight', discount_avail='$discount_avail', min_amount='$min_amount', return_time = '$return_time', low_avail_limit='$low_avail_limit', free_tax='$free_tax' WHERE productid='$productid'"); if($image_posted && !empty($image_data)) { db_query("UPDATE $sql_tbl[products] SET image_x='$image_data[image_x]', image_y='$image_data[image_y]' WHERE productid='$productid'"); } db_query("DELETE FROM $sql_tbl[product_taxes] WHERE productid='$productid'"); if($productids && $fields['taxes']) db_query("DELETE FROM $sql_tbl[product_taxes] WHERE productid IN ('".implode("','", array_keys($productids))."')"); if (!empty($taxes) and is_array($taxes)) { foreach ($taxes as $k=>$v) { if (intval($v) > 0) { db_query("REPLACE INTO $sql_tbl[product_taxes] (productid, taxid) VALUES ('$productid', '".intval($v)."')"); if($productids && $fields['taxes']) { foreach($productids as $pid => $v2) db_query("REPLACE INTO $sql_tbl[product_taxes] (productid, taxid) VALUES ('$pid', '".intval($v)."')"); } } } } if ($productids && $fields) { $tmp = array(); foreach($fields as $k => $v) { if(!in_array($k, array("efields", "price", "thumbnail", "categoryid", "categoryids", "taxes"))) { $tmp[] = $k.'="'.$$k.'"'; } } if($tmp) db_query("UPDATE $sql_tbl[products] SET ".implode(", ", $tmp)." WHERE 1".$products_condition); } # # Update products counter for selected categories # if (is_array($old_product_categories)) $categoryids = func_array_merge($old_product_categories, $categoryids); $categoryids = func_array_merge($categoryids, array($categoryid)); func_recalc_product_count($categoryids); if ($status == "created") { $top_message["content"] = func_get_langvar_by_name("msg_adm_product_add"); $top_message["type"] = "I"; } elseif ($status == "modified") { $top_message["content"] = func_get_langvar_by_name("msg_adm_product_upd"); $top_message["type"] = "I"; } if ($active_modules["Extra_Fields"]) { include $xcart_dir."/modules/Extra_Fields/extra_fields_modify.php"; } func_header_location("product_modify.php?mode=return&productid=$productid"); } else { # # Form filled with errors # $top_message["content"] = ($sku_is_exist ? func_get_langvar_by_name("msg_adm_err_sku_exist") : func_get_langvar_by_name("msg_adm_err_product_upd")); $top_message["type"] = "E"; $top_message["fillerror"] = true; $product_modified_data = $HTTP_POST_VARS; foreach ($product_modified_data as $k=>$v) $product_modified_data[$k] = stripslashes($v); $product_modified_data["productid"] = $productid; func_header_location("product_modify.php?mode=return&productid=$productid"); } } # # Detailed_Product_Images module # if ($active_modules["Detailed_Product_Images"]) { include $xcart_dir."/modules/Detailed_Product_Images/product_images_modify.php"; include $xcart_dir."/modules/Detailed_Product_Images/product_images.php"; } if (empty($active_modules["Product_Configurator"]) || !$is_pconf) { # # Subscription module # if ($active_modules["Subscriptions"]) include $xcart_dir."/modules/Subscriptions/subscription_modify.php"; # # Wholesale trading module # if ($active_modules["Wholesale_Trading"]) include $xcart_dir."/modules/Wholesale_Trading/product_wholesale.php"; # # Product Configurator module # if ($active_modules["Product_Configurator"]) include $xcart_dir."/modules/Product_Configurator/pconf_classification.php"; } #/ if ($mode != "pconf") # # Extra fields module # if ($active_modules["Extra_Fields"]) { $extra_fields_provider = ( $current_area == "A" ? $product_info["provider"] : $login ); include $xcart_dir."/modules/Extra_Fields/extra_fields.php"; } # # Product options module # if ($active_modules["Product_Options"]) { if($section == 'options') @include $xcart_dir."/modules/Product_Options/product_options.php"; if($section == 'variants') include $xcart_dir."/modules/Product_Options/product_variants.php"; } # # Manufacturers module # if ($active_modules["Manufacturers"]) @include $xcart_dir."/modules/Manufacturers/product_manufacturer.php"; # # Feature comparision module # if ($active_modules["Feature_Comparison"]) @include $xcart_dir."/modules/Feature_Comparison/product_class.php"; # # Upselling products module # if ($active_modules["Upselling_Products"]) include $xcart_dir."/modules/Upselling_Products/edit_upsales.php"; # # Customer Reviews module # include $xcart_dir."/include/reviews.php"; if (($productid != "") && !$fillerror) $product_info = func_select_product($productid, $user_account['membership']); # # Obtain VAT rates # if ($single_mode) $provider_condition = ""; elseif ($current_area == "A") $provider_condition = "AND provider='$product_info[provider]'"; else $provider_condition = "AND provider='$login'"; # # Check if image selected is not expired # if ($file_upload_data["imtype"] == "T") { if ($file_upload_data["counter"] == 1) { $file_upload_data["counter"]++; $smarty->assign("file_upload_data", $file_upload_data); } else { if ($file_upload_data["source"] == "L") @unlink($file_upload_data["file_path"]); x_session_unregister("file_upload_data"); } } if (empty($product_info)) $smarty->assign("new_product", 1); if (!empty($product_modified_data)) $product_info = $product_modified_data; if (empty($product_info)) { $sku_prefix = 'SKU'; $product_info['productcode'] = array_pop(array_pop(func_query("SELECT MAX(productid) FROM $sql_tbl[products]"))); $plus = 0; while(func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products] WHERE productcode='$sku_prefix".($product_info['productcode'].(++$plus))."'")) { } $product_info['productcode'] = $sku_prefix.($product_info['productcode']+$plus); } else { define('NEED_PRODUCT_CATEGORIES', 1); } $taxes = func_query("SELECT $sql_tbl[taxes].*, COUNT($sql_tbl[product_taxes].productid) as selected FROM $sql_tbl[taxes] LEFT JOIN $sql_tbl[product_taxes] ON $sql_tbl[product_taxes].taxid=$sql_tbl[taxes].taxid AND $sql_tbl[product_taxes].productid='$productid' GROUP BY $sql_tbl[taxes].taxid"); $smarty->assign("taxes", $taxes); $smarty->assign("location", $location); $smarty->assign("section", $section); $smarty->assign("query_string", urlencode($QUERY_STRING)); $smarty->assign("product", $product_info); $smarty->assign("productid", $product_info["productid"]); if($productids) { $objects_per_page = $config["Appearance"]["products_per_page_admin"]; $total_items = func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products] WHERE productid IN ($productid,".implode(",", array_keys($productids)).")"); $total_nav_pages = ceil($total_items/$objects_per_page)+1; include $xcart_dir."/include/navigation.php"; $smarty->assign("products", func_query("SELECT product, productcode, productid FROM $sql_tbl[products] WHERE productid IN ($productid,".implode(",", array_keys($productids)).") LIMIT $first_page, $objects_per_page")); $smarty->assign("productids", $productids); $smarty->assign("navigation_script","product_modify.php?mode=return&section=$section&productid=$productid"); $smarty->assign("first_item", $first_page+1); $smarty->assign("last_item", min($first_page+$objects_per_page, $total_items)); } $product_modified_data = ""; $smarty->assign("fillerror", $fillerror); x_session_save(); if($section == 'lng') { $smarty->assign("product_lng_code", $product_lng_code); if(!empty($avail_languages)) { foreach($avail_languages as $v) { if($v['code'] == $product_lng_code) { $smarty->assign("default_charset", $v['charset']); break; } } } $smarty->assign("avail_languages", $avail_languages); } if (!empty($categoryid)) $smarty->assign("default_categoryid", intval($categoryid)); ?>

Note this code is for 4.0.x but can easily be adapted for later versions. I don't offer any support or liability for this script. So use it at your own risk.
__________________
Cart-Lab - 100+ Social Bookmarks for X-Cart.
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:06 PM.

   

 
X-Cart forums © 2001-2020