I thought this might help someone here.
Here's a little snippet of code I just added to my upload.php that take care of all this resizing and folder placement for me on the fly. It requires gd library I believe. This comes in very handy since I give my clients the login and pass to manage their own ZenCart and they are NOT Computer savvy most of the time. FTP is beyond them.
I'm sure there are better ways to accomplish this, like with imagemagick, but I don't have the time to mess with that right now and i just wanted to share this. it also just ignores everything here if they upload something incompatible like a bmp.
Just put this code in the parse() function of the upload class in:
admin\includes\classes\upload.php
After this block of Code:
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' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''),
'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''),
'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''),
'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : ''));
}
Code To Add:
/***************************************/
/* Changes To Limit Product Image Size */
/***************************************/
if($this->file == 'products_image') {
// Get The Image as a String
$pictureString = fread(fopen($file['tmp_name'], "rb"), $file['size']);
$image = imagecreatefromstring($pictureString);
// This validates whether or not a valid image was uploaded.
if ($image !== false) {
// Dimensions For The Uploaded Picture
$larWidth = 600;
$larHeight = 600;
$larLoc = '/page/to/the/folder/large/'.substr($file['name'], 0, -4).'_LRG.jpg';
$medWidth = 350;
$medHeight = 350;
$medLoc = '/page/to/the/folder/medium/'.substr($file['name'], 0, -4).'_MED.jpg';
$smallWidth = 138;
$smallHeight = 138;
$smallLoc = $file['tmp_name'];
// Set Current For All Sizes
$lWidth = $mWidth = $sWidth = $originalWidth = imagesx($image);
$lHeight = $mHeight = $sHeight = $originalHeight = imagesy($image);
// Large
if($lWidth > $larWidth) {
$lHeight = $lHeight / ($lWidth / $larWidth);
$lWidth = $larWidth;
}
if($lHeight > $larHeight) {
$lWidth = $lWidth / ($lHeight / $larHeight);
$lHeight = $larHeight;
}
if($lWidth != $originalWidth || $lHeight != $originalHeight) {
$newImage = imagecreatetruecolor($lWidth, $lHeight);
if(imagecopyresampled($newImage,$image,0,0,0,0,$lWidth,$lHeight,$originalWidth,$originalHeight)) {
@imagejpeg($newImage, $larLoc, 90);
}
}
else {
@imagejpeg($image, $larLoc, 90);
}
// Medium
if($mWidth > $medWidth) {
$mHeight = $mHeight / ($mWidth / $medWidth);
$mWidth = $medWidth;
}
if($mHeight > $medHeight) {
$mWidth = $mWidth / ($mHeight / $medHeight);
$mHeight = $medHeight;
}
if($mWidth != $originalWidth || $mHeight != $originalHeight) {
$newImage = imagecreatetruecolor($mWidth, $mHeight);
if(imagecopyresampled($newImage,$image,0,0,0,0,$mWidth,$mHeight,$originalWidth,$originalHeight)) {
@imagejpeg($newImage, $medLoc, 90);
}
}
else {
@imagejpeg($image, $medLoc, 90);
}
// Small
if($sWidth > $smallWidth) {
$sHeight = $sHeight / ($sWidth / $smallWidth);
$sWidth = $smallWidth;
}
if($sHeight > $smallHeight) {
$sWidth = $sWidth / ($sHeight / $smallHeight);
$sHeight = $smallHeight;
}
if($sWidth != $originalWidth || $sHeight != $originalHeight) {
$newImage = imagecreatetruecolor($sWidth, $sHeight);
if(imagecopyresampled($newImage,$image,0,0,0,0,$sWidth,$sHeight,$originalWidth,$originalHeight)) {
@imagejpeg($newImage, $smallLoc, 90);
}
}
else {
@imagejpeg($image, $smallLoc, 90);
}
}
}
if($this->file == 'categories_image') {
// Get The Image as a String
$pictureString = fread(fopen($file['tmp_name'], "rb"), $file['size']);
$image = imagecreatefromstring($pictureString);
// This validates whether or not a valid image was uploaded.
if ($image !== false) {
// Dimensions For The Uploaded Picture
$catWidth = 200;
$catHeight = 200;
// Set Current For All Sizes
$cWidth = $originalWidth = imagesx($image);
$cHeight = $originalHeight = imagesy($image);
// Small
if($cWidth > $catWidth) {
$cHeight = $cHeight / ($cWidth / $catWidth);
$cWidth = $catWidth;
}
if($cHeight > $catHeight) {
$cWidth = $cWidth / ($cHeight / $catHeight);
$cHeight = $catHeight;
}
if($cWidth != $originalWidth || $cHeight != $originalHeight) {
$newImage = imagecreatetruecolor($cWidth, $cHeight);
if(imagecopyresampled($newImage,$image,0,0,0,0,$cWidth,$cHeight,$originalWidth,$originalHeight)) {
@imagejpeg($newImage, $file['tmp_name'], 90);
}
}
else {
@imagejpeg($image, $file['tmp_name'], 90);
}
}
}
/* END CHANGE */
You will need to set the location variables so that the pictures get put into the correct directories on your server. You can also change the size variables as well. Later