多图放大或缩小

model文件

use Intervention\Image\Facades\Image;


//方法
public function getThumbnailLargeMoreImagesUrlAttribute()
    {
        $more_images = json_decode($this->attributes['more_images'], true);
        $image_url = [];

        if (is_array($more_images) && !empty($more_images)) {
            foreach ($more_images as $image) {
                // 如果 image 字段本身就已经是完整的 url 就直接返回
                if (Str::startsWith($image, ['http://', 'https://'])) {
                    $image_url[] = $image;
                } else {
                    $images = explode(".", $image);
                    $image_ext = $images[count($images) - 1];
                    $image_url_pre = str_replace("." . $image_ext, "", $image);

                    $new_image = $image_url_pre.'-large.'.$image_ext;
                    $storage_image = 'storage/'.$new_image;

                    if (file_exists(public_path($storage_image))) {
                        $image_url[] = \Storage::disk('public')->url($new_image);
                    } else {
                        if (file_exists(public_path('storage/'.$image))) {
                            Image::make('storage/' . $image)->fit(540, 540)->save($storage_image);
                            $image_url[] = \Storage::disk('public')->url($new_image);
                        }
                    }
                }
            }
        }

        return $image_url;
    }

调用

$user User::find(1);

$user->thumbnail_large_more_images_url;

 2,728 total views,  2 views today

Revisions

There are no revisions for this post.

Comments are closed.