public function upload($attribute) { $class = \yii\helpers\StringHelper::basename(get_class($this->owner)) . 'Cutter'; if ($uploadImage = UploadedFile::getInstance($this->owner, $attribute)) { if (!$this->owner->isNewRecord) { $this->delete($attribute); } $cropping = $_POST[$class][$attribute . '-cropping']; $croppingFileName = md5($uploadImage->name . $this->quality . Json::encode($cropping)); $croppingFileExt = strrchr($uploadImage->name, '.'); $croppingFileDir = substr($croppingFileName, 0, 2); $croppingFileBasePath = Yii::getAlias($this->basePath) . $this->baseDir; if (!is_dir($croppingFileBasePath)) { mkdir($croppingFileBasePath, 0755, true); } $croppingFilePath = Yii::getAlias($this->basePath) . $this->baseDir . DIRECTORY_SEPARATOR . $croppingFileDir; if (!is_dir($croppingFilePath)) { mkdir($croppingFilePath, 0755, true); } $fileSavePath = $croppingFilePath . DIRECTORY_SEPARATOR . $croppingFileName . $croppingFileExt; $point = new Point($cropping['dataX'], $cropping['dataY']); $box = new Box($cropping['dataWidth'], $cropping['dataHeight']); $palette = new \Imagine\Image\Palette\RGB(); $color = $palette->color('fff', 0); Image::frame($uploadImage->tempName, 0, 'fff', 0)->rotate($cropping['dataRotate'], $color)->crop($point, $box)->save($fileSavePath, ['quality' => $this->quality]); $this->owner->{$attribute} = $this->baseDir . DIRECTORY_SEPARATOR . $croppingFileDir . DIRECTORY_SEPARATOR . $croppingFileName . $croppingFileExt; } elseif (isset($_POST[$class][$attribute . '-remove']) && $_POST[$class][$attribute . '-remove']) { $this->delete($attribute); } elseif (!empty($_POST[$class][$attribute])) { $this->owner->{$attribute} = $_POST[$class][$attribute]; } elseif (isset($this->owner->oldAttributes[$attribute])) { $this->owner->{$attribute} = $this->owner->oldAttributes[$attribute]; } }
public function testFrame() { $frameSize = 5; $original = Image::getImagine()->open($this->imageFile); $originalSize = $original->getSize(); $img = Image::frame($this->imageFile, $frameSize, '666', 0); $size = $img->getSize(); $this->assertEquals($size->getWidth(), $originalSize->getWidth() + $frameSize * 2); }
function saveImage($model) { if ($model->image) { $full = Yii::getAlias('@frontend/web/uploads/' . $model->image->baseName . '.' . $model->image->extension); $thumb = Yii::getAlias('@frontend/web/uploads/' . $model->image->baseName . '_thumb.' . $model->image->extension); $model->image->saveAs($full); $size = new \Imagine\Image\Box(150, 150); $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET; Image::frame($full)->thumbnail($size, $mode)->save($thumb); } }
public function upload($imageName) { if ($this->validate()) { $path = __DIR__ . '/../web/uploads/' . $imageName . '.' . $this->fileImage->extension; $thumb = __DIR__ . '/../web/uploads/thumbnails/' . $imageName . '.' . $this->fileImage->extension; $this->fileImage->saveAs($path); Image::frame($path)->thumbnail(new Box(100, 100))->save($thumb, ['quality' => 50]); //Image::thumbnail($path , 120, 120)->save($thumb, ['quality' => 50]); return true; } else { return false; } }
function saveThumb($model) { if ($model->image) { $full = Yii::getAlias('@frontend/web/uploads/' . $model->image); $thumb = Yii::getAlias('@frontend/web/uploads/' . pathinfo($model->image, PATHINFO_FILENAME) . '_thumb.' . pathinfo($model->image, PATHINFO_EXTENSION)); $thumb2 = Yii::getAlias('@frontend/web/uploads/' . pathinfo($model->image, PATHINFO_FILENAME) . '_thumb2.' . pathinfo($model->image, PATHINFO_EXTENSION)); $size = new \Imagine\Image\Box(150, 150); $size2 = new \Imagine\Image\Box(200, 200); $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET; //$model->image->saveAs($full); Image::frame($full)->thumbnail($size2, $mode)->save($thumb2); Image::frame($full)->thumbnail($size, $mode)->save($thumb); } }
public function uploadAvatar() { if (Yii::$app->request->isPost) { $id = Yii::$app->user->id; $path = Yii::getAlias("@frontend/web/uploads/users"); $file = UploadedFile::getInstanceByName('avatar'); if ($file) { $name = $id . '.jpg'; $file->saveAs($path . DIRECTORY_SEPARATOR . $name); $image = $path . DIRECTORY_SEPARATOR . $name; $new_name = $path . DIRECTORY_SEPARATOR . "small_" . $name; Image::frame($image, 0, '666', 0)->thumbnail(new Box(200, 200))->save($new_name, ['quality' => 100]); return true; } } }
public function actionFileUploadImages() { if (Yii::$app->request->isPost) { $id = Yii::$app->request->post("advert_id"); $path = Yii::getAlias("@frontend/web/uploads/adverts/" . $id); BaseFileHelper::createDirectory($path); $file = UploadedFile::getInstanceByName('images'); $name = time() . '.' . $file->extension; $file->saveAs($path . DIRECTORY_SEPARATOR . $name); $image = $path . DIRECTORY_SEPARATOR . $name; $new_name = $path . DIRECTORY_SEPARATOR . "small_" . $name; $size = getimagesize($image); $width = $size[0]; $height = $size[1]; Image::frame($image, 0, '666', 0)->crop(new Point(0, 0), new Box($width, $height))->resize(new Box(1000, 644))->save($new_name, ['quality' => 100]); sleep(1); return true; } }
public function actionFileUploadGeneral() { if (Yii::$app->request->isPost) { $id = Yii::$app->request->post('id'); $path = Yii::getAlias('@frontend/web/uploads/adverts/' . $id . '/general'); BaseFileHelper::createDirectory($path); $model = Advert::findOne($id); $model->scenario = 'step2'; $file = UploadedFile::getInstance($model, 'general_image'); $name = 'general.' . $file->extension; $file->saveAs($path . DIRECTORY_SEPARATOR . $name); $image = $path . DIRECTORY_SEPARATOR . $name; $new_name = $path . DIRECTORY_SEPARATOR . $name; $model->general_image = $name; $model->save(); $size = getimagesize($image); $width = $size[0]; $height = $size[1]; Image::frame($image, 0, '666', 0)->crop(new Point(0, 0), new Box($width, $height))->resize(new Box(1000, 644))->save($new_name, ['quality' => 100]); sleep(1); return true; } }
public function resizeImage($sPath, $sFileName, $ext) { Image::frame($sPath . '/' . $sFileName . '.' . $ext, 0)->thumbnail(new Box($this->iThumbSize, $this->iThumbSize))->save($sPath . '/' . $this->sThumb . '/' . $sFileName . '.' . $ext, ['quality' => 100]); Image::frame($sPath . '/' . $sFileName . '.' . $ext, 0)->thumbnail(new Box($this->iInfoSize, $this->iInfoSize))->save($sPath . '/' . $this->sInfo . '/' . $sFileName . '.' . $ext, ['quality' => 100]); Image::frame($sPath . '/' . $sFileName . '.' . $ext, 0)->thumbnail(new Box($this->iBigSize, $this->iBigSize))->save($sPath . '/' . $this->sBig . '/' . $sFileName . '.' . $ext, ['quality' => 100]); unlink($sPath . '/' . $sFileName . '.' . $ext); }
public function actionTest() { Image::frame('D:\\software\\wamp\\www\\collection\\6.jpg', 5, '666', 0)->rotate(-8)->save('D:\\software\\wamp\\www\\collection\\test.jpg', ['quality' => 50]); die; }
/** * 旋转图片 * @param string $filename 图片全路径 * @param int $angle 旋转的角度(lt 0 坐旋转,gt 0 右旋转) * @param string $qz * @param int $margin 边框大小 * @param string $color 边框颜色(有边框才会有颜色) */ public static function rotate($filename, $angle, $qz = 'ro_', $margin = 0, $color = '666') { Image::frame($filename, $margin, $color, 100)->rotate($angle)->save(self::qz($filename, $qz)); }
/** * @inheritdoc */ public function run() { if (Yii::$app->request->isPost) { $file = UploadedFile::getInstanceByName($this->uploadParam); $model = new DynamicModel(compact('file')); $model->addRule('file', $this->_validator, $this->validatorOptions)->validate(); if ($model->hasErrors()) { $result = ['error' => $model->getFirstError('file')]; } else { if ($this->unique === true && $model->file->extension) { $model->file->name = uniqid() . '.' . $model->file->extension; } if ($model->file->saveAs($this->imagePath . $model->file->name)) { //crop image Image::thumbnail($this->imagePath . $model->file->name, $this->options['thumbWidth'], $this->options['thumbHeight'])->save($this->thumbPath . $model->file->name, ['quality' => 90]); //resize image maintaining aspect ratio Image::frame($this->imagePath . $model->file->name, 0, '666', 0)->thumbnail(new Box($this->options['imageWidth'], $this->options['imageHeight']))->save($this->imagePath . $model->file->name, ['quality' => 90]); $result = ['filelink' => $this->url . $model->file->name]; } else { $result = ['error' => Yii::t('vova07/imperavi', 'ERROR_CAN_NOT_UPLOAD_FILE')]; } } Yii::$app->response->format = Response::FORMAT_JSON; return $result; } else { throw new BadRequestHttpException('Only POST is allowed'); } }
/** * Обработка изображения (уменьшение до макс. ширины, * понижение качества изображения) * @param $filename * @param $maxWidth */ private function processImage($filename, $maxWidth) { $img = Image::frame($filename, 0); $size = $img->getSize(); if ($size->getWidth() > $maxWidth) { $img->resize($size->widen($maxWidth)); } $img->save($filename, ['quality' => 70]); }
public function actionRotateImage() { $image_url = Yii::$app->request->post('url'); // frame, rotate and save an image Image::frame($image_url, 0, '777', 0)->rotate(90)->save($image_url, ['quality' => 100]); return $this->redirect(Yii::$app->request->referrer); }
public function rotateImage($imageId) { $imageQuery = (new Query())->select(['id', 'name', 'description', 'sort', 'src'])->from($this->tableName)->where(['id' => $imageId])->one(); Image::frame($this->getFilePath($imageQuery['src']), 0)->rotate(90)->save($this->getFilePath($imageQuery['src'])); }