2010-01-26 89 views
2

即时通讯使用以下脚本将jpgs转换为灰度图像。 http://bubble.ro/How_to_convert_an_image_to_grayscale_using_PHP.htmlPHP:将png和gif转换为灰度

我想升级它也将pngs(带有透明度)和gif(带有透明度)转换为灰度图像。

目前它不工作。我正在查询其文件扩展名的image-src。如果jpg,if,gif或者png我称之为合适imagecreatefrom-jpg-gif-png

但是我总是运行相同的for-loop和gifs,不幸的是只能得到灰色的矩形,每个像素都是灰色的。 Png几乎可以工作,但是PNG的透明度会变成黑色。

任何想法?

回答

1
$image = ImageCreateFromString(file_get_contents('/path/to/image.ext')); 

ImageFilter($image, IMG_FILTER_GRAYSCALE); 

ImageGIF($image); // or ImagePNG($image); 
3

使用此发现这里 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); } 

*/ 
0

谢谢!

imagefilter($ img,IMG_FILTER_GRAYSCALE);

它几乎工作。我仍然有问题与透明度。透明背景的png变成黑色(背景是黑色)。 GIF也会发生同样的情况,而且透明度的GIF不会得到正确的灰度。有几种非常灰暗的颜色,但是其中有苍白的绿色和红色。

<?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 { 
} 

?> 
1

要获得正确的画面无黑的背景,你必须遵循的策略:

首先创建新的图像项目 设置它的alphablending假 设置它的savealpha真 FIL它与透明矩形 复制你变灰的图片在它

您的代码将与此类似:

 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!


看看我balda pomoshnik