PHP:将 png 和 gif 转换为灰度

发布于 2024-08-19 18:17:12 字数 455 浏览 4 评论 0原文

我使用以下脚本将 jpg 转换为灰度图像。 http://bubble.ro/How_to_convert_an_image_to_grayscale_using_PHP.html

我想升级它也可以转换 png(使用透明度)和 gif(具有透明度)转换为灰度图像。

目前它不起作用。我正在查询 image-src 的文件扩展名。如果 jpg、if、gif 或 if png,我会调用适当的 imagecreatefrom-jpg-gif-png

但是,我总是运行相同的 for 循环,不幸的是 gif 只得到灰色矩形,每个像素都是灰色的。 PNG 几乎可以工作,但是 png 中的透明度会变成黑色。

有什么想法吗?

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 convert pngs (with transparency) and gifs (with transparency) into grayscale images.

At the moment it's not working. I'm querying the image-src for its file-extension. If jpg, if, gif, or if png i call the appropriate imagecreatefrom-jpg-gif-png

However i'm always running the same for-loop and gifs unfortunately only get gray rectangles, every pixel is gray. Png's almost work, however transprency in pngs gets transformed to black.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瞎闹 2024-08-26 18:17:12

使用这里找到的
http://hm2k.googlecode.com/svn/trunk/代码/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); }

*/

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

*/
东风软 2024-08-26 18:17:12
$image = ImageCreateFromString(file_get_contents('/path/to/image.ext'));

ImageFilter($image, IMG_FILTER_GRAYSCALE);

ImageGIF($image); // or ImagePNG($image);
$image = ImageCreateFromString(file_get_contents('/path/to/image.ext'));

ImageFilter($image, IMG_FILTER_GRAYSCALE);

ImageGIF($image); // or ImagePNG($image);
如果没结果 2024-08-26 18:17:12

要获得没有黑色背景的正确图片,您必须遵循策略:

首先创建新的图像项目
将其 alphablending 设置为 false
将其 savealpha 设置为 true
用透明矩形填充它
将您的灰色图片复制到上面,

您的代码将与此类似:

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

祝你好运!


看看我的 balda pomoshnik

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

戴着白色围巾的女孩 2024-08-26 18:17:12

谢谢你!

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

?>

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文