2013-07-17 3 views
0

je l'image suivante: http://i.stack.imgur.com/mM8cY.pngsuperposition de couleur sur l'image PNG blanc avec transparence

Ce que je voudrais faire est de créer un effet « Color Overlay » comme dans Photoshop. J'ai besoin d'une sorte de code qui me permette de changer chaque pixel blanc aux valeurs spécifiées dans les nombres RVB allant de 0 à 255. J'ai entendu parler de la classe ImageMagick, mais je ne l'ai pas trouvée nulle part, et je n'ai aucune idée de comment le faire même avec cette classe. J'essaye actuellement avec imagefilter, mais cela ne fonctionne pas avec des images blanches. Voici mon code actuel:

<?php 
$match = array(); 
if (isset($_GET['c']) && preg_match('/^#?(?:[A-Fa-f0-9]{2}){3}$/',$_GET['c'],$match)){ 
    $match = str_split($match[0],2); 
    foreach ($match as $k=>$m){ $match[$k] = intval($match[$k],16); } 

    $img = imageCreateFromPng('splat.png'); 
    $background = imagecolorallocate($img, 0, 0, 0); 
    imagecolortransparent($img, $background); 
    imagealphablending($img, false); 
    imagesavealpha($img, true); 

    //Transformation code 
    imagefilter($img, IMG_FILTER_COLORIZE, $match[0], $match[1], $match[2]); 

    header('Content-type: image/png'); 
    imagePng($img); 
    exit; 
} 
?> 

Répondre

0

J'ai trouvé la solution. Tout ce que je l'ai été - en utilisant Photoshop - j'ai ajouté une superposition de couleur rouge sur l'image, alors maintenant il ressemble à ceci: http://i.stack.imgur.com/mVARN.png

Et puis utilisé le code PHP suivant:

<?php 
$match = array(); 
$color = isset($_GET['c']) ? $_GET['c'] : false; 
if ($color === false) isset($_GET['color']) ? $_GET['color'] : false; 
$color = preg_replace('/^#/','',$color); 
if (strlen($color) == 3) $color = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2]; 
if (preg_match('/^(?:[A-Fa-f0-9]{2}){3}$/',$color,$match)){ 
    $match = str_split($match[0],2); 
    foreach ($match as $k=>$m){ $match[$k] = intval($match[$k],16); } 

    // Load image 
    $img = imageCreateFromPng('splat.png'); 
    $background = imagecolorallocate($img, 0, 0, 0); 
    imagecolortransparent($img, $background); 
    imagealphablending($img, false); 
    imagesavealpha($img, true); 

    imagefilter($img, IMG_FILTER_COLORIZE, intval(intval($match[0],16) - 255 ,16), $match[1], $match[2]); 

    header('Content-type: image/png'); 
    imagePng($img); 
    exit; 
} 
?> 

La clé ici est que J'ai soustrait la 255 valeur de couleur rouge de l'entrée avec ceci:

intval(intval($match[0],16) - 255 ,16) 

et cela a changé la couleur correctement.

Questions connexes