路由 admin-new/add-new-creatives 不支持 GET 方法。支持的方法:POST。且未找到“Imagick”类

问题描述 投票:0回答:1

我的PHP版本:8.2.4 我的 Laravel 版本:10.10

路由 admin-new/add-new-creatives 不支持 GET 方法。支持的方法:POST。和类“Imagick”未找到{“userId”:560,“异常”:“[对象](错误(代码:0):在F:\ Projects \ laravel \ Laravel 10 dplay-dsp中未找到类“Imagick” -cms-update pp\Http\Controllers dmin\A

我的路线:

    Route::get('new-upload-new-creatives', [CreativeController::class, 'uploadCreativesForCampaign']);
    Route::post('new-upload-new-creatives', [CreativeController::class, 'uploadCreativesNew']);

控制器中我的 uploadCreativesForCampaign 函数:

public function uploadCreativesForCampaign(Request $request)
    {
        $campaign = Campaign::with('campaignCreatives')->has('campaignCreatives')->select('campaign_name', 'id')->orderBy('id', 'desc')->get();
        $default_dim = Config::get('adplay.dimension_list');
        asort($default_dim);
        $video_dim = Config::get('adplay.video_dimension');
        asort($video_dim);

        if (!empty($request)) {
            $selected = Campaign::select('campaign_name', 'id')->where('campaign_name', $request->campaign_name)->first();
        }

        return view('dsp-backend.admin.creative.new-upload_creatives_form2', compact('campaign', 'default_dim', 'video_dim', 'selected'));
    }

我的 uploadCreatives 控制器中的新功能:

public function uploadCreativesNewsh(Request $request)
    {

        $exist = DB::table('creatives')->where('campaign_id', $request->campaign_id)->first();
        $index = 0;
        if (!empty($exist)) {


            if ($request->creative_type == 1) {

                foreach ($request->image_url as $key) {

                    $file = $key;
                    $destinationPath = public_path() . '/aws/temp';
                    $file_info = pathinfo($file->getClientOriginalName());
                    $original_name = Util::cleanString($file_info['filename']);
                    $ext = strtolower($file_info['extension']);
                    $img_name = md5(uniqid(rand(), true)) . $original_name . '.' . $ext;

                    $file->move($destinationPath, $img_name);


                    $campaign_data = DB::table('creatives')->where('campaign_id', $request->campaign_id)->insert([
                        'campaign_id' => $exist->campaign_id,
                        'appid' => $exist->appid,
                        'gid' => $exist->gid,
                        'creative_type' => $exist->creative_type,
                        'attribute' => $exist->attribute,
                        'image_name' => $img_name,
                        'price' => $exist->price,
                        'from_hour' => $exist->from_hour,
                        'to_hour' => $exist->to_hour,
                        'status' => 4,
                        'is_approved' => 2

                    ]);
                }


            }

            if ($request->creative_type == 302) {
                foreach ($request->vast_video as $key) {

                    $file = $key;
                    //echo '<pre>';print_r($file);exit;
                    //$file                = $request->image_url[$index];
                    $destinationPath = public_path() . '/aws/temp';
                    //echo '<pre>';print_r($destinationPath);exit;
                    $file_info = pathinfo($file->getClientOriginalName());
                    $original_name = Util::cleanString($file_info['filename']);
                    $ext = strtolower($file_info['extension']);
                    $img_name = md5(uniqid(rand(), true)) . $original_name . '.' . $ext;
                    $file->move($destinationPath, $img_name);


                    $campaign_data = DB::table('creatives')->where('campaign_id', $request->campaign_id)->insert([
                        'campaign_id' => $exist->campaign_id,
                        'appid' => $exist->appid,
                        'gid' => $exist->gid,
                        'creative_type' => $request->creative_type,
                        'attribute' => $exist->attribute,
                        'image_name' => $img_name,
                        // 'image_name' => $file_info['basename'],
                        'dimension' => 'D' . $request->vast_dimension[$index],
                        'price' => $exist->price,
                        'from_hour' => $exist->from_hour,
                        'to_hour' => $exist->to_hour,
                        'status' => 4,
                        'is_approved' => 2

                    ]);

                    $index++;
                }
            }

            if ($request->creative_type == 101) {
                foreach ($request->htmltag as $key) {
                    $campaign_data = DB::table('creatives')->where('campaign_id', $request->campaign_id)->insert([
                        'campaign_id' => $exist->campaign_id,
                        'appid' => $exist->appid,
                        'gid' => $exist->gid,
                        'creative_type' => $exist->creative_type,
                        'attribute' => $exist->attribute,
                        'htmltag' => $key,
                        'dimension' => 'D' . $request->html_dimension[$index],
                        'price' => $exist->price,
                        'from_hour' => $exist->from_hour,
                        'to_hour' => $exist->to_hour,
                        'status' => 4,
                        'is_approved' => 2

                    ]);

                    $index++;
                }
            }

        }
        return redirect()->back()->with(['success' => 'Creative Saved', 'alert-type' => 'success']);
    }

我的刀片文件格式:

public function uploadCreativesNewsh(Request $request)
    {

        $exist = DB::table('creatives')->where('campaign_id', $request->campaign_id)->first();
        $index = 0;
        if (!empty($exist)) {


            if ($request->creative_type == 1) {

                foreach ($request->image_url as $key) {

                    $file = $key;
                    $destinationPath = public_path() . '/aws/temp';
                    $file_info = pathinfo($file->getClientOriginalName());
                    $original_name = Util::cleanString($file_info['filename']);
                    $ext = strtolower($file_info['extension']);
                    $img_name = md5(uniqid(rand(), true)) . $original_name . '.' . $ext;

                    $file->move($destinationPath, $img_name);


                    $campaign_data = DB::table('creatives')->where('campaign_id', $request->campaign_id)->insert([
                        'campaign_id' => $exist->campaign_id,
                        'appid' => $exist->appid,
                        'gid' => $exist->gid,
                        'creative_type' => $exist->creative_type,
                        'attribute' => $exist->attribute,
                        'image_name' => $img_name,
                        'price' => $exist->price,
                        'from_hour' => $exist->from_hour,
                        'to_hour' => $exist->to_hour,
                        'status' => 4,
                        'is_approved' => 2

                    ]);
                }


            }

            if ($request->creative_type == 302) {
                foreach ($request->vast_video as $key) {

                    $file = $key;
                    //echo '<pre>';print_r($file);exit;
                    //$file                = $request->image_url[$index];
                    $destinationPath = public_path() . '/aws/temp';
                    //echo '<pre>';print_r($destinationPath);exit;
                    $file_info = pathinfo($file->getClientOriginalName());
                    $original_name = Util::cleanString($file_info['filename']);
                    $ext = strtolower($file_info['extension']);
                    $img_name = md5(uniqid(rand(), true)) . $original_name . '.' . $ext;
                    $file->move($destinationPath, $img_name);


                    $campaign_data = DB::table('creatives')->where('campaign_id', $request->campaign_id)->insert([
                        'campaign_id' => $exist->campaign_id,
                        'appid' => $exist->appid,
                        'gid' => $exist->gid,
                        'creative_type' => $request->creative_type,
                        'attribute' => $exist->attribute,
                        'image_name' => $img_name,
                        // 'image_name' => $file_info['basename'],
                        'dimension' => 'D' . $request->vast_dimension[$index],
                        'price' => $exist->price,
                        'from_hour' => $exist->from_hour,
                        'to_hour' => $exist->to_hour,
                        'status' => 4,
                        'is_approved' => 2

                    ]);

                    $index++;
                }
            }

            if ($request->creative_type == 101) {
                foreach ($request->htmltag as $key) {
                    $campaign_data = DB::table('creatives')->where('campaign_id', $request->campaign_id)->insert([
                        'campaign_id' => $exist->campaign_id,
                        'appid' => $exist->appid,
                        'gid' => $exist->gid,
                        'creative_type' => $exist->creative_type,
                        'attribute' => $exist->attribute,
                        'htmltag' => $key,
                        'dimension' => 'D' . $request->html_dimension[$index],
                        'price' => $exist->price,
                        'from_hour' => $exist->from_hour,
                        'to_hour' => $exist->to_hour,
                        'status' => 4,
                        'is_approved' => 2

                    ]);

                    $index++;
                }
            }

        }
        return redirect()->back()->with(['success' => 'Creative Saved', 'alert-type' => 'success']);
    }

我的刀片文件ajax:

$(document.body).on('change', '.creative_load', function(event) {
            event.preventDefault();
            var file = $(this);
            var form = file.closest('.creative_area');

            console.log(form)

            var form_data = new FormData();
            if ($(this).get(0).files.length > 0) {
                $.each($(this).get(0).files, function(index, val) {
                    form_data.append('creative[]', val);
                    console.log($(this));
                });

                form.find('.uploader').html('<div class="loader center-block"></div>');
                
                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    type: 'POST',
                    processData: false,
                    contentType: false,
                    data: form_data,
                    url: base_url + 'admin-new/add-new-creatives',
                    cache: false,
                    mimeTypes: "multipart/form-data",
                    success: function(dt) {
                        var item = $.parseJSON(dt);
                        if (dt.length > 0) {

                            for (p = 0; p < item.length; p++) {
                                var stat = '';

                                //insert dimesions in array for checking
                                dim.push(item[p]['dimension']);

                                if (item[p]['upload_status'] == 1) {
                                    var stat =
                                        '<span class="label label-success"> File type Ok </span>';
                                } else {
                                    var stat =
                                        '<span class="label label-danger"> File not supported </span>';
                                }

                                var img = '<div class="text-center well image-div">';
                                img +=
                                    '<button class="btn btn-xs btn-danger pull-right remove-img-btn" data-image="' +
                                    item[p]['filename'] +
                                    '" type="button" style="padding-left: 13px; padding-right: 13px;"> X </button>';
                                img += '<div> &nbsp; </div>';
                                img += '<div> &nbsp; </div>';
                                if (item[p]['upload_status'] == 1) {
                                    img +=
                                        '<input class="file_name" type="hidden" name="img_name[]" value="' +
                                        item[p]['filename'] + '">';
                                    img +=
                                        '<input class="dimension" type="hidden" name="dimension[]" value="' +
                                        item[p]['dimension'] + '">';
                                }

                                if (item[p]['extension'] == '.mp4') {
                                    img += '<center><video width="400" controls><source src="' +
                                        base_url + 'aws/temp/' + item[p]['filename'] +
                                        '" type="video/mp4"></video></center>';
                                    img += '<p><b style="word-break: break-word;">File name : ' + item[
                                        p]['filename'] + '</b></p>';
                                    img += '<p><b>Dimension : ' + item[p]['dimension'] + '</b></p>';
                                } else {
                                    img += '<center><img class="img-responsive " src="' + base_url +
                                        'aws/temp/' + item[p]['filename'] + '"></center>';
                                    img += '<p><b style="word-break: break-word;">Image name : ' + item[
                                        p]['filename'] + '</b></p>';
                                    img += '<p><b>Dimension : ' + item[p]['dimension'] + '</b></p>';
                                }

                                img += '<p><b> File supported / Type   : ' + stat + '</b></p>';
                                img +=
                                    '<label> Link : </label><input class="adlinks form-control input-sm" type="text" name="adlink[]" value="" placeholder="Tracking URL">';

                                if (item[p]['banner_type'] == 'carousel') {
                                    img += '<label>Call to Action text</label>';
                                    img +=
                                        '<input name="button_text[]" placeholder="Maximum 15 characters" type="text" class="form-control input-sm button_text" maxlength="15" required="required">';
                                }

                                img += '</div> ';
                                img += '<div> &nbsp; </div>';

                                form.find('.preview_contain').append(img);
                                img = '';
                            }
                            // if (item[0]['banner_type'] == 'carousel') {
                            //     $('#preview_contain').prepend('<div class="well"><input name="button_text" placeholder="Button text" type="text" class="form-control button_text"></div>');
                            // }

                        } else {
                            form.notify(dt.msg, "error", {
                                position: "top center"
                            });
                        }

                        form.find('.uploader').html('');
                    }
                });
            }
        });

