Exemplo n.º 1
0
 /**
  * Add or update a product image
  *
  * @param object $product Product object to add image
  */
 public function addProductImage($product, $method = 'auto')
 {
     /* Updating an existing product image */
     if ($id_image = (int) Tools::getValue('id_image')) {
         $image = new Image($id_image);
         if (!Validate::isLoadedObject($image)) {
             $this->_errors[] = Tools::displayError('An error occurred while loading object image.');
         } else {
             if (($cover = Tools::getValue('cover')) == 1) {
                 Image::deleteCover($product->id);
             }
             $image->cover = $cover;
             $this->validateRules('Image');
             $this->copyFromPost($image, 'image');
             if (sizeof($this->_errors) or !$image->update()) {
                 $this->_errors[] = Tools::displayError('An error occurred while updating image.');
             } elseif (isset($_FILES['image_product']['tmp_name']) and $_FILES['image_product']['tmp_name'] != NULL) {
                 $this->copyImage($product->id, $image->id, $method);
             }
         }
     } elseif (isset($_FILES['image_product']['name']) && $_FILES['image_product']['name'] != NULL) {
         if ($error = checkImageUploadError($_FILES['image_product'])) {
             $this->_errors[] = $error;
         }
         if (!sizeof($this->_errors) and isset($_FILES['image_product']['tmp_name']) and $_FILES['image_product']['tmp_name'] != NULL) {
             if (!Validate::isLoadedObject($product)) {
                 $this->_errors[] = Tools::displayError('Cannot add image because product add failed.');
             } elseif (substr($_FILES['image_product']['name'], -4) == '.zip') {
                 return $this->uploadImageZip($product);
             } else {
                 $image = new Image();
                 $image->id_product = (int) $product->id;
                 $_POST['id_product'] = $image->id_product;
                 $image->position = Image::getHighestPosition($product->id) + 1;
                 if (($cover = Tools::getValue('cover')) == 1) {
                     Image::deleteCover($product->id);
                 }
                 $image->cover = !$cover ? !sizeof($product->getImages(Configuration::get('PS_LANG_DEFAULT'))) : true;
                 $this->validateRules('Image', 'image');
                 $this->copyFromPost($image, 'image');
                 if (!sizeof($this->_errors)) {
                     if (!$image->add()) {
                         $this->_errors[] = Tools::displayError('Error while creating additional image');
                     } else {
                         $this->copyImage($product->id, $image->id, $method);
                     }
                     $id_image = $image->id;
                 }
             }
         }
     }
     if (isset($image) and Validate::isLoadedObject($image) and !file_exists(_PS_PROD_IMG_DIR_ . $image->getExistingImgPath() . '.' . $image->image_format)) {
         $image->delete();
     }
     if (sizeof($this->_errors)) {
         return false;
     }
     @unlink(_PS_TMP_IMG_DIR_ . '/product_' . $product->id . '.jpg');
     @unlink(_PS_TMP_IMG_DIR_ . '/product_mini_' . $product->id . '.jpg');
     return (isset($id_image) and is_int($id_image) and $id_image) ? $id_image : true;
 }
 /**
  * Write the posted image on disk
  * 
  * @param string $receptionPath
  * @param int $destWidth
  * @param int $destHeight
  * @param array $imageTypes
  * @param string $parentPath
  * @return boolean
  */
 private function writePostedImageOnDisk($receptionPath, $destWidth = null, $destHeight = null, $imageTypes = null, $parentPath = null)
 {
     if ($this->wsObject->method == 'PUT') {
         if (isset($_FILES['image']['tmp_name']) && $_FILES['image']['tmp_name']) {
             $file = $_FILES['image'];
             if ($file['size'] > $this->imgMaxUploadSize) {
                 throw new WebserviceException(sprintf('The image size is too large (maximum allowed is %d KB)', $this->imgMaxUploadSize / 1000), array(72, 400));
             }
             // Get mime content type
             $mime_type = false;
             if (Tools::isCallable('finfo_open')) {
                 $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
                 $finfo = finfo_open($const);
                 $mime_type = finfo_file($finfo, $file['tmp_name']);
                 finfo_close($finfo);
             } elseif (Tools::isCallable('mime_content_type')) {
                 $mime_type = mime_content_type($file['tmp_name']);
             } elseif (Tools::isCallable('exec')) {
                 $mime_type = trim(exec('file -b --mime-type ' . escapeshellarg($file['tmp_name'])));
             }
             if (empty($mime_type) || $mime_type == 'regular file') {
                 $mime_type = $file['type'];
             }
             if (($pos = strpos($mime_type, ';')) !== false) {
                 $mime_type = substr($mime_type, 0, $pos);
             }
             // Check mime content type
             if (!$mime_type || !in_array($mime_type, $this->acceptedImgMimeTypes)) {
                 throw new WebserviceException('This type of image format not recognized, allowed formats are: ' . implode('", "', $this->acceptedImgMimeTypes), array(73, 400));
             } elseif ($file['error']) {
                 throw new WebserviceException('Error while uploading image. Please change your server\'s settings', array(74, 400));
             }
             // Try to copy image file to a temporary file
             if (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) or !move_uploaded_file($_FILES['image']['tmp_name'], $tmpName)) {
                 throw new WebserviceException('Error while copying image to the temporary directory', array(75, 400));
             } else {
                 $result = $this->writeImageOnDisk($tmpName, $receptionPath, $destWidth, $destHeight, $imageTypes, $parentPath);
             }
             @unlink($tmpName);
             return $result;
         } else {
             throw new WebserviceException('Please set an "image" parameter with image data for value', array(76, 400));
         }
     } elseif ($this->wsObject->method == 'POST') {
         if (isset($_FILES['image']['tmp_name']) && $_FILES['image']['tmp_name']) {
             $file = $_FILES['image'];
             if ($file['size'] > $this->imgMaxUploadSize) {
                 throw new WebserviceException(sprintf('The image size is too large (maximum allowed is %d KB)', $this->imgMaxUploadSize / 1000), array(72, 400));
             }
             require_once _PS_ROOT_DIR_ . '/images.inc.php';
             if ($error = checkImageUploadError($file)) {
                 throw new WebserviceException('Image upload error : ' . $error, array(76, 400));
             }
             if (isset($file['tmp_name']) && $file['tmp_name'] != null) {
                 if ($this->imageType == 'products') {
                     $product = new Product((int) $this->wsObject->urlSegment[2]);
                     if (!Validate::isLoadedObject($product)) {
                         throw new WebserviceException('Product ' . (int) $this->wsObject->urlSegment[2] . ' doesn\'t exists', array(76, 400));
                     }
                     $image = new Image();
                     $image->id_product = (int) $product->id;
                     $image->position = Image::getHighestPosition($product->id) + 1;
                     if (!$image->add()) {
                         throw new WebserviceException('Error while creating image', array(76, 400));
                     }
                     if (!Validate::isLoadedObject($product)) {
                         throw new WebserviceException('Product ' . (int) $this->wsObject->urlSegment[2] . ' doesn\'t exists', array(76, 400));
                     }
                 }
                 // copy image
                 if (!isset($file['tmp_name'])) {
                     return false;
                 }
                 if ($error = checkImage($file, $this->imgMaxUploadSize)) {
                     throw new WebserviceException('Bad image : ' . $error, array(76, 400));
                 }
                 if ($this->imageType == 'products') {
                     $image = new Image($image->id);
                     if (!(Configuration::get('PS_OLD_FILESYSTEM') && file_exists(_PS_PROD_IMG_DIR_ . $product->id . '-' . $image->id . '.jpg'))) {
                         $image->createImgFolder();
                     }
                     if (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) or !move_uploaded_file($file['tmp_name'], $tmpName)) {
                         throw new WebserviceException('An error occurred during the image upload', array(76, 400));
                     } elseif (!imageResize($tmpName, _PS_PROD_IMG_DIR_ . $image->getExistingImgPath() . '.' . $image->image_format)) {
                         throw new WebserviceException('An error occurred while copying image', array(76, 400));
                     } else {
                         $imagesTypes = ImageType::getImagesTypes('products');
                         foreach ($imagesTypes as $imageType) {
                             if (!imageResize($tmpName, _PS_PROD_IMG_DIR_ . $image->getExistingImgPath() . '-' . stripslashes($imageType['name']) . '.' . $image->image_format, $imageType['width'], $imageType['height'], $image->image_format)) {
                                 $this->_errors[] = Tools::displayError('An error occurred while copying image:') . ' ' . stripslashes($imageType['name']);
                             }
                         }
                     }
                     @unlink($tmpName);
                     $this->imgToDisplay = _PS_PROD_IMG_DIR_ . $image->getExistingImgPath() . '.' . $image->image_format;
                     if (!isset($this->_errors) || !count($this->_errors)) {
                         header('Location: ' . $this->wsObject->wsUrl . $this->wsObject->urlSegment[0] . '/products/' . $product->id . '/' . $image->id);
                         die;
                     }
                 } elseif (isset($this->wsObject->urlSegment[1]) && in_array($this->wsObject->urlSegment[1], array('categories', 'manufacturers', 'suppliers', 'stores')) && isset($this->wsObject->urlSegment[2])) {
                     if (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) or !move_uploaded_file($file['tmp_name'], $tmpName)) {
                         throw new WebserviceException('An error occurred during the image upload', array(76, 400));
                     } elseif (!imageResize($tmpName, $receptionPath)) {
                         throw new WebserviceException('An error occurred while copying image', array(76, 400));
                     } else {
                         $imagesTypes = ImageType::getImagesTypes('categories');
                         foreach ($imagesTypes as $imageType) {
                             if (!imageResize($tmpName, _PS_CAT_IMG_DIR_ . (int) $this->wsObject->urlSegment[2] . '-' . stripslashes($imageType['name']) . '.jpg', (int) $imageType['width'], (int) $imageType['height'])) {
                                 $this->_errors[] = Tools::displayError('An error occurred while copying image:') . ' ' . stripslashes($imageType['name']);
                             }
                         }
                     }
                     @unlink(_PS_TMP_IMG_DIR_ . $tmpName);
                     $this->imgToDisplay = $receptionPath;
                 }
                 return true;
             }
         }
     } else {
         throw new WebserviceException('Method ' . $this->wsObject->method . ' is not allowed for an image resource', array(77, 405));
     }
 }
Exemplo n.º 3
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);
         }
     }
 }