3

I want to resize an image twice using Intervention.

I have this currently:

$img = Image::make($image_url);

$img_path = public_path() . '/images/';

$img->fit(500, 250);
$img->save($img_path . '/img_250.jpg');

$img = Image::make($image_url);

$img->fit(100, 100);
$img->save($img_path . '/img_100.jpg');

As you can see, I first want to resize the original image to 500x250, and then I want to again resize the original image (not the 500x250 image) to 100x100.

Is there a way to do this without calling Image::make() twice?

  • actually, why do you call it second time? it should work already. – naneri Jun 05 '16 at 17:51
  • @naneri They're in two different dimensions. If OP uses the first to create the other from, the second may lose image information. – Joel Hinz Jun 05 '16 at 18:04
  • @naneri Because otherwise it would resize the 500x250 image down to 100x100. I want to resize the ORIGINAL image down to 500x250 and then the ORIGINAL image again down to 100x100. –  Jun 05 '16 at 18:41
  • before resizing just after $img = image::make do this `$img2 = $img ;` then start resising $img and $img2 will be the same original image then start manipulation – Achraf Khouadja Jun 05 '16 at 19:25
  • I also use Intervention in a resizing script that requires multiple resizes per image. In my case, for each set of dimensions I do a clone of the $img object. I believe I also initially tried to reuse the original object, but settled on cloning instead. If you are concerned about efficiency, if you loop through your dimension sets, you at least limit the routine to only have one clone at a time, you skip repeating Intervention's constructor on the Image object. – mattcrowe Jun 05 '16 at 19:35
  • Maybe a combo with Intravention Image and Intravention Image cache? – Maky Jun 06 '16 at 09:19

2 Answers2

12

Here's the answer:

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

// create an image
$img = Image::make('public/foo.jpg');

// backup status
$img->backup();

// perform some modifications
$img->resize(320, 240);
$img->invert();
$img->save('public/small.jpg');

// reset image (return to backup state)
$img->reset();

// perform other modifications
$img->resize(640, 480);
$img->invert();
$img->save('public/large.jpg');
0

I'm posting this to help others who might come across a similar issue. While we can implement @user6421733's answer... There's a better way of handling different sizes of images.

Consider using Intervention's imagecache optional package. You could implement it simply too. http://image.intervention.io/use/url

It can allow you to use urls such as this http://yourhost.com/{route-name}/original/{file-name} and with little or less effort:

Josh D.
  • 303
  • 4
  • 10