Use the cuploadfile provided by yii to upload images. This class only provides the upload function and does not support image processing functions such as reduction, sharpening, and rotation, therefore, the yii extension image must be used.
I. upload images
Data Table:
CREATE TABLE `img_show` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(150) DEFAULT NULL, `url` varchar(100) DEFAULT NULL, `img_path` varchar(150) DEFAULT NULL, `handle_img_path` varchar(150) DEFAULT NULL, `add_time` int(11) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
Use Gii to generate the corresponding model, curd, and controller. The changes are as follows:
Model:
Public Function rules () {// Note: You shoshould only define rules for those attributes that/will receive user inputs. return array (Array ('add _ time', 'Numerical ', 'integeronly' => true), array ('title, img_path, handle_img_path', 'length ', 'max '=> 150), array ('url', 'length', 'max' => 100), array ('img _ path', 'file ', // defined as file type // 'allowempty '=> true, 'types' => 'jpg, PNG, GIF, Doc, docx, PDF, xls, XLSX, zip, rar, P PT, pptx, // The type of the file to be uploaded 'maxsize' => 1024*1024*10, // the size of the file to be uploaded. Note that it is not PHP. the Upload File Size in INI is 'toolarge' => 'the file size is greater than 10 MB. Upload Failed! Upload a file smaller than 10 MB! '), // The following rule is used by search (). // @ todo please remove those attributes that shocould not be searched. array ('Id, title, URL, img_path, handle_img_path, add_time ', 'safe', 'on' => 'search '),);}
Controller:
Public Function actioncreate () {$ model = new imgshow; If (isset ($ _ post ['imgshow ']) {$ model-> attributes =$ _ post ['imgshow ']; $ model-> add_time = Time (); // obtain a cuploadfile instance $ file = cuploadedfile :: getinstance ($ model, 'img _ path'); // determines whether the image is successfully renamed as if (is_object ($ file) & get_class ($ file) === 'cuploadedfile') {$ model-> img_path = '. /images/upfile/File '. time (). '_'. rand ). '. '. $ file-> extensionname; $ file-> saveas ($ model-> img_path);} // process the image/* $ image = yii: APP () -> image-> load ($ model-> img_path); $ image-> resize (600,300)-> rotate (-45)-> quality (75) -> sharpen (20); $ model-> handle_img_path = '. /images/upfile/File '. time (). '_'. rand ). '_ small '. '. '. $ file-> extensionname; $ image-> Save ($ model-> handle_img_path); */if ($ model-> Save ()) $ this-> redirect (Array ('view', 'id' => $ model-> ID);} $ this-> render ('create ', array ('model' => $ model ,));}
Views:
<Div class = "form"> <? PHP $ form = $ this-> beginwidget ('cactiveform', array ('id' => 'img-show-form', 'enableajaxvalider' => false, 'htmlopexception' => array ('enablesype '=> 'multipart/form-data'),);?> <P> <SPAN class = "required"> * </span> is required. </P> <? PHP echo $ form-> errorsummary ($ model);?> <Div class = "row"> <? PHP echo $ form-> labelex ($ model, 'title');?> <? PHP echo $ form-> textfield ($ model, 'title', array ('SIZE' => 60, 'maxlength' => 150);?> <? PHP echo $ form-> error ($ model, 'title');?> </Div> <Div class = "row"> <? PHP echo $ form-> labelex ($ model, 'url');?> <? PHP echo $ form-> textfield ($ model, 'url', array ('SIZE' => 60, 'maxlength' => 100);?> <? PHP echo $ form-> error ($ model, 'url');?> </Div> <Div class = "row"> <? PHP echo $ form-> labelex ($ model, 'img _ path');?> <! --> <? PHP // echo $ form-> textfield ($ model, 'img _ path', array ('SIZE' => 60, 'maxlength' => 150);?> <? PHP echo chtml: activefilefield ($ model, 'img _ path', array ('SIZE' => 60, 'maxlength' => 255);?> <? PHP echo $ form-> error ($ model, 'img _ path');?> </Div> <! -- <Div class = "row"> <? PHP/* echo $ form-> labelex ($ model, 'handle _ img_path '); */?> <? PHP/* echo $ form-> textfield ($ model, 'handle _ img_path ', array ('SIZE' => 60, 'maxlength' => 150 )); */?> <? PHP/* echo $ form-> error ($ model, 'handle _ img_path '); */?> </Div> --> <! -- <Div class = "row"> <? PHP/* echo $ form-> labelex ($ model, 'add _ Time'); */?> <? PHP/* echo $ form-> textfield ($ model, 'add _ Time'); */?> <? PHP/* echo $ form-> error ($ model, 'add _ Time'); */?> </Div> --> <Div class = "Row buttons"> <? PHP echo chtml: submitbutton ($ model-> isnewrecord? 'Add': 'update');?> </Div> <? PHP $ this-> endwidget ();?> </Div> <! -- Form -->
Create a folder: images/upfile in the protect directory to upload images.
Ii. Image Processing
Download image extensions: http://www.yiiframework.com/extension/image#hh1
Follow the relevant configuration in the relevant documents, put the decompressed folder image under exetension, put helper under protected, and configure the following in Main. php:
‘import‘=>array( ‘application.models.*‘, ‘application.components.*‘, ‘application.helpers.*‘, ), // application components ‘components‘=>array( ‘image‘=>array( ‘class‘=>‘application.extensions.image.CImageComponent‘, // GD or ImageMagick ‘driver‘=>‘GD‘, // ImageMagick setup path ‘params‘=>array(‘directory‘=>‘/opt/local/bin‘), ),
After the configuration is complete, open the red comment section under the Controller above to process the image.