如何以json格式更新和删除(取消链接)多个旧图像 [英] how to update and remove (unlink) multiple old images in json format

查看:105
本文介绍了如何以json格式更新和删除(取消链接)多个旧图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这仍然与我在这里的帖子有关如何删除和取消链接json格式的多张图片

this is still related to my post in here How to remove and unlink multiple images in json format

我可以存储和删除所有多个图像.但是更新和删除旧图像时我仍然有问题.我的代码在更新新图像时仍然不能删除oldimage.

i can store and delete all multiple images. but i have still problem when update and remove old image. my code still not remove the oldimage when update the new image.

我的更新控制器,我使用handlerequest方法存储图像,其中oldImage是数据库中的变量,它将与新图像进行比较.如果不一致,则删除旧图像.

my update controller, i use handlerequest method to store images, where oldImage is variable in database that will be compared with new images. if not same then remove old images.

public function update(Requests\UpdatePostRequest $request, $id)
{

    $post = Post::findOrFail($id);
    $oldImage = $post->image;
    $data = $this->handleRequest($request);
    $post->update($data);


    if($oldImage != $post->image)
    {
        foreach (json_decode($post->image, true) as $oldImage) {
        $this->removeImage($oldImage);
        }
    }

    if($request->submitbutton =='Publish')
    {
        Alert::success('Your post was updated successfully')->persistent('Close');
        return redirect(route('admins-blogpost.index'));
    }
    if($request->submitbutton =='Save Draft')
    {
        Alert::success('Your draft was updated successfully')->persistent('Close');
        return redirect(route('admins-blogpost.index'));
    }
}

我的handlerequest方法用于存储多个图像,该方法正在使用store方法.所以这个问题不在这里.

my handlerequest method to store multiple image, this method is working using store method. so this problem not here.

private function handleRequest($request)
{
    $data = $request->all();

    if($request->hasFile('image'))
    {
        $images = $request->file('image');
        foreach($images as $key=>$image){
            $fileName    = $image->getClientOriginalName();

            $destination = $this->uploadPath;

            $successUploaded = $image->move($destination, $fileName);
            if($successUploaded)
            {
                $width = config('cms_blog.image.thumbnail.width');
                $height = config('cms_blog.image.thumbnail.height');
                $extension = $image->getClientOriginalExtension();
                $thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
                Image::make($destination . '/' . $fileName)
                        ->resize($width,$height)
                        ->save($destination . '/' . $thumbnail);
                $datax[] = $fileName;
            }
            $data['image'] = json_encode($datax);
        }

    }
    return $data;
}

和删除方法

public function removeImage($image)
{
    if( ! empty($image))
    {
        $imagePath = $this->uploadPath . '/' . $image;
        $ext = substr(strrchr($image, '.'), 1);
        $thumbnail = str_replace(".{$ext}", "_thumb.{$ext}", $image);
        $thumbnailPath = $this->uploadPath . '/' . $thumbnail;
        if(file_exists($imagePath) ) unlink($imagePath);
        if(file_exists($thumbnailPath) ) unlink($thumbnailPath);
    }
}

如何解决我的问题,以便在旧图像与新图像不相同时删除旧图像?

how to fix my problem so i can remove old images when their not same with new image?

推荐答案

在您删除图像的for循环中,我相信您想遍历$oldImage而不是遍历$post->image:

In the for loop where you delete the images, I believe you wanted to iterate over $oldImage, not over $post->image:

foreach (json_decode($oldImage, true) as $item) {
    $this->removeImage($item);
}

这篇关于如何以json格式更新和删除(取消链接)多个旧图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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