يمكنني تبديل الألوان في الصورة باستخدام مكتبة GD في PHP؟

StackOverflow https://stackoverflow.com/questions/456044

سؤال

وحصلت على صورة من هذا القبيل (انها الرسم البياني):


<الفرعية> (المصدر: kitconet.com )

وأريد تغيير الألوان، وبالتالي فإن أبيض أسود، خط الرسم البياني اللون الأزرق الفاتح، الخ .. هل من الممكن تحقيق مع GD وPHP؟

هل كانت مفيدة؟

المحلول

وهذا سوف يحل محل اللون الأبيض مع الرمادي

$imgname = "test.gif";
$im = imagecreatefromgif ($imgname);

$index = imagecolorclosest ( $im,  255,255,255 ); // get White COlor
imagecolorset($im,$index,92,92,92); // SET NEW COLOR

$imgname = "result.gif";
imagegif($im, $imgname ); // save image as gif
imagedestroy($im);

نصائح أخرى

وكان لدي صعوبة في جعل هذا العمل حل. الصورة لا يمكن أن يكون صورة اللون الحقيقي. تحويل لأول مرة مع imagetruecolortopalette ()؛

$imgname = "test.gif";
$im = imagecreatefromgif ($imgname);

imagetruecolortopalette($im,false, 255);

$index = imagecolorclosest ( $im,  255,255,255 ); // get White COlor
imagecolorset($im,$index,92,92,92); // SET NEW COLOR

$imgname = "result.gif";
imagegif($im, $imgname ); // save image as gif
imagedestroy($im);

وأعرف أن هذا هو وقت متأخر وبعد وقوعها، ولكن لقد وضعت السيناريو الذي سوف نفعل ذلك على نطاق أوسع قليلا. نأمل شخص يأتي عبر هذه الوظيفة يمكن استخدامه. فإنه يأخذ عددا من مصدر الصور التي هي طبقات لون واحد (اختيارك). يمكنك تزويدها لون المصدر ولون من كل طبقة والنصي بإرجاع صورة مركبة (مع الشفافية الكاملة) الملونة على وجه التحديد إلى رمز عرافة المقدمة الخاص بك.

تحقق من التعليمات البرمجية أدناه. ويمكن الاطلاع على شرح أكثر تفصيلا على بلدي <لأ href = "http://www.stephenwalcher.com/blog/2012/02/02/how-to-make-a-transparent-image-with-dynamic-color- طبقات / "يختلط =" نوفولو "> بلوق .

function hexLighter($hex, $factor = 30) {
    $new_hex = '';

    $base['R'] = hexdec($hex{0}.$hex{1});
    $base['G'] = hexdec($hex{2}.$hex{3});
    $base['B'] = hexdec($hex{4}.$hex{5});

    foreach ($base as $k => $v) {
        $amount = 255 - $v;
        $amount = $amount / 100;
        $amount = round($amount * $factor);
        $new_decimal = $v + $amount;

        $new_hex_component = dechex($new_decimal);

        $new_hex .= sprintf('%02.2s', $new_hex_component);
    }

    return $new_hex;
}

// Sanitize/Validate provided color variable
if (!isset($_GET['color']) || strlen($_GET['color']) != 6) {
    header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request', true, 400);

    exit(0);
}

if (file_exists( "cache/{$_GET['color']}.png" )) {
    header( 'Content-Type: image/png' );
    readfile( "cache/{$_GET['color']}.png" );

    exit(0);
}

// Desired final size of image
$n_width = 50;
$n_height = 50;

// Actual size of source images
$width = 125;
$height = 125;

$image =    imagecreatetruecolor($width, $height);
            imagesavealpha($image, true);
            imagealphablending($image, false);

$n_image =  imagecreatetruecolor($n_width, $n_height);
            imagesavealpha($n_image, true);
            imagealphablending($n_image, false);

$black = imagecolorallocate($image, 0, 0, 0);
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);

imagefilledrectangle($image, 0, 0, $width, $height, $transparent);

$layers = array();
$layers_processed = array();

$layers[] = array( 'src' => 'layer01.gif', 'level' => 0 );  // Border
$layers[] = array( 'src' => 'layer02.gif', 'level' => 35 );     // Background
$layers[] = array( 'src' => 'layer03.gif', 'level' => 100 );    // White Quotes

foreach ($layers as $idx => $layer) {
    $img = imagecreatefromgif( $layer['src'] );
    $processed = imagecreatetruecolor($width, $height);

    imagesavealpha($processed, true);
    imagealphablending($processed, false);

    imagefilledrectangle($processed, 0, 0, $width, $height, $transparent);

    $color = hexLighter( $_GET['color'], $layer['level'] );
    $color = imagecolorallocate($image,
        hexdec( $color{0} . $color{1} ),
        hexdec( $color{2} . $color{3} ),
        hexdec( $color{4} . $color{5} )
    );

    for ($x = 0; $x < $width; $x++)
        for ($y = 0; $y < $height; $y++)
            if ($black === imagecolorat($img, $x, $y))
                imagesetpixel($processed, $x, $y, $color);

    imagecolortransparent($processed, $transparent);
    imagealphablending($processed, true);

    array_push($layers_processed, $processed);

    imagedestroy( $img );
}

foreach ($layers_processed as $processed) {
    imagecopymerge($image, $processed, 0, 0, 0, 0, $width, $height, 100);

    imagedestroy( $processed );
}

imagealphablending($image, true);

imagecopyresampled($n_image, $image, 0, 0, 0, 0, $n_width, $n_height, $width, $height);

imagealphablending($n_image, true);

header( 'Content-Type: image/png' );
imagepng( $n_image, "cache/{$_GET['color']}.png" );
imagepng( $n_image );

// Free up memory
imagedestroy( $n_image );
imagedestroy( $image );

ولم أحاول ذلك بنفسي ولكن هل يمكن أن ننظر في وظيفة imagecolorset () في مكتبة GD فإنه لا لون ملء مثل تأثير، التي يمكن أن تساعد مع خلفية بيضاء.

هل يمكن محاكمة imagefilter وظيفة http://lv.php.net/imagefilter - ولكن ذلك لن تتيح إمكانية الوصول المباشر إلى استبدال لون واحد مع آخر، مجرد تغيير مكونات ص / ز / ب.

ويمكن تنفيذ حل مستوى منخفض جدا باستخدام imagesetpixel http://nl2.php.net/imagesetpixel لتعيين قيم بكسل جديدة.

وIMG_FILTER_NEGATE: لعكس كل الألوان من الصورة.

http://www.php.net/manual/en/function. imagefilter.php

ويمكن أن يكون حلا؟

والنهج ببطء ولكن بالتأكيد، بالتكرار عبر كل بكسل.

function ReplaceColour($img, $r1, $g1, $b1, $r2, $g2, $b2)
{
    if(!imageistruecolor($img))
        imagepalettetotruecolor($img);
    $col1 = (($r1 & 0xFF) << 16) + (($g1 & 0xFF) << 8) + ($b1 & 0xFF);
    $col2 = (($r2 & 0xFF) << 16) + (($g2 & 0xFF) << 8) + ($b2 & 0xFF);

    $width = imagesx($img); 
    $height = imagesy($img);
    for($x=0; $x < $width; $x++)
        for($y=0; $y < $height; $y++)
        {
            $colrgb = imagecolorat($img, $x, $y);
            if($col1 !== $colrgb)
                continue; 
            imagesetpixel ($img, $x , $y , $col2);
        }   
}

وأنا لست على علم بأي مهام الجاهزة. ولكن أعتقد أنك يمكن أن تذهب الحوض الصغير كل بكسل من الصورة وتغيير لون انها ...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top