yii實現圖片上傳及縮圖產生的方法_php執行個體

來源:互聯網
上載者:User
本文執行個體講述了利用yii架構來實現圖片上傳功能並在上傳成功之後自動產生縮圖的方法,分享給大家供大家參考。具體實現方法如下:

Action檔案:
複製代碼 代碼如下:<?php
/**
* TestController.php
* Created on: 2014-1-26 12:59:36 by Outsider
*/
class TestController extends CController {

/**
* 縮圖片產生
* @ path 圖片路徑
* @ width 圖片寬度
* @ height 圖片高度
*/
public function actionGetThumb($path, $w, $h) {
$file_name = md5($path . $w . $h);
if (file_exists('./temp/' . $file_name . '.jpg')) {
header('location:/temp/' . $file_name . '.jpg');
Yii::app()->end();
}
Yii::import("ext.EPhpThumb.EPhpThumb");
$thumb = new EPhpThumb();
$thumb->init();
$thumb->create('.' . $path)
->adaptiveResize($w, $h)
->save('./temp/' . $file_name . '.jpg')
->show();
}

/*
* 圖片顯示
*/

public function actionList() {
$attache = Attache::model();
$list = $attache->findAll();
$this->render('list', array('list' => $list));
die;
}

/**
* 檔案上傳
*/
public function actionIndex() {
$path = getcwd() . 'uploads';
$dir = DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . date('m');
$dir = str_replace("\", "/", $dir);
$uploads_dir = str_replace("\", "/", $path . $dir);
if (!is_dir($uploads_dir) || !is_writeable($uploads_dir)) {
mkdir($uploads_dir, 0777, TRUE);
touch($uploads_dir . '/index.html');
}
$uploaded = false;
$model = new Upload();
if (isset($_POST['Upload'])) {
$model->attributes = $_POST['Upload'];
$file = CUploadedFile::getInstance($model, 'file');
$newName = substr(md5($file->extensionName . round((microtime(true) * 1000))), 0, 17) . '.' . $file->extensionName;
$file_name = $uploads_dir . '/' . $newName;
if ($model->validate()) {
$attache = new Attache();
$uploaded = $file->saveAs($file_name, TRUE);
$attache->name = $file->getName();
$attache->path = $dir . '/' . $newName;
$attache->create_time = time();
$attache->save();
}
}

$this->render('index', array(
'model' => $model,
'uploaded' => $uploaded,
'dir' => $uploads_dir,
));
}
}

Upload.php:
複製代碼 代碼如下:<?php
class Upload extends CFormModel {

public $file;

public function rules() {
return array(
array('file', 'file', 'types' => 'jpg, gif, png,zip'),
);
}
}
圖片顯示頁面:

自訂圖片大小,縮圖自動產生
複製代碼 代碼如下:<?php
/**
* list.php
* Created on: 2014-1-26 13:12:01 by Outsider
*/
?>
<?php foreach ($list as $v): ?>
createUrl('test/getThumb', array('path' => '/uploads' . $v['path'], 'w' => '150', 'h' => '150')) ?>">
<?php endforeach; ?>

圖片上傳表單:
複製代碼 代碼如下:<?php if($uploaded):?>

File was uploaded. Check <?php echo $dir?>.


<?php endif ?>
<?php echo CHtml::beginForm('','post',array
('enctype'=>'multipart/form-data'))?>
<?php echo CHtml::error($model, 'file')?>
<?php echo CHtml::activeFileField($model, 'file')?>
<?php echo CHtml::submitButton('Upload')?>
<?php echo CHtml::endForm()?>

希望本文所述對大家基於Yii架構的PHP程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.