Simplified Image Resizing with PHP

Mostly because I’m not very good at keeping to proper naming conventions. I came VERY close to naming it “Fred”. :smiley:

For all of you looking for a way to transform images on the fly, you might want to check the open-source project Asido: there are a lot of useful features ([URL=“http://www.asido.info/about/features/”]http://www.asido.info/about/features/), including [URL=“http://www.asido.info/about/features/#resize”]various types of image resize - like fit resize (resize if bigger) and frame resize (force-fit), [URL=“http://www.asido.info/about/features/#watermark”]watermarking, [URL=“http://www.asido.info/about/features/#copy”]image copy, [URL=“http://www.asido.info/about/features/#crop”]cropping, etc. It can use various platforms: not only GD but ImageMagick too (either via shell or via any of the php extensions like php_magickwand.dll or php_imagick.dll). Here’s the list of drivers: [URL=“http://www.asido.info/about/drivers/”]http://www.asido.info/about/drivers/

that doesn’t resize the image, that just shows it resized. if the image is heavy, the browser will download the whole huge image and then only display some of its pixels.

to resize an image with php, use the gd library.

(btw, i came here from a search for gd tutorials :slight_smile: )

Come on! Just set the height or the width (not both) and let the browser handle the resize. No need to do this - simplified to :

<img src=“images/sock001.jpg” width=“150” >
or
<img src=“images/sock001.jpg” height=“150” >

But is fine for small images but we should really resize and store the new image in a cache (file or memory)

How are you are resizing image using GD?

hi ansys and welcome to the forums :wave:

If you look at Dave’s pst #57 he gives a script that uses GD to resize the images, it uses the following functions:

ImageCreateFromJPEG
imagecreatetruecolor
[ imagecopyresampled](www.php.net/ imagecopyresampled)

Read through the manual pages about these functions and look at the user added notes. It should give you the required information.

This is interesting, seems a lot easier than .NET. However, if the image is being resized on the server prior to streaming to the client browser, aren’t you downloading a smaller version of it?

Wasteful on the people downloading, and wasteful on your bandwidth. Plus, resizing on fly is wasteful on your server resources… all of these factors makes a site unnecessarily slow. 3,000 5k thumbnails are only 14mb, (5*3000/1024) and a shop with 3,000 products should be using the kind of web space with enough storage to handle that. Therefore, the most efficient and ‘correct’ solution is to store a smaller thumbnail image. Functions such as those already suggested for automatically creating a thumb on upload are the way to go. A 5k storage overhead on each image is only a problem is you have VERY cheap web space.

If your looking for an easier way to handle image resizing, I wrote a small library a while back for my pojects: http://code.google.com/p/bair/

This function might be sufficient for some, it doesn’t seem flexible enough. I would like the function to be transparent, to be agnostic to upscales and downscales, to keep the proportions or ignore them (depending on a flag), and to scale by separate width/height constraints without any headaches. Recently I wrote a function that seems to cover all these fronts in the intuitive manner. If you’re looking for something like that - you can find it here:
http://www.mediumexposure.com/techblog/smart-image-resizing-while-preserving-transparency-php-and-gd-library

You need an “echo” in there to print the values to the screen:
<img src=“images/sock001.jpg” <?php echo(imageResize($mysock[0], $mysock[1], 150)); ?>>

For a simple temporary resize it workd fine. Good Job & Thanks

Awesome thanks!

I don’t get it.
What about using just: img src=“file.jpg” width=20% (20% of the area)

or img src=“file.jpg” width=150 (150 pixels wide)

You are still downloading the FULL image of the socks, this only displays them smaller and is pretty worthless. As akyles posted, could have been done much easier, same effect.

I’d just like to say, this script came in very very handy for me and helped me resolve an issue without having to use the GD library. Great work!

Im sorry to say that this is worthless, you can archive the exact same result by setting up the either the width or the height on the style of the image.i.e if you have an 200 width x 100 height image and you do:
<img src=“whatever.jpg” style=“width: 100px”> the height will be set up to 50px automatically so it will affect its aspect ratio. And i think its way much better to have thumbnails than doing this, because if you have big images it will take for ever to load the page, i thought this was a tutorial for actually resizing images with PHP as it states on the title.

The lameness of this script amazes me.

  1. this can easily be replaced with simple html, but there is a problem with both scripts:

  2. simple resizing will greatly reduce load time, driving customers away from your site. Thumbnails do a much better job of improving load time, even though they are a hassle.

How did this show up on the first page of my results? I want those 5 minutes of my life back.