如何保存图像在cakephp [英] how to save image in cakephp

查看:143
本文介绍了如何保存图像在cakephp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要上传图像在表中以及在folder.please帮助我,我将如何保存文件夹和数据库中的图像。请描述方法。

I need to upload image in table as well as in folder.please help me ,how i will save image in folder and database. please describe the method.

感谢
Manish

Thanks Manish

推荐答案

你可以像往常一样在PHP中。我只是这样做了几天前像这样:

You can do it "as usual" in PHP. I just did it a few days ago like this:

$path = "/img/avatars/";
$dir = getcwd().$path;
$avatarFile = "$dir$id.png";

if (isset($this->data['User']['avatar']) && $this->data['User']['avatar']['error'] == 0) {
                $avatar = $this->data['User']['avatar'];
                if (!is_uploaded_file($avatar['tmp_name'])) $this->Utils->panic($avatar);

                if (in_array($avatar['type'], array('image/jpeg','image/pjpeg','image/png'))) {

                    // load image
                    list($width,  $height) = getimagesize($avatar['tmp_name']);
                    $width = $height = min($width, $height);

                    if (in_array($avatar['type'], array('image/jpeg','image/pjpeg')))
                        $source = imagecreatefromjpeg($avatar['tmp_name']);
                    else
                        $source = imagecreatefrompng($avatar['tmp_name']);

                    // resize
                    $thumb = imagecreatetruecolor(128, 128);
                    imagecopyresized($thumb,  $source,  0,  0,  0,  0,  128,  128,  $width,  $height);

                    // save resized & unlink upload
                    imagepng($thumb, $avatarFile);

                    $success &= true;
                } else {
                    $this->User->invalidate('avatar', __("Only JPG or PNG accepted.",true));
                    $success &= false;
                }

                unlink($avatar['tmp_name']); // Delete upload in any case
            }

它甚至会为你调整大小到128x128,您可以跳过它,只需将上传的图像重命名为目标目录。

It's even going to resize it for you always to 128x128, you can skip that and just rename the uploaded image to the target dir. Google will also help you out, there is nothing Cake specific for file uploads.

上传表单:

echo $form->create('User', array(
    'enctype' => 'multipart/form-data',
    'type' => 'post',
));
echo $form->input('avatar', array('type' => 'file', 'label' => __('Avatar:',true)));

这篇关于如何保存图像在cakephp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