Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

problem with GD 2.0 and image resize


neogolit

Recommended Posts

The snapshop I am using is from 8/03/2003. This Server is running RedHat Linux v7.2. The Server also has GD installed, but its GD v2.x

 

I have a problem with the Image Resize v1.0 contrib.

When creating a new product, the original image would upload to the images/images_big directory, which is correct and create the thumbnail and put it in the images directory.

But the backgroundcolor of the thumbnail is black.

I have changed some things in 'image_resize.php', because of the GD v. 2.x, but I can't solve this little problem.

 

Any ideas?

 

Here my actual image_resize.php:

 

<?php

/*

 $Id: image_resize.php,v 1.2 2003/02/18 21:38:22 Simarilius Exp $

 Copyright (c) 2003 IN-Solution.de Henri Schmidhuber



based on



 osCommerce, Open Source E-Commerce Solutions

 http://www.oscommerce.com



 Copyright (c) 2003 osCommerce



 Released under the GNU General Public License

*/



 $in_debug="false"; // Set to true if it doesn't work, perhaps it helps



// Subdir for the big image '/' at end !! 

// it lies under the image directory

//the script generates the subdir, but you should do it yourself and don't forget to chmod it..

 define('IN_IMAGE_SUBDIR','images_big/');





// Pictures are resized to the size-settings in the OSC-Admin (here called new_width; new_height)



// How to resize; check samples included in the files

 define ('IN_SMALL_IMAGE_WAY_OF_RESIZE','1');

// 0: propotional resize; new_width OR new_height are the new max size

// 1: new_width, new_height are the propotions of the new image;

//    Image is proportional resized into new Image. 

//    IN_IMAGE_BGCOLOUR_ are the backgroundcolors of the new image if it does not fit

//    exactly.

 // 2: new_width, new_height are the propotions of the new image; 

//    Pic is resized and as much as possible and is copied to new image;

//    good if your pic proportions are nearly as the wanted small image 

//    The new image is filled completly

                                	 



// Backgroundcolor of the small pictures (if you use 1 )

 define('IN_IMAGE_BGCOLOUR_R','255');

 define('IN_IMAGE_BGCOLOUR_G','255');

define('IN_IMAGE_BGCOLOUR_B','255');



// Big Image resize:

// leave empty if you don't want to resize the big image

define ('IN_BIGIMAGE_WIDTH','');

define ('IN_BIGIMAGE_HEIGHT','');

define ('IN_BIG_IMAGE_WAY_OF_RESIZE','');



// do not change anything else



function in_copy_uploaded_file($filename, $target) {

global $in_debug;

 if (substr($target, -1) != '/') $target .= '/'; 

 $picture_name=in_str_strip_all($filename['name']);



 // Big Image      

 // path for big image:



 $target_big=$target . IN_IMAGE_SUBDIR;

 

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

 if (!is_dir($target_big)) {

  if ($in_debug=="true") echo "<br>Mkdir: '" . $target_big . "'";

   mkdir($target_big,0777);

 }	

   

// Resize Big image  

 $target_big .= $picture_name;

 if ($in_debug=="true") echo "<br>target_big: ".$target_big;

 if ($in_debug=="true") echo "<br> Copying / resizing big image ";

 if (IN_BIGIMAGE_WIDTH || IN_BIGIMAGE_HEIGHT ) {

   in_resize_image($filename['tmp_name'],$target_big,IN_BIGIMAGE_WIDTH,IN_BIGIMAGE_HEIGHT,IN_BIG_IMAGE_WAY_OF_RESIZE);       

 }

 else {

  copy($filename['tmp_name'], $target_big);

 }

   

 $target_small = $target . $picture_name;

 if ($in_debug=="true") echo "<br>target_small:  " . $target_small;

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

 in_resize_image($filename['tmp_name'],$target_small,SMALL_IMAGE_WIDTH,SMALL_IMAGE_HEIGHT,IN_SMALL_IMAGE_WAY_OF_RE
SIZE);       

 return $picture_name;

}



function in_str_strip_all ($str) {

// Strip non-alpha & non-numeric except ._-:

// 12 abc def/ghijkl'&%$mnon343dd -> 12abcdyY8-._efghijkl343ddi

// Use to get usefull Filenames for pictures

 return ereg_replace("[^[:alnum:]._-]", "", $str);

}



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

 global $in_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 

                                 

$image=GetImageSize($pic);

 $width=$image[0];

 $height=$image[1];

       if ($in_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 ($in_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 ($in_debug=="true") echo "<br><br>GD-Lib found start resizing";

 // Rechecking if Function exists (caused a error in a configuration)

       if ($in_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 ($in_debug=="true") echo "<br><br>Generated new Pic, switching fixed=" . $fixed;

	 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;

         $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));

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

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

         // backgroundcolor

         imagecolorresolvealpha ($dst_img, IN_IMAGE_BGCOLOUR_R,IN_IMAGE_BGCOLOUR_G, IN_IMAGE_BGCOLOUR_B, 0);

        // 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;

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

       break;

    

    

     case 2:

      // Thumbnail, Bild wird verkleiner 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 

         $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));

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

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

    }

    // 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 ($in_debug=="true") echo "<br>GD-Lib Image Format not supportet";

  }	

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

 }

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

 copy ($pic,$image_new);

 return false;

}

?>

 

thx for help

neogolit

Link to comment
Share on other sites

To point you in the right direction, I think the problem lies here:

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

 

To maintain quality you must resample instead of resize as you have done but you will get a black background. I think this is a bug. A way to get around this is to resample first to change the size of the image and then imagecopyresized to fit it into the correct coloured background. Only a little quality is lost this way

 

hth

David

Link to comment
Share on other sites

This is what I did in my contribution:

    $dstim = @imagecreatetruecolor ($width, $height);

$background_color = imagecolorallocate ($dstim, 255, 255, 255);

 $intim = @imagecreatetruecolor ($width, $height); 

 imagecopyresampled ($intim, $srcim, $offset_w, $offset_h, 0, 0, $scaled_w, $scaled_h, $scr_w, $scr_h);

 imagecopy ( $dstim, $intim, $offset_w, $offset_h, $offset_w, $offset_h, $scaled_w, $scaled_h);

 imagedestroy ($intim);

Creates an interim image which is resampled. Then centres it by pasting it onto a white background.

Maybe if you simply change the variable names to the ones you are using this will work.

Link to comment
Share on other sites

  • 5 months later...

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.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...