General Statistics
390 Tutorials
202 Total Votes
16 User(s) Online
644032 Views Total
3000 Registered Members
24355 Posts Total
Rooneyfeld is our newest member
About this Tutorial: External Thumbnail Creator
External Thumbnail Creator
Rated 4 / 4 Votes

External Thumbnail Creator

Create a thumbnail of any image on the internet. Take advantage of the predefined image functions!


Date Added: December 7, 2005 [ 10931 Views - 4 Votes ] [ Rate ]

Author: Chaos King [ PHP Coding ] [ 8 Comment(s) ]

External Thumbnail Creator
CODE
// Calculate the new height
$theight = $twidth * $height / $width;

// Create new image
$thumb = @imagecreatetruecolor ( $twidth, $theight )
or die ("Can't create Image!");

// Resize the image into a thumb
imagecopyresized($thumb, $pic, 0, 0, 0, 0,
$twidth, $theight, $width, $height);

We will then calculate the new height of the thumbnail using some simple math. Next, we use imagecreatetruecolor() to tell PHP that we are creating a new image with the specified width and height. Then, we make a copy of that image to a new resized thumbail using the data from the image, the height, width and so forth.


CODE
// Change page type to a jpeg
header ("Content-type: image/jpeg");

// Create jpeg image from thumbnail
imagejpeg($thumb,"",75); }
else
{
exit();
}


The finally of the script. We now create a new header telling the page to mimic as if it were a jpeg. We then create the final jpeg image with the resized data, height, width and the final image is produced. We then end the statement saying if the image was not found, we will stop the script.
Full Version
CODE

// Get image info from variable
$url = $_GET['url'];
$img = getimagesize($url) or die();

// Checks if URL is image
if ( $img[2] == 1 )
{
$pic = imagecreatefromgif( $url ) or die();
}
else if ( $img[2] == 2 )
{
$pic = imagecreatefromjpeg( $url ) or die();
}
else if ( $img[2] ==3 )
{
$pic = imagecreatefrompng( $url ) or die();
}
else
{
exit();
}


// If an image is found and we can determine that it is an image
if ( $pic )
{
// Get width and height from the image
$width = imagesx( $pic );
$height = imagesy( $pic );

// Width that thumbnail should be
$twidth = $_GET['width'];

// Valid width?
if ( !is_numeric( $twidth ) || $twidth <= 0 || !intval( $twidth ) || $twidth > $width )
{
exit();
}

// Calculate the new height
$theight = $twidth * $height / $width;

// Create new image
$thumb = @imagecreatetruecolor ( $twidth, $theight )
or die ("Can't create Image!");

// Resize the image into a thumb
imagecopyresized($thumb, $pic, 0, 0, 0, 0,
$twidth, $theight, $width, $height);

// Change page type to a jpeg
header ("Content-type: image/jpeg");

// Create jpeg image from thumbnail
imagejpeg($thumb,"",75);

}
else
{
exit();
}

Now to use the script. Go to thumb.php?width={WIDTH}&url={URL} changing what is nessesary and you will see before your eyes, a thumbail of the image you wanted.

Total Pages (4)
<<Prev 1 2 3 [4] 

Welcome User
Welcome Guest
( Login | Register )

Latest Tutorial Index - RSS Feed
Latest Resources - RSS Feed
Latest Tutorials - RSS Feed
Vote for Us
  • VixxIMG.com - Free Image Hosting, Free Myspace Image Hosting, Free Image Uploads
  • PageRank Plus - Google PageRank checking, with a twist.
Random Affilaites