create thumbnails
Moderator: General Moderators
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
create thumbnails
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
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.
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.
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
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);
}- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
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.
Good luck!
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();
?>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:
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.
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
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?
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?
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.
An article I wrote about converting things to greyscale in numberous different ways using GD.
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
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
sorry im being real stupid i just have never done any of these image stuff and it all is quite complicated for me
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);