Tagged with "PHP |"
Feb 23, 2011 - Article, Training    No Comments

PHP for beginners – Part 3

Welcome again…..

comment

Very essential part of any project is comment and documentation .  In order to understand the code for a next person , you must comment your program. Prefer a code done by a famous programmer , you can see well comment programming format

//  comment here Read more »

About Vijeesh Kumar

avatar

Feb 18, 2011 - Article, Technology, Training    3 Comments

PHP for beginners – Part 2

PHP for beginners – Part 2

All php script tags starts with <?php  and ends with ?>

<?php

code………….

?>

no space in between <? and php

for single line print command <?=   ?> can be used

for single line <?  ?> can be used

Any way try to use <?php  ?> Read more »

About Vijeesh Kumar

avatar

Feb 18, 2011 - Article, Technology, Training    No Comments

PHP for beginners

PHP

  • PHP stands for “PHP Hypertext Preprocessor”
  • an HTML-embedded server-side scripting language
  • used to make web pages dynamic
    • process form information
    • authenticate users
    • provide different content depending on context
    • interface with other services: database, e-mail, etc
  • generates HTML and/or client-side scripts sent to client browsers
  • similar syntax to Javascript

Why PHP ?

free and opensource , anyone can run the server , simple syntax (almost similar to C ), lot of built-in functionality Read more »

About Vijeesh Kumar

avatar

Jul 6, 2010 - Article    No Comments

Creating Thumbnail Images

Creating Thumbnail Images

When you allow users to upload images to your site (see “#54: Uploading Images to a Directory” onSection 7.6), you often need a small preview image (called a thumbnail) for inclusion on larger pages and image gallery browsing. This section shows you a function called mkthumb() that creates thumbnails with the help of GD. To use the function, you need the following:

  • A PHP-writable directory for writing the thumbnails (if you don’t know how to make a directory writable, see “File Permissions” on Section 7.1)
  • The GD library

The mkthumb() function takes two parameters: the name of the input image file and the desired name of the thumbnail. You need to independently verify that the image file exists and has a .jpg, .gif, or .png extension. If you haven’t done that already, check out “#54: Uploading Images to a Directory” on Section 7.6. With all that out of the way, start by defining default values for the maximum thumbnail width and height (for this function, these dimensions must be equal):

<?php
function mkthumb($filename, $thumbname) {
    /* Generate an image thumbnail. */
    $thumb_width = 125;
    $thumb_height = $thumb_width;

Now load the image into memory based on the file extension. The GD imagecreatefromformat()functions return an image resource handle. One thing you’ll notice right away is that there is no error checking here, primarily because mkthumb() assumes that you’ve taken care of the prerequisites. If you need error checking, check $src_image afterward to see if it exists.

	if (preg_match('/\.gif$/i', $filename)) {
	    $src_image = imagecreatefromgif($filename);
	} else if (preg_match('/\.png$/i', $filename)) {
	    $src_image = imagecreatefrompng($filename);
	} else {
	    /* Assume JPEG by default. */
	    $src_image = imagecreatefromjpeg($filename);
	}

Now extract the pixel width and height from the image with the imagesx() and imagesy() functions:

	$width = imagesx($src_image);
	$height = imagesy($src_image);

You resize the image only when the original image is larger than the maximum thumbnail dimensions, so check to see if this is the case:

	if (($height > $thumb_height) || ($width > $thumb_width)) {

When resizing an image, you need to know the final image dimensions in pixels. You can’t use the maximum thumbnail height and width, because the original image is probably not square. If you try to shoehorn a non-square rectangle into a square, you will end up with a distorted image. To solve this problem, find the longest side of the original image, and then create a scaling ratio based on the ratio between length of the longest side and the length of the thumbnail side:

	/* Create a thumbnail. */
	if ($width > $height) {
	    $ratio = $thumb_width / $width;
	} else {
	    $ratio = $thumb_height / $height;
	}

Use the ratio to find the exact thumbnail dimensions in pixels, and create a new image resource handle for the resized image:

	$new_width = round($width * $ratio);
	$new_height = round($height * $ratio);
	$dest_image = ImageCreateTrueColor($new_width, $new_height);

Now you can perform the resizing with imagecopyresampled():

	imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0,
	                   $new_width, $new_height, $width, $height);

You can then free the memory for the original image:

	imagedestroy($src_image);

At this point, $dest_image is an image resource containing the resized image, ready for output. But what if we didn’t need to resize? The code falls through to the following else clause, which says that it’s fine to assign $dest_image to the original image handle:

	} else {
	    /* Image is already small enough; just output. */
	    $dest_image = $src_image;
	}

Because we now know that $dest_image is ready for output, we can write it to a file and clean up:

    imagejpeg($dest_image, $thumbname);
    imagedestroy($dest_image);
}

?>

Here’s a very simple example script that uses the mkthumb() function:

<?php
include("mkthumb.inc");
$upfile = $_FILES["upfile"]["tmp_name"];
$fn = $_FILES["upfile"]["name"];
$thumb_filename = "thumbs/$fn";

if ($upfile) {
    mkthumb($upfile, $thumb_filename);
    print "<img src=\"$thumb_filename\" />";
} else { ?>
    <form action="thumb.php" enctype="multipart/form-data" method="post">
    Upload an image:<br />
    <input name="upfile" type="file" /><br />
    <input name="Submit" type="submit" value="Upload Image" />
    <?
}
?>

Obviously, this script makes a lot of assumptions, and you’d never want to use it for general users. Again, use “#54: Uploading Images to a Directory” on Section 7.6 for a full-blown image uploader.

There are several optimizations that you can do for mkthumb() in order to suit your particular needs. For example, JPEG is a lossy format. If you make a thumbnail of a cartoon-style GIF or PNG, the resulting thumbnail will be of poor quality. To fix this problem, you may elect to write in the PNG format with theimagepng() function instead of imagejpeg(). You could also try to get tricky and resize into the image’s original format. However, this may backfire on you, because GIF images have a limited color map, and when you resize an image, the color map tends to change.

Overall, though, beware of trying to be too tricky with PHP image manipulation. PHP is typically used on demand, so you have little time for fancy operations. In addition to the user getting impatient, the webserver won’t let a script run for very long.

About Vijeesh Kumar

avatar

Powered By Indic IME

Switch to our mobile site