0

I am trying to resize images in Laravel 4.2 using intervention, but i want to move images to 2 different folders at a time.

If i run the below code

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();

It(above code) runs proper for uploads folder but, if i create another folder called thumbnail inside uploads & add below line i'll get error as....,

("Intervention\Image\Exception\NotReadableException","message":"Image source not readable")........

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

Thanks in advance

Supritha
  • 1
  • 4

4 Answers4

0

I finally managed to resize images in Laravel 4.2 using intervention, also to move images to different folders at a time in one line of code.

See the code snippet below,

$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);

As you can see, I have created 3 folders named 1280x1024, 316x255, 118x95 in uploads folder in public_path.

So if you use above code, after resize() give the path to save() function and keep on repeating to save all resized images at a time.

If anyone knows even better answers, please try to post it and improve it.

Ruchita Sheth
  • 840
  • 9
  • 27
Supritha
  • 1
  • 4
0

The issue with the above code is original file being resized to a blurry images. It can be avoided by using aspectRatio as used in the below code.

Also set memory limit to -1. So that it'll be easier to upload a larger images for a Web page, 30 to 60 KB and larger.

Implementation will be much like:

$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);
Supritha
  • 1
  • 4
0

Use the intervention/image package

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");
}
Rahman Rezaee
  • 1,943
  • 16
  • 24
-1
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();
}