Page 1 of 2

create thumbnails

Posted: Wed Jul 13, 2005 7:41 am
by shiznatix
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

Posted: Wed Jul 13, 2005 8:43 am
by shiznatix
ok lemme refine this post, i found a handy function to deal with jpegs but there is a possibility or any image format so how can i find the type of image it is then create a black and white image from a colored image

Posted: Wed Jul 13, 2005 9:59 am
by pickle
getimagesize() is a common, pretty much fool-proof way of determining what type the image is.


There are a whole slew of imagecreatefrom...() functions that you can use to import images of different formats.

imagecopymergegray() can be used to make your images gray scaled.

Posted: Wed Jul 13, 2005 10:13 am
by shiznatix
on the imagecopymergegray is probebly what i need but the manul gives me no real idea how to use it and i tried replacing imagecopyresampled() with imagecopymergegray() but it says wrong prameter count help on how to use that would be great, here is the function i am using

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); 
}

Posted: Wed Jul 13, 2005 10:19 am
by evilmonkey
This is my prized script, it resizes images to any size you want while keeping the aspect ratio. Use it wisely. It does not do anything about greyscaling.

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();

?>
Good luck!

Posted: Wed Jul 13, 2005 10:22 am
by pickle
I found a code snippet:
http://fundisom.com/phpsnippets/snip/im ... _an_image/
The "magic" numbers in there make me a little worried though.

In your code, just adding a call to imagecopymergegray should do it:

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
}

Posted: Wed Jul 13, 2005 10:24 am
by shiznatix
pickel, i get this error
Warning: Wrong parameter count for imagecopymergegray() in /home/users/andrew/public_html/model/admin/admin.php on line 323

Posted: Wed Jul 13, 2005 10:29 am
by shiznatix
never mind i got it, it doesnt need the last 2 $new_w $new_h stuff it just needs somtin that thankfully php just called pct in their manual because i enjoy trying to learn estonian, php, css and now trying to learn obscure abreviations that are never explained

EDIT: all is working but its not actually very grayscale, the other image is being shown through. is there any way to make it ONLY grayscale?

Posted: Wed Jul 13, 2005 11:09 am
by onion2k
http://www.ooer.com/index.php?section=php&id=23

An article I wrote about converting things to greyscale in numberous different ways using GD.

Posted: Thu Jul 14, 2005 4:59 am
by shiznatix
onion2k that seams like exactally what i need but after make this function from your codes (sorry i kinda copy/paste) i dont know how to make the final image and save it. heres what i got

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));
}
sorry im being real stupid i just have never done any of these image stuff and it all is quite complicated for me

Posted: Thu Jul 14, 2005 5:18 am
by onion2k

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);
Tweaked the code slightly coz there was a bug with the loop maths. Should work now though.

Posted: Thu Jul 14, 2005 5:38 am
by shiznatix
that script looks real nice and all but it times out, one of the loops is killing it but i dont know where

Posted: Thu Jul 14, 2005 6:01 am
by onion2k
Works for me. How big is your source image?

Posted: Thu Jul 14, 2005 6:06 am
by shiznatix
errr it was like extreamly huge but i just tried a thumbnail of that picture and it works fine, many thanks

Posted: Thu Jul 14, 2005 6:07 am
by onion2k
I knew it wouldn't be my code. My code always works.

:wink: