Forums / All Other Contributions/Addons / Automatic Resize of Uploaded Images?

Automatic Resize of Uploaded Images?

Views: 751

Results 1 to 3 of 3
23 Feb 2012, 7:30 PM
#1
jjj0923 avatar

jjj0923

New Zenner

Join Date:
Nov 2011
Posts:
59
Plugin Contributions:
0

Automatic Resize of Uploaded Images?

Not sure why Zencart doesn't do this - perhaps I'm missing something really basic in the configuration of the product - I admit I'm a newbie...

In any event once I turned my customer loose with his store he started uploading HUGE images.

He's not a computer geek and does not know how to resize images, he''s a business man but we use the Lightbox add-on and his images were taking over the screen, so I did a little digging around in the code last night and wrote this code this morning.

On his site I've established a maximum image size of 600 x 800 - you can change it to anything you want. I'm running on a dedicated centos 5.7 box and have Image Magick installed (which I use to resize the images). This code only works for JPG files but could easily be conditionalized to test the file extension and do gifs also. My customers only uses jpgs.

I don't profess to be a crackerjack programmer but I've written a decent amount of php over the years and it has always worked for me.

The code below is a snippet from /YOURADMINFOLDER/includes/classes/upload.php

look for this code:

          if ($this->message_location == 'direct') {
            $messageStack->add(sprintf(SUCCESS_FILE_SAVED_SUCCESSFULLY,$this->filename), 'success');
          } else {
            $messageStack->add_session(sprintf(SUCCESS_FILE_SAVED_SUCCESSFULLY,$this->filename), 'success');
          }

          return true;

and replace it with:

          if ($this->message_location == 'direct') {
            $messageStack->add(sprintf(SUCCESS_FILE_SAVED_SUCCESSFULLY,$this->filename), 'success');
          } else {
            $messageStack->add_session(sprintf(SUCCESS_FILE_SAVED_SUCCESSFULLY,$this->filename), 'success');
          }

//		mod jjj 2/23/12 to resize uploaded images

		$theimage = $this->destination . $this->filename;
		$myimg = ImageCreateFromJpeg($theimage);
		$theimagew = imagesx($myimg);
		$theimageh = imagesy($myimg);
		$thestring = $theimage . " width=". $theimagew . " height=". $theimageh . "\n\r";
		if ( ($theimagew > 600) or ($theimageh > 800) ) 
		  {	
		    $newwidth = $theimagew;
		    $newheight = $theimageh;
		    while ( ($newwidth > 600) or ($newheight > 800) )
		     {
		      $newwidth = round ($newwidth * .99);
		      $newheight = round ($newheight * .99);
		     }
		  $syscmd = "mogrify -resize ". $newwidth . "x" . $newheight . " " . $theimage;	
		  system($syscmd, $retval);

		}
          return true;
23 Feb 2012, 8:08 PM
#2
jjj0923 avatar

jjj0923

New Zenner

Join Date:
Nov 2011
Posts:
59
Plugin Contributions:
0

Re: Automatic Resize of Uploaded Images?

in case you're wondering that $thestring is for - I had some debugging code in there originally that would append information to a text file and I forgot to remove that line before posting the code.

:)

23 Feb 2012, 9:32 PM
#3
jjj0923 avatar

jjj0923

New Zenner

Join Date:
Nov 2011
Posts:
59
Plugin Contributions:
0

Re: Automatic Resize of Uploaded Images?

this code will do the trick to handle gifs and pngs

		$theimage = $this->destination . $this->filename;
		if (stristr($theimage,'.jpg')) { $myimg = ImageCreateFromJpeg($theimage); }
		if (stristr($theimage,'.gif')) { $myimg = ImageCreateFromGif($theimage); }
		if (stristr($theimage,'.png')) { $myimg = ImageCreateFrompng($theimage); }