Example #1
0
 private function _postValidation()
 {
     $yalign = Tools::getValue('yalign');
     $xalign = Tools::getValue('xalign');
     $transparency = intval(Tools::getValue('transparency'));
     $image_types = Tools::getValue('image_types');
     if (empty($transparency)) {
         $this->_postErrors[] = $this->l('Transparency is required.');
     } elseif ($transparency < 0 || $transparency > 100) {
         $this->_postErrors[] = $this->l('Transparency is not in allowed range.');
     }
     if (empty($yalign)) {
         $this->_postErrors[] = $this->l('Y-Align is required.');
     } elseif (!in_array($yalign, $this->yaligns)) {
         $this->_postErrors[] = $this->l('Y-Align is not in allowed range.');
     }
     if (empty($xalign)) {
         $this->_postErrors[] = $this->l('X-Align is required.');
     } elseif (!in_array($xalign, $this->xaligns)) {
         $this->_postErrors[] = $this->l('X-Align is not in allowed range.');
     }
     if (empty($image_types)) {
         $this->_postErrors[] = $this->l('At least one image type is required.');
     }
     if (isset($_FILES['PS_WATERMARK']['tmp_name']) and !empty($_FILES['PS_WATERMARK']['tmp_name'])) {
         if (!isPicture($_FILES['PS_WATERMARK'], array('image/gif'))) {
             $this->_postErrors[] = $this->l('image must be at GIF format');
         }
     }
     return !sizeof($this->_postErrors) ? true : false;
 }
Example #2
0
/**
 * Check image upload
 *
 * @param array $file Upload $_FILE value
 * @param integer $maxFileSize Maximum upload size
 */
function checkImage($file, $maxFileSize)
{
    if ($file['size'] > $maxFileSize) {
        return Tools::displayError('image is too large') . ' (' . $file['size'] / 1000 . Tools::displayError('KB') . '). ' . Tools::displayError('Maximum allowed:') . ' ' . $maxFileSize / 1000 . Tools::displayError('KB');
    }
    if (!isPicture($file)) {
        return Tools::displayError('image format not recognized, allowed formats are: .gif, .jpg, .png');
    }
    if ($file['error']) {
        return Tools::displayError('error while uploading image; change your server\'s settings');
    }
    return false;
}
 public function uploadImageZip($product)
 {
     // Move the ZIP file to the img/tmp directory
     if (!($zipfile = tempnam(_PS_TMP_IMG_DIR_, 'PS')) or !move_uploaded_file($_FILES['image_product']['tmp_name'], $zipfile)) {
         $this->_errors[] = Tools::displayError('An error occurred during the ZIP file upload.');
         return false;
     }
     // Unzip the file to a subdirectory
     $subdir = _PS_TMP_IMG_DIR_ . uniqid() . '/';
     try {
         if (!Tools::ZipExtract($zipfile, $subdir)) {
             throw new Exception(Tools::displayError('An error occurred while unzipping your file.'));
         }
         $types = array('.gif' => 'image/gif', '.jpeg' => 'image/jpeg', '.jpg' => 'image/jpg', '.png' => 'image/png');
         $_POST['id_product'] = (int) $product->id;
         $imagesTypes = ImageType::getImagesTypes('products');
         $highestPosition = Image::getHighestPosition($product->id);
         foreach (scandir($subdir) as $file) {
             if ($file[0] == '.') {
                 continue;
             }
             // Create image object
             $image = new Image();
             $image->id_product = (int) $product->id;
             $image->position = ++$highestPosition;
             $image->cover = $highestPosition == 1 ? true : false;
             // Call automated copy function
             $this->validateRules('Image', 'image');
             $this->copyFromPost($image, 'image');
             if (sizeof($this->_errors)) {
                 throw new Exception('');
             }
             if (!$image->add()) {
                 throw new Exception(Tools::displayError('Error while creating additional image'));
             }
             if (filesize($subdir . $file) > $this->maxImageSize) {
                 $image->delete();
                 throw new Exception(Tools::displayError('Image is too large') . ' (' . filesize($subdir . $file) / 1000 . Tools::displayError('kB') . '). ' . Tools::displayError('Maximum allowed:') . ' ' . $this->maxImageSize / 1000 . Tools::displayError('kB'));
             }
             $ext = substr($file, -4) == 'jpeg' ? '.jpeg' : substr($file, -4);
             $type = isset($types[$ext]) ? $types[$ext] : '';
             if (!isPicture(array('tmp_name' => $subdir . $file, 'type' => $type))) {
                 $image->delete();
                 throw new Exception(Tools::displayError('Image format not recognized, allowed formats are: .gif, .jpg, .png'));
             }
             if (!($new_path = $image->getPathForCreation())) {
                 throw new Exception(Tools::displayError('An error occurred during new folder creation'));
             }
             if (!imageResize($subdir . $file, $new_path . '.' . $image->image_format)) {
                 $image->delete();
                 throw new Exception(Tools::displayError('An error occurred while resizing image.'));
             }
             foreach ($imagesTypes as $k => $imageType) {
                 if (!imageResize($image->getPathForCreation() . '.jpg', $image->getPathForCreation() . '-' . stripslashes($imageType['name']) . '.jpg', $imageType['width'], $imageType['height'])) {
                     $image->delete();
                     throw new Exception(Tools::displayError('An error occurred while copying image.') . ' ' . stripslashes($imageType['name']));
                 }
             }
             Module::hookExec('watermark', array('id_image' => $image->id, 'id_product' => $image->id_product));
         }
     } catch (Exception $e) {
         if ($error = $e->getMessage()) {
         }
         $this->_errors[] = $error;
         Tools::deleteDirectory($subdir);
         return false;
     }
     Tools::deleteDirectory($subdir);
     return true;
 }
