Skip to content

Instantly share code, notes, and snippets.

@karneds
Created January 11, 2016 10:40
Show Gist options
  • Select an option

  • Save karneds/dd75f823901755312d4c to your computer and use it in GitHub Desktop.

Select an option

Save karneds/dd75f823901755312d4c to your computer and use it in GitHub Desktop.
<?php
$config = [
'components' => [
...
'urlManager' => [
'class'=>'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules'=>[
...
'thumbs/<width:\d+>x<height:\d+>/<file:.+>' => 'thumbs/index',
'thumbs/-x<height:\d+>/<file:.+>' => 'thumbs/index',
'thumbs/<width:\d+>x-/<file:.+>' => 'thumbs/index',
]
]
]
];
?>
<?php
public function getPreview($width = null, $height = null)
{
if ($this->preview) {
if ($height && $width) {
return preg_replace('#\%2F#i', '/', Url::to(['/thumbs/index', 'file' => $this->preview, 'width' => $width, 'height' => $height]));
}
return '/uploads/' . $this->preview;
}
return;
}
<?php
namespace app\controllers;
use Imagine\Image\ManipulatorInterface;
use Yii;
use yii\helpers\Url;
use yii\imagine\Image;
use yii\web\Controller;
use yii\web\Response;
class ThumbsController extends Controller
{
public function actionIndex($width = '', $height = '', $file = '')
{
if (($width || $height) && $file) {
if (file_exists(Yii::getAlias('@uploads/' . $file))) {
$dest = \Yii::getAlias('@thumbs') . '/' . $width . 'x' . $height . '/' . $file;
$destDir = pathinfo($dest, PATHINFO_DIRNAME);
if (!is_dir($destDir))
mkdir($destDir, 0777, true);
Image::thumbnail(Yii::getAlias('@uploads/' . $file), $width, $height, ManipulatorInterface::THUMBNAIL_INSET)
->save($dest, ['quality' => 70]);
return $this->redirect(Url::to());
}
}
Yii::$app->response->format = Response::FORMAT_RAW;
Yii::$app->response->headers->setDefault('Content-type', 'image/gif');
return '\107\111\106\70\71\141\001\000\001\000\360\001\000\377\377\377\000\000\000\041\371\004\001\012\000\000\000\054\000\000\000\000\001\000\001\000\000\002\002\104\001\000\073';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment