Skip to main content

Creating image thumbnails with PHP!

If we intend to make an image gallery, and we would like to show the thumbails of the images, it wouldn't be a wise idea to show the original images with their height & width modified in the tag, that would result in low image quality & higher download time. The solution to this problem would be creating thumbnails at the server end and display them instead.
This solution can be approached in two ways, you can create the thumbnails once and for all or create thumbnails everytime at runtime.
For this purpose, I wrote a class which will create the thumbnail.

The class:
PHP Code:
/*
* File : thumb.class.php
* Description : Class for creating thumbnails
*/

class Thumb
{
var $filename;
var $img;
var $height;
var $width;
var $ratio = 10;
var $oldim;
var $oldh;
var $oldw;


function filename($file) /* used for setting filename */
{
$this->filename = $file;
}

function setRatio($ratio) /* used for setting ratio */
{
$this->ratio = $ratio;
}

function init() /* creates the GD stream */
{
$this->img = imagecreatetruecolor($this->width,$this->height);
}

function calculateSize() /* to calculate the output size */
{
//$this->info = getimagesize($this->filename); /* returns height,width,type,attribute */
$this->oldim = imagecreatefromjpeg($this->filename);
$this->oldw = imagesx($this->oldim);
$this->oldh = imagesy($this->oldim);

/* code to re-calculate size maintaining the aspect ratio */
$this->width = ($this->oldw/100)*$this->ratio;
$this->height = ($this->oldh/100)*$this->ratio;

$this->init();
return true;
}

function finish()
{
$this->calculateSize();
imagecopyresampled($this->img,$this->oldim,0,0,0,0,$this->width,$this->height,$this->oldw,$this->oldh);
header("Content-Type: image/jpeg");
imagejpeg($this->img);
}
}

Comments

Popular posts from this blog

Learn phpfox

PHPFox  is a social network script, it is an internet application and when you install it, it is a website. The  phpfox  script comes in 3 packages, each with a different set of modules. it has two products: 1. Nebula (upto phpfox 3.8) 2. Neutron (Newer) You can check the demo on :  http://www.phpfox.com =================================================== To clear cache in phpfox follow the following steps, admincp >> Tools >> Maintenance >> Cache Manager >> Click on Clear All button =================================================== To work facebook app on local Following settings need to done in facebook app   1) go => setting => Advance 2) see "OAuth Settings" area and set "Valid OAuth redirect URIs" =  http:// projectdomain /index.php?do=/user/login/, http:// projectdomain .com/index.php?do=/user/register/, http:// projectdomain .com, http:// projectdomain .com/index.php 3) en...

How to Make Your Own PHP Captcha Generator

In this article we will create file based simple yet successful captcha generator. 3 Major Anti-spamming techniques used? Mathematical Operation like Random number + Random Number = -> The user must specify the answer Random word -> User must type the word Random question -> Obvious one which the user should answer correctly [ex: Are you human?] How Captcha works? The captcha generator generates an IMAGE with the question and then put up a session variable storing the value. User input though an input box. Using php POST, we compare the session variable data with the user input and tell whether its a bot or human. Its coding time The Code First let's write the php script which generates the captcha image. We use the simple header-content change technique, from which we can easily bring up an image from a given text. captcha.php PHP Code: array("Num" => "Num"), 1 => array("Are y...

Joomla Flash Tutorial

Joomla Flash Tutorial Adding a custom-made Flash animation created with Anim-FX to Frontpage is a snap By following these simple steps, you will be up and running in no time at all and impressing all of your visitors with your talents! First create your Flash animations in Anim-FX, and make sure the *.swf and *.txt files are in the base html directory.  Do not make the mistake of uploading them into the images or images/flash directory.  Also the flash does not work if loaded into the template directory.  Both files must be in the html directory where the index.php file is located. Then you can either copy the HTML generated by Anim-FX into the index.php file to the appropriate location or use a Joomla Flash Module.  Edit the index.php file which drives your Joomla template. Method 1: Editing the index.php for your Joomla template: Copy the follow...