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

Take images OUT of the database - creating img hard copies

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #1  
Old 02-18-2003, 02:46 PM
 
usermike usermike is offline
 

Advanced Member
  
Join Date: Jan 2003
Posts: 41
 

Default Take images OUT of the database - creating img hard copies

This script will copy the images out of your database and make hard copies of them. Furthermore, it creates a thumbnail of each image for use on the category display pages.

This is helpful to those who want a faster load time and images cached for users among other things. Right now, the images are stored as BLOBs in the database. This is not always the most efficient way to store data.

Enjoy!

Call this file img_ext.php and place it in the root:
Code:
<? set_time_limit(60*60*60*60); //this should be the same info as in your config file $db_hostname =""; $db_login =""; $db_password =""; $databasename =""; $image_path = "skin1/images"; $thumb_path = "skin1/images/thumbs"; $thumb_width = "70"; function thumbnail($image_path,$thumb_path,$image_name,$thumb_width) { $src_img = imagecreatefromjpeg("$image_path/$image_name"); $origw=imagesx($src_img); $origh=imagesy($src_img); $new_w = $thumb_width; $diff=$origw/$new_w; $new_h=$origh/$diff; $dst_img = imagecreate($new_w,$new_h); imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img)); //change 100 to something smaller for a smaller file size, lower quality image imagejpeg($dst_img, "$thumb_path/$image_name", 100); return true; } $con_id=mysql_connect($db_hostname,$db_login,$db_password); mysql_select_db($databasename); if(!$con_id) { echo "Error while connecting to database"; } $sql = "SELECT * FROM xcart_thumbnails"; $rs = @mysql_query($sql); while($row=mysql_fetch_array($rs)) { $data = $row["image"]; $name = $row["productid"]; $type = $row["image_type"]; $name=$name.".jpg"; $fp=fopen("skin1/images/$name","w"); fwrite($fp,$data); fclose($fp); //create thumbnail thumbnail($image_path,$thumb_path,$name,$thumb_width); } echo "All images are dumped."; ?>

The code above creates the images. Please note this code is for existing products. If you add more products, you either have to change the upload of the image or simply rerun the script.

Then, in product.tpl, switch:

Code:
{include file="product_thumbnail.tpl" productid=$product.productid image_x=$product.image_x image_y=$product.image_y product=$product.product}

to:

Code:
[img]{$ImagesDir}/{$product.productid}.jpg[/img]

And in products.tpl, switch:

Code:
{include file="product_thumbnail.tpl" productid=$products[product].productid image_x=70 product=$products[product].product}

to:

Code:
[img]{$ImagesDir}/thumbs/{$products[product].productid}.jpg[/img]

That should do it. You could delete the BLOB images out of the tables in the database to make it a lot lighter. This code is only for the main images, but can also be used for the detailed images, but that would also require changing some more code... If anyone needs to do that, let me know.
Reply With Quote
  #2  
Old 02-19-2003, 12:46 AM
 
funkydunk funkydunk is offline
 

X-Man
  
Join Date: Oct 2002
Location: Cambridge, UK
Posts: 2,210
 

Default

cool code mike
__________________
ex x-cart guru
Reply With Quote
  #3  
Old 02-19-2003, 12:48 AM
 
funkydunk funkydunk is offline
 

X-Man
  
Join Date: Oct 2002
Location: Cambridge, UK
Posts: 2,210
 

Default

Just a thought on this - it would be really great if x-cart had a configuration setting to say..store images in database Y/N?

rrf - future release maybe?? bearing in mind the amount of posts on this issue
__________________
ex x-cart guru
Reply With Quote
  #4  
Old 02-19-2003, 01:47 AM
 
rrf rrf is offline
 

X-Cart team
  
Join Date: Sep 2002
Posts: 543
 

Default

Quote:
Originally Posted by funkydunk
Just a thought on this - it would be really great if x-cart had a configuration setting to say..store images in database Y/N?

rrf - future release maybe?? bearing in mind the amount of posts on this issue

As I've already said somewhere on these forums, the new release (3.4.0) will have support for this configuration setting.
__________________
Sincerely yours,
Ruslan R. Fazliev,
CEO

Twitter: @aznakai
Reply With Quote
  #5  
Old 02-19-2003, 01:49 AM
  shan's Avatar 
shan shan is offline
 

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

Default

http://forum.x-cart.com/viewtopic.php?t=1660

Heres where we left off on that thread
__________________
Looking for a reliable X-cart host ?
You wont go wrong with either of these.

EWD Hosting
Hands On Hosting
Reply With Quote
  #6  
Old 02-19-2003, 02:33 AM
 
mike2468 mike2468 is offline
 

Member
  
Join Date: Dec 2002
Location: USA - Midwest
Posts: 19
 

Default Script File

Mike,

Tried out your script and it worked perfect. Now, when I went and modified the products.tpl and product.tpl files is where I had the problem. After the modification, I received a parse error stating an error in products.tpl.php. Not sure what this meant so I went back to my original method for now until I can do some further testing on this. Is it possible this will not work with the latest version of Xcart?

Mike...
Reply With Quote
  #7  
Old 02-19-2003, 03:07 AM
 
funkydunk funkydunk is offline
 

X-Man
  
Join Date: Oct 2002
Location: Cambridge, UK
Posts: 2,210
 

Default

Quote:
Originally Posted by rrf
As I've already said somewhere on these forums, the new release (3.4.0) will have support for this configuration setting.

Just seen that post...superb news
__________________
ex x-cart guru
Reply With Quote
  #8  
Old 02-19-2003, 06:23 AM
 
usermike usermike is offline
 

Advanced Member
  
Join Date: Jan 2003
Posts: 41
 

Default

I just tested it with 3.3.2 and it works fine. Why don't you either post or pm me some of your code that you changed and I will take a look at it.

-Mike
Reply With Quote
  #9  
Old 06-03-2003, 09:43 PM
 
Frommeyer Frommeyer is offline
 

Advanced Member
  
Join Date: Apr 2003
Location: Chicago
Posts: 30
 

Default

UserMike,

Thanks for your code, but maybe you could help me out with a slight problem in my attempts to modify your php script and both template tags to convert and call GIF images instead of JPEG images.

In brief, I'm using X-Cart version 3.3.5 and my images are in GIF format (all have transparent backgrounds). I've modified the the original X-Cart templates to have 140 image widths for thumbnails and 560 widths for detailed images. Every thing works OK.

But, I'd like to use your php script and template modifications for my GIF images. Unfortunately, I keep getting errors no matter what modifications I make to your script.

In advance, that you for your time and consideration of my request!

Sincerely,

Jim
Reply With Quote
  #10  
Old 06-03-2003, 10:16 PM
 
Frommeyer Frommeyer is offline
 

Advanced Member
  
Join Date: Apr 2003
Location: Chicago
Posts: 30
 

Default

Hmmmm....

I think I found part of "my problem" in that I had to manually create a "thumbs" directory within the images directory and then manually transfer the thubnail images into that directory.

That seems to work OK for the thumbnail images.

However, it appears that the larger Detailed Image is still being pulled from MySQL databse...............
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 12:16 PM.

   

 
X-Cart forums © 2001-2020