在Yii2中添加水印

时间:2018-02-02 05:53:05

标签: php yii2 watermark

我试图在yii项目中为图像添加水印而不使用任何扩展名。现在我处于水印正在添加到图像的位置,但是当我尝试将水印图像保存到DB中时,我收到错误“调用资源上的成员函数saveAs()”。

添加水印之前的代码

//upload profileImageFile
if(UploadedFile::getInstance($model, 'profileImageFile')){
    $file = UploadedFile::getInstance($model, 'profileImageFile'); 
    $filename = $file->baseName . '_' . time() . '.' . $file->extension;
    $path = AUTO::PROFILE_IMAGE_PATH;
    $imageModel = new Image();    

                    if($imageModel->upload($file, $path, $filename)){
                        $imageModel->filename = $filename;
                        $imageModel->path = $path . $filename;
                        $imageModel->status = Image::STATUS_ACTIVE;
                        $imageModel->save();
                        $model->profile_image_id = $imageModel->id;
                    }
                }

添加水印后的代码

//upload profileImageFile
                if(UploadedFile::getInstance($model, 'profileImageFile')){
                    $file = UploadedFile::getInstance($model, 'profileImageFile'); // get the object
                    $filename = $file->baseName . '_' . time() . '.' . $file->extension;
                    $path = AUTO::PROFILE_IMAGE_PATH;
                    $imageModel = new Image();

                    //add watermark
                    $watermarkImage = imagecreatefrompng('C:/xampp/htdocs/Autobank/frontend/web/images/watermark_image.png');
                    $autoImage = imagecreatefromjpeg($file->tempName);
                    $marge_right = 10;
                    $marge_bottom = 10;
                    $sx = imagesx($watermarkImage);
                    $sy = imagesy($watermarkImage);
                    imagecopy($autoImage, $watermarkImage, imagesx($autoImage) - $sx - $marge_right, imagesy($autoImage) - $sy - $marge_bottom, 0, 0, imagesx($watermarkImage), imagesy($watermarkImage));


                    if($imageModel->upload($autoImage, $path, $filename)){
                        $imageModel->filename = $filename;
                        $imageModel->path = $path . $filename;
                        $imageModel->status = Image::STATUS_ACTIVE;
                        $imageModel->save();
                        $model->profile_image_id = $imageModel->id;
                    }
                }

这是Image.php模型类中的upload()方法。

public function upload($file, $path, $filename)
{
    $old = umask(0);
    if (!file_exists ($path))
        mkdir ($path, 0755, true);
    umask($old);

    $file->saveAs($path . $filename);
    return true;
}

有谁能告诉我这有什么问题以及如何将水印图像保存到数据库中?

0 个答案:

没有答案