Exemplo n.º 1
0
 private static function convertTo($source, $targetFormat, $deleteSource, $quality = 100)
 {
     $ext = FileUtil::getFileExtension($source);
     $absoluteSource = FileUtil::getAbsolutePath($source);
     $tempImage = null;
     if ($ext === 'jpg' || $ext === 'jpeg') {
         $tempImage = imagecreatefromjpeg($absoluteSource);
     } else {
         if ($ext === 'png') {
             $tempImage = imagecreatefrompng($absoluteSource);
         } else {
             if ($ext === 'gif') {
                 $tempImage = imagecreatefromgif($absoluteSource);
             } else {
                 throw new InvalidImageFormatException("Image with format ." . $ext . " can not be converted");
             }
         }
     }
     $targetFilename = FileUtil::getFilePathWithoutExtension($absoluteSource) . '.' . $targetFormat;
     if ($absoluteSource === $targetFilename) {
         imagedestroy($tempImage);
         return;
     }
     try {
         switch ($targetFormat) {
             case 'jpeg':
                 imagejpeg($tempImage, $targetFilename, $quality);
                 break;
             case 'png':
                 imagepng($tempImage, $targetFilename);
                 break;
             case 'gif':
                 imagegif($tempImage, $targetFilename);
                 break;
             default:
                 throw new InvalidImageFormatException("Conversion image to ." . $targetFormat . " not supported");
         }
         if ($deleteSource) {
             FileUtil::deleteFile($source);
         }
         imagedestroy($tempImage);
     } catch (\Exception $e) {
         imagedestroy($tempImage);
     }
 }
Exemplo n.º 2
0
 public function saveImage($catalogId, $productId)
 {
     if (!$this->isPostRequest()) {
         exit;
     }
     try {
         $uploadedFile = $_FILES['productImage'];
         if (empty($uploadedFile) || $uploadedFile['error'] != 0) {
             messages::add('Product_ImageNotUploaded', 1, 'error');
             $this->redirectBack();
         }
         $fileExt = FileUtil::getFileExtension($uploadedFile['name']);
         if (!in_array($fileExt, ImageUtil::getSupportedExtension())) {
             messages::add('Product_InvalidImageFormat', 1, 'error');
             $this->redirectBack();
         }
         $imgDir = ProductModel::getInstance()->getImagesDir($catalogId);
         FileUtil::createDirIfNotExist($imgDir);
         $targetFilename = $productId . '.' . $fileExt;
         move_uploaded_file($uploadedFile['tmp_name'], $imgDir . $targetFilename);
         ImageUtil::convertToJpeg($imgDir . $targetFilename);
         messages::add('Product_ImageSaved', 1, 'success');
         $this->systemRedirect('/catalog/editProducts/' . $catalogId);
     } catch (PhException $e) {
         messages::add('Product_ImageUploadError', 1, 'success');
         $this->redirectBack();
     }
 }
Exemplo n.º 3
0
 public function getMaxImageSizeToUpload()
 {
     return FileUtil::getMaxFileSizeToUpload();
 }