Welcome Guest ( Log In | Register )


Why register for free on WebRadiance?
Welcome to WebRadiance forums! WebRadiance is a place for discussion and help on a large range of web design and development related topics such as HTML, CSS, ASP & .NET, PHP and SEO. It is provided for free, without ads and is a friendly place for beginners and professionals alike. By joining us today you can take part in our rapidly growing community.

Registering enables you to:
  • have your say by posting new messages and taking part in topics and polls
  • keep up to date with the latest news and trends in the web design and development world
  • send private messages to other members
  • have your questions answered by our knowledgeable community
  • add a link to your website in our links area
  • receive emails when someone replies to your posts

Resize Image Function

A simple function that resizes an image

 
Reply to this topicStart new topic
> Resize Image Function, A simple function that resizes an image
locateSTYLE
post Dec 1 2008, 04:26 PM
Post #1


W.R. Private
Group Icon

Group: Members
Posts: 18
Joined: 26-September 08
From: Dallas, TX
Member No.: 1,191



Here's a simple little function I've created that resizes images using PHP's GD Image Library. I made it as simple and easy to use as I could think of. I'd love to hear of any improvements or suggestions. I use this function quite a lot to create thumbnails on the fly without actually saving a new image. Here's the function:

CODE
<?
function image($id, $quality, $width, $height, $directory) {
if(file_exists($directory."/".$id.".jpg")) { $ext = ".jpg"; }
elseif(file_exists($directory."/".$id.".gif")) { $ext = ".gif"; }
elseif(file_exists($directory."/".$id.".png")) { $ext = ".png"; }
elseif(file_exists($directory."/".$id.".bmp")) { $ext = ".bmp"; }
$file = $directory."/".$id.$ext;
if(file_exists($file)) {
header('Content-type: image/jpeg');
list($imgWidth, $imgHeight) = getimagesize($file);
$modHeight = $width * $imgHeight;
$modHeight = $modHeight / $imgWidth;
$modWidth = $width;
if($modHeight < $height) {
$modWidth = $height * $imgWidth;
$modWidth = $modWidth / $imgHeight;
$modHeight = $height;
}
$image = imagecreatetruecolor($modWidth, $modHeight);
$source = imagecreatefromjpeg($file);
imagecopyresized($image, $source, 0, 0, 0, 0, $modWidth, $modHeight, $imgWidth, $imgHeight);
imagejpeg($image, NULL, $quality);
} else {
die("Image not found.");
}
}
image(strip_tags(addslashes(trim($_GET['id']))), strip_tags(addslashes(trim($_GET['q']))), strip_tags(addslashes(trim($_GET['w']))), strip_tags(addslashes(trim($_GET['h']))), strip_tags(addslashes(trim($_GET['d']))));
?>


Save the following code as "image.php." To use it simply call the HTML img tag to the fucntion.
CODE
<img src="image.php?id=1&q=100&w=150&h=150&d=images" alt="" />


Here's what the variables encoded in the URL mean:
q - Quality (1-100)
w - Width (px)
h - Height (px)
d - Directory (location of where the image is located)
id - ID (The filename without the extension )


--------------------
locateSTYLE | Design Community
Free Website Templates, Stock Images and affordable hosting.

DesignsByBAM | Innovative Kitchen Design
Young College Singles | Find your perfect match for FREE!
toughSEARCH | 100% Free Online Personals

Free Online Dating
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
Tags
No Tag inserted yet

1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

RSS Lo-Fi Version Time is now: 8th January 2009 - 07:03 AM
Contact Us
Web Design Forum | Web Development Forum | Web Help | WordPress Help