尝试使用 imagick 的 laravel 镜像包 inistit 还是不行。

php laravel xampp
1个回答
0
投票

出现此问题是因为我的其他控制器中的以下函数的“Imagick”类

public function uploadMultipleCreative(Request $request)
{
    
    $default_dimension   = $request->dimnsion;
    $files               = $request->creative;
    $file_count          = count($request->creative);
    $supported_dimension = Config::get('adplay.dimension_list');
    $destinationPath     = public_path() . '/aws/temp';
    $session_id          = Session::getId(); //  Session::regenerate()
    $banner_type         = Session::get('banner_type');
    $uploaded_files      = array();
   // echo '<pre>';print_r($files);exit;

    foreach ($files as $k => $file) {
        $rules     = array('file' => 'required|mimes:png,gif,jpeg,txt,pdf,mp4'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
        $validator = Validator::make(array('file' => $file), $rules);
        //if ($validator->passes()) {

        $file_dim = getimagesize($file);
        if ($file_dim) {
            $file_dim = $file_dim[0] . 'x' . $file_dim[1]; // upload file dimension
        } else {
            $file_dim = $default_dimension[0]; // upload file dimension
        }

        // Set file name
        $image_info    = pathinfo($file->getClientOriginalName());
        $original_name = Util::cleanString($image_info['filename']);
        $img_name      = md5(uniqid(rand(), true)) . $original_name;
        $ext           = strtolower($image_info['extension']);

        //****************************************************
        if ($ext == 'gif' || $ext == 'bmp') {
            $img_dimension  = getimagesize($file);
            $logo_dimension = getimagesize(public_path() . '/logo/adplay-logo.png');

            $logo = new \Imagick();
            $logo->readImage(public_path() . '/logo/adplay-logo.png');

            $imagick = new \Imagick();
            $imagick->readImage($file);

            if ($ext == 'gif') {
                $imagick = $imagick->coalesceImages();
                do {
                    $imagick->compositeImage($logo, \Imagick::COMPOSITE_OVER, 0, $img_dimension[1] - $logo_dimension[1]);
                    //$imagick->resizeImage(120, 120, \Imagick::FILTER_BOX, 1);
                } while ($imagick->nextImage());
                $imagick = $imagick->deconstructImages();
                $imagick->writeImages($destinationPath . '/' . $img_name . '.gif', true);

                $image_extension = '.gif';
            } else {
                $imagick->compositeImage($logo, \Imagick::COMPOSITE_OVER, 0, $img_dimension[1] - $logo_dimension[1]);
                $imagick->writeImages($destinationPath . '/' . $img_name . '.bmp', true);

                $image_extension = '.bmp';
            }

            $imagick->clear();
            $imagick->destroy();
        } else if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png') {
            $stamp        = imagecreatefrompng(public_path() . '/logo/adplay-logo.png');
            $marge_right  = 0;
            $marge_bottom = 0;
            $sx           = imagesx($stamp);
            $sy           = imagesy($stamp);

            if ($ext == 'jpg' || $ext == 'jpeg') {
                $im = imagecreatefromjpeg($file);
                //echo '<pre>';print_r($im);
                //imagecopy($im, $stamp, $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
                imagejpeg($im, $destinationPath . '/' . $img_name . '.jpg', 80);

                $image_extension = '.jpg';
            } elseif ($ext == 'png') {
                $im = imagecreatefrompng($file);
                //echo '<pre>';print_r($im);exit;

                //imagecopy($im, $stamp, $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
                imagesavealpha($im, true);
                imagepng($im, $destinationPath . '/' . $img_name . '.png', 9);

                $image_extension = '.png';
            }

            imagedestroy($im);
        } else if ($ext == 'mp4') {
            $file->move($destinationPath, $img_name . '.mp4');
            $image_extension = '.mp4';
        } else {
            $image_extension = '';
        }

        //****************************************************
        if (in_array($file_dim, $supported_dimension)) {
            $f = array(
                'filename'      => $img_name . $image_extension,
                'extension'     => $image_extension,
                'dimension'     => $file_dim,
                'upload_status' => 1,
                'session_id'    => $session_id,
                'banner_type'   => $banner_type,
            );
        } else {
            $f = array(
                'filename'      => $img_name . $image_extension,
                'extension'     => $image_extension,
                'dimension'     => $file_dim,
                'upload_status' => 0,
                'session_id'    => $session_id,
                'banner_type'   => $banner_type,
            );
        }
        if (empty($image_extension)) {
            $f['upload_status'] = 0;
        }
        if ($banner_type == 'carousel' && $image_extension == '.gif') {
            $f['upload_status'] = 0;
        }

        $uploaded_files[] = $f;
        //}
    }

    if ($banner_type == 'carousel') {
        $uploaded_files = array_slice($uploaded_files, 0, 6);
    }

    return json_encode($uploaded_files);
}

