/**
  * Create thumnbnail and return it's URL on success
  *
  * @param  string $path file path
  * @param $stat
  * @return false|string
  * @internal param string $mime file mime type
  * @author Dmitry (dio) Levashov
  */
 protected function createTmb($path, $stat)
 {
     if (!$stat || !$this->canCreateTmb($path, $stat)) {
         return false;
     }
     $name = $this->tmbname($stat);
     $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . $name;
     // copy image into tmbPath so some drivers does not store files on local fs
     if (($src = $this->fopenCE($path, 'rb')) == false) {
         return false;
     }
     if (($trg = fopen($tmb, 'wb')) == false) {
         $this->fcloseCE($src, $path);
         return false;
     }
     while (!feof($src)) {
         fwrite($trg, fread($src, 8192));
     }
     $this->fcloseCE($src, $path);
     fclose($trg);
     $result = false;
     $tmbSize = $this->tmbSize;
     if ($this->imgLib === 'imagick') {
         try {
             $imagickTest = new imagick($tmb);
             $imagickTest->clear();
             $imagickTest = true;
         } catch (Exception $e) {
             $imagickTest = false;
         }
     }
     if ($this->imgLib === 'imagick' && !$imagickTest || ($s = @getimagesize($tmb)) === false) {
         if ($this->imgLib === 'imagick') {
             try {
                 $imagick = new imagick();
                 $imagick->setBackgroundColor(new ImagickPixel($this->options['tmbBgColor']));
                 $imagick->readImage($this->getExtentionByMime($stat['mime'], ':') . $tmb);
                 $imagick->setImageFormat('png');
                 $imagick->writeImage($tmb);
                 $imagick->clear();
                 if (($s = @getimagesize($tmb)) !== false) {
                     $result = true;
                 }
             } catch (Exception $e) {
             }
         }
         if (!$result) {
             unlink($tmb);
             return false;
         }
         $result = false;
     }
     /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
     if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) {
         $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png');
     } else {
         if ($this->options['tmbCrop']) {
             $result = $tmb;
             /* Resize and crop if image bigger than thumbnail */
             if (!($s[0] > $tmbSize && $s[1] <= $tmbSize || $s[0] <= $tmbSize && $s[1] > $tmbSize) || $s[0] > $tmbSize && $s[1] > $tmbSize) {
                 $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, false, 'png');
             }
             if ($result && ($s = @getimagesize($tmb)) != false) {
                 $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize) / 2) : 0;
                 $y = $s[1] > $tmbSize ? intval(($s[1] - $tmbSize) / 2) : 0;
                 $result = $this->imgCrop($result, $tmbSize, $tmbSize, $x, $y, 'png');
             } else {
                 $result = false;
             }
         } else {
             $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, true, 'png');
         }
         if ($result) {
             $result = $this->imgSquareFit($result, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png');
         }
     }
     if (!$result) {
         unlink($tmb);
         return false;
     }
     return $name;
 }
 /**
  * Create thumnbnail and return it's URL on success
  *
  * @param  string $path file path
  * @param $stat
  * @return false|string
  * @internal param string $mime file mime type
  * @author Dmitry (dio) Levashov
  */
 protected function createTmb($path, $stat)
 {
     if (!$stat || !$this->canCreateTmb($path, $stat)) {
         return false;
     }
     $name = $this->tmbname($stat);
     $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . $name;
     $maxlength = -1;
     $imgConverter = null;
     // check imgConverter
     $mime = strtolower($stat['mime']);
     list($type) = explode('/', $mime);
     if (isset($this->imgConverter[$mime])) {
         $imgConverter = $this->imgConverter[$mime]['func'];
         if (!empty($this->imgConverter[$mime]['maxlen'])) {
             $maxlength = intval($this->imgConverter[$mime]['maxlen']);
         }
     } else {
         if (isset($this->imgConverter[$type])) {
             $imgConverter = $this->imgConverter[$type]['func'];
             if (!empty($this->imgConverter[$type]['maxlen'])) {
                 $maxlength = intval($this->imgConverter[$type]['maxlen']);
             }
         }
     }
     if ($imgConverter && !is_callable($imgConverter)) {
         return false;
     }
     // copy image into tmbPath so some drivers does not store files on local fs
     if (($src = $this->fopenCE($path, 'rb')) == false) {
         return false;
     }
     if (($trg = fopen($tmb, 'wb')) == false) {
         $this->fcloseCE($src, $path);
         return false;
     }
     stream_copy_to_stream($src, $trg, $maxlength);
     $this->fcloseCE($src, $path);
     fclose($trg);
     // call imgConverter
     if ($imgConverter) {
         if (!call_user_func_array($imgConverter, array($tmb, $stat, $this))) {
             file_exists($tmb) && unlink($tmb);
             return false;
         }
     }
     $result = false;
     $tmbSize = $this->tmbSize;
     if ($this->imgLib === 'imagick') {
         try {
             $imagickTest = new imagick($tmb);
             $imagickTest->clear();
             $imagickTest = true;
         } catch (Exception $e) {
             $imagickTest = false;
         }
     }
     if ($this->imgLib === 'imagick' && !$imagickTest || ($s = getimagesize($tmb)) === false) {
         if ($this->imgLib === 'imagick') {
             $bgcolor = $this->options['tmbBgColor'];
             if ($bgcolor === 'transparent') {
                 $bgcolor = 'rgba(255, 255, 255, 0.0)';
             }
             try {
                 $imagick = new imagick();
                 $imagick->setBackgroundColor(new ImagickPixel($bgcolor));
                 $imagick->readImage($this->getExtentionByMime($stat['mime'], ':') . $tmb);
                 $imagick->setImageFormat('png');
                 $imagick->writeImage($tmb);
                 $imagick->clear();
                 if (($s = getimagesize($tmb)) !== false) {
                     $result = true;
                 }
             } catch (Exception $e) {
             }
         }
         if (!$result) {
             file_exists($tmb) && unlink($tmb);
             return false;
         }
         $result = false;
     }
     /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
     if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) {
         $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png');
     } else {
         if ($this->options['tmbCrop']) {
             $result = $tmb;
             /* Resize and crop if image bigger than thumbnail */
             if (!($s[0] > $tmbSize && $s[1] <= $tmbSize || $s[0] <= $tmbSize && $s[1] > $tmbSize) || $s[0] > $tmbSize && $s[1] > $tmbSize) {
                 $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, false, 'png');
             }
             if ($result && ($s = getimagesize($tmb)) != false) {
                 $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize) / 2) : 0;
                 $y = $s[1] > $tmbSize ? intval(($s[1] - $tmbSize) / 2) : 0;
                 $result = $this->imgCrop($result, $tmbSize, $tmbSize, $x, $y, 'png');
             } else {
                 $result = false;
             }
         } else {
             $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, true, 'png');
         }
         if ($result) {
             if ($s = getimagesize($tmb)) {
                 if ($s[0] !== $tmbSize || $s[1] !== $tmbSize) {
                     $result = $this->imgSquareFit($result, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png');
                 }
             }
         }
     }
     if (!$result) {
         unlink($tmb);
         return false;
     }
     return $name;
 }