Create a thumbnail of any image on the internet. Take advantage of the predefined image functions!
Date Added: December 7, 2005 [ 14575 Views - 4 Votes ] [ Rate ]
Author: Chaos King [ PHP Coding ] [ 8 Comment(s) ]
// Create jpeg image from thumbnail
imagejpeg($thumb,"",75); }
else
{
exit();
}
// 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();
}