给定函数通过删除“Imagick”类中的所有内容解决了该问题

public function uploadMultipleCreative(Request $request)
{
    $default_dimension   = $request->dimension;
    $files               = $request->creative;
    $file_count          = count($request->creative);
    $supported_dimension = Config::get('adplay.dimension_list');
    $destinationPath     = public_path() . '/aws/temp';
    $session_id          = Session::getId();
    $banner_type         = Session::get('banner_type');
    $uploaded_files      = [];

    foreach ($files as $k => $file) {
        $rules     = ['file' => 'required|mimes:png,gif,jpeg,txt,pdf,mp4'];
        $validator = Validator::make(['file' => $file], $rules);

        // Extract file dimensions
        $file_dim = null;
        $image_info = getimagesize($file);
        if ($image_info) {
            $file_dim = $image_info[0] . 'x' . $image_info[1];
        } else {
            $file_dim = $default_dimension[0]; // Assign default dimension or handle accordingly
        }

        $image_extension = '';
        $img_name = '';
        $ext = strtolower($file->getClientOriginalExtension());

        if (in_array($ext, ['gif', 'bmp'])) {
            if ($ext === 'gif') {
                // Handle GIF files
                // You can move the file to the destination or perform any other operations
                $img_name = md5(uniqid(rand(), true));
                $file->move($destinationPath, $img_name . '.gif');
        
                // Prepare file information for storage
                $f = [
                    'filename'      => $img_name . '.gif',
                    'extension'     => '.gif',
                    'dimension'     => $file_dim,
                    'upload_status' => 1, // Assuming success
                    'session_id'    => $session_id,
                    'banner_type'   => $banner_type,
                ];
        
                $uploaded_files[] = $f;
            } elseif ($ext === 'bmp') {
                // Handle BMP files by converting to PNG or JPEG using GD library
                $img = imagecreatefrombmp($file->getRealPath()); // Custom function to convert BMP to GD image resource
            
                // Example: Convert BMP to PNG
                $img_name = md5(uniqid(rand(), true));
                $destinationFile = $destinationPath . '/' . $img_name . '.png';
                imagepng($img, $destinationFile, 9);
            
                // Free memory
                imagedestroy($img);
            
                // Prepare file information for storage
                $f = [
                    'filename'      => $img_name . '.png',
                    'extension'     => '.png',
                    'dimension'     => $file_dim,
                    'upload_status' => 1, // Assuming success
                    'session_id'    => $session_id,
                    'banner_type'   => $banner_type,
                ];
            
                $uploaded_files[] = $f;
            }
            
        }
         elseif (in_array($ext, ['jpg', 'jpeg', 'png'])) {
            $im = null;
            if ($ext == 'jpg' || $ext == 'jpeg') {
                $im = imagecreatefromjpeg($file);
            } elseif ($ext == 'png') {
                $im = imagecreatefrompng($file);
                imagesavealpha($im, true);
            }

            // Resize the image (example: resizing to 300x200)
            $resized_im = imagescale($im, 300, 200);

            // Watermark logic (example: adding watermark to bottom right corner)
            $stamp = imagecreatefrompng(public_path() . '/logo/adplay-logo.png');
            $sx    = imagesx($stamp);
            $sy    = imagesy($stamp);
            $marge_right = 10;
            $marge_bottom = 10;
            $dest_x = imagesx($resized_im) - $sx - $marge_right;
            $dest_y = imagesy($resized_im) - $sy - $marge_bottom;
            imagecopy($resized_im, $stamp, $dest_x, $dest_y, 0, 0, $sx, $sy);

            // Save the manipulated image
            $img_name = md5(uniqid(rand(), true));
            $destinationFile = $destinationPath . '/' . $img_name . '.' . $ext;
            if ($ext == 'jpg' || $ext == 'jpeg') {
                imagejpeg($resized_im, $destinationFile, 80);
                $image_extension = '.jpg';
            } elseif ($ext == 'png') {
                imagepng($resized_im, $destinationFile, 9);
                $image_extension = '.png';
            }

            // Destroy images to free memory
            imagedestroy($im);
            imagedestroy($stamp);
            imagedestroy($resized_im);
        } elseif ($ext == 'mp4') {
            // Handle MP4 files
            $img_name = md5(uniqid(rand(), true));
            $file->move($destinationPath, $img_name . '.mp4');
            $image_extension = '.mp4';
        } else {
            // Handle unsupported file types
            $image_extension = '';
        }

        // Prepare file information for storage
        $f = [];
        if (in_array($file_dim, $supported_dimension)) {
            $f = [
                'filename'      => $img_name . $image_extension,
                'extension'     => $image_extension,
                'dimension'     => $file_dim,
                'upload_status' => 1,
                'session_id'    => $session_id,
                'banner_type'   => $banner_type,
            ];
        } else {
            $f = [
                'filename'      => $img_name . $image_extension,
                'extension'     => $image_extension,
                'dimension'     => $file_dim,
                'upload_status' => 0,
                'session_id'    => $session_id,
                'banner_type'   => $banner_type,
            ];
        }
        if (empty($image_extension)) {
            $f['upload_status'] = 0;
        }
        if ($banner_type == 'carousel' && $image_extension == '.gif') {
            $f['upload_status'] = 0;
        }

        $uploaded_files[] = $f;
    }

    // Additional logic if required...

    return json_encode($uploaded_files);
}
© www.soinside.com 2019 - 2024. All rights reserved.