how create a thumbnail?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

how create a thumbnail?

Post by itsmani1 »

hello ppl?

i want to create thumbnail image. in other words an image resizer.
is there any function in php for it.
i mean i want a function where i will give one of both height or width and i will resize the image for me !


Regards !
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

You need to use the GD library:

http://de2.php.net/manual/en/function.i ... esized.php

has the following example

Code: Select all

<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreate($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>
This could be used as a starting point.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

i am using (PHP-4.1.1)
how can i use GD. is GD lib is available for 4.1.1 or i hav to install it ?
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

Code: Select all

$imgfile = '2.jpg';
Header("Content-type: image/".$_GET&#1111;"type"]);

switch($_GET&#1111;"type"])&#123;
default:
   $function_image_create = "ImageCreateFromJpeg";
   $function_image_new = "ImageJpeg";
break;
case "jpg":
   $function_image_create = "ImageCreateFromJpeg";
   $function_image_new = "ImageJpeg";
case "jpeg":
   $function_image_create = "ImageCreateFromJpeg";
   $function_image_new = "ImageJpeg";
break;
case "png":
   $function_image_create = "ImageCreateFromPng";
   $function_image_new = "ImagePNG";
break;
case "gif":
   $function_image_create = "ImageCreateFromGif";
   $function_image_new = "ImagePNG";
break;
&#125;

list($width, $height) = getimagesize($imgfile);

// the new weight of the thumb

$newheight = 80;

// Proportion is maintained

$newwidth = (int) (($width*80)/$height);

$thumb = ImageCreateTrueColor($newwidth,$newheight);
$source = @function_image_create($imgfile);

ImageCopyResized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

@$function_image_new($thumb);
By using the above code :
Fatal error: Call to undefined function: imagecreatetruecolor() in c:\apache\htdocs\images\resize.php on line 69

wot could be the problem !
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

http://www.php.net/manual/en/ref.image.php

Look at the installation section
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

itsmani, read your private messages.

there are several Code Snippets that involve thumbnail generation.
Post Reply