コード例 #1
0
ファイル: plugin.php プロジェクト: idea-lab/Spectrum
 private function resize_imagick($src, $width, $height, $quality, $preserveExif)
 {
     try {
         $img = new imagick($src);
         if (strtoupper($img->getImageFormat()) === 'JPEG') {
             $img->setImageCompression(imagick::COMPRESSION_JPEG);
             $img->setImageCompressionQuality($quality);
             if (!$preserveExif) {
                 try {
                     $orientation = $img->getImageOrientation();
                 } catch (ImagickException $e) {
                     $orientation = 0;
                 }
                 $img->stripImage();
                 if ($orientation) {
                     $img->setImageOrientation($orientation);
                 }
             }
         }
         $img->resizeImage($width, $height, Imagick::FILTER_LANCZOS, true);
         $result = $img->writeImage($src);
         $img->clear();
         $img->destroy();
         return $result ? true : false;
     } catch (Exception $e) {
         return false;
     }
 }
コード例 #2
0
 /**
  * Sets up an array of files to be deleted
  *
  * @param Model $model Model instance
  * @param string $field Name of field being modified
  * @param array $data array of data
  * @param array $options array of configuration settings for a field
  * @return boolean
  **/
 protected function _prepareFilesForDeletion(Model $model, $field, $data, $options = array())
 {
     if ($options['keepFilesOnDelete'] === true) {
         return array();
     }
     if (!strlen($data[$model->alias][$field])) {
         return $this->__filesToRemove;
     }
     if (!empty($options['fields']['dir']) && isset($data[$model->alias][$options['fields']['dir']])) {
         $dir = $data[$model->alias][$options['fields']['dir']];
     } else {
         if (in_array($options['pathMethod'], array('_getPathFlat', '_getPathPrimaryKey'))) {
             $model->id = $data[$model->alias][$model->primaryKey];
             $dir = call_user_func(array($this, '_getPath'), $model, $field);
         } else {
             CakeLog::error(sprintf('Cannot get directory to %s.%s: %s pathMethod is not supported.', $model->alias, $field, $options['pathMethod']));
         }
     }
     $filePathDir = $this->settings[$model->alias][$field]['path'] . (empty($dir) ? '' : $dir . DS);
     $filePath = $filePathDir . $data[$model->alias][$field];
     $pathInfo = $this->_pathinfo($filePath);
     if (!isset($this->__filesToRemove[$model->alias])) {
         $this->__filesToRemove[$model->alias] = array();
     }
     $this->__filesToRemove[$model->alias][] = $filePath;
     $this->__foldersToRemove[$model->alias][] = $dir;
     $createThumbnails = $options['thumbnails'];
     $hasThumbnails = !empty($options['thumbnailSizes']);
     if (!$createThumbnails || !$hasThumbnails) {
         return $this->__filesToRemove;
     }
     $directorySeparator = empty($dir) ? '' : DIRECTORY_SEPARATOR;
     $mimeType = $this->_getMimeType($filePath);
     $isMedia = $this->_isMedia($mimeType);
     $isImagickResize = $options['thumbnailMethod'] == 'imagick';
     $thumbnailType = $options['thumbnailType'];
     if ($isImagickResize) {
         if ($isMedia) {
             $thumbnailType = $options['mediaThumbnailType'];
         }
         if (!$thumbnailType || !is_string($thumbnailType)) {
             try {
                 $srcFile = $filePath;
                 $image = new imagick();
                 if ($isMedia) {
                     $image->setResolution(300, 300);
                     $srcFile = $srcFile . '[0]';
                 }
                 $image->readImage($srcFile);
                 $thumbnailType = $image->getImageFormat();
             } catch (Exception $e) {
                 $thumbnailType = 'png';
             }
         }
     } else {
         if (!$thumbnailType || !is_string($thumbnailType)) {
             $thumbnailType = $pathInfo['extension'];
         }
         if (!$thumbnailType) {
             $thumbnailType = 'png';
         }
     }
     foreach ($options['thumbnailSizes'] as $size => $geometry) {
         $fileName = str_replace(array('{size}', '{geometry}', '{filename}', '{primaryKey}', '{time}', '{microtime}'), array($size, $geometry, $pathInfo['filename'], $model->id, time(), microtime()), $options['thumbnailName']);
         $thumbnailPath = $options['thumbnailPath'];
         $thumbnailPath = $this->_pathThumbnail($model, $field, compact('geometry', 'size', 'thumbnailPath'));
         $thumbnailFilePath = "{$thumbnailPath}{$dir}{$directorySeparator}{$fileName}.{$thumbnailType}";
         $this->__filesToRemove[$model->alias][] = $thumbnailFilePath;
     }
     return $this->__filesToRemove;
 }
コード例 #3
0
ファイル: upload.php プロジェクト: ryantology/upload
 function _prepareFilesForDeletion(&$model, $field, $data, $options)
 {
     if (!strlen($data[$model->alias][$field])) {
         return $this->__filesToRemove;
     }
     $dir = $data[$model->alias][$options['fields']['dir']];
     $filePathDir = $this->settings[$model->alias][$field]['path'] . $dir . DS;
     $filePath = $filePathDir . $data[$model->alias][$field];
     $pathInfo = $this->_pathinfo($filePath);
     $this->__filesToRemove[$model->alias] = array();
     $this->__filesToRemove[$model->alias][] = $filePath;
     $createThumbnails = $options['thumbnails'];
     $hasThumbnails = !empty($options['thumbnailSizes']);
     if (!$createThumbnails || !$hasThumbnails) {
         return $this->__filesToRemove;
     }
     $DS = DIRECTORY_SEPARATOR;
     $mimeType = $this->_getMimeType($filePath);
     $isMedia = $this->_isMedia($model, $mimeType);
     $isImagickResize = $options['thumbnailMethod'] == 'imagick';
     $thumbnailType = $options['thumbnailType'];
     if ($isImagickResize) {
         if ($isMedia) {
             $thumbnailType = $options['mediaThumbnailType'];
         }
         if (!$thumbnailType || !is_string($thumbnailType)) {
             try {
                 $srcFile = $filePath;
                 $image = new imagick();
                 if ($isMedia) {
                     $image->setResolution(300, 300);
                     $srcFile = $srcFile . '[0]';
                 }
                 $image->readImage($srcFile);
                 $thumbnailType = $image->getImageFormat();
             } catch (Exception $e) {
                 $thumbnailType = 'png';
             }
         }
     } else {
         if (!$thumbnailType || !is_string($thumbnailType)) {
             $thumbnailType = $pathInfo['extension'];
         }
         if (!$thumbnailType) {
             $thumbnailType = 'png';
         }
     }
     foreach ($options['thumbnailSizes'] as $size => $geometry) {
         $fileName = str_replace(array('{size}', '{filename}'), array($size, $pathInfo['filename']), $options['thumbnailName']);
         $thumbnailPath = $options['thumbnailPath'];
         $thumbnailPath = $this->_pathThumbnail($model, $field, compact('geometry', 'size', 'thumbnailPath'));
         $thumbnailFilePath = "{$thumbnailPath}{$dir}{$DS}{$fileName}.{$thumbnailType}";
         $this->__filesToRemove[$model->alias][] = $thumbnailFilePath;
     }
     return $this->__filesToRemove;
 }