Beispiel #1
0
 /**
  * Resize and cache image file.
  *
  * @param $file
  * @param $width
  * @param $height
  * @param null $folder
  * @return string
  */
 public function resizeImage($file, $width, $height, $folder = null)
 {
     // init vars
     $width = (int) $width;
     $height = (int) $height;
     $fileInfo = pathinfo($file);
     $thumbName = $fileInfo['filename'] . '_' . md5($file . $width . $height) . '.' . $fileInfo['extension'];
     $cacheTime = 86400;
     if (!$folder) {
         $folder = $fileInfo['dirname'];
     }
     $thumbFile = $folder . $thumbName;
     if ($this->check() && (!is_file($thumbFile) || $cacheTime > 0 && time() > filemtime($thumbFile) + $cacheTime)) {
         $Thumbnail = new ImageThumbnail($file);
         if ($width > 0 && $height > 0) {
             $Thumbnail->setSize($width, $height);
             $Thumbnail->save($thumbFile);
         } else {
             if ($width > 0 && $height == 0) {
                 $Thumbnail->sizeWidth($width);
                 $Thumbnail->save($thumbFile);
             } else {
                 if ($width == 0 && $height > 0) {
                     $Thumbnail->sizeHeight($height);
                     $Thumbnail->save($thumbFile);
                 } else {
                     $File = new File($file);
                     if (file_exists($file)) {
                         $File->copy($thumbFile);
                     }
                 }
             }
         }
     }
     if (is_file($thumbFile)) {
         return $thumbFile;
     }
     return $file;
 }
Beispiel #2
0
 /**
  * Upload user profile image.
  *
  * @param User $user
  */
 protected function _uploadAvatar(User $user)
 {
     if (isset($this->_tmpImage) && file_exists($this->_tmpImage['tmp_name'])) {
         $imageInfo = pathinfo($this->_tmpImage['name']);
         $imageName = 'orig_' . $user->get('id') . '.' . $imageInfo['extension'];
         $folderPath = $this->_path->entityFolderPath($user->get('id'), DS);
         $imagePath = $folderPath . $imageName;
         $Folder = new Folder();
         //  Delete user folder if upload new image.
         if (is_dir($folderPath)) {
             $Folder->delete($folderPath);
         }
         if (!is_dir($folderPath)) {
             $Folder->create($folderPath);
         }
         if (is_dir($folderPath) && move_uploaded_file($this->_tmpImage['tmp_name'], $imagePath)) {
             $user->set('image', $imageName);
             $thumbName = 'thumb_' . $user->get('id') . '.' . $imageInfo['extension'];
             //  Resize original image.
             $Resize = new ImageThumbnail($imagePath);
             $Resize->sizeWidth(450);
             $Resize->save($imagePath);
             //  Resize and crop thumb.
             $Thumbnail = new ImageThumbnail($imagePath);
             $Thumbnail->setResize(false);
             $Thumbnail->setSize($this->_config['thumbWidth'], $this->_config['thumbHeight']);
             $Thumbnail->save($folderPath . $thumbName);
         }
     }
 }