How to resize a image using imagine .

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

How to resize a image using imagine .

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to resize a image using imagine .

Post by Celauran »

Looks like you're getting a 404 there, rather than a problem with Imagine itself. Can you confirm via HTTP response codes?
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: How to resize a image using imagine .

Post by gautamz07 »

Can you confirm via HTTP response codes?
how do i do that ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to resize a image using imagine .

Post by Celauran »

Through your browser's developer tools is probably easiest.
Attachments
Screen Shot 2015-10-07 at 10.50.47 AM.png
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: How to resize a image using imagine .

Post by gautamz07 »

Thanks for the handy trick :D will try it
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: How to resize a image using imagine .

Post by gautamz07 »

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to resize a image using imagine .

Post by Celauran »

So your POST route is failing. I'd check the form action first and foremost.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: How to resize a image using imagine .

Post 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'));
Swhite
Forum Newbie
Posts: 7
Joined: Thu Feb 04, 2016 1:19 am

Re: How to resize a image using imagine .

Post 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);
Post Reply