Simplified Image Resizing with PHP

This is an awesome code. I did how ever notice one error with the image size. They were the same size, then I noticed the the echo ws missing. So instead of:
<img src=“myimage.jpg” <?php imageResize($mysock[0], $mysock[1], 150);?> />

Try:

<img src=“gallery/abstract1600_3_084.jpg” <?php echo imageResize($mysock[0], $mysock[1], 150);?> />

Thanks for the code!

Thanks so much for the code!! Uf, I am running out of time with my web-page development and I was lucky to find your code!
I would like to add just one more (not that important thing)
you can write this code:
echo “<img src=\”$url\" height=\“$height\” width=\“$width\” border=0 alt=\“\” />";
like this as well:
echo “<img src=‘$url’ height=‘$height’ width=‘$width’ border=0 alt=‘’ />”;
K

This will increase your overhead when used on lots of images. I was looking for a method of resizing and saving them permenantly.

Thanks a lot. Very Helpful tutorial.

Nice trick, but it is not efficient, because the real goal is to make REAL, saved, thumbnails that will make the website FAST, and if the visitor wants to see the full sized image he will click on the provided link.

Can you put a pHp code with real image RESIZE function???

There is actually a php function that does this exact thing, (create dynamic thumbnails in real-time), although the way it does it is way more efficient. It’s called phpthumbs and it works very well.

Perfect! Just what I need! Tnx!

I have an idea for a website which might be far fetched but I need a solution.

I want to use PHP to take a screenshot of a website I tell it to, add this to a database possibly, and be resized by PHP. I need screenshots of maybe up to 90 websites for a website I want to create for uni, but I certainly do not want to have to take these screenshots myself, and have to obtain these maybe on a weekly basis so I want somethign to do it for me, resize and store…

Any suggestions? email to haddock-s@ulster.ac.uk please

not working

Not very practical, like you say. The idea of a thumbnail is to give the user a quick idea of what something will look like without requiring them to download a large file… The other issue is that having a browser resize an image, even when maintaining aspect ratios, is a poor choice for quality.

Resizing images with the browser is a very bad ideea. you should create a thumbanil when uploading a new photo :slight_smile:

or use phpThumb!

@Alishah and Alex

Um… what are you both talking about? Alishah, you’re describing using something like JavaScript to resize an image which requires a user to download the full size. Alex, you’re talking about the browser resizing the images as well.

Why!? All of the code posted by Greg happens on the server side. So not only will the file size will be smaller than the full image when a user downloads it, but the server takes care of resizing; not the browser.

You guys are idiots and need to learn to read. You also need to learn the difference between server-side and client-side technologies.

Kris… you need to learn a thing or two about how HTML renders images.

The code originally posted by Greg does NOT actually resize the image! Alex and Alishah are right: resizing the image using the browser IS a bad idea.

Look at the output of the code: it creates a normal image tag, but dynamically sets the width and height properties. That’s just great, but the problem is the image itSELF is the same size, it’s just SHOWN smaller! To make this clearer:

Let’s say you have an image that’s ~1MB in size, at, say, 1280x1024. Let’s say you needed it to show up as a thumbnail on your page. Using greg’s method, the filesize is STILL 1MB even if you make the dimensions as well as 13 x 10 pixels!

The whole point of even USING thumbnails is so the user can download a small version of the file before deciding if they want to see the large version. This does not solve that problem. You should really be using imageMagick or another true server-side method to do image resizing.

Or an even BETTER way to do this is just specify just one of the dimension in the <img> tag. That will keep the aspect ratio. No PHP, no Javascript. The thing is though, the image will look screwed up no matter what. You will get pictures that have jagged lines in, people that look like their eyes are falling out and a still have a huge download. There is no way around it really, you need thumbnails and the original picture. Don’t be lazy man!!! This could be accomplished EASILY using a database.

Actually, all the code does is… retrieve & mathematically scale the image dimensions on server side. Then the HTML tag loads the image, client-side. The file size will remain the same. I think the code is excellent, for what it was made to do.
An image can be dynamically scaled to a variety of sizes & displayed many times throughout the website for good formatting. If you guys are worrying about file sizes, then manage that from your upload script. After which, use Greg’s code.

But how could i use this tech for large no of pictures at a time…

Please mail me at p.sivamurugan@gmail.com

I can’t get this script to work… I copied and pasted directly off this page and just changed the image src… I thought it might have something to do with the way the return statement was escapted, but I fooled with that and it still doesn’t work…

I’ve seen a lot of “I can’t get the script to work”, and “But that doesn’t actually resize the image!”, and I got to thinking about what I’ve worked on recently using the GD libraries, and I found this very small random image rotation script that I wrote for my personal site. The code is listed below, and it allows for setting the width, height and quality of the image. I’m sure that you can easily modify it to show a specific image with little effort. ^ ^


<?php
      //rotate.php - Random Image Rotation Script -
      $folder = './my_gallery';
      $fileArray = glob("$folder/*.jpg");
      $w = 150;       // default width.
      $quality = 80;  // default quality.
      $h = -1;        // just sets the variable, in case the query string doesn't include it.
      srand();
      $idx = rand(0,count($fileArray) - 1);
      $fileName = $fileArray[$idx];
      $time = date ("l, F jS, Y g:i:s A");
      foreach ($_GET as $key => $value) {
        $$key = stripslashes($value);
      }
      list($srcWidth, $srcHeight) = getimagesize($fileName);
      $srcImg = ImageCreateFromJPEG($fileName);
      $aspectRatio =  $srcWidth / $srcHeight;
      $h = ($h != -1) ? $h : ($w / $aspectRatio); // Makes sure that the image is properly proportioned
      $dstImg = imagecreatetruecolor($w, $h);
      imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $w, $h, $srcWidth, $srcHeight); // I've noticed that on larger images, resampling is a bit slower than simple resizing, but this produces a MUCH better image.
      header("Content-type: image/jpeg");
      imagejpeg($dstImg,"",$quality);
      imagedestroy($srcImg);
      imagedestroy($dstImg);
?>
/*
  The image is called with the following HTML tag:
  <img src="rotate.php?w=100&q=80">
  where w is the images width, and q is the images JPEG quality.
  
  Note that I left off any width or height quantifiers in the HTML tag. While poor coding practice, it proves the resizing of the image indeed works.
*/

Below is the final product, in triplicate:
75 px width, 100% quality
100 px width, 67% quality
125 px width, 33% quality

Hope this helps. ^ ^

Thanks for sharing Dave but why is the script called ‘rotate’?!!!