0

For the website I am building I need to resize some images to make web page loading lighter. I use this code:

list($width, $height, $type, $attr) = getimagesize($img_path);
    $thumb = imagecreatetruecolor(50, 50 * $height/$width);
    if($type == 2) $source = imagecreatefromjpeg($img_path);
    if($type == 3) $source = imagecreatefrompng($img_path);
    imagecopyresized($thumb, $source, 0, 0, 0, 0, 50, 50 * $height/$width, $width, $height);

Now I could save them on the sever to keep them ready whenever necessary but considering the little space I have available I would rather avoid.

So my question is: is there a way to create a "temporary" image from the code I already have and use it inside an <img> tag?

For example <img src="$img_resized"/>.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Take a look at [TimThumb](https://www.binarymoon.co.uk/projects/timthumb/) – gaganshera May 06 '17 at 14:37
  • refer to this http://stackoverflow.com/questions/3971841/how-to-resize-images-proportionally-keeping-the-aspect-ratio – LF00 May 06 '17 at 14:44

1 Answers1

0

That's called Resizing Images on the Fly

I suggest you use one of these libraries for this:

meenie/munee

or

mos/cimage

Install the library using composer:

composer require meenie/munee or composer require mos/cimage

Then you can link your photos by using one of these:

Meenie: <img src="/path/to/image.jpg?resize=width[100]">

cImage: <img src="/host/img.php?src=test.png&width=100">
Agu Dondo
  • 12,638
  • 7
  • 57
  • 68