Page 1 of 1

How to resize a image using imagine .

Posted: Wed Oct 07, 2015 9:21 am
by gautamz07
Hey guys i have configured my laravel project to have the imagine package , i have used the steps defined HERE https://github.com/orchestral/imagine to configure my laravel project .

I have the following code in my view:

Code: Select all

<div id="admin">

    <h1>Products Admin Panel</h1><hr>

    <p>Here you can view, delete, and create new products.</p>

    <h2>Products</h2><hr>

    {{ Form::open(array('url'=>'admin/fileupload/create' , 'files'=>true)) }}
    <p>
        {{ Form::file('image') }}
    </p>
    {{ Form::submit('Create Product' , array('class'=>'secondary-cart-btn')) }}
    {{ Form::close() }}

</div>  <!-- end admin -->
and i have the below code in my controller:

Code: Select all

public function postCreate() {

             $fileupload = new Fileupload;
        
             $height = 200;
             $width = 400;
             $mode   = ImageInterface::THUMBNAIL_OUTBOUND;
             $size   = new Box($width, $height);

             $image = Input::file('image');
             $filename = date('Y-m-d-H-i-s')."-".$image->getClientOriginalName();
             $path = public_path('img/testsave/' .$filename);
             $thumbnail   = Imagine::open($path ."jpg")->thumbnail($size, $mode);    
             $fileupload->save();
        
            return Redirect::to('admin/products/index')
                ->with('message' , 'Product created');
    }
I am not sure what the below line really does:

Code: Select all

$mode   = ImageInterface::THUMBNAIL_OUTBOUND;
So i beleive i can used Imagine meathods like so:

Code: Select all

Imagine::open
I get the following error as of now , when i click the submit button

http://i.imgur.com/35QXajK.jpg ,

Is the code in my controller correct ? what am i doing wrong ? :(

My objective is to grab the image once the user presses submit , change the name and use imagine to resize the image. and save it.

Re: How to resize a image using imagine .

Posted: Wed Oct 07, 2015 9:31 am
by Celauran
Looks like you're getting a 404 there, rather than a problem with Imagine itself. Can you confirm via HTTP response codes?

Re: How to resize a image using imagine .

Posted: Wed Oct 07, 2015 9:36 am
by gautamz07
Can you confirm via HTTP response codes?
how do i do that ?

Re: How to resize a image using imagine .

Posted: Wed Oct 07, 2015 9:50 am
by Celauran
Through your browser's developer tools is probably easiest.

Re: How to resize a image using imagine .

Posted: Wed Oct 07, 2015 9:55 am
by gautamz07
Thanks for the handy trick :D will try it

Re: How to resize a image using imagine .

Posted: Wed Oct 07, 2015 9:58 am
by gautamz07

Re: How to resize a image using imagine .

Posted: Wed Oct 07, 2015 10:04 am
by Celauran
So your POST route is failing. I'd check the form action first and foremost.

Re: How to resize a image using imagine .

Posted: Thu Oct 08, 2015 3:39 am
by gautamz07
your right , was using get , instead of post :

Route::get('admin/fileupload/create' , array('uses' => 'FileuploadController@postCreate'));

changed that to

Route::post('admin/fileupload/create' , array('uses' => 'FileuploadController@postCreate'));

Re: How to resize a image using imagine .

Posted: Mon Feb 08, 2016 6:29 am
by Swhite
Hey, try this code :

<?php
$source = 'image.jpeg';
$destination = 'resize.jpg';
$width = 300;
$height = 500;

$imagine = new Imagine\Gd\Imagine();
$size = new Imagine\Image\Box($width, $height);
$mode = Imagine\Image\ImageInterface::THUMBNAIL_INSET;
$resizeimg = $imagine->open($source)
->thumbnail($size, $mode);
$sizeR = $resizeimg->getSize();
$widthR = $sizeR->getWidth();
$heightR = $sizeR->getHeight();

$preserve = $imagine->create($size);
$startX = $startY = 0;
if ( $widthR < $width ) {
$startX = ( $width - $widthR ) / 2;
}
if ( $heightR < $height ) {
$startY = ( $height - $heightR ) / 2;
}
$preserve->paste($resizeimg, new Imagine\Image\Point($startX, $startY))
->save($destination);