public function testResize() { $obj = Imagick::open($this->getPath('original.jpg')); $obj->resize(150, 150)->saveTo($this->getTempPath('resize-1.jpg')); $this->assertFileExists($this->getTempPath('thumb.jpg')); $resize = Imagick::open($this->getTempPath('resize-1.jpg')); $this->assertEquals(150, $resize->getWidth()); $this->assertNotEquals(150, $resize->getHeight()); $obj = Imagick::open($this->getPath('original.jpg')); $obj->resize(150, false)->saveTo($this->getTempPath('resize-2.jpg')); $this->assertFileExists($this->getTempPath('thumb.jpg')); $resize = Imagick::open($this->getTempPath('resize-2.jpg')); $this->assertEquals(150, $resize->getWidth()); $this->assertNotEquals(150, $resize->getHeight()); $obj = Imagick::open($this->getPath('original.jpg')); $obj->resize(false, 150)->saveTo($this->getTempPath('resize-3.jpg')); $this->assertFileExists($this->getTempPath('thumb.jpg')); $resize = Imagick::open($this->getTempPath('resize-3.jpg')); $this->assertNotEquals(150, $resize->getWidth()); $this->assertEquals(150, $resize->getHeight()); $obj = Imagick::open($this->getPath('original.jpg')); try { $obj->resize(false, false)->saveTo($this->getTempPath('resize-4.jpg')); $this->assertTrue(false); } catch (\yii\base\InvalidConfigException $e) { $this->assertTrue(true); } }
public function actionCropimage() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; if (!empty($_POST['img'])) { $output_dir = 'themes/giicms/webroot/uploads/'; Imagick::open($output_dir . 'thumbnails/' . $_POST['img'])->crop($_POST['x1'], $_POST['y1'], $_POST['w'], $_POST['h'])->thumb(150, 150)->saveTo($output_dir . 'thumbnails/150-' . $_POST['img']); Imagick::open($output_dir . 'thumbnails/150-' . $_POST['img'])->thumb(60, 60)->saveTo($output_dir . 'thumbnails/60-' . $_POST['img']); $user = User::findOne(Yii::$app->user->id); $user->avatar = $_POST['img']; $user->save(); return ['ok']; } }
/** * Save file to disk */ private function saveFile($eventName) { $file = $this->owner->{$this->fileVar}; $error = false; if ($file !== null) { $fileModelClass = $this->fileModel; $transaction = $fileModelClass::getDb()->beginTransaction(); $successDownloaded = []; // save files if ($this->imageSizes !== false && is_array($this->imageSizes)) { $sizeCount = count($this->imageSizes); $sizeNumber = 0; foreach ($this->imageSizes as $sizeName => $size) { $sizeNumber++; $deleteTempFile = false; if ($sizeNumber == $sizeCount) { $deleteTempFile = true; } if (isset($size['folder']) && $size['folder'] !== '') { $path = $this->fileFolder . '/' . $size['folder'] . '/'; } else { $path = $this->fileFolder . '/'; } // save to DB $fileName = $this->saveToDb($file, $path, $sizeName); if ($fileName !== false) { if (isset($size['folder']) && $size['folder'] !== '') { $filePath = $this->getFolderPath() . $size['folder'] . '/' . $fileName; } else { $filePath = $this->getFolderPath() . '/' . $fileName; } if ($file->saveAs($filePath, $deleteTempFile)) { $successDownloaded[] = $filePath; if (isset($size['width']) && isset($size['height'])) { Imagick::open($filePath)->resize($size['width'], $size['height'])->saveTo($filePath); } } else { $error = true; break; } } else { $error = true; } } } else { $path = $this->fileFolder . '/'; $fileName = $this->saveToDb($file, $path); if ($fileName !== false) { $filePath = $this->getFolderPath() . $fileName; if (!$file->saveAs($filePath)) { $error = true; } } else { $error = true; } } } if ($error) { foreach ($successDownloaded as $path) { unlink($path); } $transaction->rollBack(); } else { $transaction->commit(); } }
/** * Save image to disk */ private function saveImage($file) { // $file = $this->owner->{$this->imageVariable}; $error = false; if ($file !== null && $file !== '') { $imageModelClass = $this->imageModel; $transaction = $imageModelClass::getDb()->beginTransaction(); if ($this->isHashEnabled) { $hash = md5_file($file->tempName); $hashDir = $hash[0] . $hash[1] . '/' . $hash[2] . $hash[4]; } else { $hash = false; } $successDownloaded = []; // save files if ($this->imageSizes !== false && is_array($this->imageSizes)) { $sizeCount = count($this->imageSizes); $sizeNumber = 0; $saveInfo = $this->saveToDb($file, $hash); $imageId = $saveInfo['id']; $isExist = $saveInfo['exist']; if ($imageId === false) { throw new ServerErrorHttpException("Cant save to DB"); } foreach ($this->imageSizes as $sizeName => $size) { $sizeNumber++; $deleteTempFile = false; if ($sizeNumber == $sizeCount) { $deleteTempFile = true; } if (isset($size['folder']) && $size['folder'] !== '') { $path = $this->imageFolder . '/' . $size['folder'] . '/'; } else { $path = $this->imageFolder . '/'; } if ($hash !== false) { $hashDir = $hash[0] . $hash[1] . '/' . $hash[2] . $hash[4]; $path .= $hashDir . '/'; } if (!file_exists(Yii::getAlias($path))) { mkdir(Yii::getAlias($path), 0777, true); } // save to DB if ($isExist === false) { $error = $this->addImageSize($imageId, $path, $sizeName); } else { $error = false; } $fileName = $imageId . '.' . $file->extension; if ($error === false) { if (isset($size['folder']) && $size['folder'] !== '') { $filePath = $this->getFolderPath() . '/' . $size['folder']; if ($hash !== false) { $filePath .= '/' . $hashDir; } $filePath .= '/' . $fileName; } else { $filePath = $this->getFolderPath() . '/' . $fileName; } if ($file->saveAs($filePath, $deleteTempFile)) { $successDownloaded[] = $filePath; if (isset($size['width']) && isset($size['height'])) { Imagick::open($filePath)->resize($size['width'], $size['height'])->saveTo($filePath); } } else { $error = true; break; } } else { $error = true; } } } else { $path = $this->imageFolder . '/'; $saveInfo = $this->saveToDb($file, $hash); $imageId = $saveInfo['id']; $isExist = $saveInfo['exist']; if ($isExist === false) { $error = $this->addImageSize($imageId, $path); } else { $error = false; } if ($imageId !== false) { $fileName = $imageId . '.' . $file->extension; $filePath = $this->getFolderPath(); if ($hash !== false) { $filePath .= '/' . $hashDir; } $filePath .= '/' . $fileName; $filePath = $this->getFolderPath() . '/' . $fileName; if (!$file->saveAs($filePath)) { $error = true; } } else { $error = true; } } if ($error) { foreach ($successDownloaded as $path) { unlink($path); } $transaction->rollBack(); } else { $transaction->commit(); } } }