create thumbnails
Posted: Wed Jul 13, 2005 7:41 am
ok i have users upload a image but now i need for the script to make a perdy small black and white thumbnail of that image. i have no idea how to do this. any help would be splendid
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
function createFeatured($image_path, $file, $width, $height)
{
copy($file, $image_path);
$src_img = imagecreatefromJPEG($image_path);
$new_h = $height;
$new_w = $width;
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
imagejpeg($dst_img, $image_path);
}Code: Select all
<?php
ob_start();
/*image resizing code
*returns: jpeg image that is destroyed as soon as it's outputted
*paramaeters that must be passed: $_GET['max_width'], $max_hieght, $file
*$_GET['max_width']/$_GET['max_height'] are the maximum size of the picture in pizels
*if not set, they default to 100 */
if (@!$_GET['max_width'])
$_GET['max_width'] = 100;
if (@!$_GET['max_height'])
$_GET['max_height'] = 100;
$directory = "pics"; //direcotry where $file is stored
$file = $directory."/".$_GET['file'];
$size = GetImageSize($file);
$width = $size[0];
$height = $size[1];
$x_ratio = $_GET['max_width'] / $width;
$y_ratio = $_GET['max_height'] / $height;
if ( ($width <= $_GET['max_width']) && ($height <= $_GET['max_height']) ) {
$tn_width = $width;
$tn_height = $height;
}
else if (($x_ratio * $height) < $_GET['max_height']) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $_GET['max_width'];
}
else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $_GET['max_height'];
}
$src = ImageCreateFromJpeg($file);
$dst = ImageCreateTrueColor($tn_width,$tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0,
$tn_width,$tn_height,$width,$height);
header("Content-type: image/jpeg");
ImageJpeg ($dst, '', 75);
ImageDestroy($src);
ImageDestroy($dst);
//end image resizing code
ob_end_flush();
?>Code: Select all
function createFeatured($image_path, $file, $width, $height)
{
copy($file, $image_path);
$src_img = imagecreatefromJPEG($image_path);
$new_h = $height;
$new_w = $width;
$dst_img = imagecreatetruecolor($new_w,$new_h);
$gray_img = imagecreatetruecolor($new_w,$new_h);//new line
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
imagecopymergegray($gray_img,$dst_img,0,0,0,0,$new_w,$new_h,$new_w,$new_h);//new line
imagejpeg($gray_img, $image_path); //modified
}Code: Select all
function createFeatured($file)
{
$source = imagecreatefromjpeg($file);
$image = imagecreate(imagesx($source),imagesy($source));
for ($i=0;$i<256;$i++)
{
$palette[$i] = imagecolorallocate($image,$i,$i,$i);
}
for ($y=1;$y<imagesy($source);$y++)
{
for ($x=1;$x<imagesx($source);$x++)
{
$rgb = imagecolorat($source,$x-1,$y-1);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> & 0xFF;
$b = $rgb & 0xFF; //Convert to greyscale
$g = greyscale($r,$g,$b);
imagesetpixel($image,$x,$y,$palette[$g]);
}
}
$colors = (($r*0.299)+($g*0.587)+($b*0.114));
}Code: Select all
function createFeatured($file,$saveas,$quality)
{
$source = imagecreatefromjpeg($file);
$image = imagecreate(imagesx($source),imagesy($source));
for ($i=0;$i<256;$i++)
{
$palette[$i] = imagecolorallocate($image,$i,$i,$i);
}
for ($y=1;$y<=imagesy($source);$y++)
{
for ($x=1;$x<=imagesx($source);$x++)
{
$rgb = imagecolorat($source,$x-1,$y-1);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> & 0xFF;
$b = $rgb & 0xFF; //Convert to greyscale
$g = greyscale($r,$g,$b);
imagesetpixel($image,$x-1,$y-1,$palette[$g]);
}
}
//Save the file
imagejpeg($image,$saveas,$quality);
//Output to the browser
header("Content-type: image/jpeg");
imagejpeg($image,"",$quality);
}
function greyscale($r,$g,$b) {
return round(($r*0.299)+($g*0.587)+($b*0.114));
}
createFeatured("xxx.jpg","xxx_bw.jpg",80);