Example #4
0
 public function addProductImages3D($product_id, $pack = "3d_image_pack", $dirs = "images3d")
 {
     if (isset($_FILES[$pack]) && $_FILES[$pack] && $_FILES[$pack]['name']) {
         if ($error = checkImageUploadError($_FILES[$pack])) {
             $this->_errors[] = $error;
         }
         if (!sizeof($this->_errors) and !empty($_FILES[$pack]['tmp_name'])) {
             try {
                 if (!($zipfile = tempnam(_PS_TMP_IMG_DIR_, 'PS')) or !move_uploaded_file($_FILES[$pack]['tmp_name'], $zipfile)) {
                     return false;
                     //throw new Exception(Tools::displayError('An error occurred during the ZIP file upload.'));
                 }
                 $subdir = _PS_IMG_DIR_ . $dirs . '/' . $product_id . '/';
                 Tools::deleteDirectory($subdir);
                 if (!Tools::ZipExtract($zipfile, $subdir)) {
                     throw new Exception(Tools::displayError('An error occurred while unzipping your file.'));
                 }
                 $types = array('.gif' => 'image/gif', '.jpeg' => 'image/jpeg', '.jpg' => 'image/jpg', '.png' => 'image/png');
                 $files = array_slice(scandir($subdir), 2);
                 foreach ($files as $file) {
                     if (filesize($subdir . $file) > $this->maxImageSize) {
                         throw new Exception(Tools::displayError('Image is too large') . ' (' . filesize($subdir . $file) / 1000 . Tools::displayError('kB') . '). ' . Tools::displayError('Maximum allowed:') . ' ' . $this->maxImageSize / 1000 . Tools::displayError('kB'));
                     }
                     $ext = substr($file, -4) == 'jpeg' ? '.jpeg' : substr($file, -4);
                     $type = isset($types[$ext]) ? $types[$ext] : '';
                     if (!isPicture(array('tmp_name' => $subdir . $file, 'type' => $type))) {
                         throw new Exception(Tools::displayError('Image format not recognized, allowed formats are: .gif, .jpg, .png'));
                     }
                     imageResize($subdir . $file, $subdir . $file, 600, 600);
                     //Module::hookExec('watermark', array('id_image' => $image->id, 'id_product' => $image->id_product));
                 }
                 $images_3d_count = count($files);
             } catch (Exception $e) {
                 $error = $e->getMessage();
                 $this->_errors[] = $error;
                 Tools::deleteDirectory($subdir);
             }
             unlink($zipfile);
         }
     }
 }