Page 1 of 1

how create a thumbnail?

Posted: Thu Mar 03, 2005 4:16 am
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 !

Posted: Thu Mar 03, 2005 4:29 am
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.

Posted: Thu Mar 03, 2005 5:36 am
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 ?

Posted: Thu Mar 03, 2005 5:48 am
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 !

Posted: Thu Mar 03, 2005 5:59 am
by CoderGoblin
http://www.php.net/manual/en/ref.image.php

Look at the installation section

Posted: Thu Mar 03, 2005 7:59 am
by feyd
itsmani, read your private messages.

there are several Code Snippets that involve thumbnail generation.