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)
-   -   quick hack to eliminate thumbnail image jaggies (libgd req) (https://forum.x-cart.com/showthread.php?t=17012)

B00MER 10-14-2005 01:25 PM

The code I posted from include/product_modify.php has the section of code you'll need to copy into the if ($store_in == "FS") condition and setup accordingly. However most of the code is arleady there just needs to be reconfigured to work properly with importing, etc.

hth.

golfguy 10-15-2005 02:43 PM

The product_modify.php seems to be working. xcart_thumbnails.thumb_path contains

/home/teststore/domains/teststore.com/public_html/skin1/images/thumb/6.jpg

and I checked with FTP and the file exists, the thumbnal image displays but the larger "thumbnail" on the detail page no longer displays. It's got "little red x" Syndrome

xcart_thumbnails.image_path contains

/home/teststore/domains/teststore.com/public_html/images/t_6.jpg

but the file does not exist

Did I delete something out of product_modify that I shouldn't have?

golfguy 10-16-2005 05:57 PM

My test store still has the product detail image problem, but I thought I'd pass along a work around to the import problem.

This is based on my decision to name the new "mini" thumbnails the same as the image_path with "_width" as a suffix.

I batch resized my full size images with Fireworks (Photoshop can do this as well) appending "_120" to the file name, and uploaded them to the same place as my other images.

after you have created the thumb_path column, while still in SQL

Code:

update xcart_thumbnails set thumb_path = image_path;
update xcart_thumbnails
  set thumb_path = replace(thumb_path,'.jpg','_120.jpg');


B00MER 10-16-2005 09:12 PM

If all you did was copy the commented code and the sql changes at the end of the commented code, should be all you need for both the new and update products sections of the code. Make sure you've got the proper full paths to where you are going to be storing the mini thumbs. It should NOT affect the "Product Detail" thumbnails.

creative xpress 10-17-2005 09:19 AM

I tried the same code from the previous files and now I get an error concerning the fopen, fwrite, fclose, and image destroy. as not a valid stream resource. fopen claims no such file or directory.

Is it possible that it is doing this because I am updating products rather than adding new ones?

Thanks

B00MER 10-17-2005 10:36 AM

More than likely the directory you put in for the $handle variable isn't readable or writeable by the web server. Make sure the proper chmod and chown are on the fopen path you specified.

:idea: Keep in mind this was code snippet I had laying around from the orginal mod that was written for 3.5.9 and ported to 4.0.x afterwards.

:!: Be sure GD library is installed and enabled. Written with: GD Version bundled (2.0.28 compatible) If you want to find the version or see if its available or enabled: X-Cart admin -> Summary find php and click "details".

Here's a cleaned up version of the product_modify.php file with no watermarking code commented out, may help debugging it easier.

If you need to find if the table is appearing simply put {$thumb} in product_thumbnail.tpl and you should see a full path to the image.
Code:

{if $thumb ne ""}[img]{$thumb}[/img]
{else}
{* original product_thumbnail.tpl *}
{/if}


... And here's product_modify.php:

Code:

# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# GD by B00MER (www.cart-lab.com)
#
# Apply SQL Patch:
# ALTER TABLE `xcart_thumbnails` ADD `thumb_path` VARCHAR( 255 ) NOT NULL
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Generate New thumbnail


                                                # get image
                                                $photoImage = ImageCreateFromJPEG($image_data["image"]);
                                                ImageAlphaBlending($photoImage, true);
                                                #ImageAntiAlias($photoImage, true);
                                                                               
                                                # get size of original image
                                                $size = getimagesize($image_data["image"]);
       
                                                # 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("/home/user/xcart/files/images/thumb/".$productid.".jpg", "wb");
                                                fwrite($handle, $thumb_imagedata_);
                                                fclose($handle);                               
                                               
                                                # reference thumbnail image to store in db
                                                $image_data[thumb_image]="/home/user/xcart/files/images/thumb/".$productid.".jpg";
                                               
                                                # clean up
                                                ImageDestroy($photoImage);
                                                ImageDestroy($logoImage);                                       
                       
                                                #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]')");       

# end of mod
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-                                       

                                } 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")  {
                                       
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# Update thumbnail with GD by B00MER (www.cart-lab.com)
                                               
                                                # get image
                                                $photoImage = ImageCreateFromJPEG($image_data["image"]);
                                                ImageAlphaBlending($photoImage, true);
                                                #ImageAntiAlias($photoImage, true);
                                               
                                                # get size of original image
                                                $size = getimagesize($image_data["image"]);       
                                                                       
                                                # 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("/home/user/xcart/files/images/thumb/".$productid.".jpg", "wb");
                                                fwrite($handle, $thumb_imagedata_);
                                                fclose($handle);                               
                                               
                                                # reference thumbnail image to store in db
                                                # this must be a full writeable and readable directory the webserver can write to safely
                                                $image_data[thumb_image]="/home/user/xcart/files/images/thumb/".$productid.".jpg";
                                               
                                                # clean up
                                                ImageDestroy($photoImage);
                                                ImageDestroy($logoImage);                                       
                                                                       
                                                $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]')");

# end of mod
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


:!: Also there are two lines I kept in. imagecopyresampled ... and imagecopyresized. If your having issues you can try and swap the two out to see if you get different results. There is also another GD function call ImageAntiAlias, it may need to be uncommented/enabled as well.

References:

:arrow: http://us2.php.net/imageantialias
:arrow: http://us2.php.net/manual/en/function.imagecopyresampled.php
:arrow: http://us2.php.net/manual/en/function.imagecopyresized.php

B00MER 10-17-2005 10:43 AM

Simple update script
 
Here's a simple script that may help with the updating of existing products:
Code:

<?php
# grab all images paths and redefine and copy to new field
# cart-lab.com

require "./auth.php";

$sql=func_query("SELECT productid,image_path,image_type FROM $sql_tbl[thumbnails] WHERE image_type = 'image/pjpeg'");

foreach($sql as $k=>$v) {
        $small_thumb = eregi_replace("thumb","small",$v[image_path]);
        if(file_exists($small_thumb)) {
                db_query("UPDATE $sql_tbl[thumbnails] SET thumb_path='$small_thumb' WHERE productid='$v[productid]'");
                print "Updating $v[productid]...
";
        } else {
                db_query("UPDATE $sql_tbl[thumbnails] SET thumb_path='' WHERE productid='$v[productid]'");
                print "File Not Found.
";
        }
}
print "Done.";
?>


creative xpress 10-20-2005 03:59 PM

Boomer - Would I just put that script into its own php file and then run it or does this script go into an existing php page?

B00MER 10-21-2005 10:08 AM

No it would be a seperate php page. The script is useful if you have generated thumbnails via photoshop batch persay and are wanting to update the existing products with the new generated thumbnails.

mffowler 10-21-2005 01:18 PM

Thanks again Boomer- I am going to give your code a whirl on one of our latest sites.....

I'll see what I can contribute once it's working the images.

- Mike


All times are GMT -8. The time now is 11:15 AM.

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