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
- Rakesh Sharma
-
Web Designer | Blogger | Web Addict
Follow on Google+
Recommend on Google +1
RSS Feed
Subscribe Recent Posts
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....
-
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...
-
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...
-
Cooliris is simply the fastest and most stunning way to browse photos and videos from the Web or your desktop for free. Effortlessly scroll...
-
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...
-
jQM (jQuery Mobile) becoming very popular now a days. Mobile developers from across the world contributing their knowledge to make it more u...
-
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...
-
In this post I'm sharing a list of XML based CMS (Content Management System) to help web designers and developers. CMS usually implement...
-
Today I would like to share that how to add a Facebook fan list to your blog or website (like I have added here at right sidebar). This is t...
-
Today I am really happy to anounce that I have launched new social book marking site called ' TutLinks '. This is a first networkin...
1 comments :
By using PHP it is too easy to resize image.
Post a Comment