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)
-   -   Take images OUT of the database - creating img hard copies (https://forum.x-cart.com/showthread.php?t=1557)

usermike 02-18-2003 02:46 PM

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.

funkydunk 02-19-2003 12:46 AM

cool code mike 8)

funkydunk 02-19-2003 12:48 AM

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

rrf 02-19-2003 01:47 AM

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.

shan 02-19-2003 01:49 AM

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

Heres where we left off on that thread

mike2468 02-19-2003 02:33 AM

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...

funkydunk 02-19-2003 03:07 AM

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 8)

usermike 02-19-2003 06:23 AM

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

Frommeyer 06-03-2003 09:43 PM

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

Frommeyer 06-03-2003 10:16 PM

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...............


All times are GMT -8. The time now is 01:28 PM.

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