/** * Save good images from urls * @static * @param $images array with image urls * @param $good_id */ public static function SaveImages($images, $good_id) { $imagePath = $_SERVER['DOCUMENT_ROOT'] . '/images/goods/'; foreach ($images as $url) { $path_parts = pathinfo($url); $name = $good_id . '_' . substr(md5(microtime()), 0, 5) . '.' . $path_parts['extension']; while (file_exists($imagePath . $name)) { $name = $good_id . '_' . substr(md5(microtime()), 0, 5) . '.' . $path_parts['extension']; } if (!empty($url)) { file_put_contents($imagePath . $name, file_get_contents($url)); $image = new GoodImage(); $image->good_id = $good_id; $image->image = $name; if ($image->save()) { //create small pictures $image = Yii::app()->image->load($imagePath . $name); $image->resize(200, 200, Image::AUTO); $image->save($imagePath . 's_' . $name); $image = Yii::app()->image->load($imagePath . $name); $image->resize(100, 100, Image::AUTO); $image->save($imagePath . 'xs_' . $name); $image = Yii::app()->image->load($imagePath . $name); $image->resize(400, 400, Image::AUTO); $image->save($imagePath . 'l_' . $name); } } } }
public function loadModel($id) { if (($model = GoodImage::model()->findByPk($id)) === null) { throw new CHttpException(404, 'Страница не найдена'); } return $model; }
/** * Save good images */ public function SaveImages() { $imagePath = $_SERVER['DOCUMENT_ROOT'] . '/images/goods/'; $i = 1; foreach ($this->image as $file) { if ($file === null) { continue; } $name = $this->id . '_' . substr(md5(microtime()), 0, 5) . '.' . $file->extensionName; while (file_exists($imagePath . $name)) { $name = $this->id . '_' . substr(md5(microtime()), 0, 5) . '.' . $file->extensionName; } if (!empty($file)) { $file->saveAs($imagePath . $name); $i++; $image = new GoodImage(); $image->good_id = $this->id; $image->image = $name; if ($image->save()) { //create Thumbnails $image = Yii::app()->image->load($imagePath . $name); $image->resize(200, 200, Image::AUTO); $image->save($imagePath . 's_' . $name); $image = Yii::app()->image->load($imagePath . $name); $image->resize(100, 100, Image::AUTO); $image->save($imagePath . 'xs_' . $name); $image = Yii::app()->image->load($imagePath . $name); $image->resize(400, 400, Image::AUTO); $image->save($imagePath . 'l_' . $name); } } } }
/** * Deletes a particular model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id the ID of the model to be deleted */ public function actionDelete($id) { if (Yii::app()->request->isPostRequest) { // we only allow deletion via POST request $images = GoodImage::model()->findAllByAttributes(array('good_id' => $id)); if (isset($images)) { foreach ($images as $image) { $image->delete(); } } if (Yii::app()->params['server'] == CAlexHelper::DEVELOPMENT) { $this->loadModel($id)->delete(); } // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser if (!isset($_GET['ajax'])) { $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin')); } } else { throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.'); } }