function getResources($uploads_dir, $id, $maxWidth, $connection = 'default')
{
    $attachments = Attachment::on($connection)->where('id', $id)->get();
    if (count($attachments) == 1) {
        $attachment = $attachments[0];
        switch ($attachment->source) {
            case Source::EMAIL:
                $adapter = new Local($uploads_dir . '/email/');
                break;
            case Source::TWITTER:
                $adapter = new Local($uploads_dir . '/twitter/');
                break;
            case Source::TELEGRAM:
                $adapter = new Local($uploads_dir . '/telegram/');
                break;
            default:
                break;
        }
        $filesystem = new Filesystem($adapter);
        $filesystem->addPlugin(new ListWith());
        if ($filesystem->has($attachment->filePath)) {
            $data = $filesystem->read($attachment->filePath);
            $fp['data'] = $data;
            $fp['mime'] = $filesystem->getMimetype($attachment->filePath);
            if ($maxWidth > 0) {
                $imagine = new \Imagine\Gd\Imagine();
                $image = $imagine->load($data);
                $size = $image->getSize();
                if ($size->getWidth() > $maxWidth) {
                    // AWIDTH : AHEIGHT = NWIDTH : NHEIGHT
                    // HHEIGHT = AHEIGHT * NWIDTH / AWIDTH
                    $height = $size->getHeight() * $maxWidth / $size->getWidth();
                    $width = $maxWidth;
                    $fp['data'] = $image->resize(new Box($width, $height), ImageInterface::FILTER_UNDEFINED)->show('png');
                    //FILTER_QUADRATIC
                }
            }
            return $fp;
        }
    }
    return false;
}
Пример #2
0
 /**
  * @param bool $bDownload
  * @param bool $bThumbnail = false
  *
  * @return bool
  */
 private function rawSmart($bDownload, $bThumbnail = false)
 {
     $sRawKey = (string) $this->GetActionParam('RawKey', '');
     $aValues = $this->getDecodedRawKeyValue($sRawKey);
     $sFolder = isset($aValues['Folder']) ? $aValues['Folder'] : '';
     $iUid = isset($aValues['Uid']) ? (int) $aValues['Uid'] : 0;
     $sMimeIndex = isset($aValues['MimeIndex']) ? (string) $aValues['MimeIndex'] : '';
     $sContentTypeIn = isset($aValues['MimeType']) ? (string) $aValues['MimeType'] : '';
     $sFileNameIn = isset($aValues['FileName']) ? (string) $aValues['FileName'] : '';
     $sFileHashIn = isset($aValues['FileHash']) ? (string) $aValues['FileHash'] : '';
     $bDetectImageOrientation = !!$this->Config()->Get('labs', 'detect_image_exif_orientation', true);
     if (!empty($sFileHashIn)) {
         $this->verifyCacheByKey($sRawKey);
         $oAccount = $this->getAccountFromToken();
         $sContentTypeOut = empty($sContentTypeIn) ? \MailSo\Base\Utils::MimeContentType($sFileNameIn) : $sContentTypeIn;
         $sFileNameOut = $this->MainClearFileName($sFileNameIn, $sContentTypeIn, $sMimeIndex);
         $rResource = $this->FilesProvider()->GetFile($oAccount, $sFileHashIn);
         if (\is_resource($rResource)) {
             $sFileNameOut = \MailSo\Base\Utils::ConvertEncoding($sFileNameOut, \MailSo\Base\Enumerations\Charset::UTF_8, \MailSo\Base\Enumerations\Charset::CP858);
             \header('Content-Type: ' . $sContentTypeOut);
             \header('Content-Disposition: attachment; ' . \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut)), true);
             \header('Accept-Ranges: none', true);
             \header('Content-Transfer-Encoding: binary');
             \MailSo\Base\Utils::FpassthruWithTimeLimitReset($rResource);
             return true;
         }
         return false;
     } else {
         if (!empty($sFolder) && 0 < $iUid) {
             $this->verifyCacheByKey($sRawKey);
         }
     }
     $oAccount = $this->initMailClientConnection();
     $self = $this;
     return $this->MailClient()->MessageMimeStream(function ($rResource, $sContentType, $sFileName, $sMimeIndex = '') use($self, $oAccount, $sRawKey, $sContentTypeIn, $sFileNameIn, $bDownload, $bThumbnail, $bDetectImageOrientation) {
         if ($oAccount && \is_resource($rResource)) {
             $sContentTypeOut = $sContentTypeIn;
             if (empty($sContentTypeOut)) {
                 $sContentTypeOut = $sContentType;
                 if (empty($sContentTypeOut)) {
                     $sContentTypeOut = empty($sFileName) ? 'text/plain' : \MailSo\Base\Utils::MimeContentType($sFileName);
                 }
             }
             $sFileNameOut = $sFileNameIn;
             if (empty($sFileNameOut)) {
                 $sFileNameOut = $sFileName;
             }
             $sFileNameOut = $self->MainClearFileName($sFileNameOut, $sContentTypeOut, $sMimeIndex);
             $self->cacheByKey($sRawKey);
             $bDone = false;
             if ($bThumbnail && !$bDownload) {
                 try {
                     $oImagine = new \Imagine\Gd\Imagine();
                     $bDone = true;
                     $oImage = $oImagine->load(\stream_get_contents($rResource));
                     $oImage = $this->correctImageOrientation($oImage, $bDetectImageOrientation, 60);
                     \header('Content-Disposition: inline; ' . \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut . '_thumb60x60.png')), true);
                     $oImage->show('png');
                 } catch (\Exception $oException) {
                     $self->Logger()->WriteExceptionShort($oException);
                 }
             }
             if (!$bDone && !$bDownload && $bDetectImageOrientation && \in_array($sContentTypeOut, array('image/png', 'image/jpeg', 'image/jpg')) && \MailSo\Base\Utils::FunctionExistsAndEnabled('gd_info')) {
                 try {
                     $oImagine = new \Imagine\Gd\Imagine();
                     $bDone = true;
                     $oImage = $oImagine->load(\stream_get_contents($rResource));
                     $oImage = $this->correctImageOrientation($oImage, $bDetectImageOrientation);
                     \header('Content-Disposition: inline; ' . \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut)), true);
                     $oImage->show($sContentTypeOut === 'image/png' ? 'png' : 'jpg');
                 } catch (\Exception $oException) {
                     $self->Logger()->WriteExceptionShort($oException);
                 }
             }
             if (!$bDone) {
                 \header('Content-Type: ' . $sContentTypeOut);
                 \header('Content-Disposition: ' . ($bDownload ? 'attachment' : 'inline') . '; ' . \trim(\MailSo\Base\Utils::EncodeHeaderUtf8AttributeValue('filename', $sFileNameOut)), true);
                 \header('Accept-Ranges: none', true);
                 \header('Content-Transfer-Encoding: binary');
                 \MailSo\Base\Utils::FpassthruWithTimeLimitReset($rResource);
             }
         }
     }, $sFolder, $iUid, true, $sMimeIndex);
 }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 public static function load($string)
 {
     return static::$instance->load($string);
 }