im using the following script to convert jpgs into grayscale-images. http://bubble.ro/How_to_convert_an_image_to_grayscale_using_PHP.html
i want to upgrade it to also co
To obtain right pictures without black background you must follow strategy:
first create your new image item set it's alphablending to false set it's savealpha to true fil it with transparent rectangle copy your grayed picture over it
your code will be similar to this :
switch ($handle->file_src_name_ext) {
case 'gif':
$sourceIm = imagecreatefromgif($savepath.$handle->file_dst_name);
break;
case 'jpg':
$sourceIm = imagecreatefromjpeg($savepath.$handle->file_dst_name);
break;
case 'png':
default:
$sourceIm = imagecreatefrompng($savepath.$handle->file_dst_name);
break;
}
$iw = imagesx($sourceIm);
$ih = imagesy($sourceIm);
$im = imagecreatetruecolor($iw, $ih);
if (function_exists('imagecolorallocatealpha')) {
imagealphablending($im, false);
imagesavealpha($im, true);
$transparent = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefilledrectangle($im, 0, 0, $iw, $ih, $transparent);
}
if ($sourceIm ) {
imagefilter($sourceIm, IMG_FILTER_GRAYSCALE);
}
imagecopyresampled($im, $sourceIm, 0, 0, 0, 0, $iw, $ih, $iw, $ih);
switch ($handle->file_src_name_ext) {
case 'gif':
imagepng($im, $savepath.'grd'.$row->id.'.gif');
break;
case 'jpg':
imagejpeg($im, $savepath.'grd'.$row->id.'.jpg');
break;
case 'png':
default:
imagepng($im, $savepath.'grd'.$row->id.'.png');
break;
}
goodluck!
take a look at my balda pomoshnik
Use this found here http://hm2k.googlecode.com/svn/trunk/code/php/functions/imagegray.php
<?php
function imagegray($img) {
if (!file_exists($img)) { user_error("'$img' file was not found."); return; }
list($width, $height, $type) = getimagesize($img);
switch ($type) {
case 1:
$img = imagecreatefromgif($img);
break;
case 2:
$img = imagecreatefromjpeg($img);
break;
case 3:
default:
$img = imagecreatefrompng($img);
break;
}
imagefilter($img, IMG_FILTER_GRAYSCALE);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
}
/*because i'm british*/
function imagegrey($img) {
return imagegray($img);
}
/*
//example usage
$i=isset($_REQUEST['i'])?$_REQUEST['i']:'';
if ($i) { imagegrey($i); }
*/
thank you!
imagefilter($img, IMG_FILTER_GRAYSCALE);
it's almost working. i'm still having problems with transprency. pngs with transparent background get transformed to black (the background is black). The same happens to gifs and moreover gifs with transprency don't get properly grayscaled. There are a few very graytoned colors, however there are pale greentones and redtones in it.
<?php
$src = $_GET['src'];
$img_ext;
if (preg_match('/\.(\w{3,4})$/i', $src, $reg)) {
$img_ext = strtolower($reg[1]);
}
$source_file = $src;
if ($img_ext == "jpg" || $img_ext == "jpeg") { //jpg
$im = imagecreatefromjpeg($source_file);
} else if ($img_ext == "gif") {
$im = imagecreatefromgif($source_file); //gif
} else if ($img_ext == "png") {
$im = imagecreatefrompng($source_file); //png
} else {
}
ImageFilter($im, IMG_FILTER_GRAYSCALE);
if ($img_ext == "jpg" || $img_ext == "jpeg") { //jpg
header('Content-type: image/jpeg');
imagejpeg($im);
} else if ($img_ext == "gif") { //gif
header('Content-type: image/gif');
imagegif($im);
} else if ($img_ext == "png") { //png
header('Content-type: image/png');
imagepng($im);
} else {
}
?>
$image = ImageCreateFromString(file_get_contents('/path/to/image.ext'));
ImageFilter($image, IMG_FILTER_GRAYSCALE);
ImageGIF($image); // or ImagePNG($image);