【问题标题】:Resize to 3 different size at a time to different path using intervention in laravel 5.0使用 laravel 5.0 中的干预一次将大小调整为 3 个不同的大小以适应不同的路径
【发布时间】:2015-08-16 15:29:41
【问题描述】:

我正在尝试使用干预在 Laravel 4.2 中调整图像大小,但我想一次将图像移动到 2 个不同的文件夹。

如果我运行以下代码

if(Input::hasFile('product_images')) {
    $images = Input::file('product_images');
      foreach($images as $image) {
      if($image->isValid()) {
        $file_name = microtime();
        $file_name = str_replace(' ', '_', $file_name);
        $file_name = str_replace('.', '_', $file_name);
        $file_name = $file_name . '.' . $image->getClientOriginalExtension();
        $file_name = $image->getClientOriginalExtension();
        $image->move(public_path() . '/uploads/', $file_name);
        $file = Image::make(sprintf('uploads/%s', $file_name))->resize(800, 600)->save();

它(上面的代码)适用于上传文件夹,但是,如果我在上传文件夹中创建另一个名为 thumbnail 的文件夹并添加以下行,我会得到错误...,

("Intervention\Image\Exception\NotReadableException","message":"图像源不可读")........

    $file = Image::make(sprintf('uploads/thumbnail/%s', $file_name))->resize(75,75)->save();

提前致谢

【问题讨论】:

    标签: image laravel-5 resize blurry intervention


    【解决方案1】:

    上述代码的问题是原始文件被调整为模糊图像。可以通过使用下面代码中使用的 aspectRatio 来避免。

    还将内存限制设置为 -1。这样可以更轻松地为网页上传更大的图像,30 到 60 KB 甚至更大。

    实现很像:

    $imageType = array(
                    'thumbnail' => array(
                        'width' => 50,
                        'height' => 50                    
                    ),
                    'detail_page' => array(
                        'width' => 200,
                        'height' => 200                    
                    ),
                    'product' => array(
                        'width' => 400,
                        'height' => 400                    
                    ),
                );
    
    if(Request::hasFile('image')) {
    $image = Request::file('image');
        if($image->isValid()) {
            ini_set('memory_limit', '-1');
            $file_name = microtime();
            $file_name = str_replace(' ', '_', $file_name);
            $file_name = str_replace('.', '_', $file_name);
            $file_name = $file_name . '.' . $image->getClientOriginalExtension();
            $image->move(public_path() . '/uploads/', $file_name);
            $response['file_name'] = $file_name;
    
    foreach ($imageType as $key => $value) {
    
        $file = Image::make(sprintf('uploads/%s', $file_name))->resize($value['width'], $value['height'],
                function($constraint) {
                           $constraint->aspectRatio();
                });
    
        $file->save(public_path() . '/uploads/'.$value['width'].'X'.$value['height'].'/'. $file_name);
    }
    
    $product_image_url = URL::to('uploads/' . $file_name);
    

    【讨论】:

      【解决方案2】:

      我终于设法在 Laravel 4.2 中使用干预来调整图像大小,还可以在一行代码中一次将图像移动到不同的文件夹。

      见下面的代码sn-p,

      $file = Image::make(sprintf('uploads/%s', $file_name))
        ->resize(800, 600)->save()
        ->resize('1280', '1024')->save(public_path() . '/uploads/1280x1024/' . $file_name)
        ->resize('316', '255')->save(public_path() . '/uploads/316x255/' . $file_name)
        ->resize('118', '95')->save(public_path() . '/uploads/118x95/' . $file_name);
      

      如您所见,我在 public_path 的上传文件夹中创建了 3 个文件夹,分别命名为 1280x1024、316x255、118x95。

      所以如果你使用上面的代码,在 resize() 之后给出 save() 函数的路径并继续重复以一次保存所有调整大小的图像。

      如果有人知道更好的答案,请尝试发布并改进它。

      【讨论】:

        【解决方案3】:

        使用干预/图像包

        public function upload() {
              $image = \Image::make(\Input::file('image'));
              $path = storage_path('app')."/";
        
             // encode image to png
             $image->encode('png');
             // save original
             $image->save($path."original.png");
             //resize
             $image->resize(300,200);
             // save resized
             $image->save($path."resized.png");
        }
        

        【讨论】:

          【解决方案4】:
          if(Input::file()){
              $image = Input::file('image');
              $filename  = time() . '.' . $image->getClientOriginalExtension();
              $path = public_path('profilepics/' . $filename);
          
              Image::make($image->getRealPath())->resize(200, 200)->save($path);
              $user->image = $filename;
              $user->save();
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-03-28
            • 2018-09-21
            • 2016-05-28
            • 2011-11-15
            相关资源
            最近更新 更多