1

我将如何使用干预图像使上传的图像保留其扩展名?

这是我保存用户头像的当前代码:

/**
* Saves avatar in filesystem.
*
* @param  UploadedImage $image
* @param  string $path
* @return void
*/
public function saveAvatar($image, array $paths)
{
foreach ($paths as $k => $v)
{
  //delete any previous user avatars
  File::delete(public_path($v));

  if ($k == 'big')
  {
    $encoded = Imagine::make($image['avatar']->getRealPath())
      ->resize(300, 300);

    Imagine::make($encoded)->save(public_path($v), 100);
  }
  else
  {
    $encoded = Imagine::make($image['avatar']->getRealPath())
      ->resize(100, 100);

    Imagine::make($encoded)->save(public_path($v), 100);
  }
}
}

此代码将文件拆分为 2 个文件和一个大文件和一个原始文件,但默认情况下它们会保存为 jpeg,但是如果我希望 jpeg 和 png 不仅保存一个扩展名怎么办。

这是通过Controller上传的过程(最近改了):

 /**
  * Uploads and associates user avatar.
  *
  * @param  string $username
  * @return void
  */
  public function avatar($name)
  {
    $input = ['avatar' => Input::file('avatar')];
    $inputExt = pathinfo( $input->getClientOriginalName() );

    if ( ! $this->validator->setRules('avatar')->with($inputExt)->passes())
    {
      return Redirect::back()->withErrors($this->validator->errors());
    }

    $this->user->uploadAvatar($inputExt, $name);

    return Redirect::back()->withSuccess( trans('users.uploaded avatar success') );
}

这是 UserRepository

  /**
   * Uploads provided avatar and associates with user.
   *
   * @param  array  $input
   * @param  string $id
   * @return void
   */
  public function uploadAvatar(array $input, $id)
  {
    $user = User::find($id);

    $paths['big'] = "assets/uploads/avatars/$id";
    $paths['small'] = "assets/uploads/avatars/$id.small";

    $this->images->saveAvatar($input, $paths);

    $user->avatar = $paths['big'];
    $user->save();
  }
4

3 回答 3

0

这是我为特定项目保存不同大小的图像的代码,我认为它回答了您的问题。

     /**
     * Save image. Generate Different Size versions.
     *
     * @param Request $request
     * @param null $id
     * @return mixed
     */
    public function storeImages(Request $request, $id = null)
    {
        try {
            $project   = Project::find($id);
            $imageInfo = pathinfo(Input::file('image')->getClientOriginalName());
            //append image name with project id
            $imageName      = $imageInfo['filename'] . '_' . $id . '.' . $imageInfo['extension'];
            $smallImageName = $imageInfo['filename'] . '_' . $id . '_small' . '.' . $imageInfo['extension'];
            $thumbImageName = $imageInfo['filename'] . '_' . $id . '_thumb' . '.' . $imageInfo['extension'];

            if (Image::where('name', '=', $imageName)->count()) { //if already have this image, delete it
                Image::where('name', '=', $imageName)->delete();
            }
            $prefix     = Image::getPathPrefix();
            $uploadPath = config('app.image_paths.PROJECT') . $id . DIRECTORY_SEPARATOR;

            if (!Storage::exists($uploadPath)) {
                Storage::makeDirectory($uploadPath);
            }

            $image = \Image::make(Input::file('image'))->save($prefix . $uploadPath . $imageName);
            $image->fit(config('app.image_size.SMALL.height'),
                config('app.image_size.SMALL.width'))->save($prefix . $uploadPath . $smallImageName);
            $image->fit(config('app.image_size.THUMB.height'),
                config('app.image_size.THUMB.width'))->save($prefix . $uploadPath . $thumbImageName);

            $imageObj = Image::create([
                'name'              => $imageName,
                'small_image'       => $smallImageName,
                'thumb_image'       => $thumbImageName,
                'original_filename' => $imageInfo['basename'],
                'mime'              => $image->mime(),
                'created_at'        => Carbon::now(),
                'updated_at'        => Carbon::now(),
                'upload_path'       => $uploadPath
            ]);

            $project->images()->save($imageObj);

            return json_encode([
                'jsonrpc' => '2.0',
                'result'  => 'OK',
                'id'      => $imageObj->id
            ]);
        } catch (\Exception $e) {
            return json_encode([
                'jsonrpc' => '2.0',
                'result'  => false,
                'id'      => $e->getMessage()
            ]);
        }
    }
于 2015-06-29T05:09:44.373 回答
0

我相信问题在于你是双重“制造”。

这应该可以解决问题:

/**
* Saves avatar in filesystem.
*
* @param  UploadedImage $image
* @param  string $path
* @return void
*/
public function saveAvatar($image, array $paths)
{
foreach ($paths as $k => $v)
{
  //delete any previous user avatars
  File::delete(public_path($v));

  if ($k == 'big')
  {
    $encoded = Imagine::make($image['avatar']->getRealPath())
      ->resize(300, 300)->save(public_path($v), 100);
  }
  else
  {
    $encoded = Imagine::make($image['avatar']->getRealPath())
      ->resize(100, 100)->save(public_path($v), 100);
  }
}
}
于 2017-02-25T21:20:38.410 回答
0

对于像我一样偶然发现这一点的人:

Intervention\Image\Image::save()

图像类型将由文件扩展名定义。例如,如果您传递 foo.jpg,图像将被保存为 JPG 文件。如果没有可用的扩展,库将首先尝试使用图像的 MIME 类型来定义编码,如果这也失败了,图像将被编码为 JPEG。

http://image.intervention.io/api/save

此功能仅将图像编码为正确的 mime 类型,并且不添加扩展名。您仍然需要检测文件的扩展名并自行附加。

于 2016-02-16T17:22:59.220 回答