Exemplo n.º 1
0
 public static function getThumbnailUrl($fullPath, $width, $height = 0, $dir = null)
 {
     $thumbnailPath = self::getThumbnailPath($fullPath, $width, $height, $dir);
     $thumbnailUrl = XenForo_Application::$externalDataUrl . self::_getThumbnailRelativePath($fullPath, $width, $height, $dir);
     if (file_exists($thumbnailPath) && filesize($thumbnailPath) > 0) {
         return $thumbnailUrl;
     }
     $tempPath = self::_getTempPath($fullPath);
     $imageInfo = self::getImageInfo($tempPath);
     $image = null;
     if (!empty($imageInfo['typeInt'])) {
         $image = XenForo_Image_Abstract::createFromFile($tempPath, $imageInfo['typeInt']);
     }
     if (empty($image)) {
         // could not open the image, create a new image
         $longer = max($width, $height);
         $image = XenForo_Image_Abstract::createImage($longer, $longer);
         $imageInfo['typeInt'] = IMAGETYPE_PNG;
     }
     if ($width === '' && $height > 0) {
         // stretch width
         $targetHeight = $height;
         $targetWidth = $targetHeight / $image->getHeight() * $image->getWidth();
         $image->thumbnail($targetWidth, $targetHeight);
     } elseif ($height === '' && $width > 0) {
         // stretch height
         $targetWidth = $width;
         $targetHeight = $targetWidth / $image->getWidth() * $image->getHeight();
         $image->thumbnail($targetWidth, $targetHeight);
     } elseif ($width > 0 && $height > 0) {
         // exact crop
         $origRatio = $image->getWidth() / $image->getHeight();
         $cropRatio = $width / $height;
         if ($origRatio > $cropRatio) {
             $thumbnailHeight = $height;
             $thumbnailWidth = $height * $origRatio;
         } else {
             $thumbnailWidth = $width;
             $thumbnailHeight = $width / $origRatio;
         }
         if ($thumbnailWidth <= $image->getWidth() && $thumbnailHeight <= $image->getHeight()) {
             $image->thumbnail($thumbnailWidth, $thumbnailHeight);
             $image->crop(0, 0, $width, $height);
         } else {
             // thumbnail requested is larger then the image size
             if ($origRatio > $cropRatio) {
                 $image->crop(0, 0, $image->getHeight() * $cropRatio, $image->getHeight());
             } else {
                 $image->crop(0, 0, $image->getWidth(), $image->getWidth() / $cropRatio);
             }
         }
     } elseif ($width > 0) {
         // square crop
         $image->thumbnailFixedShorterSide($width);
         $image->crop(0, 0, $width, $width);
     }
     // TODO: progressive jpeg
     XenForo_Helper_File::createDirectory(dirname($thumbnailPath), true);
     $outputPath = tempnam(XenForo_Helper_File::getTempDir(), __CLASS__);
     /** @noinspection PhpParamsInspection */
     $image->output($imageInfo['typeInt'], $outputPath);
     XenForo_Helper_File::safeRename($outputPath, $thumbnailPath);
     return $thumbnailUrl;
 }