This article mainly introduces how yii implements Image Upload and thumbnail generation. It analyzes in detail the principles and implementation methods of Image Upload and thumbnail generation, and is a very practical technique, for more information about how to use the yii framework to upload images and automatically generate thumbnails after successful uploads. The specific implementation method is as follows:
Action file:
The Code is as follows:
<? Php
/**
* TestController. php
* Created on: 12:59:36 by Outsider
*/
Class TestController extends CController {
/**
* Create a thumbnail
* @ Path: Image path
* @ Width: Image width
* @ Height the Image 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 ();
}
/*
* Image Display
*/
Public function actionList (){
$ Attache = Attache: model ();
$ List = $ attache-> findAll ();
$ This-> render ('LIST', array ('LIST' => $ list ));
Die;
}
/**
* File Upload
*/
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:
The Code is as follows:
<? Php
Class Upload extends CFormModel {
Public $ file;
Public function rules (){
Return array (
Array ('file', 'file', 'types' => 'jpg, gif, png, zip '),
);
}
}
Image Display page:
Custom image size, automatically generated by thumbnails
The Code is as follows:
<? Php
/**
* List. php
* Created on: 13:12:01 by Outsider
*/
?>
<? Php foreach ($ list as $ v):?>
CreateUrl ('test/getthumb', array ('path' => '/uploads '. $ v ['path'], 'w' => '000000', 'H' => '000000')?> ">
<? Php endforeach;?>
Image Upload form:
The Code is as follows:
<? Php if ($ uploaded):?>
File was uploaded. Check <? Php echo $ dir?>.
<? Php endif?>
<? Php echo CHtml: beginForm ('', 'post', array
('Enabledype '=> 'multipart/form-data')?>
<? Php echo CHtml: error ($ model, 'file')?>
<? Php echo CHtml: activeFileField ($ model, 'file')?>
<? Php echo CHtml: submitButton ('upload')?>
<? Php echo CHtml: endForm ()?>
I hope this article will help you design PHP programs based on the Yii framework.