View Single Post
  #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