public function run($class_name) { $path = realpath(Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . $class_name); $class_name = ucfirst($class_name); if ($path && is_dir($path) && is_writable($path)) { $dir = key($_GET); $filename = $_GET[$dir]; $pk = pathinfo($filename, PATHINFO_FILENAME); $image = Images::model()->findByPk($pk); if ($image != null) { $image->resize($dir); } } elseif (class_exists($class_name)) { $dir = key($_GET); $filename = $_GET[$dir]; $size = explode('x', $dir); $path = realpath(Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . $class_name); if (YII_DEBUG && !file_exists($path . DIRECTORY_SEPARATOR . $dir)) { mkdir($path . DIRECTORY_SEPARATOR . $dir, 0777); } if ($path !== FALSE && file_exists($path . DIRECTORY_SEPARATOR . $dir) && is_file($path . DIRECTORY_SEPARATOR . $filename) && $size[0] > 0 && $size[1] > 0) { Yii::import('ext.iwi.Iwi'); $image = new Iwi($path . DIRECTORY_SEPARATOR . $filename); $image->adaptive($size[0], $size[1]); $image->save($path . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $filename, 0644, TRUE); $mime = CFileHelper::getMimeType($path . DIRECTORY_SEPARATOR . $filename); header('Content-Type: ' . $mime); $image->render(); exit; } } return parent::run($class_name); }
/** * Resize image to this size * * @param string $thumb @see $this->previews */ public function resize($thumb) { Yii::import('ext.iwi.Iwi'); $folder = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'photos'; $file = $this->id . '.' . $this->extension; //$folder = DIRECTORY_SEPARATOR . trim($folder, DIRECTORY_SEPARATOR . ' '); if (!isset($this->previews[$thumb])) { return 0; } $size = $this->previews[$thumb]; if (!is_file($folder . DIRECTORY_SEPARATOR . $file)) { return 0; } $out_file = $folder . DIRECTORY_SEPARATOR . $thumb . DIRECTORY_SEPARATOR . $file; //output file if (is_file($out_file)) { return $out_file; } $image = new Iwi($folder . DIRECTORY_SEPARATOR . $file); if ($size[$this->WIDTH] == 0) { $image->resize(0, $size[$this->HEIGHT], Image::HEIGHT); } elseif ($size[$this->HEIGHT] == 0) { $image->resize($size[$this->WIDTH], 0, Image::WIDTH); } else { $image->adaptive($size[$this->WIDTH], $size[$this->HEIGHT])->crop($size[$this->WIDTH], $size[$this->HEIGHT], 'top', 'center')->sharpen(10); } if (!file_exists($folder . DIRECTORY_SEPARATOR . $thumb)) { mkdir($folder . DIRECTORY_SEPARATOR . $thumb, 777); } $image->save($out_file); return $out_file; }
public static function AddContent($imageFile, $name, $type, $targetRatio = null) { if ($targetRatio == null) { $targetRatio = 4 / 3; } if (is_string($imageFile)) { $pathParts = pathinfo($imageFile); $originalExtension = strtolower($pathParts['extension']); $imageFileName = $imageFile; } else { $pathParts = pathinfo($imageFile->getName()); $originalExtension = strtolower($pathParts['extension']); $imageFileName = 'temp' . uniqid() . '.' . $originalExtension; $imageFile->saveAs(Yii::app()->params['temp'] . '/' . $imageFileName); } $content = new Content(); $content->Content_name = $name; $content->Content_type = $type; $content->save(); $content->refresh(); $path = Yii::app()->params['uploads'] . '/' . $content->Content_ID; $link = Yii::app()->params['siteBase'] . '/uploads/' . $content->Content_ID; $path .= '.' . $originalExtension; $link .= '.' . $originalExtension; rename(Yii::app()->params['temp'] . '/' . $imageFileName, $path); $image = new Iwi($path); $sourceRatio = $image->width / $image->height; if ($sourceRatio > $targetRatio) { $height = $image->height; if ($height > Content::MAX_HEIGHT) { $height = Content::MAX_HEIGHT; } $width = (int) $height * $targetRatio; } elseif ($sourceRatio < $targetRatio) { $width = $image->width; if ($width > Content::MAX_WIDTH) { $width = Content::MAX_WIDTH; } $height = (int) $width / $targetRatio; } else { $height = $image->height; $width = $image->width; if ($height > Content::MAX_HEIGHT) { $height = Content::MAX_HEIGHT; $width = (int) $height * $targetRatio; } } $image->adaptive($width, $height); $image->save(); $content->Link = $link; $content->save(); return $content; }
public function actionEditBackground() { Yii::import('ext.iwi.Iwi'); $id = Yii::app()->user->id; if (!$id > 0) { throw new CException('Not Found', 404); } Yii::app()->log->routes[3]->enabled = false; if (isset($_GET['ajax'])) { $upload = Files::model()->uploadFile('avatar'); if ($upload !== false) { echo CJSON::encode(array('message' => $upload['filename'] . '.' . $upload['extension'], 'status' => 'OK')); } else { echo CJSON::encode(array('message' => 'Ошибка загрузки файла', 'status' => 'ERROR')); } exit; } if (Yii::app()->request->getPost('height') > 0) { $post = array('height' => (int) Yii::app()->request->getPost('height'), 'width' => (int) Yii::app()->request->getPost('width'), 'left' => (int) Yii::app()->request->getPost('left'), 'top' => (int) Yii::app()->request->getPost('top'), 'image' => trim(Yii::app()->request->getPost('image', ''), '/')); $filename = pathinfo($post['image'], PATHINFO_FILENAME); $path = realpath(Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR); $file = Files::model()->findByPk($filename); $userpath = $path . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'photos' . DIRECTORY_SEPARATOR; $image = new Iwi($userpath . $file->id . '.' . $file->extension); $image->resize($post['width'], $post['height']); if ($image->save($userpath . 'cover' . DIRECTORY_SEPARATOR . $file->id . '.' . $file->extension)) { $user = UserProfile::model()->find("user_id=:uid", array('uid' => $id)); $oldavatar = false; if ($user->background != $filename) { $oldavatar = $user->background; } $user->background = $filename; $user->bgposition = $post['left'] . ' ' . $post['top']; if ($user->save()) { // if($oldavatar!=false && $oldavatar != 'userTestBG.png' && is_file($userpath . $oldavatar)) // FileHelper::cleanUpload('UserProfile', $oldavatar); echo CJSON::encode(array('message' => $filename, 'status' => 'OK')); } else { echo CJSON::encode(array('message' => $user->getErrors(), 'status' => 'ERROR')); } exit; } else { //System error must be handled by administration. Yii::log("Ошибка при создании файла аватара '{$post['image']}'. Пользователь #{$id}", 'email'); echo CJSON::encode(array('status' => 'ERROR', 'message' => 'Ну удалось сохранить аватар. Попробуйте позже.')); } } //else // echo CJSON::encode(array('status'=>'ERROR','message'=>'Не хватает параметров.')); //if(isset($_GET['ajax'])) exit; //$this->render('edit_avatar'); }
public function actionResize() { $image = Yii::app()->request->getPost('image', false); $type = Yii::app()->request->getPost('type', false); if ($image !== false && in_array($type, array('avatar'))) { $file = pathinfo($image, PATHINFO_FILENAME); $file = Files::model()->findByPk($file); if ($file != NULL) { $tmp = TemporaryFiles::model()->find('md5file=:md5f', array(':md5f' => $md5file)); if ($tmp == null) { $to = realpath(Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'TemporaryFiles') . DIRECTORY_SEPARATOR . $filename; if ($to && @copy($image, $to)) { $image = $to; $size = getimagesize($image); if ($size[0] > 656 || $size[1] > 520) { Yii::import('ext.iwi.Iwi'); $img = new Iwi($image); $img->resize(656, 520, 2); $img->save(); } $tmp = new TemporaryFiles(); $tmp->md5file = $md5file; $tmp->filename = $filename; $tmp->upload_date = time(); $tmp->save(); } else { if (!is_file($to)) { echo CJSON::encode(array('message' => 'Ошибка копирования файла!', 'status' => 'ERROR')); exit; } else { echo CJSON::encode(array('message' => basename($to), 'status' => 'OK')); exit; } } } echo CJSON::encode(array('message' => $tmp->filename, 'status' => 'OK')); } echo CJSON::encode(array('message' => 'Файл не найден', 'status' => 'ERROR')); } exit; }
public function actionEditBackground() { Yii::import('ext.iwi.Iwi'); $id = Yii::app()->user->id; if (!$id > 0) { throw new CException('Not Found', 404); } $filev = new FileValidator(); $filev->types = array('jpg', 'jpeg', 'png', 'gif'); $filev->sizes = array('min' => 962, 250); $upload = Images::model()->uploadFile('avatar', true, $filev); if ($upload !== false && !isset($upload['error'])) { //$size = getimagesize(Yii::getPathOfAlias('webroot').'/upload/photos/'.$upload['filename'].'.'.$upload['extension']); //if($size[0]<962 || $size[1]<250){ // echo CJSON::encode(array('message'=>'Размер изображения должен быть не меньше 962 в ширине на 250 по высоте пикселей.','status'=>'ERROR')); // exit(); //} echo CJSON::encode(array('message' => $upload['filename'] . '.' . $upload['extension'], 'status' => 'OK')); } else { if (isset($upload['error'])) { echo CJSON::encode(array('message' => $upload['error'], 'status' => 'ERROR')); } } if (Yii::app()->request->getPost('height') > 0) { $post = array('height' => (int) Yii::app()->request->getPost('height'), 'width' => (int) Yii::app()->request->getPost('width'), 'left' => (int) Yii::app()->request->getPost('left'), 'top' => (int) Yii::app()->request->getPost('top'), 'image' => trim(Yii::app()->request->getPost('image', ''), '/')); $filename = basename($post['image']); $path = realpath(Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR); $userpath = $path . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'UserProfile' . DIRECTORY_SEPARATOR; $image = new Iwi($path . DIRECTORY_SEPARATOR . $post['image']); $image->resize($post['width'], $post['height'])->crop(962, 250, $post['top'], $post['left']); if ($image->save($userpath . $filename)) { $user = UserProfile::model()->find("user_id=:uid", array('uid' => $id)); $oldavatar = false; if ($user->background != $filename) { $oldavatar = $user->background; } $user->background = $filename; if ($user->save()) { if ($oldavatar != false && $oldavatar != 'userTestBG.png' && is_file($userpath . $oldavatar)) { FileHelper::cleanUpload('UserProfile', $oldavatar); } echo CJSON::encode(array('message' => $filename, 'status' => 'OK')); } else { echo CJSON::encode(array('message' => $user->getErrors(), 'status' => 'ERROR')); } exit; } else { //System error must be handled by administration. Yii::log("Ошибка при создании файла аватара '{$post['image']}'. Пользователь #{$id}", 'email'); echo CJSON::encode(array('status' => 'ERROR', 'message' => 'Ну удалось сохранить аватар. Попробуйте позже.')); } } //else // echo CJSON::encode(array('status'=>'ERROR','message'=>'Не хватает параметров.')); //if(isset($_GET['ajax'])) exit; //$this->render('edit_avatar'); }
public static function crop($image) { Yii::import('ext.iwi.Iwi'); $picture = new Iwi($image); $picture->crop(180, 100, 0, 0); $picture->save(FALSE, 0777, FALSE); }
public function resize($folder, $file, $sizes) { $previews = $this->previews; if (is_string($sizes)) { $previews = isset($this->previews[$sizes]) ? $this->previews[$sizes] : $this->previews; } else { if (is_array($sizes)) { $previews = array_intersect_key($this->previews, array_fill_keys($sizes, '1')); } } if ($folder === null) { $folder = Yii::getPathOfAlias('webroot') . '/upload/photos'; } if ($file === null) { $file = $this->id . '.' . $this->extension; } $folder = '/' . trim($folder, '/ '); var_dump($previews); exit; foreach ($this->previews as $sub => $size) { if (is_file($folder . '/' . $sub . '/' . $file)) { continue; } $image = new Iwi($folder . '/' . $file); if ($size[$this->WIDTH] == 0) { $image->resize(0, $size[$this->HEIGHT], Image::HEIGHT); } elseif ($size[$this->HEIGHT] == 0) { $image->resize($size[$this->WIDTH], 0, Image::WIDTH); } else { $image->adaptive($size[$this->WIDTH], $size[$this->HEIGHT])->crop($size[$this->WIDTH], $size[$this->HEIGHT], 'top', 'center')->sharpen(10); } if (!file_exists($folder . '/' . $sub)) { mkdir($folder . '/' . $sub, 777); } $image->save($folder . '/' . $sub . '/' . $file); } }