Writing your our own php script to resize the image dynamically will control both the image size and the file size. Check out the image to the left - which was resized with a php script.
This is how do we do this. You going to make use of the PHP GD library to manipulate the image, and a $_GET parameter to tell PHP which image to re-size. First, you need to open up your main screenshot file and get it’s size and type. $src_img = imagecreatefrompng('image.png');
$srcsize = getimagesize('image.png');imagecreatfrompng() creates an image object and stores it in $src_image. If the file is a different type you can replace “png” with “jpeg” or “gif.” getimagesize() stores an array in $srcsize that contains the width of the image ($srcsize[0]), the height of the image ($srcsize[1]), and the filetype of the image ($srcsize[2]). Now you need to create new dimensions for the new image and actually create an image. Let's resize the screenshot to 200 pixels wide - and then adjust the height to maintain the ratio. $dest_x = 200;
$dest_y = (200 / $srcsize[0]) * $srcsize[1];
$dst_img = imagecreatetruecolor($dest_x, $dest_y);imagecopyresampled() copies an image ($dst_img) into a new image ($src_img) with a set of given attributes (including our sizes). The “Header” line tells the browser that this is an image - so that it’s not displayed as a bunch of gibberish. Finally, the imagepng() function actually displays the image. Now, you need to clean up and get rid of the image objects. Otherwise, you could end up eatting a lot of memory from the server. imagedestroy($src_img);
imagedestroy($dst_img);
If you load the script directly, you should simply see the image. PHP creates an image resource and displays it in the browser - as if “resize-image.php” was really “image.png.” Therefore you need to include this new image in an HTML page is use the php script as the src attribute of our <img> tag. Like this. <img src='resize-image.php' />
The script will execute, create an image, and use that as the src. The last thing you need to do is modify your script so that we can pass it some information. It wouldn’t any good if you had to write a new php script for every image you wanted to resize. Instead, we can use the $_GET array to ---- a variable (the image filename) to our image resizing script. Here’s the entire image resizing script, with the $_GET variable used for the filename.
// Create source image and dimensions$src_img = imagecreatefrompng($_GET['image']);
$srcsize = getimagesize($_GET['image']);
$dest_x = 200;
$dest_y = (200 / $srcsize[0]) * $srcsize[1];
$dst_img = imagecreatetruecolor($dest_x, $dest_y);
// Resize imageimagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $srcsize[0], $srcsize[1]);
// Output imageheader("content-type: image/png");
imagepng($dst_img);
// Deletes imagesimagedestroy($src_img);
imagedestroy($dst_img);
To use this, you need to slightly modify your HTML and add an “image” parameter to the URL of your script. Like so… <img src='resize-image.php?image=image.png' />
Now you can resize images on the fly the way you want, and when you want.
source:code-sucks.com
Friday, February 27, 2009
PHP Tutorial: How to resize an image by using PHP
Subscribe to:
Post Comments (Atom)
About Me
Popular Posts
-
If you are looking 'how to put a any form in a blogger' then this post is going to help you. Putting a form in blogger is very easy. All you...
-
When you write a CSS for your project you never know what kind of bug or issue you will face at time of browser compatibility. Internet Ex...
-
Now a days every one intend to aware about HTML5 more and more. According to experts HTML5 is a future of the web. There are some interestin...
-
In web development scripts like jQuery and Ajax becomes very handy for web developers. If you are a learner or a expert these scripts is rea...
-
Using framework in a project is really a challenging task. Now a days there are numbers of open source framework available across the web an...
-
Now a days every designer and developers search across the web for free icons. A good icons plays a vital role in web designing because it i...
-
In this post I'm sharing a list of XML based CMS (Content Management System) to help web designers and developers. CMS usually implemented a...
-
Recently I searched web for interesting and useful iPhone apps that will help iPhone users to have good exercise with these powerful apps. I...
-
Hello my dear readers. I am writing this post after a very long time as I was very busy in my projects so I didn't get time to write. This ...
-
If you are looking for free e-books for Web Design and Development then this post is for you. Here I have shared some very useful e-books wi...
Community News
- 30 Appealing iOS Icon Designs
- Feel Alluring Using Mac Using OS X Mountain Lion Wallpaper
- Make your Android More Functional Via Useful Apps
- How to write a Design Brief?
- Brochure Design: 50+ Brilliant Layouts
- Learn How-to Rise Productivity By Mac - 30 Handy Apps
- 15 Beutiful Wordpress Ecommerce Themes For You To Setup Your Wordpress Shopping Cart
- Now Fireworks isn’t Complicated For You [Fireworks Tutorial]
- 35 Awesome and Attractive Wordpress Widgets For Bloggers
- 25 Must Have iOS Apps For Tweeting





1 comments:
By using PHP it is too easy to resize image.
Post a Comment