Image Resize Component for CakePHP

Hi my friends. Every CakePHP developer googling several times to find an Image resize component for their development . Here I tried to give them a solution . I used it in several times in several apps , though the main logic I found on google after that I made it for CakePHP. Please check the attached zip to include and source code.

And one most important things is that image resize should be maintained aspect ratio. In the source code you will find that a component called “Image” which I import in a function of a Controller. After that initialize the component like

$MyImageCom = new ImageComponent();

Now prepare the image for re-sizing , assume that your image already store in “img” , member folder under “webroot” and the name store in $imagename , like $imagename=”abc.jpg” . Now you can resize it by calling the function “resize” with width , height even you can set the background color from here by given RGB code. Now tell the component to save to the particular path by calling “save” function with the full path of the new image name. With the above you can resize a single picture with multiple size. That’s it buddy.

 

Here is the source code:

App::import('Component', 'Image');
$MyImageCom = new ImageComponent();

$MyImageCom->prepare("img/members/".$imagename);
$MyImageCom->resize(320,200);//width,height,Red,Green,Blue
$MyImageCom->save("img/members/".$Largeimage[0].'_L.'.$Largeimage[1]);

$MyImageCom->prepare("img/members/".$imagename);
$MyImageCom->resize(92,92);//width,height,Red,Green,Blue
$MyImageCom->save("img/members/".$Largeimage[0].'_M.'.$Largeimage[1]);

$MyImageCom->prepare("img/members/".$imagename);
$MyImageCom->resize(103,103);//width,height,Red,Green,Blue
$MyImageCom->save("img/members/".$Largeimage[0].'_S.'.$Largeimage[1]);

Here I attached an example source code for your reference. Please feel free to contact me if you found anything wrong or need any help. I always try to be responsive as soon as possible.

Download Source code:Here

 

10 thoughts on “Image Resize Component for CakePHP

  • September 19, 2013 at 5:11 am
    Permalink

    thank you very much

    Reply
  • December 17, 2013 at 5:01 pm
    Permalink

    Is before save, the image name should change , or we can keep the old image name for saving image?

    Reply
  • February 7, 2014 at 6:49 pm
    Permalink

    Thanks.You Are Saviour..

    Reply
    • February 10, 2014 at 8:51 am
      Permalink

      Thanks , we will try to update it regularly.

      Reply
    • September 19, 2014 at 9:02 am
      Permalink

      Hi Will,
      It’s great , thanks for sharing your tutorial . Keep it up bro.
      Thanks,
      Bivash
      Blog For Developer Team Member

      Reply
  • November 30, 2014 at 2:27 am
    Permalink

    Hi there, i study your blog occasionally andd i personal a similar
    one and i was just curious if you get lots of spam comments?
    If so how do you reduce it, any plugin or anything you may suggest?
    I get so much lately it’s driving me insane so any help is very
    much appreciated.

    Reply
  • March 3, 2015 at 10:51 am
    Permalink

    Thanks very much

    i would like to suggest you add a demo link of it.It will helpfull for every one who want to use it.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *