PHP GD:将图像颜色乘以色调 [英] PHP GD: Multiply image colors with tint color

查看:207
本文介绍了PHP GD:将图像颜色乘以色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用GD将给定颜色(RGB)乘以图像资源(RGBA)的每个像素吗?例如,如果给定的色调颜色是红色(255,0,0),则图像资源上的黑色像素应该保持黑色(因为255 * 0 = 0),并且更亮的像素应该受到更多的红色色调因素的影响。 / p>

我尝试了

  imagefilter($ sprite,IMG_FILTER_COLORIZE,255,0 ,0); 

但只更改黑色像素。 NEGATE,IMG_FILTER_COLORIZE,NEGATE也不会工作。

解决方案

这似乎对我有用:

 <?php 
//用于从任何上传的文件创建图像的函数

function imageCreateFromAny($ filepath){

$ type = exif_imagetype($ filepath); // []如果没有exif,你可以使用getImageSize()
$ allowedTypes = array(
1,// [] gif
2,// [] jpg
3,// [] png
6 // [] bmp
);
if(!in_array($ type,$ allowedTypes)){
return false;
}
switch($ type){
case 1:
$ im = imageCreateFromGif($ filepath);
break;
case 2:
$ im = imageCreateFromJpeg($ filepath);
break;
case 3:
$ im = imageCreateFromPng($ filepath);
break;
case 6:
$ im = imageCreateFromBmp($ filepath);
break;
}
return $ im;
}

//设置变量
$ filter_r = 255;
$ filter_g = 0;
$ filter_b = 0;
$ suffixe = - red;
$ path =original-source / image.jpg;

if(is_file($ path)){
$ image = imageCreateFromAny($ path);

//反转inteded颜色red
$ filter_r_opp = 255 - $ filter_r; // = 0
$ filter_g_opp = 255 - $ filter_g; // = 255
$ filter_b_opp = 255 - $ filter_b; // = 255
// color is nowaqua

/ * FAST METHOD * /
imagefilter($ image,IMG_FILTER_NEGATE);
imagefilter($ image,IMG_FILTER_COLORIZE,$ filter_r_opp,$ filter_g_opp,$ filter_b_opp);
imagefilter($ image,IMG_FILTER_NEGATE);

$ new_path = substr($ path,0,strlen($ path)-4)$ suffixe。。jpg;
imagejpeg($ image,$ new_path);

imagedestroy($ image);

echo'Red shading success。';
}
else {
echo'红色阴影失败。
}

?>


can I use GD to multiply given color (RGB) with every pixel of an image resource (RGBA)? For example, if the given tint color is red (255, 0, 0), a black pixel on the image resource should stay black (since 255 * 0 = 0) and brighter pixels should be affected more by that red tint factor.

I tried

imagefilter($sprite, IMG_FILTER_COLORIZE, 255, 0, 0);

but that only changes black pixels. NEGATE, IMG_FILTER_COLORIZE, NEGATE also won't work.

解决方案

This seems to work for me:

<?php
// function for creating images from any uploaded

function imageCreateFromAny($filepath) { 

    $type = exif_imagetype($filepath); // [] if you don't have exif you could use getImageSize() 
    $allowedTypes = array( 
        1,  // [] gif 
        2,  // [] jpg 
        3,  // [] png 
        6   // [] bmp 
    ); 
    if (!in_array($type, $allowedTypes)) { 
    return false; 
    } 
    switch ($type) { 
        case 1 : 
            $im = imageCreateFromGif($filepath); 
        break; 
        case 2 : 
            $im = imageCreateFromJpeg($filepath); 
        break; 
        case 3 : 
            $im = imageCreateFromPng($filepath); 
        break; 
        case 6 : 
            $im = imageCreateFromBmp($filepath); 
        break; 
    }    
    return $im;  
}

// set up variables
$filter_r=255;
$filter_g=0;
$filter_b=0;
$suffixe="-red";
$path="original-source/image.jpg";

if(is_file($path)){
    $image=imageCreateFromAny($path);

    // invert inteded color "red"
    $filter_r_opp = 255 - $filter_r; // = 0
    $filter_g_opp = 255 - $filter_g; // = 255
    $filter_b_opp = 255 - $filter_b; // = 255
    // color is now "aqua"

    /* FAST METHOD */
    imagefilter($image, IMG_FILTER_NEGATE);
    imagefilter($image, IMG_FILTER_COLORIZE, $filter_r_opp, $filter_g_opp, $filter_b_opp);
    imagefilter($image, IMG_FILTER_NEGATE);

    $new_path=substr($path,0,strlen($path)-4).$suffixe.".jpg";
    imagejpeg($image,$new_path);

    imagedestroy($image);

    echo 'Red shading success.';
}
else {
    echo 'Red shading failed.';
}

?>

这篇关于PHP GD:将图像颜色乘以色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