0

i have a php image upload upload form and what i want to do is before the image is saved on my server, it needs to be resized to make sure the height doesnt exceed the max height ive set, and the width doesnt exceed the max width i set. I was thinking the way to do it would first be a) check if the width is greater than max width. If so, PROPORTIONALLY resize image so the width equals the max width. Then, check the height. If the height exceeds max height, proportionally resize the image so the height equals the max height.

Any suggestions? GD is installed on my server..

Assume that the image is echo $_FILES["file"]["name"];

Jonah Katz
  • 5,230
  • 16
  • 67
  • 90
  • these are all your options: they all require some installation + configuration i believe: http://www.php.net/manual/en/refs.utilspec.image.php – Francis Yaconiello Jul 09 '12 at 17:48
  • If you want lightweight, don't resize the image twice, just do some math and only resize once. – jeroen Jul 09 '12 at 17:50

2 Answers2

2

Using gd is as lightweight as it's going to get. The idea that you're going to resize images without using a library is basically just humorous.

chaos
  • 122,029
  • 33
  • 303
  • 309
  • The problem with gd is the installation process (at the ones i have read) requires access to the server and the ability to install libraries, which i cannot. Can i just simply include a php file in my site using a `require 'gs_whatever.php`? – Jonah Katz Jul 09 '12 at 17:43
  • I have cpanel. How can i check? – Jonah Katz Jul 09 '12 at 17:45
  • @JonahKatz: Have you verified that your web host doesn't have `gd` installed? It's pretty standard, I'd expect any professional web host's PHP install to have it. I doubt you're going to find an implemented-in-PHP PHP image manipulation library like you describe because nobody ever needs one because everybody has `gd`. – chaos Jul 09 '12 at 17:45
  • 1
    @JonahKatz: Why don't you just write a PHP script that calls a `gd` function and see if it runs? Or, if it isn't locked down, you can make a script that runs `` and look through the output of that to see what modules are there. – chaos Jul 09 '12 at 17:46
  • Ok youre right, the server does have GD installed. So now i need to figure out how to use gd to do what i want.. – Jonah Katz Jul 09 '12 at 17:53
  • @JonahKatz: maxhud's deleted answer was a fine start. (It was based on `gd` functions.) – chaos Jul 09 '12 at 17:54
0

I've answered a similar question to what you are asking:

Resize images with PHP

It uses GD b/c like chaos says, it doesn't get any more lightweight.

This script resizes an image on demand, but caches each size of each image that has ever been called so that it doesn't have to be resized again.

Community
  • 1
  • 1
Francis Yaconiello
  • 10,829
  • 2
  • 35
  • 54
  • Hmm its close but not exactly what i want. I dont want to pass the size, because i do not know the size. Every uploaded image is different size, it should be scaled to fit the max height/width – Jonah Katz Jul 09 '12 at 17:52
  • you pass in the maximum w/h you want to use, and it scales the source image down to that requirement – Francis Yaconiello Jul 09 '12 at 17:53
  • it will create a "thumbnailed" version of the original with the following naming convention "ORIGINALFILENAME_WIDTHxHEIGHT.jpg" essentially, it would make a duplicate of the file if it was already a perfect fit. – Francis Yaconiello Jul 09 '12 at 17:56
  • also note, it keeps the original file for future resizes (should you change your design and need to regenerate your thumbnails) – Francis Yaconiello Jul 09 '12 at 17:57