create thumbnails

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

User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

create thumbnails

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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); 
}
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Works for me. How big is your source image?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

errr it was like extreamly huge but i just tried a thumbnail of that picture and it works fine, many thanks
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I knew it wouldn't be my code. My code always works.

:wink:
Post Reply