예제 #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;
 }