Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Image Resize v1.3a & post MS1


dreamscape

Recommended Posts

Posted

Hey all,

 

I am wondering if anyone has managed to update this contrib to work with post MS1 snapshots that use the new image upload method??

 

I'm going to take a stab at it, but thought I'd post here 1st to see if anyone has already done so.

 

thanks

The only thing necessary for evil to flourish is for good men to do nothing

- Edmund Burke

Posted
Hey all,

 

I am wondering if anyone has managed to update this contrib to work with post MS1 snapshots that use the new image upload method??

 

I'm going to take a stab at it, but thought I'd post here 1st to see if anyone has already done so.

 

thanks

 

actually i have that on my todo-list, but won't have time to code till weekend...

i plan doing it by modding the new upload class to check if upload goes to images/products / manufacturers etc... then call other copy function - the new class should make the job much easier ... but what i'm worring a bit about is the multiple images patch that i have to adapt too... well, its possible and i think the code will be just cleaner than before :)

Posted

hehe, I couldn't wait till the weekend to see what you came up with.

 

So here it is... it may need some cleaning up but its pretty much working very nicely...

 

Save as admin /includes/classes/upload.php:

<?php

/*

 $Id: upload.php,v 1.1 2003/03/22 02:44:57 hpdl Exp $



 osCommerce, Open Source E-Commerce Solutions

 http://www.oscommerce.com



 Copyright (c) 2003 osCommerce



 Released under the GNU General Public License

*/



 class upload {

   var $file, $filename, $destination, $permissions, $extensions, $product, $image_subdir, $big_destination, $debug, $tmp_filename;



   function upload($file = '', $destination = '', $permissions = '777', $extensions = '', $product = '', $image_subdir = BIG_IMAGE_SUBDIR, $debug = THUMB_DEBUG) {

     $this->set_file($file);

     $this->set_destination($destination);

     $this->set_permissions($permissions);

     $this->set_extensions($extensions);

  $this->set_product($product);

  $this->set_image_subdir($image_subdir);

  

     if (tep_not_null($this->file) && tep_not_null($this->destination)) {

       if ( ($this->parse() == true) && ($this->save() == true) ) {

         return true;

       } else {

// self destruct

         $this = null;



         return false;

       }

     }

   }



   function parse() {

     global $messageStack;



     if (isset($_FILES[$this->file])) {

       $file = array('name' => $_FILES[$this->file]['name'],

                     'type' => $_FILES[$this->file]['type'],

                     'size' => $_FILES[$this->file]['size'],

                     'tmp_name' => $_FILES[$this->file]['tmp_name']);

     } elseif (isset($GLOBALS['HTTP_POST_FILES'][$this->file])) {

       global $HTTP_POST_FILES;



       $file = array('name' => $HTTP_POST_FILES[$this->file]['name'],

                     'type' => $HTTP_POST_FILES[$this->file]['type'],

                     'size' => $HTTP_POST_FILES[$this->file]['size'],

                     'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);

     } else {

       $file = array('name' => $GLOBALS[$this->file . '_name'],

                     'type' => $GLOBALS[$this->file . '_type'],

                     'size' => $GLOBALS[$this->file . '_size'],

                     'tmp_name' => $GLOBALS[$this->file]);

     }



     if ( tep_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {

       if (sizeof($this->extensions) > 0) {

         if (!in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) {

           $messageStack->add_session(ERROR_FILETYPE_NOT_ALLOWED, 'error');



           return false;

         }

       }



       $this->set_file($file);

       $this->set_filename($file['name']);

       $this->set_tmp_filename($file['tmp_name']);



       return $this->check_destination();

     } else {

       $messageStack->add_session(WARNING_NO_FILE_UPLOADED, 'warning');



       return false;

     }

   }



   function save() {

     global $messageStack, $debug;



     if (substr($this->destination, -1) != '/') $this->destination .= '/';



  if ($this->product == true) { // Product image upload; lets create the small & big image

    if (substr($this->image_subdir, -1) != '/') $this->image_subdir .= '/';

 $this->set_big_destination($this->destination . $this->image_subdir);

 

 // Create Thumbnail    

 if ($debug=="true") echo "<br>target_small:  " . $this->destination . $this->filename;

 if ($debug=="true") echo "<br> Resizing small image ";

 if ($this->create_thumbnail($this->file['tmp_name'],$this->destination . $this->filename,SMALL_IMAGE_WIDTH,SMALL_IMAGE_HEIGHT,IN_SMALL_IMAGE_WAY_OF_RESIZE)) {

   chmod($this->destination . $this->filename, $this->permissions);

 }

  

 // If !path make it, if you have problems remove the line

 if (!is_dir($this->big_destination)) {

   if ($debug=="true") echo "<br>Mkdir: '" . $this->big_destination . "'";

	 mkdir($this->big_destination, $this->permissions);

 }



 // Resize Big image or Upload as is

 if ($debug=="true") echo "<br><br>target_big: ".$this->big_destination;

 if (IN_BIGIMAGE_WIDTH || IN_BIGIMAGE_HEIGHT ) {

   if ($debug=="true") echo "<br> Resizing big image ";

   if ($this->create_thumbnail($this->file['tmp_name'], $this->big_destination . $this->filename,IN_BIGIMAGE_WIDTH,IN_BIGIMAGE_HEIGHT,IN_BIG_IMAGE_WAY_OF_RESIZE)) {

	 chmod($this->big_destination . $this->filename, $this->permissions);

	 $messageStack->add_session(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');

	 return true;

   } else {

	 $messageStack->add_session(ERROR_FILE_NOT_SAVED, 'error');

	 return false;

   }

 } else {

   if ($debug=="true") echo "<br> Copying big image ";

   if (move_uploaded_file($this->file['tmp_name'], $this->big_destination . $this->filename)) { 

	 chmod($this->big_destination . $this->filename, $this->permissions);

	 $messageStack->add_session(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');

	 return true;

   } else {

	 $messageStack->add_session(ERROR_FILE_NOT_SAVED, 'error');

	 return false;

   }

 }

  } else { // Not a product image.  Better not resize it.

 if (move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) { 

   chmod($this->destination . $this->filename, $this->permissions);

   $messageStack->add_session(SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');

   return true;

 } else {

   $messageStack->add_session(ERROR_FILE_NOT_SAVED, 'error');

         return false;

 }

  }

}



function create_thumbnail($pic,$image_new,$new_width,$new_height,$fixed=0) {

 global $debug;

// resize Picture if possible

// $fixed: 

// 0: propotional resize; new_width or new_height are max Size

// 1: new_width, new_height are new sizes; Image is proportional resized into new Image.  IN_IMAGE_BGCOLOUR are the backgroundcolors

 // 2: new_width, new_height are new sizes; Thumbnail, Pic is resized and part of it is copied to new imaget 

$dst_img='';                               	 

$image=GetImageSize($pic);

 $width=$image[0];

 $height=$image[1];

       if ($debug=="true") echo "<br>Starting in_resize_image()<br>        temp_pic:" . $pic . "<br>        image_new:" . $image_new . "<br>        width:" . $width; 

 if (($new_width && $width>=$new_width)  || ( $new_height && $height>=$new_height)  ) {

// JPG-Resizen

if ($debug=="true") echo "<br><br>Image has to be resized; checking for gd-lib";

if (function_exists('imagecreatefromjpeg')) {   // check if php with gd-lib-support is installed

   if ($debug=="true") echo "<br><br>GD-Lib found start resizing";

   if ($debug=="true") echo "<br> Image Format:" . $image[2];



   if ($image[2]==1) {

     if (function_exists('imagecreatefromgif')) {

   $src_img=imagecreatefromgif($pic);

	 }

 }

   if ($image[2]==2) {

	 if (function_exists('imagecreatefromjpeg')) {

   $src_img=imagecreatefromjpeg($pic);

	 }

 }

   if ($image[2]==3) {

	 if (function_exists('imagecreatefrompng')) {

   $src_img=imagecreatefrompng($pic);

	 }

 }

   if ($src_img) {

     if ($debug=="true") echo "<br><br>Generated new SRC-Pic";



      switch ($fixed) {

    case 0:

      // proportionaler resize; width oder height ist die maximale Gr??e

        $x=$new_width/$width;

         $y=$new_height/$height;

         if (($y>0 && $y<$x) || $x==0) $x=$y;

         $width_big =$width*$x;

         $height_big =$height*$x;

         switch (IN_GD_LIB_VERSION) {

           case '2':

             $dst_img = imagecreatetruecolor($width_big,$height_big);

             imagecopyresampled($dst_img,$src_img,0,0,0,0,$width_big,$height_big,imagesx($src_img),imagesy($src_img));

             if ($debug=="true") echo "<br> GD-LIB 2 - Generated new Pic";

             break;

           default:

             $dst_img = imagecreate($width_big,$height_big);

             imagecopyresized($dst_img,$src_img,0,0,0,0,$width_big,$height_big,imagesx($src_img),imagesy($src_img));

             if ($debug=="true") echo "<br> GD-LIB 1 - Generated new Pic";

         }

         break;

       case 1:

      // Bild wird proportional verkleinert in das neue Bild kopiert

      if ($new_width > 0)  $x = $new_width / $width;

         if ($new_height > 0) $y = $new_height / $height;

         

         if (($y > 0 && $y < $x) || $x == 0) $x = $y;

         $width_big = $width * $x;

         $height_big = $height * $x;

         

         if ($new_width > 0 && $new_width > $width_big) $dst_width = $new_width;

           else $dst_width = $width_big;

         if ($new_height > 0 && $new_height > $height_big) $dst_height = $new_height;

           else $dst_height = $height_big;

        

         // copy new picture into center of $dst_img

        if ($dst_width > $width_big) $dstX=($dst_width - $width_big)/2;

           else $dstX = 0;

         if ($dst_height > $height_big) $dstY=($dst_height - $height_big)/2;

           else $dstY = 0;

         switch (IN_GD_LIB_VERSION) {

           case '2':

             $dst_img = imagecreatetruecolor($dst_width,$dst_height);

             $colorallocate = ImageColorAllocate ($dst_img, IN_IMAGE_BGCOLOUR_R, IN_IMAGE_BGCOLOUR_G, IN_IMAGE_BGCOLOUR_B);

             imagefilledrectangle($dst_img,0,0,$dst_width,$dst_height,$colorallocate);

             imagecopyresampled($dst_img,$src_img,$dstX,$dstY,0,0,$width_big,$height_big,imagesx($src_img),imagesy($src_img));

             if ($debug=="true") echo "<br> GD-LIB 2 - Generated new Pic";

             break;

           default:

             $dst_img = imagecreate($dst_width,$dst_height);

             ImageColorAllocate ($dst_img, IN_IMAGE_BGCOLOUR_R,IN_IMAGE_BGCOLOUR_G, IN_IMAGE_BGCOLOUR_B);

             imagecopyresized($dst_img,$src_img,$dstX,$dstY,0,0,$width_big,$height_big,imagesx($src_img),imagesy($src_img));

             if ($debug=="true") echo "<br> GD-LIB 1 - Generated new Pic";

         }

       break;

     case 2:

      // Thumbnail, Bild wird verkleinert und ein Ausschnitt wird ins neue kopiert

     if ($new_width > 0)  $x = $new_width / $width;

         if ($new_height > 0) $y = $new_height / $height;

         if (($x > 0 && $y > $x) || $x==0) $x = $y;

         $width_big = $width * $x;

         $height_big = $height * $x;

        // Bild verkleinern 

         switch (IN_GD_LIB_VERSION) {

           case '2':

             $dst_img = imagecreatetruecolor($new_width,$new_height);

             $tmp_img = imagecreatetruecolor($width_big,$height_big);

             imagecopyresampled($tmp_img,$src_img,0,0,0,0,$width_big,$height_big,imagesx($src_img),imagesy($src_img));

             imagecopy($dst_img,$tmp_img,0,0,0,0,$new_width,$new_height);

             if ($debug=="true") echo "<br> GD-LIB 2 - Generated new Pic";

             break;

           default:

             $dst_img = imagecreate($new_width,$new_height);

             $tmp_img = imagecreate($width_big,$height_big);

             imagecopyresized($tmp_img,$src_img,0,0,0,0,$width_big,$height_big,imagesx($src_img),imagesy($src_img));

             imagecopy($dst_img,$tmp_img,0,0,0,0,$new_width,$new_height);

             if ($debug=="true") echo "<br> GD-LIB 1 - Generated new Pic";



         }

         break;

    }

    // Copy Picture

       if ($image[2]==1) imagegif($dst_img,$image_new);

       if ($image[2]==2) imagejpeg($dst_img,$image_new);

       if ($image[2]==3 )imagepng($dst_img,$image_new);

      return true;

    }

  else if ($debug=="true") echo "<br>GD-Lib Image Format not supportet";

  }	

 else if ($debug=="true") echo "<br>NO GD-Lib found";

 }

 // pic couldn't be resized, so copy original

 copy ($pic,$image_new);

 return false;

}





   function set_file($file) {

     $this->file = $file;

   }



   function set_destination($destination) {

     $this->destination = $destination;

   }



   function set_big_destination($big_destination) {

     $this->big_destination = $big_destination;

   }



   function set_permissions($permissions) {

     $this->permissions = octdec($permissions);

   }



   function set_product($product) {

     $this->product = $product;

   }

 

   function set_image_subdir($image_subdir) {

     $this->image_subdir = $image_subdir;

   }



   function set_filename($filename) {

     $this->filename = $filename;

   }



   function set_tmp_filename($filename) {

     $this->tmp_filename = $filename;

   }



   function set_extensions($extensions) {

     if (tep_not_null($extensions)) {

       if (is_array($extensions)) {

         $this->extensions = $extensions;

       } else {

         $this->extensions = array($extensions);

       }

     } else {

       $this->extensions = array();

     }

   }



   function check_destination() {

     global $messageStack;



     if (!is_writeable($this->destination)) {

       if (is_dir($this->destination)) {

         $messageStack->add_session(sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');

       } else {

         $messageStack->add_session(sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');

       }



       return false;

     } else {

       return true;

     }

   }

 }

?>

 

 

In admin /categories.php, find this around line 490:

// copy image only if modified

     if ($products_image = new upload('products_image', DIR_FS_CATALOG_IMAGES)) {

and replace with:

// copy image only if modified

     if ($products_image = new upload('products_image', DIR_FS_CATALOG_IMAGES, '777', '', true)) {

The change here is the addition of a new variable being passed to let the uploads class know it is a product image

 

 

For ease of use, I have moved the configuraion variables to admin under Configuration->Images. Import this SQL query into the database:

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Debug Thumbnail Creation', 'THUMB_DEBUG', 'false', 'Set to true if it is not working. Perhaps it helps.', '4', '100', 'tep_cfg_select_option(array('true', 'false'), ', now());

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('GD-Lib version', 'IN_GD_LIB_VERSION', '2', '1 for old GD-Lib version (1.x)<br>2 for new GD-Lib Version (2.x)', '4', '101', 'tep_cfg_select_option(array('1', '2'), ', now());

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Big Image Subdirectory', 'BIG_IMAGE_SUBDIR', 'images_big/', 'Subdir for the big image. It lies under the image directory. The script generates the subdir, but you should do it yourself and don't forget to chmod it.', '4', '102', now());

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Small Image Resize Method', 'IN_SMALL_IMAGE_WAY_OF_RESIZE', '0', 'Small image width & height set the size<br><br>0: Propotional resize<br><br>1: Propotional resize; New image is the small image width & height. The background is set below if it does not fit exactly.<br><br>2: The new image is filled completly.', '4', '103', 'tep_cfg_select_option(array('0', '1', '2'), ', now());

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Small Image Background (Red Color)', 'IN_IMAGE_BGCOLOUR_R', '255', 'Small Image Background Color:<br>The red component of the bg color (RGB) for the small images if you use resize method 1.<br><br>Example: In the color 255,150,0; the red component is 255', '4', '104', now());

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Small Image Background (Green Color)', 'IN_IMAGE_BGCOLOUR_G', '255', 'Small Image Background Color:<br>The green component of the bg color (RGB) for the small images if you use resize method 1.<br><br>Example: In the color 255,150,0; the green component is 150', '4', '105', now());

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Small Image Background (Blue Color)', 'IN_IMAGE_BGCOLOUR_B', '255', 'Small Image Background Color:<br>The blue component of the bg color for (RGB) the small images if you use resize method 1.<br><br>Example: In the color 255,150,0; the blue component is 0', '4', '106', now());

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Big Image Resize Method', 'IN_BIG_IMAGE_WAY_OF_RESIZE', '0', 'Big image width & height set the size<br><br>0: Propotional resize<br><br>1: Propotional resize; New image is the big image width & height.<br><br>2: The new image is filled completly.', '4', '107', 'tep_cfg_select_option(array('0', '1', '2'), ', now());

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Big Image Width', 'IN_BIGIMAGE_WIDTH', '', 'Big Image resize width:<br>Leave empty if you don't want to resize the big image', '4', '108', now());

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Big Image Height', 'IN_BIGIMAGE_HEIGHT', '', 'Big Image resize height:<br>Leave empty if you don't want to resize the big image', '4', '109', now());

 

and that is all folks... I've tested it and its working good.

 

the code may need cleaned up a little but I'm no programmer, so I did the best I know how.

The only thing necessary for evil to flourish is for good men to do nothing

- Edmund Burke

Posted

How do use this script with image's category

 

Hardware = 1 small image

sofware = 1 small image

DVD = 1 small image

Posted

sorry,

 

 

Image's category

 

Hardware :

 

CDRom = 1 small image

Graphics cards = 1 small image

Keybords = 1 small image

Memory=1 small image

Posted

Hey Dreamscape

 

Thanks for working on the upload.php - if I install this do I still need to make the changes in popup_image.php and general.php?

 

Thanks

 

Del

  • 3 months later...
Posted

Hello,

I am having problems making this contrib work with MS2. We have installed Image Resize v1.5 and are getting the following error:

target_small: /var/www/html/store/images/sdr010792.jpg

Resizing small image  

Warning: getimagesize: Read error! in /var/www/html/store/admin/includes/classes/upload.php on line 174

 

Starting in_resize_image()

       temp_pic:/tmp/php1isb3M

       image_new:/var/www/html/store/images/sdr010792.jpg

       width:

 

target_big: /var/www/html/store/images/images_big/

Copying big image

 

Line 174 of upload.php reads as follows:

	// resize Picture if possible

$dst_img='';                               	 

$image=GetImageSize($pic);

$width=$image[0];

$height=$image[1];

 

The product I am working with is here:http://www.shopdr.com/store/product_info.php?products_id=45

 

I was also getting a

imagecreatetruecolor():
error with GD 2.0 while running upload.php but I have not been able to reproduce this error.

 

We are running PHP 4.3.3 and GD (2.0.15 compatible).

 

Any help you may provide would be greatly appreciated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...