Solved

Image Resize to fit custom width and height.

Posted on 2008-06-16
15
528 Views
Last Modified: 2011-10-19
Hi

Can anyone recommend some code for resizing uploaded images to fit a custom width and height whilst keeping the correct ratio.

I have the image upload working fine but need code which takes the existing with and height and then resizes the image to fit a 186x123 thumb ( i can make it a square if need be)

I need the image aligned to the top.

I then need to pad out the image with whitespace on the left and right if the width is below 186.

I need to do the same with the height but if possible crop the whitespace from the bottom, if it goes below the defined height??

Has anyone got any suggestions!

Many thanks

--s--
0
Comment
Question by:socross
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
15 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 21792705
0
 
LVL 1

Author Comment

by:socross
ID: 21792881
Ok Great Looks like it does everything i need

Could you advise how i could work it into my existing example, its a bit of mess at the moment as im reusing old code, i just need to get it working asap!

See attached file, it has the functions i use to upload the image and then two functions for creating a thumb and a main image, its the thumb i need to get working.

Many many thanks

--s--
core-fnc-image-upload.php.txt
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21792898
The script I showed actually does not create a thumb on upload. It allows the creation of thumbs on the fly from your html.

You just create an <img> tag with <img src="thumbnailer.php?i=myimage.jpg&x=168&y=168" /> and have the script create your thumbnail.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:socross
ID: 21793079
What impact does this have on server load, what happens if the image is quite large.

Does the image catch have something to do with this!

--s--
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21793108
The size of the image should be filtered in the upload phase. As to realtime thumbnailing performance; that should be fine as a cache is build for the thumbnails that are created.
0
 
LVL 1

Author Comment

by:socross
ID: 21793128
Excellent, looks ideal!

Will get it implemented this afternoon and get back to you.

Many thanks

--s--
0
 
LVL 1

Author Comment

by:socross
ID: 21798198
Have implemented the script and it works great!!

With regards the catching, can you confirm where the folder needs to be placed and confim what you mean by '... to which the thumbnailer has write access." Does this just mean chmod 777.

I have placed the script in a directory called classes which is in the rot directory.

Many thanks

--s--

0
 
LVL 2

Expert Comment

by:ayus80
ID: 21800328
study GDImage script for php
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21800834
Doesn't need to be 777, can be 666 I think or even 664 if the php is running as apache user and the directory is owned by the apache user.
0
 
LVL 1

Author Comment

by:socross
ID: 21800853
ok great but where does the folder need to be in relation to the thumnailer script??

--s--
0
 
LVL 1

Author Comment

by:socross
ID: 21831758
roonaan

great script works a treat, just need to get the cache working, where do i need to store the cache folder??

Regards

--s--
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21832251
You can create an image_cache folder, or edit the script to change the path to something you prefer.
0
 
LVL 1

Author Comment

by:socross
ID: 21832517
Ok

Im still not 100% where i create the cache folder in relation to the thumbnailer script.

-s-
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21832550
In same folder:

folder/thumbnailer.php
folder/imagecache/
0
 
LVL 1

Author Comment

by:socross
ID: 21923199
Great Script

--s--
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
assigning javascript variable to php variable 8 70
how can I see the time difference in only seconds? 4 40
Position image fpdf 4 19
PHP shopping Cart 7 8
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question