LoginSignup
1
3

More than 5 years have passed since last update.

【CakePHP2】画像を保存するUploadPack【MQ02】

Last updated at Posted at 2017-02-28

概要

 今回は画像を保存して表示です。

 (参考サイト:CakePHP画像リサイズ最強プラグインUploadPackをCake初心者でも分かるよう徹底解説

 上のサイト通りではあるのですが、なぜか最初にやった時に良く分からなかったので、コードそのまま載せます。

 環境はCakePHP2.9.4です。

DB

 今回は「images」テーブルを作りました。
 大事なのは「img01_file_name」のように「_file_name」という名前のカラムを用意することです。

photo_db.PNG

Plugin(プラグイン)

1.GitHubからダウンロードしてapp/Pluginに置く

2.upload_packという名前に変える

3.Pluginを読み込むように設定する
 (最終行に追加?)

app/Config/bootstrap.php
<?php

    CakePlugin::loadAll();
}

フォルダのアクセス権限設定

 画像を保存するフォルダ(upload)下のすべてのファイルの権限を変える。

app/webroot
mkdir upload
chmod 777 -R /upload

(追記)
「実行権限いらないだろう」と776で書いていたのですが、777でないとダメでした。

Model

Model/Image.php

<?php
App::uses('AppModel', 'Model');

class Image extends AppModel{

        var $name = 'Image';
        var $actsAs = array(

                'UploadPack.Upload' => array(
                        // 画像保存用フィールド名(_file_nameは書かない)
                        'img' => array(
                                'quality' => 95,
                                'path' => ':webroot/upload/:model/:style:id.:extension',

                                // styleを指定しないと、サムネイルが保存されない
                                'styles' => array(
                                        'big' => '100w', // オリジナルサイズ
                                        'small' => '80h', //
                                        'thumb' => '50x50',
                                        // 勝手にoriginalとして保存してくれてた
                                ),
                        ),
                ),

                'Search.Searchable',
        );

        public $filterArgs = array(
                'id' => array('type' => 'value'),
                'title' => array('type' => 'like'),
                'img_file_name' => array('type' => 'like'),
                'mailaddress' => array('type' => 'like'),
                'created' => array('type' => 'value'),

                /*

                [type]

                value, int              ->      等号、不等号などを使う場合
                like, string        ->      あいまい検索をする場合
                expression              ->      BETWEEN等
                subquery                ->      INを使いたい時
                query                   ->      一番自由度の高いタイプ

                 */
        );
}

Controller

Controller/ImagesController.php
<?php

class ImagesController extends AppController {

        var $uses = array('Image'); // Imageモデルを使うよー

        public $components = array(
                'Paginator',
        );

        // ヘルパーの読み込みを忘れずに行う
        var $helpers = array('Form', 'UploadPack.Upload');

        public function index() {
                $this->set('images', $this->Image->find('all'));
        }

        public function add() {
                if (!empty($this->data)) {
                        if ($this->Image->save($this->data)) {
                                // /index じゃダメ、地味に微調整
                                $this->redirect('index');
                        }
                }
        }


}

?>

View

View/Images/index.ctp

<h1>UploadPack</h1>
<p>This is UploadPack Test!</p>
<h2><?php echo $this->Html->link("Add New Image", "/images/add/"); ?></h2>

<table>
        <tr>
                <th>TITLE</th>
                <th>IMAGE</th>
                <th>FILENAME</th>
                <th>MailAddress</th>
                <th>CREATED</th>
        </tr>

<div id="contents">

        <?php foreach($images as $image): ?>
        <tr>
                <td><?php echo $image['Image']['title'];?></td>
                <td><?php echo $this->Upload->uploadImage($image['Image'], 'Image.img', array(
                        // 'style' => 'original'
                        'style' => 'thumb'
                )); ?></td>
                <td><?php echo $image['Image']['img_file_name'];?></td>
                <td><?php echo $image['Image']['mailaddress'];?></td>
                <td><?php echo $image['Image']['created'];?></td>
        </tr>
        <?php endforeach;?>

        <div class="navigation">
                <?php echo $this->Paginator->next('次のページ'); ?>
        </div>

</div>

</table>

View/Images/add.ctp

<h1>画像新規登録</h1>
<?php echo $this->Form->create('Image', array('type' => 'file')); ?>
<table>
        <tr>
                <th>TITLE</th>
                <td><?php echo $this->Form->text('Image.title'); ?></td>
        </tr>
        <tr>
                <th>Mail</th>
                <td><?php echo $this->Form->text('Image.mailaddress'); ?></td>
        </tr>
        <tr>
                <th>IMAGE</th>
        <td>
                <?php echo $this->Form->file('Image.img');?>
                <?php echo $this->Form->error('Image.img');?>
        </td>
</tr>
<tr>
<th></th>
<td>
<?php echo $this->Form->end('新規登録');?></td>
</tr>
</table>

以上です

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3